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