blob: 287fb2fea6c9d0c68370ecb273d241e1dd3acc10 [file] [log] [blame] [view]
Laszlo Csomor39fc88c2015-03-02 16:22:29 +00001Rules
2=====
3
Laurent Le Brunc18dadc2015-03-31 19:38:30 +00004**Status: Experimental**. Expect some breaking changes in the API.
Laszlo Csomor39fc88c2015-03-02 16:22:29 +00005
Laszlo Csomor39fc88c2015-03-02 16:22:29 +00006Rule creation
7-------------
8
9In a Skylark extension, use the `rule` function to create a new rule and store
10it in a global variable. [See example](cookbook.md#empty).
11
12Attributes
13----------
14
Laurent Le Brun9ba067d2015-05-22 13:55:23 +000015An attribute is a rule argument, such as `srcs` or `deps`. You must list
Laszlo Csomor39fc88c2015-03-02 16:22:29 +000016the attributes and their type when you define a rule.
17
18```python
19sum = rule(
Googler6976e6c2015-07-27 17:25:00 +000020 implementation=impl,
21 attrs = {
22 "number": attr.int(default = 1),
23 "deps": attr.label_list(),
24 },
Laszlo Csomor39fc88c2015-03-02 16:22:29 +000025)
26```
27
28If an attribute starts with `_`, it is private and users cannot set it. It
29is useful in particular for label attributes (your rule will have an
30implicit dependency on this label).
31
Laurent Le Brun8d4ffc42015-06-24 15:21:50 +000032The following attributes are implicitly added to every rule: `deprecation`,
33`features`, `name`, `tags`, `testonly`, `visibility`. Test rules also have the
34following attributes: `args`, `flaky`, `local`, `shard_count`, `size`, `timeout`.
Laurent Le Brun6d450dc2015-05-06 17:18:17 +000035
36To access an attribute, use `ctx.attr.<attribute_name>`. The name and the
37package of a rule are available with `ctx.label.name` and `ctx.label.package`.
38
Laurent Le Brund7bd77a2015-03-13 19:01:03 +000039[See example.](cookbook.md#attr)
40
Laszlo Csomor39fc88c2015-03-02 16:22:29 +000041The rule implementation function
42--------------------------------
43
44Every rule has to have an implementation function. This contains the actual
45logic of the rule and is executed strictly in the Analysis Phase. The function
46has exactly one input parameter, `ctx` and it may return
47the [runfiles](#runfiles) and [providers](#providers)
48of the rule. The input parameter `ctx` can be used to access attribute values,
49outputs and dependent targets and files. It also has some helper functions.
David Chen24f2d992015-08-17 17:25:46 +000050See [the library](lib/ctx.html) for more context. Example:
Laszlo Csomor39fc88c2015-03-02 16:22:29 +000051
52```python
53def impl(ctx):
54 ...
55 return struct(
56 runfiles = ...,
57 my_provider = ...,
58 ...
59 )
60
61my_rule = rule(
Googler6976e6c2015-07-27 17:25:00 +000062 implementation=impl,
63 ...
Laszlo Csomor39fc88c2015-03-02 16:22:29 +000064)
65```
66
67Files
68-----
69
70There are two kinds of files: files stored in the file system and generated
71files. For each generated file, there must be one and only one generating
72action, and each action must generate one ore more output files. Otherwise
73Bazel will throw an error.
74
75Targets
76-------
77
78Every build rule corresponds to exactly one target. A target can create
79[actions](#actions), can have dependencies (which can be files or
Laszlo Csomor7fad36a2015-03-25 17:05:04 +000080other build rules), [output files](#output-files) (generated by
Laszlo Csomor39fc88c2015-03-02 16:22:29 +000081its actions) and [providers](#providers).
82
83A target `y` is depending on target `x` if `y` has a label or label list type
84attribute where `x` is declared:
85
86```python
87my_rule(
Googler6976e6c2015-07-27 17:25:00 +000088 name = "x",
Laszlo Csomor39fc88c2015-03-02 16:22:29 +000089)
90
91my_rule(
92 name = "y",
Googler6976e6c2015-07-27 17:25:00 +000093 deps = [":x"],
Laszlo Csomor39fc88c2015-03-02 16:22:29 +000094)
95```
96
97In the above case it's possible to access targets declared in `my_rule.deps`:
98
99```python
100def impl(ctx):
Laurent Le Brunb7320622015-05-20 11:38:56 +0000101 for dep in ctx.attr.deps:
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000102 # Do something with dep
103 ...
104
105my_rule = rule(
Googler6976e6c2015-07-27 17:25:00 +0000106 implementation=impl,
107 attrs = {
108 "deps": attr.label_list(),
109 },
110 ...
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000111)
112```
113
David Chen3f16b562015-07-23 15:04:50 +0000114<a name="output-files"></a>
115Output files
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000116------------
117
118A target can declare output files, which must be generated by the target's
119actions. There are three ways to create output files in Skylark:
120
121* If the rule is marked `executable`, it creates an output file of the same name
122 as the rule's. [See example](cookbook.md#outputs-executable)
123
124* The rule can declare default `outputs`, which are always generated.
125 [See example](cookbook.md#outputs-default)
126
127* The rule can have output or output list type attributes. In that case the
128 output files come from the actual attribute values.
129 [See example](cookbook.md#outputs-custom)
130
131All output files must have exactly one generating action. See the
David Chen24f2d992015-08-17 17:25:46 +0000132[library](lib/ctx.html#outputs) for more context.
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000133
Laurent Le Brunc7dd1b12015-06-15 13:53:34 +0000134Default outputs
135---------------
136
137Every rule has a set of default outputs. This is used:
138
139* When the user runs `bazel build` on your target. Bazel will build the default
140 outputs of the rule.
141
142* When the target is used as a dependency to another rule. A rule can access
David Chen24f2d992015-08-17 17:25:46 +0000143 the default outputs by using [target.files](lib/Target.html#files).
Laurent Le Brunc7dd1b12015-06-15 13:53:34 +0000144 This is the case for example if you use a rule in the `srcs` attribute of a
145 `genrule`.
146
147To decide what goes in the default outputs of a rule, use the `files` provider.
148If unspecified, it will contain all the declared outputs.
149
150```python
151def _impl(ctx):
152 # ...
153 return struct(files = set([file1, file2]))
154```
155
156This can be useful for exposing files generated with
David Chen24f2d992015-08-17 17:25:46 +0000157[ctx.new_file](lib/ctx.html#new_file). You can also have "implicit
Laurent Le Brunc7dd1b12015-06-15 13:53:34 +0000158outputs", i.e. files that are declared in the rule, but not in the default
159outputs (like `_deploy.jar` in `java_binary`).
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000160
161Actions
162-------
163
164There are three ways to create actions:
165
166* `ctx.action`
167* `ctx.file_action`
168* `ctx.template_action`
169
170Actions take a set (can be empty) of input files and generate a (non-empty)
171set of output files.
172The set of input and output files must be known during the analysis phase. It
173might depend on the value of attributes and information from dependencies, but
174it cannot depend on the result of the execution. For example, if your action
Googler5ae8daf2015-05-11 14:27:03 +0000175runs the unzip command, you must specify which files you expect to be inflated
176(before running unzip).
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000177
178Actions are comparable to pure functions: They should depend only on the
179provided inputs, and avoid accessing computer information, username, clock,
180network or I/O devices (except for reading inputs and writing outputs).
181
182**If a command generates a file that is not listed in the outputs**: It is fine.
183The file will be ignored and cannot be used by other rules.
184
185**If a command does not generate a file that is listed in the outputs**: It is an
186execution error and the build will fail. This happens for instance when a
187compilation fails.
188
189**If a command generates an unknown number of outputs and you want to keep them
190all**, you may group them in a zip file. This way, you will be able to declare
191your output.
192
193**If a command does not list a file it uses as an input**, the action execution
194will most likely result in an error. The file is not guaranteed to be available
195to the action, so if it **is** there, it's due to a coincidence or error.
196
197**If a command lists a file as an input, but does not use it**: It is fine. However
198it can affect the action execution order resulting in sub-optimal performance.
199
200Dependencies are resolved by Bazel, which will decide which actions are
201executed. It is an error if there is a cycle in the dependency graph. Creating
202an action does not guarantee that it will be executed: It depends on whether
203its outputs are needed for the build.
204
Laurent Le Brunfd4615a2015-06-26 14:18:11 +0000205Configurations
206--------------
207
208By default, a target is built in the target configuration. For each label
209attribute, you can decide whether the dependency should be built in the same
210configuration, or in the host configuration.
211
212In general, sources, dependent libraries and executables that will be needed at
213runtime can use the same configuration.
214
215Tools that are executed as part of the build (e.g. compilers, code generators)
216should be built for the host configuration. In this case, specify `cfg=HOST_CFG`
217in the attribute.
218
219`DATA_CFG` is present for legacy reasons and should be used for the `data`
220attributes.
221
Florian Weikertc6fd0b62015-08-24 13:06:31 +0000222<a name="fragments"></a>
223Configuration Fragments
224--------------
225
226Rules may access configuration fragments such as `cpp`, `java` and `jvm`.
227However, all required fragments have to be declared in order to avoid access
228errors:
229
230```python
231def impl(ctx):
232 # Using ctx.fragments.cpp would lead to an error since it was not declared.
233 x = ctx.fragments.java
234 ...
235
236my_rule = rule(
237 implementation=impl,
Florian Weikert3f8aac92015-09-07 12:06:02 +0000238 fragments = ["java"], #Required fragments of the target configuration
239 host_fragments = ["java"], #Required fragments of the host configuration
Florian Weikertc6fd0b62015-08-24 13:06:31 +0000240 ...
241)
242```
243
Florian Weikert3f8aac92015-09-07 12:06:02 +0000244`ctx.fragments` only provides configuration fragments for the target
245configuration. If you want to access fragments for the host configuration,
246please use `ctx.host_fragments` instead.
247
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000248Providers
249---------
250
251Providers are used to access information from another rule. A rule depending on
252another rule has access to the data the latter provides. These data can be e.g.
253output files, the libraries the dependent rule is using to link or compile, or
254anything the depending rule should know about. Using providers is the only way
255to exchange data between rules.
256
257A rule can only access data provided by its direct dependencies, not that of
258transitive dependencies: if rule `top` depends on `middle` and `middle` depends
259on `bottom`, then `middle` is a direct dependency of `top` and `bottom` is a
260transitive dependency of `top`. In this scenario `top` can only access data
261provided by `middle`. If `middle` also provides the data that `bottom` provided
262to it then and only then can `top` access it.
263
264Only the following data types are allowed to pass using providers:
265
Han-Wen Nienhuys0bacd7c2015-03-06 15:46:58 +0000266* `bool`
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000267* `integer`
268* `string`
269* `file`
270* `label`
271* `None`
272* anything composed of these types and `lists`, `dicts`, `sets` or `structs`
273
274Providers are created from the return value of the rule implementation function:
275
276```python
277def dependent_rule_implementation(ctx):
278 ...
279 return struct(
280 transitive_data = set(["a", "b", "c"])
281 )
282```
283
284A depending rule might access these data as struct fields of the depending
285`target`:
286
287```python
288def depending_rule_implementation(ctx):
289 ...
290 s = set()
Laurent Le Brunb7320622015-05-20 11:38:56 +0000291 for dep_target in ctx.attr.deps:
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000292 s += dep_target.transitive_data
293 ...
294```
295
296Providers are only available during the analysis phase. Examples of usage:
297
298* [mandatory providers](cookbook.md#mandatory-providers)
299* [optional providers](cookbook.md#optional-providers)
300
301Runfiles
302---------
303
304Runfiles are a set of files used by the (often executable) output of a rule
305during runtime (as opposed to build time, i.e. when the binary itself is
306generated).
307Bazel creates a directory tree containing symlinks pointing to the
308runfiles during execution, to stage this environment for the binary which can
309thus access them during runtime.
310
311Runfiles can be added manually during rule creation and/or collected
312transitively from dependent rules:
313
314```python
315def rule_implementation(ctx):
316 ...
317 transitive_runfiles = set()
Laurent Le Brunb7320622015-05-20 11:38:56 +0000318 for dep in ctx.attr.special_dependencies:
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000319 transitive_runfiles += dep.transitive_runtime_files
320
321 runfiles = ctx.runfiles(
322 # Add some files manually.
Googler6976e6c2015-07-27 17:25:00 +0000323 files = [ctx.file.some_data_file],
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000324 # Add transitive files from dependencies manually.
Googler6976e6c2015-07-27 17:25:00 +0000325 transitive_files = transitive_runfiles,
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000326 # Collect runfiles from the common locations: transitively from srcs,
327 # deps and data attributes.
Googler6976e6c2015-07-27 17:25:00 +0000328 collect_default = True,
Laszlo Csomor39fc88c2015-03-02 16:22:29 +0000329 )
330 # Add a field named "runfiles" to the return struct in order to actually
331 # create the symlink tree.
332 return struct(runfiles = runfiles)
333```
334
335Note that non-executable rule outputs can also have runfiles. For example, a
336library might need some external files during runtime, and every depending
337binary should know about it.
338
339Also note that if an action uses an executable, the executable's runfiles can
340be used when the action executes.
Laurent Le Brun1fb10af2015-06-26 14:06:16 +0000341
342Executable rules
343----------------
344
345To make a rule executable, set `executable=True` in the
David Chen24f2d992015-08-17 17:25:46 +0000346[rule function](lib/Globals.html#rule). During the analysis
Laurent Le Brun1fb10af2015-06-26 14:06:16 +0000347phase, the rule must generate the output file `ctx.outputs.executable`.
348[See example](cookbook.md#outputs-executable)
349
350When the rule is executable, users can run it using `bazel run`.
351
352Test rules
353----------
354
355To create a test rule, set `test=True` in the
David Chen24f2d992015-08-17 17:25:46 +0000356[rule function](lib/Globals.html#rule). The name of the rule must
Laurent Le Brun1fb10af2015-06-26 14:06:16 +0000357also end with `_test`. Test rules are implicitly executable, which means they
358must generate the output file `ctx.outputs.executable`.
359
360Test rules inherit the following attributes: `args`, `flaky`, `local`,
361`shard_count`, `size`, `timeout`.
362
363Test rules are run using `bazel test`.