xref: /aosp_15_r20/build/bazel/docs/internal_concepts.md (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1*7594170eSAndroid Build Coastguard Worker# Soong-Bazel equivalents
2*7594170eSAndroid Build Coastguard Worker
3*7594170eSAndroid Build Coastguard WorkerThis doc aims to describe *internal*-facing implementation concepts. For
4*7594170eSAndroid Build Coastguard Workerexternal-facing, see
5*7594170eSAndroid Build Coastguard Workerhttps://android.googlesource.com/platform/build/bazel/+/refs/heads/master/docs/concepts.md.
6*7594170eSAndroid Build Coastguard Worker
7*7594170eSAndroid Build Coastguard Worker[TOC]
8*7594170eSAndroid Build Coastguard Worker
9*7594170eSAndroid Build Coastguard Worker## Overview
10*7594170eSAndroid Build Coastguard Worker
11*7594170eSAndroid Build Coastguard WorkerSoong/Ninja                                                     | Bazel                                                                                    | Remarks
12*7594170eSAndroid Build Coastguard Worker--------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | -------
13*7594170eSAndroid Build Coastguard Workermake phony goal, e.g. "dist", "sdk", "apps_only", "droidcore"   | Top level `filegroup` rule target                                                        | [Details](#phony-goal)
14*7594170eSAndroid Build Coastguard WorkerNinja build target (phony)                                      | (readable) alias to a file target                                                        |
15*7594170eSAndroid Build Coastguard WorkerNinja build target (non-phony)                                  | File target                                                                              |
16*7594170eSAndroid Build Coastguard Worker`ModuleFactory`                                                 | `RuleConfiguredTargetFactory`                                                            |
17*7594170eSAndroid Build Coastguard Worker`Module type` (e.g. `cc_library`)                               | Rule class (e.g. `cc_library`)                                                           |
18*7594170eSAndroid Build Coastguard WorkerModule object instance                                          | Target (instance of a rule)                                                              | [Details](#instance)
19*7594170eSAndroid Build Coastguard WorkerModule properties                                               | [Rule attributes](https://docs.bazel.build/versions/main/skylark/rules.html#attributes)  | [Details](#props)
20*7594170eSAndroid Build Coastguard WorkerModule name                                                     | Target label                                                                             |
21*7594170eSAndroid Build Coastguard WorkerModule variant                                                  | (Split) configured target                                                                |
22*7594170eSAndroid Build Coastguard Worker[LoadHooks](#loadhooks)                                         | [macros (ish)](https://docs.bazel.build/versions/main/skylark/macros.html)               |
23*7594170eSAndroid Build Coastguard WorkerTop-down mutators on modules                                    | Split configuration on targets                                                           | Allows building multiple "variants" of the same build artifact in the same build.
24*7594170eSAndroid Build Coastguard WorkerBottom-up mutators on modules                                   | [Aspects](https://docs.bazel.build/versions/main/skylark/aspects.html) on targets        |
25*7594170eSAndroid Build Coastguard Worker[Build statement (Ninja)](#ninja-build-statement)               | Action (result of ctx.actions.run)                                                       |
26*7594170eSAndroid Build Coastguard Worker[Rule statement (Ninja)](#ninja-rules)                          | [ctx.actions.run() API](https://docs.bazel.build/versions/main/skylark/lib/actions.html) |
27*7594170eSAndroid Build Coastguard Worker`out/soong/build.ninja` and `out/build-<target>.ninja`          | Action graph (serialized)                                                                |
28*7594170eSAndroid Build Coastguard WorkerPool (ninja)                                                    | Thread pools / `ExecutorService`                                                         |
29*7594170eSAndroid Build Coastguard WorkerBlueprint's Registration and Parse, `ResolveDependencies` phase | Loading phase                                                                            |
30*7594170eSAndroid Build Coastguard WorkerBlueprint's [Generate and Write phases](#blueprint-analysis)    | Analysis Phase                                                                           |
31*7594170eSAndroid Build Coastguard WorkerNinja execution                                                 | Execution phase                                                                          |
32*7594170eSAndroid Build Coastguard WorkerBlueprints/`Android.bp` files                                   | `BUILD`/`BUILD.bazel` files                                                              |
33*7594170eSAndroid Build Coastguard Worker[Namespaces](#namespaces)                                       | [Packages](#pkgs)                                                                        | Most Soong modules are within the global namespace
34*7594170eSAndroid Build Coastguard Worker[Mutators](#mutators)                                           | Configuration keys (ish)                                                                 |
35*7594170eSAndroid Build Coastguard Worker[Variation](#variation)                                         | Configuration value                                                                      |
36*7594170eSAndroid Build Coastguard Worker[Singleton](#singleton)                                         | Aspect-ish                                                                               |
37*7594170eSAndroid Build Coastguard WorkerTarget (system + vendor + product)                              | [Platform](https://docs.bazel.build/versions/main/platforms.html)                        |
38*7594170eSAndroid Build Coastguard WorkerBash scripts e.g. envsetup functions, `soong_ui.bash`)          | Repository rule                                                                          |
39*7594170eSAndroid Build Coastguard WorkerProduct and board configuration makefile and env variables      | Configuration in Bazel (ish)                                                             | [Details](#config)
40*7594170eSAndroid Build Coastguard Worker[Dependency Tags](#deptags)                                     | Provider names                                                                           |
41*7594170eSAndroid Build Coastguard Worker
42*7594170eSAndroid Build Coastguard Worker## Remarks
43*7594170eSAndroid Build Coastguard Worker
44*7594170eSAndroid Build Coastguard Worker### Phony goals {#phony-goal}
45*7594170eSAndroid Build Coastguard Worker
46*7594170eSAndroid Build Coastguard WorkerSoong maintains the make terminology of
47*7594170eSAndroid Build Coastguard Worker[goals](https://www.gnu.org/software/make/manual/html_node/Goals.html) to denote
48*7594170eSAndroid Build Coastguard Workerwhat should be built. All modules can be specified by name as a goal, in
49*7594170eSAndroid Build Coastguard Workeraddition, phony goals are supported.
50*7594170eSAndroid Build Coastguard Worker
51*7594170eSAndroid Build Coastguard WorkerA Phony goal creates a Make-style phony rule, a rule with no commands that can
52*7594170eSAndroid Build Coastguard Workerdepend on other phony rules or real files. Phony can be called on the same name
53*7594170eSAndroid Build Coastguard Workermultiple times to add additional dependencies. These are often used to build
54*7594170eSAndroid Build Coastguard Workermany targets at once. The default goal for Android's build system is `droid`.
55*7594170eSAndroid Build Coastguard WorkerSome other common phony goals include: `nothing` (perform loading/analysis),
56*7594170eSAndroid Build Coastguard Worker`docs`, `checkbuild`, `apps_only`.
57*7594170eSAndroid Build Coastguard Worker
58*7594170eSAndroid Build Coastguard WorkerSome common phony goals are defined in
59*7594170eSAndroid Build Coastguard Worker[`build/make/core/main.mk`](http://cs.android.com/android/platform/superproject/+/master:build/make/core/main.mk)
60*7594170eSAndroid Build Coastguard WorkerThe purpose is to help `soong_ui` to determine what top level files to build.
61*7594170eSAndroid Build Coastguard Worker
62*7594170eSAndroid Build Coastguard Worker### Module/Target {#instance}
63*7594170eSAndroid Build Coastguard Worker
64*7594170eSAndroid Build Coastguard WorkerWhen a Module is instantiated by Blueprint (which calls the appropriate
65*7594170eSAndroid Build Coastguard Worker`ModuleFactory`), the [property structs](#props) are populated by Blueprint.
66*7594170eSAndroid Build Coastguard Worker
67*7594170eSAndroid Build Coastguard WorkerBlueprint performs no additional operations on these properties, such that
68*7594170eSAndroid Build Coastguard Workerdependencies on other modules and references to source files are unresolved
69*7594170eSAndroid Build Coastguard Workerinitially. [`Mutators`](#mutators) then introspect the values of properties to
70*7594170eSAndroid Build Coastguard Worker[specify dependencies](https://cs.android.com/android/platform/superproject/+/master:build/blueprint/module_ctx.go;l=871-886,918-960;drc=030150d8f9d164783ea661f07793c45198739cca)
71*7594170eSAndroid Build Coastguard Workerbetween modules, which
72*7594170eSAndroid Build Coastguard Worker[Blueprint resolves](https://cs.android.com/android/platform/superproject/+/master:build/blueprint/context.go;l=1630,1667;drc=5c4abb15e3b84ab0bcedfa119e2feb397d1fb106).
73*7594170eSAndroid Build Coastguard WorkerSource files (including globs) and output paths for references to other modules
74*7594170eSAndroid Build Coastguard Workerare resolved during [blueprint analysis](#blueprint-analysis) via the various
75*7594170eSAndroid Build Coastguard Worker`Path[s]ForModuleSrc[Excludes]` functions within
76*7594170eSAndroid Build Coastguard Worker[build/soong/android/paths.go](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/paths.go).
77*7594170eSAndroid Build Coastguard Worker
78*7594170eSAndroid Build Coastguard WorkerFor a Bazel target instance, the dependencies and source file references within
79*7594170eSAndroid Build Coastguard Worker[`attrs`](#attributes) have been resolved by Bazel.
80*7594170eSAndroid Build Coastguard Worker
81*7594170eSAndroid Build Coastguard WorkerBazel
82*7594170eSAndroid Build Coastguard Worker[implementation](https://github.com/bazelbuild/bazel/blob/a20b32690a71caf712d1d241f01fef16649562ba/src/main/java/com/google/devtools/build/lib/skyframe/TransitiveBaseTraversalFunction.java#L113-L140)
83*7594170eSAndroid Build Coastguard Workerto collect deps.
84*7594170eSAndroid Build Coastguard Worker
85*7594170eSAndroid Build Coastguard Worker### Properties/Attributes {#props}
86*7594170eSAndroid Build Coastguard Worker
87*7594170eSAndroid Build Coastguard Worker#### Properties
88*7594170eSAndroid Build Coastguard Worker
89*7594170eSAndroid Build Coastguard WorkerWithin Soong/Blueprint, properties are represented as Go structs, which can be
90*7594170eSAndroid Build Coastguard Workernested, with no depth limit. Properties can be primitive or pointer types, but
91*7594170eSAndroid Build Coastguard Workerthey must be one of these types: `int64`, `string`, `bool`, `list`.
92*7594170eSAndroid Build Coastguard Worker
93*7594170eSAndroid Build Coastguard WorkerThese properties can be defined from various structs within the module type
94*7594170eSAndroid Build Coastguard Workerfactory itself (via
95*7594170eSAndroid Build Coastguard Worker[AddProperties](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=1276;drc=8631cc7327919845c9d9037188cbd483d22ba077))
96*7594170eSAndroid Build Coastguard Workeror from common helper functions such as:
97*7594170eSAndroid Build Coastguard Worker
98*7594170eSAndroid Build Coastguard Worker*   `InitAndroidModule`:
99*7594170eSAndroid Build Coastguard Worker    [specifies](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=1042-1045;drc=8631cc7327919845c9d9037188cbd483d22ba077)
100*7594170eSAndroid Build Coastguard Worker    name-related, common, and dist properties.
101*7594170eSAndroid Build Coastguard Worker*   `InitAndroidArchModule`: adds
102*7594170eSAndroid Build Coastguard Worker    [host/device properies](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=1077;drc=8631cc7327919845c9d9037188cbd483d22ba077)
103*7594170eSAndroid Build Coastguard Worker
104*7594170eSAndroid Build Coastguard WorkerGo comments for a property will be treated as documentation to describe the
105*7594170eSAndroid Build Coastguard Workerproperty. In some cases, these comments describe a default value for the
106*7594170eSAndroid Build Coastguard Workerproperty. However, the default value is not based on the comment or field
107*7594170eSAndroid Build Coastguard Workerdefinition but resolved somewhere within the module's mutators or build. These
108*7594170eSAndroid Build Coastguard Workerdefaults are often determined using Blueprint
109*7594170eSAndroid Build Coastguard Worker[`proptools`](https://cs.android.com/android/platform/superproject/+/master:build/blueprint/proptools/proptools.go)
110*7594170eSAndroid Build Coastguard Worker`*Default` functions. For example, `cc` modules have a property
111*7594170eSAndroid Build Coastguard Worker[`include_build_directory`](https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/compiler.go;l=265;drc=135bf55281d79576f33469ce4f9abc517a614af5),
112*7594170eSAndroid Build Coastguard Workerwhich is described in the comments. The default value is
113*7594170eSAndroid Build Coastguard Worker[resolved](https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/compiler.go;l=265;drc=135bf55281d79576f33469ce4f9abc517a614af5)
114*7594170eSAndroid Build Coastguard Workerwhen compiler flags are being determined.
115*7594170eSAndroid Build Coastguard Worker
116*7594170eSAndroid Build Coastguard WorkerIn general, these can be set in an Android.bp file. However, if the property is
117*7594170eSAndroid Build Coastguard Workertagged with `` `blueprint:"mutated"` ``, it can only be set programmatically
118*7594170eSAndroid Build Coastguard Workerwithin Blueprint/Soong. Additionally, `mutated` tagged properties also support
119*7594170eSAndroid Build Coastguard Worker`map` and `int` types in addition to those mentioned above. These `mutated`
120*7594170eSAndroid Build Coastguard Workerproperties are used to propagate data that gets set during mutations, which
121*7594170eSAndroid Build Coastguard Workerensures that the information is copied successfully to module variants during
122*7594170eSAndroid Build Coastguard Workermutation.
123*7594170eSAndroid Build Coastguard Worker
124*7594170eSAndroid Build Coastguard WorkerSoong supports additional property tags to provide additional
125*7594170eSAndroid Build Coastguard Workerfunctionality/information about a property:
126*7594170eSAndroid Build Coastguard Worker
127*7594170eSAndroid Build Coastguard Worker*   `` `android:arch_variant` ``: This specifies that a property can be
128*7594170eSAndroid Build Coastguard Worker    configured for different architectures, operating systems, targets, etc. The
129*7594170eSAndroid Build Coastguard Worker    [arch mutator](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/arch.go;l=597;drc=135bf55281d79576f33469ce4f9abc517a614af5),
130*7594170eSAndroid Build Coastguard Worker    will merge target-specific properties into the correct variant for
131*7594170eSAndroid Build Coastguard Worker    properties with this tag.
132*7594170eSAndroid Build Coastguard Worker
133*7594170eSAndroid Build Coastguard Worker    Note: if a nested property is arch-variant, all recursively nesting structs
134*7594170eSAndroid Build Coastguard Worker    that can be specified in an Android.bp file must also be tagged as
135*7594170eSAndroid Build Coastguard Worker    arch-variant.
136*7594170eSAndroid Build Coastguard Worker
137*7594170eSAndroid Build Coastguard Worker*   `` `android:variant_prepend` ``: When merging properties for the arch
138*7594170eSAndroid Build Coastguard Worker    variant, the arch-specific values should be *prepended* rather than appended
139*7594170eSAndroid Build Coastguard Worker    to existing property values.
140*7594170eSAndroid Build Coastguard Worker
141*7594170eSAndroid Build Coastguard Worker*   `` `android:path` ``: This specifies that this property will contain some
142*7594170eSAndroid Build Coastguard Worker    combination of:
143*7594170eSAndroid Build Coastguard Worker
144*7594170eSAndroid Build Coastguard Worker    *   module-relative paths
145*7594170eSAndroid Build Coastguard Worker    *   references to other modules in the form:
146*7594170eSAndroid Build Coastguard Worker        *   `":<name>{.<tag>}"`, where `{.<tag>}` is optional to specify a
147*7594170eSAndroid Build Coastguard Worker            non-default output file, specific to the module type
148*7594170eSAndroid Build Coastguard Worker        *   `"<namespace>:<name>{.<tag>}""`
149*7594170eSAndroid Build Coastguard Worker
150*7594170eSAndroid Build Coastguard Worker    Note: Dependencies to other modules for these properties will be
151*7594170eSAndroid Build Coastguard Worker    automatically added by the
152*7594170eSAndroid Build Coastguard Worker    [pathdeps mutator](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/path_properties.go;l=40;drc=40131a3f9e5ac974a44d3bd1293d31d585dc3a07).
153*7594170eSAndroid Build Coastguard Worker
154*7594170eSAndroid Build Coastguard Worker#### Attributes
155*7594170eSAndroid Build Coastguard Worker
156*7594170eSAndroid Build Coastguard WorkerSimilar to properties,
157*7594170eSAndroid Build Coastguard Worker[attributes](https://docs.bazel.build/versions/main/skylark/lib/attr.html) only
158*7594170eSAndroid Build Coastguard Workersupport a few types. The difference is that Bazel attributes cannot be nested .
159*7594170eSAndroid Build Coastguard Worker
160*7594170eSAndroid Build Coastguard WorkerSome attributes are
161*7594170eSAndroid Build Coastguard Worker[common](https://docs.bazel.build/versions/2.1.0/be/common-definitions.html#common-attributes)
162*7594170eSAndroid Build Coastguard Workeracross many/all rule classes, including (but not limited to) `name`, `tag`,
163*7594170eSAndroid Build Coastguard Worker`visibility`.
164*7594170eSAndroid Build Coastguard Worker
165*7594170eSAndroid Build Coastguard WorkerThe definition of an attribute can contain settings, such as: its default value,
166*7594170eSAndroid Build Coastguard Workerwhether it is mandatory ot have a value, and its documentation.
167*7594170eSAndroid Build Coastguard Worker
168*7594170eSAndroid Build Coastguard WorkerTo specify a source file or reference to another module, use `label` or
169*7594170eSAndroid Build Coastguard Worker`label_list` attribute types (rather than regular `string` or `string_list`
170*7594170eSAndroid Build Coastguard Workertypes). These support additional restrictions (as compared to `string*` types),
171*7594170eSAndroid Build Coastguard Workersuch as:
172*7594170eSAndroid Build Coastguard Worker
173*7594170eSAndroid Build Coastguard Worker*   whether files are supported
174*7594170eSAndroid Build Coastguard Worker*   the providers that must be given by a dependency
175*7594170eSAndroid Build Coastguard Worker*   whether the dependency should be executable
176*7594170eSAndroid Build Coastguard Worker*   the configuration (host, target)
177*7594170eSAndroid Build Coastguard Worker*   aspects
178*7594170eSAndroid Build Coastguard Worker
179*7594170eSAndroid Build Coastguard WorkerUnlike Soong, when accessing this attribute within the rule's implementation (at
180*7594170eSAndroid Build Coastguard Workeranlysis time), the label(s) will be resolved to the file or target they refer
181*7594170eSAndroid Build Coastguard Workerto.
182*7594170eSAndroid Build Coastguard Worker
183*7594170eSAndroid Build Coastguard WorkerAttributes do not need to specify whether they accept
184*7594170eSAndroid Build Coastguard Worker[configurable attribute](https://docs.bazel.build/versions/main/configurable-attributes.html).
185*7594170eSAndroid Build Coastguard WorkerHowever, the rule definition can specify the configuration or specify a
186*7594170eSAndroid Build Coastguard Worker[configuration transition](https://docs.bazel.build/versions/main/skylark/lib/transition.html).
187*7594170eSAndroid Build Coastguard Worker
188*7594170eSAndroid Build Coastguard WorkerHowever, not all target definitions within a `BUILD` file are invoking a rule.
189*7594170eSAndroid Build Coastguard WorkerInstead, they may invoke a Starlark macro, which is a load-time wrapper around
190*7594170eSAndroid Build Coastguard Workerrules. Arguments for a macro are not typed. If macros are used, their arguments
191*7594170eSAndroid Build Coastguard Workerwould have to be wrangled into an attribute-compatible type.
192*7594170eSAndroid Build Coastguard Worker
193*7594170eSAndroid Build Coastguard Worker### LoadHooks
194*7594170eSAndroid Build Coastguard Worker
195*7594170eSAndroid Build Coastguard Worker[LoadHooks](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/hooks.go;l=24-36;drc=07656410df1836a70bea3054e50bb410ecbf8e07)
196*7594170eSAndroid Build Coastguard Workerprovide access to :
197*7594170eSAndroid Build Coastguard Worker
198*7594170eSAndroid Build Coastguard Worker*   append/prepend additional properties to the module
199*7594170eSAndroid Build Coastguard Worker    (`AppendProperties`/`PrependProperties`)
200*7594170eSAndroid Build Coastguard Worker*   create a new module `CreateModule`
201*7594170eSAndroid Build Coastguard Worker
202*7594170eSAndroid Build Coastguard Worker`LoadHooks` make it easier to extend existing module factories to always specify
203*7594170eSAndroid Build Coastguard Workercertain properties or to split a single `Android.bp` definition into multiple
204*7594170eSAndroid Build Coastguard WorkerModule instances .
205*7594170eSAndroid Build Coastguard Worker
206*7594170eSAndroid Build Coastguard Worker### Build Statement (ninja) {#ninja-build-statement}
207*7594170eSAndroid Build Coastguard Worker
208*7594170eSAndroid Build Coastguard Worker[Ninja build statements](https://ninja-build.org/manual.html#_build_statements) can be
209*7594170eSAndroid Build Coastguard Workerexpanded from [Ninja rules](https://ninja-build.org/manual.html#_rules), which are like
210*7594170eSAndroid Build Coastguard Workertemplates.
211*7594170eSAndroid Build Coastguard Worker
212*7594170eSAndroid Build Coastguard Worker```
213*7594170eSAndroid Build Coastguard Worker# rule
214*7594170eSAndroid Build Coastguard Workerrule cattool
215*7594170eSAndroid Build Coastguard Worker  depfile = out/test/depfile.d
216*7594170eSAndroid Build Coastguard Worker  command = ${in} ${out}
217*7594170eSAndroid Build Coastguard Worker
218*7594170eSAndroid Build Coastguard Worker# build statement
219*7594170eSAndroid Build Coastguard Workerbuild out/test/output.txt: cattool test/cattool.sh test/one test/two
220*7594170eSAndroid Build Coastguard Worker
221*7594170eSAndroid Build Coastguard Worker# build statement
222*7594170eSAndroid Build Coastguard Workerbuild out/test/other_output.txt: cattool test/cattool.sh test/three test/four
223*7594170eSAndroid Build Coastguard Worker```
224*7594170eSAndroid Build Coastguard Worker
225*7594170eSAndroid Build Coastguard WorkerRules for `Android.mk` modules (`out/build-<target>.ninja`) and build statements
226*7594170eSAndroid Build Coastguard Workerare 1:1. That is every rule is only used once by a single build statement.
227*7594170eSAndroid Build Coastguard Worker
228*7594170eSAndroid Build Coastguard WorkerSoong (`out/soong/build.ninja`) rules are reused extensively in build statements
229*7594170eSAndroid Build Coastguard Worker(1:many). For example the `Cp` rule is a commonly used rule for creating build
230*7594170eSAndroid Build Coastguard Workerstatements which copy files.
231*7594170eSAndroid Build Coastguard Worker
232*7594170eSAndroid Build Coastguard Worker### Ninja Rules in Soong {#ninja-rules}
233*7594170eSAndroid Build Coastguard Worker
234*7594170eSAndroid Build Coastguard WorkerIn Soong, Ninja rules can be defined in two ways:
235*7594170eSAndroid Build Coastguard Worker
236*7594170eSAndroid Build Coastguard Worker*   [rule_builder](http://cs.android.com/android/platform/superproject/+/master:build/soong/android/rule_builder.go)
237*7594170eSAndroid Build Coastguard Worker*   [package_ctx](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/package_ctx.go;l=102-293;drc=77cdcfdeafd383ef1f1214226c47eb20c902a28f)
238*7594170eSAndroid Build Coastguard Worker
239*7594170eSAndroid Build Coastguard Worker### Blueprint Generate & Write phase {#blueprint-analysis}
240*7594170eSAndroid Build Coastguard Worker
241*7594170eSAndroid Build Coastguard Worker1.  [`ResolveDependencies`](https://cs.android.com/android/platform/superproject/+/master:build/blueprint/context.go;l=1547;drc=5c4abb15e3b84ab0bcedfa119e2feb397d1fb106)
242*7594170eSAndroid Build Coastguard Worker    Running a series of Mutators, to add dependencies, split modules with
243*7594170eSAndroid Build Coastguard Worker    variations, etc
244*7594170eSAndroid Build Coastguard Worker1.  [`PrepareBuildActions`](https://cs.android.com/android/platform/superproject/+/master:build/blueprint/context.go;l=2367;drc=5c4abb15e3b84ab0bcedfa119e2feb397d1fb106):
245*7594170eSAndroid Build Coastguard Worker
246*7594170eSAndroid Build Coastguard Worker    1.  Running Modules’ `GenerateBuildActions` to generate Ninja statements,
247*7594170eSAndroid Build Coastguard Worker        which in turn calls each module's
248*7594170eSAndroid Build Coastguard Worker        [`GenerateAndroidBuildActions`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=445-448;drc=8631cc7327919845c9d9037188cbd483d22ba077).
249*7594170eSAndroid Build Coastguard Worker    1.  Running Singletons to generate Ninja statements that generate docs,
250*7594170eSAndroid Build Coastguard Worker        android.mk statements, etc
251*7594170eSAndroid Build Coastguard Worker
252*7594170eSAndroid Build Coastguard Worker### Soong namespaces {#namespace}
253*7594170eSAndroid Build Coastguard Worker
254*7594170eSAndroid Build Coastguard WorkerModule
255*7594170eSAndroid Build Coastguard Worker[Namespaces](https://android.googlesource.com/platform/build/soong/+/master/README.md#namespaces)
256*7594170eSAndroid Build Coastguard Workercan import other namespaces, and there’s a module name lookup algorithm which
257*7594170eSAndroid Build Coastguard Workerterminates in the global namespace.
258*7594170eSAndroid Build Coastguard Worker
259*7594170eSAndroid Build Coastguard WorkerNote: this is not widely used and most Soong modules are in the global
260*7594170eSAndroid Build Coastguard Workernamespace.
261*7594170eSAndroid Build Coastguard Worker
262*7594170eSAndroid Build Coastguard Worker### Bazel packages {#pkgs}
263*7594170eSAndroid Build Coastguard Worker
264*7594170eSAndroid Build Coastguard Worker[Packages](https://docs.bazel.build/versions/main/build-ref.html#packages) can
265*7594170eSAndroid Build Coastguard Workernest subpackages recursively, but they are independent containers of Bazel
266*7594170eSAndroid Build Coastguard Workertargets. This means that Bazel target names only need to be unique within a
267*7594170eSAndroid Build Coastguard Workerpackage.
268*7594170eSAndroid Build Coastguard Worker
269*7594170eSAndroid Build Coastguard Worker### Mutators
270*7594170eSAndroid Build Coastguard Worker
271*7594170eSAndroid Build Coastguard Workerblueprint invokes mutators are invoking in the order they are registered (e.g.
272*7594170eSAndroid Build Coastguard Workertop-down and bottom-up can be interleaved). Each mutator applys a single
273*7594170eSAndroid Build Coastguard Workervisitation to every module in the graph.
274*7594170eSAndroid Build Coastguard Worker
275*7594170eSAndroid Build Coastguard WorkerMutators visiting module can parallelized, while maintaining their ordering, by
276*7594170eSAndroid Build Coastguard Workercalling `.Parallel()`.
277*7594170eSAndroid Build Coastguard Worker
278*7594170eSAndroid Build Coastguard WorkerWhile top-down and bottom-up mutators differ in their purposes, the interface
279*7594170eSAndroid Build Coastguard Workeravailable to each contains many similarities. Both have access to:
280*7594170eSAndroid Build Coastguard Worker[`BaseModuleContext`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=139;drc=8631cc7327919845c9d9037188cbd483d22ba077)
281*7594170eSAndroid Build Coastguard Workerand
282*7594170eSAndroid Build Coastguard Worker[`BaseMutatorContext`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/mutator.go;l=246;drc=2ada09a5463a0108d713773679c5ba2c35450fa4).
283*7594170eSAndroid Build Coastguard Worker
284*7594170eSAndroid Build Coastguard WorkerIn addition to the registration order, Soong supports phase-based ordering of
285*7594170eSAndroid Build Coastguard Workermutators:
286*7594170eSAndroid Build Coastguard Worker
287*7594170eSAndroid Build Coastguard Worker1.  Pre-Arch: mutators that need to run before arch-variation. For example,
288*7594170eSAndroid Build Coastguard Worker    defaults are handled at this stage such properties from defaults are
289*7594170eSAndroid Build Coastguard Worker    correctly propagated to arch-variants later.
290*7594170eSAndroid Build Coastguard Worker
291*7594170eSAndroid Build Coastguard Worker1.  (Hard-coded)
292*7594170eSAndroid Build Coastguard Worker    [`archMutator`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/arch.go;l=597;drc=135bf55281d79576f33469ce4f9abc517a614af5)
293*7594170eSAndroid Build Coastguard Worker    splits a module into the appropriate target(s). Next, the arch- and
294*7594170eSAndroid Build Coastguard Worker    OS-specific properties are merged into the appropriate variant.
295*7594170eSAndroid Build Coastguard Worker
296*7594170eSAndroid Build Coastguard Worker1.  Pre-Deps: mutators that can/need to run before deps have been resolved, for
297*7594170eSAndroid Build Coastguard Worker    instance, creating variations that have an impact on dependency resolution.
298*7594170eSAndroid Build Coastguard Worker
299*7594170eSAndroid Build Coastguard Worker1.  (Hard-coded)
300*7594170eSAndroid Build Coastguard Worker    [`depsMutator`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/mutator.go;l=502;drc=2ada09a5463a0108d713773679c5ba2c35450fa4),
301*7594170eSAndroid Build Coastguard Worker    which calls the `DepsMutator` function that *must* be part of a Soong
302*7594170eSAndroid Build Coastguard Worker    `Module`'s interface.
303*7594170eSAndroid Build Coastguard Worker
304*7594170eSAndroid Build Coastguard Worker1.  Post-Deps: mutators that need to run after deps have been resolved
305*7594170eSAndroid Build Coastguard Worker
306*7594170eSAndroid Build Coastguard Worker1.  Final-Deps like post-deps but variations cannot be created
307*7594170eSAndroid Build Coastguard Worker
308*7594170eSAndroid Build Coastguard Worker#### Top-down Mutator
309*7594170eSAndroid Build Coastguard Worker
310*7594170eSAndroid Build Coastguard WorkerA top-down mutator is invoked on a module before its dependencies.
311*7594170eSAndroid Build Coastguard Worker
312*7594170eSAndroid Build Coastguard WorkerThe general purpose is to propagate dependency info from a module to its
313*7594170eSAndroid Build Coastguard Workerdependencies.
314*7594170eSAndroid Build Coastguard Worker
315*7594170eSAndroid Build Coastguard Worker#### Bottom-up Mutator
316*7594170eSAndroid Build Coastguard Worker
317*7594170eSAndroid Build Coastguard WorkerA bottom-up mutator is invoked on a module only after the mutator has been
318*7594170eSAndroid Build Coastguard Workerinvoked on all its dependencies.
319*7594170eSAndroid Build Coastguard Worker
320*7594170eSAndroid Build Coastguard WorkerThe general purpose of a bottom-up mutator is to split modules into variants.
321*7594170eSAndroid Build Coastguard Worker
322*7594170eSAndroid Build Coastguard Worker### Soong/Blueprint Variation {#variation}
323*7594170eSAndroid Build Coastguard Worker
324*7594170eSAndroid Build Coastguard WorkerA tuple (name of mutator, variation / config value) passed to
325*7594170eSAndroid Build Coastguard Worker`CreateVariations`.
326*7594170eSAndroid Build Coastguard Worker
327*7594170eSAndroid Build Coastguard Worker### Configuration {#config}
328*7594170eSAndroid Build Coastguard Worker
329*7594170eSAndroid Build Coastguard WorkerSoong's config process encompasses both *what* should build and *how* it should
330*7594170eSAndroid Build Coastguard Workerbuild. This section focuses on the *how* aspect.
331*7594170eSAndroid Build Coastguard Worker
332*7594170eSAndroid Build Coastguard WorkerWe do not cover how Soong's configuration will be implemented in Bazel, but the
333*7594170eSAndroid Build Coastguard Workergeneral capabilities of Bazel to configure builds.
334*7594170eSAndroid Build Coastguard Worker
335*7594170eSAndroid Build Coastguard Worker#### Soong
336*7594170eSAndroid Build Coastguard Worker
337*7594170eSAndroid Build Coastguard WorkerAndroid users can configure their builds based on:
338*7594170eSAndroid Build Coastguard Worker
339*7594170eSAndroid Build Coastguard Worker*   Specifying a target (via lunch, banchan, tapas, or Soong’s command line
340*7594170eSAndroid Build Coastguard Worker    options)
341*7594170eSAndroid Build Coastguard Worker*   Environment variables
342*7594170eSAndroid Build Coastguard Worker
343*7594170eSAndroid Build Coastguard WorkerSome environment variables or command line options are used directly to alter
344*7594170eSAndroid Build Coastguard Workerthe build. However, specification of target product encompasses many aspects of
345*7594170eSAndroid Build Coastguard Workerboth *what* and *how* things are built. This configuration is currently handled
346*7594170eSAndroid Build Coastguard Workerwithin Make but is in the process of being migrated to Starlark.
347*7594170eSAndroid Build Coastguard Worker
348*7594170eSAndroid Build Coastguard WorkerSoong
349*7594170eSAndroid Build Coastguard Worker[invokes Kati](https://cs.android.com/android/platform/superproject/+/master:build/soong/ui/build/dumpvars.go;drc=7ae80a704494bbb934dced97ed97eb55a21a9a00)
350*7594170eSAndroid Build Coastguard Workerto run in a "config" mode, also commonly known as "product config". This mode
351*7594170eSAndroid Build Coastguard Workerlimits the scope of what `.mk` files are parsed. The product-specific handlers
352*7594170eSAndroid Build Coastguard Workerare largely in:
353*7594170eSAndroid Build Coastguard Worker
354*7594170eSAndroid Build Coastguard Worker*   [`product_config.mk`](https://cs.android.com/android/platform/superproject/+/master:build/make/core/product_config.mk;drc=d189ab71f3505ea28324ebfaced2466af5eb0af7):
355*7594170eSAndroid Build Coastguard Worker    this subset of functionality is also commonly referred to as "product
356*7594170eSAndroid Build Coastguard Worker    config"
357*7594170eSAndroid Build Coastguard Worker*   [`board_config.mk`](https://cs.android.com/android/platform/superproject/+/master:build/make/core/board_config.mk)
358*7594170eSAndroid Build Coastguard Worker
359*7594170eSAndroid Build Coastguard WorkerHowever, these cover only a subset of
360*7594170eSAndroid Build Coastguard Worker[`config.mk`](https://cs.android.com/android/platform/superproject/+/master:build/make/core/config.mk).
361*7594170eSAndroid Build Coastguard WorkerThis ensures that all values have appropriate defaults and specify details
362*7594170eSAndroid Build Coastguard Workernecessary to the build. Some examples:
363*7594170eSAndroid Build Coastguard Worker
364*7594170eSAndroid Build Coastguard Worker*   [handling of version defaults](https://cs.android.com/android/platform/superproject/+/master:build/make/core/version_defaults.mk)
365*7594170eSAndroid Build Coastguard Worker*   [rbe setup](https://cs.android.com/android/platform/superproject/+/master:build/make/core/rbe.mk)
366*7594170eSAndroid Build Coastguard Worker*   [user-defined config](https://cs.android.com/android/platform/superproject/+/master:build/make/core/config.mk;l=300-308;drc=ee20ae1a8dcdfe7b843d65099000708800d9b93a):
367*7594170eSAndroid Build Coastguard Worker    [buildspec.mk](http://cs.android.com/android/platform/superproject/+/master:build/make/buildspec.mk.default)
368*7594170eSAndroid Build Coastguard Worker    is similar to
369*7594170eSAndroid Build Coastguard Worker    [`.bazelrc`](https://docs.bazel.build/versions/main/guide.html#bazelrc-the-bazel-configuration-file)
370*7594170eSAndroid Build Coastguard Worker    file.
371*7594170eSAndroid Build Coastguard Worker*   ensuring
372*7594170eSAndroid Build Coastguard Worker    [`PRODUCT_SHIPPING_API_LEVEL`](https://cs.android.com/android/platform/superproject/+/master:build/make/core/config.mk;l=729-745;drc=ee20ae1a8dcdfe7b843d65099000708800d9b93a)
373*7594170eSAndroid Build Coastguard Worker    is defaulted if not specified by the target.
374*7594170eSAndroid Build Coastguard Worker
375*7594170eSAndroid Build Coastguard WorkerFinally, Kati dumps variables to be consumed by Soong:
376*7594170eSAndroid Build Coastguard Worker
377*7594170eSAndroid Build Coastguard Worker*   environment variables specifically requested by Soong
378*7594170eSAndroid Build Coastguard Worker*   writes
379*7594170eSAndroid Build Coastguard Worker    [`soong.variables`](http://cs.android.com/android/platform/superproject/+/master:build/make/core/soong_config.mk),
380*7594170eSAndroid Build Coastguard Worker    a JSON file
381*7594170eSAndroid Build Coastguard Worker
382*7594170eSAndroid Build Coastguard WorkerThroughout Soong, environment variables can be accessed to alter the build via
383*7594170eSAndroid Build Coastguard Workerthe `Config`:
384*7594170eSAndroid Build Coastguard Worker
385*7594170eSAndroid Build Coastguard Worker*   [`GetEnv`](http://cs.android.com/search?q=f:soong%20%5C.GetEnv%5C%28%20-f:%2Fui%2F%20-f:%2Fcmd%2F&sq=)
386*7594170eSAndroid Build Coastguard Worker*   [`GetEnvWithDefault`](http://cs.android.com/search?q=f:soong%20%5C.GetEnvWithDefault%5C%28%20-f:%2Fui%2F%20-f:%2Fcmd%2F&sq=)
387*7594170eSAndroid Build Coastguard Worker*   [`IsEnvTrue`](http://cs.android.com/search?q=f:soong%20%5C.IsEnvTrue%5C%28%20-f:%2Fui%2F%20-f:%2Fcmd%2F&sq=)
388*7594170eSAndroid Build Coastguard Worker*   [`IsEnvFalse`](http://cs.android.com/search?q=f:soong%20%5C.IsEnvFalse%5C%28%20-f:%2Fui%2F%20-f:%2Fcmd%2F&sq=)
389*7594170eSAndroid Build Coastguard Worker
390*7594170eSAndroid Build Coastguard WorkerSoong
391*7594170eSAndroid Build Coastguard Worker[loads the `soong.variables`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/config.go;l=174;drc=b078ade28d94c85cec78e9776eb31948a5647070)
392*7594170eSAndroid Build Coastguard Workerconfig file, stored as
393*7594170eSAndroid Build Coastguard Worker[`productVariables`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/variable.go;l=163;drc=16e77a9b303a71018eb6630f12f1414cd6ad615c).
394*7594170eSAndroid Build Coastguard WorkerThese variables are used in three ways:
395*7594170eSAndroid Build Coastguard Worker
396*7594170eSAndroid Build Coastguard Worker*   Direct access from `Config`, for example: paths can be
397*7594170eSAndroid Build Coastguard Worker    [opted out](https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/sanitize.go;l=364,371,393;drc=582fc2d1dde6c70687e6a0bea192f2a2ef67bbd5)
398*7594170eSAndroid Build Coastguard Worker    of specific sanitizers
399*7594170eSAndroid Build Coastguard Worker*   In limited cases, users can use these within their `Android.bp` file to
400*7594170eSAndroid Build Coastguard Worker    control what is built or perform variable replacement.
401*7594170eSAndroid Build Coastguard Worker    [`variableProperties`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/variable.go;l=38;drc=16e77a9b303a71018eb6630f12f1414cd6ad615c)
402*7594170eSAndroid Build Coastguard Worker    limits which configuration variables can be specified within an `Android.bp`
403*7594170eSAndroid Build Coastguard Worker    file and which properties they can apply to. The values specified within an
404*7594170eSAndroid Build Coastguard Worker    `Android.bp` file, are merged/replaced by the
405*7594170eSAndroid Build Coastguard Worker    [`VariableMutator`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/variable.go;l=539;drc=16e77a9b303a71018eb6630f12f1414cd6ad615c),
406*7594170eSAndroid Build Coastguard Worker    which appends performs string replacement if requested and merges the
407*7594170eSAndroid Build Coastguard Worker    properties into the modules.
408*7594170eSAndroid Build Coastguard Worker*   Through
409*7594170eSAndroid Build Coastguard Worker    [Soong Config Variables](https://android.googlesource.com/platform/build/soong/+/refs/heads/master/README.md#soong-config-variables):
410*7594170eSAndroid Build Coastguard Worker    which allow users to specify additional configuration variables that can be
411*7594170eSAndroid Build Coastguard Worker    used within an `Android.bp` file for the module type and properties they
412*7594170eSAndroid Build Coastguard Worker    request. Soong config variable structs are
413*7594170eSAndroid Build Coastguard Worker    [dynamically generated](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/soongconfig/modules.go;l=257;drc=997f27aa0353dabf76d063d78ee5d4495da85651)
414*7594170eSAndroid Build Coastguard Worker    via reflection. In the
415*7594170eSAndroid Build Coastguard Worker    [factory](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/soong_config_modules.go;l=423;drc=18fd09998223d004a926b02938e4cb588e4cc934),
416*7594170eSAndroid Build Coastguard Worker    the properties to merge into the module instance are
417*7594170eSAndroid Build Coastguard Worker    [identified](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/soongconfig/modules.go;l=416;drc=997f27aa0353dabf76d063d78ee5d4495da85651)
418*7594170eSAndroid Build Coastguard Worker    based on the config variable's type.
419*7594170eSAndroid Build Coastguard Worker
420*7594170eSAndroid Build Coastguard WorkerThe product configuration also provides information about architecture and
421*7594170eSAndroid Build Coastguard Workeroperating system, both for target(s) and host. This is used within the
422*7594170eSAndroid Build Coastguard Worker[`archMutator`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/arch.go;l=569-597;drc=135bf55281d79576f33469ce4f9abc517a614af5)
423*7594170eSAndroid Build Coastguard Workerto split a module into the required variants and merge target-specific
424*7594170eSAndroid Build Coastguard Workerproperties into the appropriate variant. Only properties which have been tagged
425*7594170eSAndroid Build Coastguard Workerwith `android:"arch_variant"` can be specified within an `Android.bp` as
426*7594170eSAndroid Build Coastguard Workerarch/os/target-specific. For example:
427*7594170eSAndroid Build Coastguard Worker
428*7594170eSAndroid Build Coastguard Worker```go
429*7594170eSAndroid Build Coastguard Workertype properties struct {
430*7594170eSAndroid Build Coastguard Worker  // this property will be arch-variant
431*7594170eSAndroid Build Coastguard Worker  Arch_variant_not_nested *string `android:"arch_variant"`
432*7594170eSAndroid Build Coastguard Worker
433*7594170eSAndroid Build Coastguard Worker  Nested_with_arch_variant struct {
434*7594170eSAndroid Build Coastguard Worker    // this property is arch-variant
435*7594170eSAndroid Build Coastguard Worker    Arch_variant_nested *string `android:"arch_variant"`
436*7594170eSAndroid Build Coastguard Worker
437*7594170eSAndroid Build Coastguard Worker    // this property is **not** arch-variant
438*7594170eSAndroid Build Coastguard Worker    Not_arch_variant_nested *string
439*7594170eSAndroid Build Coastguard Worker  } `android:"arch_variant"`
440*7594170eSAndroid Build Coastguard Worker
441*7594170eSAndroid Build Coastguard Worker  Nested_no_arch_variant struct {
442*7594170eSAndroid Build Coastguard Worker    // this property is **NOT** arch-variant
443*7594170eSAndroid Build Coastguard Worker    No_arch_variant_nested_not_arch_variant *string `android:"arch_variant"`
444*7594170eSAndroid Build Coastguard Worker
445*7594170eSAndroid Build Coastguard Worker    // this property is **not** arch-variant
446*7594170eSAndroid Build Coastguard Worker    No_arch_variant_nested *string
447*7594170eSAndroid Build Coastguard Worker  }
448*7594170eSAndroid Build Coastguard Worker}
449*7594170eSAndroid Build Coastguard Worker```
450*7594170eSAndroid Build Coastguard Worker
451*7594170eSAndroid Build Coastguard WorkerThe arch/os/target-specific structs are
452*7594170eSAndroid Build Coastguard Worker[dynamically generated](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/arch.go;l=780-787;drc=135bf55281d79576f33469ce4f9abc517a614af5)
453*7594170eSAndroid Build Coastguard Workerbased on the tags using reflection.
454*7594170eSAndroid Build Coastguard Worker
455*7594170eSAndroid Build Coastguard Worker#### Bazel
456*7594170eSAndroid Build Coastguard Worker
457*7594170eSAndroid Build Coastguard WorkerBazel documentation covers configurable builds fairly extensively, so this is a
458*7594170eSAndroid Build Coastguard Workershort overview that primarily links to existing Bazel documentation rather than
459*7594170eSAndroid Build Coastguard Workerrepeating it here.
460*7594170eSAndroid Build Coastguard Worker
461*7594170eSAndroid Build Coastguard Worker[Configurable attributes](https://docs.bazel.build/versions/main/configurable-attributes.html),
462*7594170eSAndroid Build Coastguard Worker(aka `select()`) allows users to toggle values of build rule attributes on the
463*7594170eSAndroid Build Coastguard Workercommand line.
464*7594170eSAndroid Build Coastguard Worker
465*7594170eSAndroid Build Coastguard WorkerWithin a `rule`, the value of a `select` will have been resolved based on the
466*7594170eSAndroid Build Coastguard Workerconfiguration at analysis phase. However, within a macro (at loading phase,
467*7594170eSAndroid Build Coastguard Workerbefore analysis phase), a `select()` is an opaque type that cannot be inspected.
468*7594170eSAndroid Build Coastguard WorkerThis restricts what operations are possible on the arguments passed to a macro.
469*7594170eSAndroid Build Coastguard Worker
470*7594170eSAndroid Build Coastguard WorkerThe conditions within a `select` statement are one of:
471*7594170eSAndroid Build Coastguard Worker
472*7594170eSAndroid Build Coastguard Worker*   [`config_setting`](https://docs.bazel.build/versions/main/be/general.html#config_setting)
473*7594170eSAndroid Build Coastguard Worker*   [`constraint_value`](https://docs.bazel.build/versions/main/be/platform.html#constraint_value)
474*7594170eSAndroid Build Coastguard Worker
475*7594170eSAndroid Build Coastguard WorkerA `config_setting` is a collection of build settings, whether defined by Bazel,
476*7594170eSAndroid Build Coastguard Workeror user-defined.
477*7594170eSAndroid Build Coastguard Worker
478*7594170eSAndroid Build Coastguard WorkerUser-defined
479*7594170eSAndroid Build Coastguard Worker[build settings](https://docs.bazel.build/versions/main/skylark/config.html#defining-build-settings)
480*7594170eSAndroid Build Coastguard Workerallow users to specify additional configuration, which *optionally* can be
481*7594170eSAndroid Build Coastguard Workerspecified as a flag. In addition to specifying build settings within a
482*7594170eSAndroid Build Coastguard Worker`config_setting`, rules can depend directly on them.
483*7594170eSAndroid Build Coastguard Worker
484*7594170eSAndroid Build Coastguard WorkerIn addition, Bazel supports
485*7594170eSAndroid Build Coastguard Worker[`platform`s](https://docs.bazel.build/versions/main/be/platform.html#platform),
486*7594170eSAndroid Build Coastguard Workerwhich is a named collection of constraints. Both a target and host platform can
487*7594170eSAndroid Build Coastguard Workerbe specified on the command line.
488*7594170eSAndroid Build Coastguard Worker[More about platforms](https://docs.bazel.build/versions/main/platforms.html).
489*7594170eSAndroid Build Coastguard Worker
490*7594170eSAndroid Build Coastguard Worker## Communicating between modules/targets
491*7594170eSAndroid Build Coastguard Worker
492*7594170eSAndroid Build Coastguard Worker### Soong communication
493*7594170eSAndroid Build Coastguard Worker
494*7594170eSAndroid Build Coastguard WorkerThere are many mechanisms to communicate between Soong modules. Because of this,
495*7594170eSAndroid Build Coastguard Workerit can be difficult to trace the information communicated between modules.
496*7594170eSAndroid Build Coastguard Worker
497*7594170eSAndroid Build Coastguard Worker#### Dependency Tags {#deptags}
498*7594170eSAndroid Build Coastguard Worker
499*7594170eSAndroid Build Coastguard WorkerDependency tags are the primary way to filter module dependencies by what
500*7594170eSAndroid Build Coastguard Workerpurpose the dependency serves. For example, to filter for annotation processor
501*7594170eSAndroid Build Coastguard Workerplugins in the deps of a Java library module, use `ctx.VisitDirectDeps` and
502*7594170eSAndroid Build Coastguard Workercheck the tags:
503*7594170eSAndroid Build Coastguard Worker
504*7594170eSAndroid Build Coastguard Worker```
505*7594170eSAndroid Build Coastguard Workerctx.VisitDirectDeps(func(module android.Module) {
506*7594170eSAndroid Build Coastguard Worker  tag := ctx.OtherModuleDependencyTag(module)
507*7594170eSAndroid Build Coastguard Worker  if tag == pluginTag { patchPaths += ":" + strings.Split(ctx.OtherModuleDir(module), "/")[0] }
508*7594170eSAndroid Build Coastguard Worker  }
509*7594170eSAndroid Build Coastguard Worker)
510*7594170eSAndroid Build Coastguard Worker```
511*7594170eSAndroid Build Coastguard Worker
512*7594170eSAndroid Build Coastguard WorkerAt this point the module managing the dependency, may have enough information to
513*7594170eSAndroid Build Coastguard Workercast it to a specific type or interface and perform more specific operations.
514*7594170eSAndroid Build Coastguard Worker
515*7594170eSAndroid Build Coastguard WorkerFor instance, shared libraries and executables have
516*7594170eSAndroid Build Coastguard Worker[special handling](http://cs.android.com/android/platform/superproject/+/master:build/soong/cc/cc.go;l=2771-2776;drc=5df7bd33f7b64e2b880856e3193419697a8fb693)
517*7594170eSAndroid Build Coastguard Workerfor static library dependencies: where the coverage files and source based ABI
518*7594170eSAndroid Build Coastguard Workerdump files are needed explicitly. Based on the dependency tag, the module is
519*7594170eSAndroid Build Coastguard Workercast to a concrete type, like `cc.Module`, where internal fields are accessed
520*7594170eSAndroid Build Coastguard Workerand used to obtain the desired data.
521*7594170eSAndroid Build Coastguard Worker
522*7594170eSAndroid Build Coastguard WorkerUsage of dependency tags can be more evident when used between module types
523*7594170eSAndroid Build Coastguard Workerrepresenting different langauges, as the functions must be exported in Go due to
524*7594170eSAndroid Build Coastguard WorkerSoong's language-based package layout. For example, rust uses `cc` module's
525*7594170eSAndroid Build Coastguard Worker[`HasStubVariants`](http://cs.android.com/android/platform/superproject/+/master:build/soong/rust/rust.go;l=1457-1458;drc=9f59e8db270f58a3f2e4fe5bc041f84363a5877e).
526*7594170eSAndroid Build Coastguard Worker
527*7594170eSAndroid Build Coastguard Worker#### Interfaces
528*7594170eSAndroid Build Coastguard Worker
529*7594170eSAndroid Build Coastguard WorkerA common mechanism for a module to communicate information about itself is to
530*7594170eSAndroid Build Coastguard Workerdefine or implement a Go interface.
531*7594170eSAndroid Build Coastguard Worker
532*7594170eSAndroid Build Coastguard WorkerSome interfaces are common throughout Soong:
533*7594170eSAndroid Build Coastguard Worker
534*7594170eSAndroid Build Coastguard Worker*   [`SourceFileProducer`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=2967;drc=8707cd74bf083fe4a31e5f5aa5e74bd2a47e9e58),
535*7594170eSAndroid Build Coastguard Worker    by implementing `Srcs() Paths`
536*7594170eSAndroid Build Coastguard Worker*   [`OutputFileProducer`](http://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=2974;drc=8707cd74bf083fe4a31e5f5aa5e74bd2a47e9e58)
537*7594170eSAndroid Build Coastguard Worker    by implementing `OutputFiles(string) (Paths, error)`
538*7594170eSAndroid Build Coastguard Worker*   [`HostToolProvider`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=3032;drc=8707cd74bf083fe4a31e5f5aa5e74bd2a47e9e58)
539*7594170eSAndroid Build Coastguard Worker    by implementing `HostToolPath() OptionalPath`
540*7594170eSAndroid Build Coastguard Worker
541*7594170eSAndroid Build Coastguard Worker`SourceFileProducer` and `OutputFileProducer` are used to resolve references to
542*7594170eSAndroid Build Coastguard Workerother modules via `android:"path"` references.
543*7594170eSAndroid Build Coastguard Worker
544*7594170eSAndroid Build Coastguard WorkerModules may define additional interfaces. For example, `genrule` defines a
545*7594170eSAndroid Build Coastguard Worker[`SourceFileGenerator` interface](http://cs.android.com/android/platform/superproject/+/master:build/soong/genrule/genrule.go;l=98-102;drc=2ada09a5463a0108d713773679c5ba2c35450fa4).
546*7594170eSAndroid Build Coastguard Worker
547*7594170eSAndroid Build Coastguard Worker#### Providers
548*7594170eSAndroid Build Coastguard Worker
549*7594170eSAndroid Build Coastguard WorkerSoong has Bazel-inspired providers, but providers are not used in all cases yet.
550*7594170eSAndroid Build Coastguard Worker
551*7594170eSAndroid Build Coastguard WorkerUsages of providers are the easiest, simplest, and cleanest communication
552*7594170eSAndroid Build Coastguard Workerapproach in Soong.
553*7594170eSAndroid Build Coastguard Worker
554*7594170eSAndroid Build Coastguard WorkerIn the module providing information, these are specified via
555*7594170eSAndroid Build Coastguard Worker[`SetProvider`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=212;drc=5a34ffb350fb295780e5c373fd1c78430fa4e3ed)
556*7594170eSAndroid Build Coastguard Workerand
557*7594170eSAndroid Build Coastguard Worker[`SetVariationProvider`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/mutator.go;l=719;drc=5a34ffb350fb295780e5c373fd1c78430fa4e3ed).
558*7594170eSAndroid Build Coastguard Worker
559*7594170eSAndroid Build Coastguard WorkerIn the module retrieving information,
560*7594170eSAndroid Build Coastguard Worker[`HasProvider`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=205-206;drc=8631cc7327919845c9d9037188cbd483d22ba077)
561*7594170eSAndroid Build Coastguard Workerand
562*7594170eSAndroid Build Coastguard Worker[`Provider`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=198-203;drc=8631cc7327919845c9d9037188cbd483d22ba077)
563*7594170eSAndroid Build Coastguard Workeror
564*7594170eSAndroid Build Coastguard Worker[`OtherModuleHasProvider`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=195-196;drc=8631cc7327919845c9d9037188cbd483d22ba077)
565*7594170eSAndroid Build Coastguard Workerand
566*7594170eSAndroid Build Coastguard Worker[`OtherModuleProvider`](https://cs.android.com/android/platform/superproject/+/master:build/soong/android/module.go;l=189-193;drc=8631cc7327919845c9d9037188cbd483d22ba077)
567*7594170eSAndroid Build Coastguard Workerare used to test existence and retrieve a provider.
568*7594170eSAndroid Build Coastguard Worker
569*7594170eSAndroid Build Coastguard Worker### Bazel communication
570*7594170eSAndroid Build Coastguard Worker
571*7594170eSAndroid Build Coastguard WorkerTargets primarily communicate with each other via providers in Bazel rule
572*7594170eSAndroid Build Coastguard Workerimplementations. All rules have access to any of the providers but rules will
573*7594170eSAndroid Build Coastguard Workerpick and choose which ones to access based on their needs. For example, all
574*7594170eSAndroid Build Coastguard Workerrules can access `JavaInfo` provider, which provides information about compile
575*7594170eSAndroid Build Coastguard Workerand rolled-up runtime jars for javac and java invocations downstream. However,
576*7594170eSAndroid Build Coastguard Workerthe `JavaInfo` provider is only useful to `java_*` rules or rules that need jvm
577*7594170eSAndroid Build Coastguard Workerinformation.
578*7594170eSAndroid Build Coastguard Worker
579*7594170eSAndroid Build Coastguard Worker#### Starlark rules
580*7594170eSAndroid Build Coastguard Worker
581*7594170eSAndroid Build Coastguard Worker[Providers](https://docs.bazel.build/versions/main/skylark/rules.html#providers)
582*7594170eSAndroid Build Coastguard Workerare pieces of information exposed to other modules.
583*7594170eSAndroid Build Coastguard Worker
584*7594170eSAndroid Build Coastguard WorkerOne such provider is `DefaultInfo`, which contains the default output files and
585*7594170eSAndroid Build Coastguard Worker[`runfiles`](https://docs.bazel.build/versions/main/skylark/rules.html#runfiles).
586*7594170eSAndroid Build Coastguard Worker
587*7594170eSAndroid Build Coastguard WorkerRule authors can also create
588*7594170eSAndroid Build Coastguard Worker[custom providers](https://docs.bazel.build/versions/main/skylark/lib/Provider.html#modules.Provider)
589*7594170eSAndroid Build Coastguard Workeror implement existing providers to communicate information specific to their
590*7594170eSAndroid Build Coastguard Workerrule logic. For instance, in Android Starlark
591*7594170eSAndroid Build Coastguard Worker[`cc_object`](http://cs/android/build/bazel/rules/cc_object.bzl?l=86-87&rcl=42607e831f8ff73c82825b663609cafb777c18e1)
592*7594170eSAndroid Build Coastguard Workerrule implementation, we return a
593*7594170eSAndroid Build Coastguard Worker[`CcInfo`](https://docs.bazel.build/versions/main/skylark/lib/CcInfo.html)
594*7594170eSAndroid Build Coastguard Workerprovider and a custom
595*7594170eSAndroid Build Coastguard Worker[`CcObjectInfo`](http://cs/android/build/bazel/rules/cc_object.bzl?l=17-21&rcl=42607e831f8ff73c82825b663609cafb777c18e1)
596*7594170eSAndroid Build Coastguard Workerprovider.
597*7594170eSAndroid Build Coastguard Worker
598*7594170eSAndroid Build Coastguard Worker#### Native rules
599*7594170eSAndroid Build Coastguard Worker
600*7594170eSAndroid Build Coastguard WorkerFor implementation of native rules in Java,
601*7594170eSAndroid Build Coastguard Worker[`ruleContext.getPrerequisite`](https://github.com/bazelbuild/bazel/blob/a20b32690a71caf712d1d241f01fef16649562ba/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java#L911-L983)
602*7594170eSAndroid Build Coastguard Workeris used to extract providers from dependencies.
603*7594170eSAndroid Build Coastguard Worker
604*7594170eSAndroid Build Coastguard Worker#### `depset` construction
605*7594170eSAndroid Build Coastguard Worker
606*7594170eSAndroid Build Coastguard Worker[`depset`](https://docs.bazel.build/versions/main/glossary.html#depset) are used
607*7594170eSAndroid Build Coastguard Workerin conjunction with providers to accumulate data efficiently from transitive
608*7594170eSAndroid Build Coastguard Workerdependencies. used to accumulate data from transitive dependencies.
609*7594170eSAndroid Build Coastguard Worker
610*7594170eSAndroid Build Coastguard Worker#### `exports`
611*7594170eSAndroid Build Coastguard Worker
612*7594170eSAndroid Build Coastguard WorkerSome target have an `exports` attribute by convention, like
613*7594170eSAndroid Build Coastguard Worker[`java_library.exports`](https://docs.bazel.build/versions/main/be/java.html#java_import.exports).
614*7594170eSAndroid Build Coastguard WorkerThis attribute is commonly used to propagate transitive dependencies to the
615*7594170eSAndroid Build Coastguard Workerdependent as though the dependent has a direct edge to the transitive
616*7594170eSAndroid Build Coastguard Workerdependencies.
617