preprocessor.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. .. _preprocessor:
  2. =================
  3. Text Preprocessor
  4. =================
  5. The build system contains a text preprocessor similar to the C preprocessor,
  6. meant for processing files which have no built-in preprocessor such as XUL
  7. and JavaScript documents. It is implemented at ``python/mozbuild/mozbuild/preprocessor.py`` and
  8. is typically invoked via :ref:`jar_manifests`.
  9. While used to preprocess CSS files, the directives are changed to begin with
  10. ``%`` instead of ``#`` to avoid conflict of the id selectors.
  11. Directives
  12. ==========
  13. Variable Definition
  14. -------------------
  15. define
  16. ^^^^^^
  17. ::
  18. #define variable
  19. #define variable value
  20. Defines a preprocessor variable.
  21. Note that, unlike the C preprocessor, instances of this variable later in the
  22. source are not automatically replaced (see #filter). If value is not supplied,
  23. it defaults to ``1``.
  24. Note that whitespace is significant, so ``"#define foo one"`` and
  25. ``"#define foo one "`` is different (in the second case, ``foo`` is defined to
  26. be a four-character string).
  27. undef
  28. ^^^^^
  29. ::
  30. #undef variable
  31. Undefines a preprocessor variable.
  32. Conditionals
  33. ------------
  34. if
  35. ^^
  36. ::
  37. #if variable
  38. #if !variable
  39. #if variable==string
  40. #if variable!=string
  41. Disables output if the conditional is false. This can be nested to arbitrary
  42. depths. Note that in the equality checks, the variable must come first, and
  43. the comparison operator must not be surrounded by any whitespace.
  44. else
  45. ^^^^
  46. ::
  47. #else
  48. Reverses the state of the previous conditional block; for example, if the
  49. last ``#if`` was true (output was enabled), an ``#else`` makes it off
  50. (output gets disabled).
  51. .. warning:: An ``#else`` is relative to the last conditional block only,
  52. unlike the C preprocessor.
  53. It does not matter whether any blocks before it were true. This behavior
  54. changed on trunk (Gecko 1.9) on 2006-12-07; see Bug 277122 for details.
  55. ::
  56. #if 1
  57. always included
  58. #elif 1
  59. never included
  60. #else
  61. always included
  62. #endif
  63. endif
  64. ^^^^^
  65. ::
  66. #endif
  67. Ends the conditional block.
  68. ifdef / ifndef
  69. ^^^^^^^^^^^^^^
  70. ::
  71. #ifdef variable
  72. #ifndef variable
  73. An ``#if`` conditional that is true only if the preprocessor variable
  74. variable is defined (in the case of ``ifdef``) or not defined (``ifndef``).
  75. elif / elifdef / elifndef
  76. ^^^^^^^^^^^^^^^^^^^^^^^^^
  77. ::
  78. #elif variable
  79. #elif !variable
  80. #elif variable == string
  81. #elif variable != string
  82. #elifdef variable
  83. #elifndef variable
  84. A shorthand to mean an ``#else`` combined with the relevant conditional.
  85. The following two blocks are equivalent::
  86. #ifdef foo
  87. block 1
  88. #elifdef bar
  89. block 2
  90. #endif
  91. ::
  92. #ifdef foo
  93. block 1
  94. #else
  95. #ifdef bar
  96. block 2
  97. #endif
  98. #endif
  99. .. warning:: An ``#elif``, ``#elifdef``, or ``#elifndef`` is relative to
  100. the last conditional block only (as well as the condition it implies),
  101. unlike the C preprocessor. It does not matter whether any blocks before
  102. it were true. This behavior changed on trunk (Gecko 1.9) on 2006-12-07.
  103. See Bug 277122 for details.
  104. File Inclusion
  105. --------------
  106. include
  107. ^^^^^^^
  108. ::
  109. #include filename
  110. The file specified by filename is processed as if the contents was placed
  111. at this position. This also means that preprocessor conditionals can even
  112. be started in one file and ended in another (but is highly discouraged).
  113. There is no limit on depth of inclusion, or repeated inclusion of the same
  114. file, or self inclusion; thus, care should be taken to avoid infinite loops.
  115. includesubst
  116. ^^^^^^^^^^^^
  117. ::
  118. #includesubst @variable@filename
  119. Same as a ``#include`` except that all instances of variable in the included
  120. file is also expanded as in ``#filter`` substitution
  121. expand
  122. ^^^^^^
  123. ::
  124. #expand string
  125. All variables wrapped in ``__`` are replaced with their value, for this line
  126. only. If the variable is not defined, it expands to an empty string. For
  127. example, if ``foo`` has the value ``bar``, and ``baz`` is not defined, then::
  128. #expand This <__foo__> <__baz__> gets expanded
  129. Is expanded to::
  130. This <bar> <> gets expanded
  131. filter / unfilter
  132. ^^^^^^^^^^^^^^^^^
  133. ::
  134. #filter filter1 filter2 ... filterN
  135. #unfilter filter1 filter2 ... filterN
  136. ``#filter`` turns on the given filter.
  137. Filters are run in alphabetical order on a per-line basis.
  138. ``#unfilter`` turns off the given filter. Available filters are:
  139. emptyLines
  140. strips blank lines from the output
  141. slashslash
  142. strips everything from the first two consecutive slash (``/``)
  143. characters until the end of the line
  144. spaces
  145. collapses consecutive sequences of spaces into a single space,
  146. and strips leading and trailing spaces
  147. substitution
  148. all variables wrapped in @ are replaced with their value. If the
  149. variable is not defined, it is a fatal error. Similar to ``#expand``
  150. and ``#filter``
  151. attemptSubstitution
  152. all variables wrapped in ``@`` are replaced with their value, or an
  153. empty string if the variable is not defined. Similar to ``#expand``.
  154. literal
  155. ^^^^^^^
  156. ::
  157. #literal string
  158. Output the string (i.e. the rest of the line) literally, with no other fixups.
  159. This is useful to output lines starting with ``#``, or to temporarily
  160. disable filters.
  161. Other
  162. -----
  163. #error
  164. ^^^^^^
  165. ::
  166. #error string
  167. Cause a fatal error at this point, with the error message being the
  168. given string.