xref: /aosp_15_r20/build/soong/README.md (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker# Soong
2*333d2b36SAndroid Build Coastguard Worker
3*333d2b36SAndroid Build Coastguard WorkerSoong is one of the build systems used in Android. There are altogether three:
4*333d2b36SAndroid Build Coastguard Worker* The legacy Make-based build system that is controlled by files called
5*333d2b36SAndroid Build Coastguard Worker  `Android.mk`.
6*333d2b36SAndroid Build Coastguard Worker* Soong, which is controlled by files called `Android.bp`.
7*333d2b36SAndroid Build Coastguard Worker* The upcoming Bazel-based build system that is controlled by files called
8*333d2b36SAndroid Build Coastguard Worker  `BUILD.bazel`.
9*333d2b36SAndroid Build Coastguard Worker
10*333d2b36SAndroid Build Coastguard Worker`Android.bp` file are JSON-like declarative descriptions of "modules" to build;
11*333d2b36SAndroid Build Coastguard Workera "module" is the basic unit of building that Soong understands, similarly to
12*333d2b36SAndroid Build Coastguard Workerhow "target" is the basic unit of building for Bazel (and Make, although the
13*333d2b36SAndroid Build Coastguard Workertwo kinds of "targets" are very different)
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard WorkerSee [Simple Build
16*333d2b36SAndroid Build Coastguard WorkerConfiguration](https://source.android.com/compatibility/tests/development/blueprints)
17*333d2b36SAndroid Build Coastguard Workeron source.android.com to read how Soong is configured for testing.
18*333d2b36SAndroid Build Coastguard Worker
19*333d2b36SAndroid Build Coastguard Worker### Contributing
20*333d2b36SAndroid Build Coastguard Worker
21*333d2b36SAndroid Build Coastguard WorkerCode reviews are handled through the usual code review system of Android,
22*333d2b36SAndroid Build Coastguard Workeravailable [here](https://android-review.googlesource.com/dashboard/self).
23*333d2b36SAndroid Build Coastguard Worker
24*333d2b36SAndroid Build Coastguard WorkerFor simple changes (fixing typos, obvious optimizations, etc.), sending a code
25*333d2b36SAndroid Build Coastguard Workerreview request is enough. For more substantial changes, file a bug in our
26*333d2b36SAndroid Build Coastguard Worker[bug tracker](https://issuetracker.google.com/issues/new?component=381517) or
27*333d2b36SAndroid Build Coastguard Workeror write us at [email protected] .
28*333d2b36SAndroid Build Coastguard Worker
29*333d2b36SAndroid Build Coastguard Worker## Android.bp file format
30*333d2b36SAndroid Build Coastguard Worker
31*333d2b36SAndroid Build Coastguard WorkerBy design, Android.bp files are very simple.  There are no conditionals or
32*333d2b36SAndroid Build Coastguard Workercontrol flow statements - any complexity is handled in build logic written in
33*333d2b36SAndroid Build Coastguard WorkerGo.  The syntax and semantics of Android.bp files are intentionally similar
34*333d2b36SAndroid Build Coastguard Workerto [Bazel BUILD files](https://bazel.build/concepts/build-files) when possible.
35*333d2b36SAndroid Build Coastguard Worker
36*333d2b36SAndroid Build Coastguard Worker### Modules
37*333d2b36SAndroid Build Coastguard Worker
38*333d2b36SAndroid Build Coastguard WorkerA module in an Android.bp file starts with a module type, followed by a set of
39*333d2b36SAndroid Build Coastguard Workerproperties in `name: value,` format:
40*333d2b36SAndroid Build Coastguard Worker
41*333d2b36SAndroid Build Coastguard Worker```
42*333d2b36SAndroid Build Coastguard Workercc_binary {
43*333d2b36SAndroid Build Coastguard Worker    name: "gzip",
44*333d2b36SAndroid Build Coastguard Worker    srcs: ["src/test/minigzip.c"],
45*333d2b36SAndroid Build Coastguard Worker    shared_libs: ["libz"],
46*333d2b36SAndroid Build Coastguard Worker    stl: "none",
47*333d2b36SAndroid Build Coastguard Worker}
48*333d2b36SAndroid Build Coastguard Worker```
49*333d2b36SAndroid Build Coastguard Worker
50*333d2b36SAndroid Build Coastguard WorkerEvery module must have a `name` property, and the value must be unique across
51*333d2b36SAndroid Build Coastguard Workerall Android.bp files.
52*333d2b36SAndroid Build Coastguard Worker
53*333d2b36SAndroid Build Coastguard WorkerThe list of valid module types and their properties can be generated by calling
54*333d2b36SAndroid Build Coastguard Worker`m soong_docs`. It will be written to `$OUT_DIR/soong/docs/soong_build.html`.
55*333d2b36SAndroid Build Coastguard WorkerThis list for the current version of Soong can be found [here](https://ci.android.com/builds/latest/branches/aosp-build-tools/targets/linux/view/soong_build.html).
56*333d2b36SAndroid Build Coastguard Worker
57*333d2b36SAndroid Build Coastguard Worker### File lists
58*333d2b36SAndroid Build Coastguard Worker
59*333d2b36SAndroid Build Coastguard WorkerProperties that take a list of files can also take glob patterns and output path
60*333d2b36SAndroid Build Coastguard Workerexpansions.
61*333d2b36SAndroid Build Coastguard Worker
62*333d2b36SAndroid Build Coastguard Worker* Glob patterns can contain the normal Unix wildcard `*`, for example `"*.java"`.
63*333d2b36SAndroid Build Coastguard Worker
64*333d2b36SAndroid Build Coastguard Worker  Glob patterns can also contain a single `**` wildcard as a path element, which
65*333d2b36SAndroid Build Coastguard Worker  will match zero or more path elements. For example, `java/**/*.java` will match
66*333d2b36SAndroid Build Coastguard Worker  `java/Main.java` and `java/com/android/Main.java`.
67*333d2b36SAndroid Build Coastguard Worker
68*333d2b36SAndroid Build Coastguard Worker* Output path expansions take the format `:module` or `:module{.tag}`, where
69*333d2b36SAndroid Build Coastguard Worker  `module` is the name of a module that produces output files, and it expands to
70*333d2b36SAndroid Build Coastguard Worker  a list of those output files. With the optional `{.tag}` suffix, the module
71*333d2b36SAndroid Build Coastguard Worker  may produce a different list of outputs according to `tag`.
72*333d2b36SAndroid Build Coastguard Worker
73*333d2b36SAndroid Build Coastguard Worker  For example, a `droiddoc` module with the name "my-docs" would return its
74*333d2b36SAndroid Build Coastguard Worker  `.stubs.srcjar` output with `":my-docs"`, and its `.doc.zip` file with
75*333d2b36SAndroid Build Coastguard Worker  `":my-docs{.doc.zip}"`.
76*333d2b36SAndroid Build Coastguard Worker
77*333d2b36SAndroid Build Coastguard Worker  This is commonly used to reference `filegroup` modules, whose output files
78*333d2b36SAndroid Build Coastguard Worker  consist of their `srcs`.
79*333d2b36SAndroid Build Coastguard Worker
80*333d2b36SAndroid Build Coastguard Worker### Variables
81*333d2b36SAndroid Build Coastguard Worker
82*333d2b36SAndroid Build Coastguard WorkerAn Android.bp file may contain top-level variable assignments:
83*333d2b36SAndroid Build Coastguard Worker```
84*333d2b36SAndroid Build Coastguard Workergzip_srcs = ["src/test/minigzip.c"],
85*333d2b36SAndroid Build Coastguard Worker
86*333d2b36SAndroid Build Coastguard Workercc_binary {
87*333d2b36SAndroid Build Coastguard Worker    name: "gzip",
88*333d2b36SAndroid Build Coastguard Worker    srcs: gzip_srcs,
89*333d2b36SAndroid Build Coastguard Worker    shared_libs: ["libz"],
90*333d2b36SAndroid Build Coastguard Worker    stl: "none",
91*333d2b36SAndroid Build Coastguard Worker}
92*333d2b36SAndroid Build Coastguard Worker```
93*333d2b36SAndroid Build Coastguard Worker
94*333d2b36SAndroid Build Coastguard WorkerVariables are scoped to the remainder of the file they are declared in, as well
95*333d2b36SAndroid Build Coastguard Workeras any child Android.bp files.  Variables are immutable with one exception - they
96*333d2b36SAndroid Build Coastguard Workercan be appended to with a += assignment, but only before they have been
97*333d2b36SAndroid Build Coastguard Workerreferenced.
98*333d2b36SAndroid Build Coastguard Worker
99*333d2b36SAndroid Build Coastguard Worker### Comments
100*333d2b36SAndroid Build Coastguard Worker
101*333d2b36SAndroid Build Coastguard WorkerAndroid.bp files can contain C-style multiline `/* */` and C++ style single-line
102*333d2b36SAndroid Build Coastguard Worker`//` comments.
103*333d2b36SAndroid Build Coastguard Worker
104*333d2b36SAndroid Build Coastguard Worker### Types
105*333d2b36SAndroid Build Coastguard Worker
106*333d2b36SAndroid Build Coastguard WorkerVariables and properties are strongly typed. Variables are dynamically typed
107*333d2b36SAndroid Build Coastguard Workerbased on the first assignment, and properties are statically typed by the
108*333d2b36SAndroid Build Coastguard Workermodule type.  The supported types are:
109*333d2b36SAndroid Build Coastguard Worker* Bool (`true` or `false`)
110*333d2b36SAndroid Build Coastguard Worker* Integers (`int`)
111*333d2b36SAndroid Build Coastguard Worker* Strings (`"string"`)
112*333d2b36SAndroid Build Coastguard Worker* Lists of strings (`["string1", "string2"]`)
113*333d2b36SAndroid Build Coastguard Worker* Maps (`{key1: "value1", key2: ["value2"]}`)
114*333d2b36SAndroid Build Coastguard Worker
115*333d2b36SAndroid Build Coastguard WorkerMaps may contain values of any type, including nested maps. Lists and maps may
116*333d2b36SAndroid Build Coastguard Workerhave trailing commas after the last value.
117*333d2b36SAndroid Build Coastguard Worker
118*333d2b36SAndroid Build Coastguard WorkerStrings can contain double quotes using `\"`, for example `"cat \"a b\""`.
119*333d2b36SAndroid Build Coastguard Worker
120*333d2b36SAndroid Build Coastguard Worker### Operators
121*333d2b36SAndroid Build Coastguard Worker
122*333d2b36SAndroid Build Coastguard WorkerThe `+` operator:
123*333d2b36SAndroid Build Coastguard Worker* Sums integers.
124*333d2b36SAndroid Build Coastguard Worker* Concatenates strings and lists.
125*333d2b36SAndroid Build Coastguard Worker* Produces the union of maps.
126*333d2b36SAndroid Build Coastguard Worker
127*333d2b36SAndroid Build Coastguard WorkerConcatenating maps produces a map whose keys are the union of the given maps'
128*333d2b36SAndroid Build Coastguard Workerkeys, and whose mapped values are the union of the given maps' corresponding
129*333d2b36SAndroid Build Coastguard Workermapped values.
130*333d2b36SAndroid Build Coastguard Worker
131*333d2b36SAndroid Build Coastguard Worker### Defaults modules
132*333d2b36SAndroid Build Coastguard Worker
133*333d2b36SAndroid Build Coastguard WorkerA `defaults` module can be used to repeat the same properties in multiple
134*333d2b36SAndroid Build Coastguard Workermodules. For example:
135*333d2b36SAndroid Build Coastguard Worker
136*333d2b36SAndroid Build Coastguard Worker```
137*333d2b36SAndroid Build Coastguard Workercc_defaults {
138*333d2b36SAndroid Build Coastguard Worker    name: "gzip_defaults",
139*333d2b36SAndroid Build Coastguard Worker    shared_libs: ["libz"],
140*333d2b36SAndroid Build Coastguard Worker    stl: "none",
141*333d2b36SAndroid Build Coastguard Worker}
142*333d2b36SAndroid Build Coastguard Worker
143*333d2b36SAndroid Build Coastguard Workercc_binary {
144*333d2b36SAndroid Build Coastguard Worker    name: "gzip",
145*333d2b36SAndroid Build Coastguard Worker    defaults: ["gzip_defaults"],
146*333d2b36SAndroid Build Coastguard Worker    srcs: ["src/test/minigzip.c"],
147*333d2b36SAndroid Build Coastguard Worker}
148*333d2b36SAndroid Build Coastguard Worker```
149*333d2b36SAndroid Build Coastguard Worker
150*333d2b36SAndroid Build Coastguard Worker### Packages
151*333d2b36SAndroid Build Coastguard Worker
152*333d2b36SAndroid Build Coastguard WorkerThe build is organized into packages where each package is a collection of related files and a
153*333d2b36SAndroid Build Coastguard Workerspecification of the dependencies among them in the form of modules.
154*333d2b36SAndroid Build Coastguard Worker
155*333d2b36SAndroid Build Coastguard WorkerA package is defined as a directory containing a file named `Android.bp`, residing beneath the
156*333d2b36SAndroid Build Coastguard Workertop-level directory in the build and its name is its path relative to the top-level directory. A
157*333d2b36SAndroid Build Coastguard Workerpackage includes all files in its directory, plus all subdirectories beneath it, except those which
158*333d2b36SAndroid Build Coastguard Workerthemselves contain an `Android.bp` file.
159*333d2b36SAndroid Build Coastguard Worker
160*333d2b36SAndroid Build Coastguard WorkerThe modules in a package's `Android.bp` and included files are part of the module.
161*333d2b36SAndroid Build Coastguard Worker
162*333d2b36SAndroid Build Coastguard WorkerFor example, in the following directory tree (where `.../android/` is the top-level Android
163*333d2b36SAndroid Build Coastguard Workerdirectory) there are two packages, `my/app`, and the subpackage `my/app/tests`. Note that
164*333d2b36SAndroid Build Coastguard Worker`my/app/data` is not a package, but a directory belonging to package `my/app`.
165*333d2b36SAndroid Build Coastguard Worker
166*333d2b36SAndroid Build Coastguard Worker    .../android/my/app/Android.bp
167*333d2b36SAndroid Build Coastguard Worker    .../android/my/app/app.cc
168*333d2b36SAndroid Build Coastguard Worker    .../android/my/app/data/input.txt
169*333d2b36SAndroid Build Coastguard Worker    .../android/my/app/tests/Android.bp
170*333d2b36SAndroid Build Coastguard Worker    .../android/my/app/tests/test.cc
171*333d2b36SAndroid Build Coastguard Worker
172*333d2b36SAndroid Build Coastguard WorkerThis is based on the Bazel package concept.
173*333d2b36SAndroid Build Coastguard Worker
174*333d2b36SAndroid Build Coastguard WorkerThe `package` module type allows information to be specified about a package. Only a single
175*333d2b36SAndroid Build Coastguard Worker`package` module can be specified per package and in the case where there are multiple `.bp` files
176*333d2b36SAndroid Build Coastguard Workerin the same package directory it is highly recommended that the `package` module (if required) is
177*333d2b36SAndroid Build Coastguard Workerspecified in the `Android.bp` file.
178*333d2b36SAndroid Build Coastguard Worker
179*333d2b36SAndroid Build Coastguard WorkerUnlike most module type `package` does not have a `name` property. Instead the name is set to the
180*333d2b36SAndroid Build Coastguard Workername of the package, e.g. if the package is in `top/intermediate/package` then the package name is
181*333d2b36SAndroid Build Coastguard Worker`//top/intermediate/package`.
182*333d2b36SAndroid Build Coastguard Worker
183*333d2b36SAndroid Build Coastguard WorkerE.g. The following will set the default visibility for all the modules defined in the package and
184*333d2b36SAndroid Build Coastguard Workerany subpackages that do not set their own default visibility (irrespective of whether they are in
185*333d2b36SAndroid Build Coastguard Workerthe same `.bp` file as the `package` module) to be visible to all the subpackages by default.
186*333d2b36SAndroid Build Coastguard Worker
187*333d2b36SAndroid Build Coastguard Worker```
188*333d2b36SAndroid Build Coastguard Workerpackage {
189*333d2b36SAndroid Build Coastguard Worker    default_visibility: [":__subpackages__"]
190*333d2b36SAndroid Build Coastguard Worker}
191*333d2b36SAndroid Build Coastguard Worker```
192*333d2b36SAndroid Build Coastguard Worker
193*333d2b36SAndroid Build Coastguard Worker### Referencing Modules
194*333d2b36SAndroid Build Coastguard Worker
195*333d2b36SAndroid Build Coastguard WorkerA module `libfoo` can be referenced by its name
196*333d2b36SAndroid Build Coastguard Worker
197*333d2b36SAndroid Build Coastguard Worker```
198*333d2b36SAndroid Build Coastguard Workercc_binary {
199*333d2b36SAndroid Build Coastguard Worker    name: "app",
200*333d2b36SAndroid Build Coastguard Worker    shared_libs: ["libfoo"],
201*333d2b36SAndroid Build Coastguard Worker}
202*333d2b36SAndroid Build Coastguard Worker```
203*333d2b36SAndroid Build Coastguard Worker
204*333d2b36SAndroid Build Coastguard WorkerObviously, this works only if there is only one `libfoo` module in the source
205*333d2b36SAndroid Build Coastguard Workertree. Ensuring such name uniqueness for larger trees may become problematic. We
206*333d2b36SAndroid Build Coastguard Workermight also want to use the same name in multiple mutually exclusive subtrees
207*333d2b36SAndroid Build Coastguard Worker(for example, implementing different devices) deliberately in order to describe
208*333d2b36SAndroid Build Coastguard Workera functionally equivalent module. Enter Soong namespaces.
209*333d2b36SAndroid Build Coastguard Worker
210*333d2b36SAndroid Build Coastguard Worker#### Namespaces
211*333d2b36SAndroid Build Coastguard Worker
212*333d2b36SAndroid Build Coastguard WorkerThe presence of the `soong_namespace {..}` in an Android.bp file defines a
213*333d2b36SAndroid Build Coastguard Worker**namespace**. For instance, having
214*333d2b36SAndroid Build Coastguard Worker
215*333d2b36SAndroid Build Coastguard Worker```
216*333d2b36SAndroid Build Coastguard Workersoong_namespace {
217*333d2b36SAndroid Build Coastguard Worker    ...
218*333d2b36SAndroid Build Coastguard Worker}
219*333d2b36SAndroid Build Coastguard Worker...
220*333d2b36SAndroid Build Coastguard Worker```
221*333d2b36SAndroid Build Coastguard Worker
222*333d2b36SAndroid Build Coastguard Workerin `device/google/bonito/Android.bp` informs Soong that within the
223*333d2b36SAndroid Build Coastguard Worker`device/google/bonito` package the module names are unique, that is, all the
224*333d2b36SAndroid Build Coastguard Workermodules defined in the Android.bp files in the `device/google/bonito/` tree have
225*333d2b36SAndroid Build Coastguard Workerunique names. However, there may be modules with the same names outside
226*333d2b36SAndroid Build Coastguard Worker`device/google/bonito` tree. Indeed, there is a module `"pixelstats-vendor"`
227*333d2b36SAndroid Build Coastguard Workerboth in `device/google/bonito/pixelstats` and in
228*333d2b36SAndroid Build Coastguard Worker`device/google/coral/pixelstats`.
229*333d2b36SAndroid Build Coastguard Worker
230*333d2b36SAndroid Build Coastguard WorkerThe name of a namespace is the path of its directory. The name of the namespace
231*333d2b36SAndroid Build Coastguard Workerin the example above is thus `device/google/bonito`.
232*333d2b36SAndroid Build Coastguard Worker
233*333d2b36SAndroid Build Coastguard WorkerAn implicit **global namespace** corresponds to the source tree as a whole. It
234*333d2b36SAndroid Build Coastguard Workerhas empty name.
235*333d2b36SAndroid Build Coastguard Worker
236*333d2b36SAndroid Build Coastguard WorkerA module name's **scope** is the smallest namespace containing it. Suppose a
237*333d2b36SAndroid Build Coastguard Workersource tree has `device/my` and `device/my/display` namespaces. If `libfoo`
238*333d2b36SAndroid Build Coastguard Workermodule is defined in `device/my/display/lib/Android.bp`, its namespace is
239*333d2b36SAndroid Build Coastguard Worker`device/my/display`.
240*333d2b36SAndroid Build Coastguard Worker
241*333d2b36SAndroid Build Coastguard WorkerThe name uniqueness thus means that module's name is unique within its scope. In
242*333d2b36SAndroid Build Coastguard Workerother words, "//_scope_:_name_" is globally unique module reference, e.g,
243*333d2b36SAndroid Build Coastguard Worker`"//device/google/bonito:pixelstats-vendor"`. _Note_ that the name of the
244*333d2b36SAndroid Build Coastguard Workernamespace for a module may be different from module's package name: `libfoo`
245*333d2b36SAndroid Build Coastguard Workerbelongs to `device/my/display` namespace but is contained in
246*333d2b36SAndroid Build Coastguard Worker`device/my/display/lib` package.
247*333d2b36SAndroid Build Coastguard Worker
248*333d2b36SAndroid Build Coastguard Worker#### Name Resolution
249*333d2b36SAndroid Build Coastguard Worker
250*333d2b36SAndroid Build Coastguard WorkerThe form of a module reference determines how Soong locates the module.
251*333d2b36SAndroid Build Coastguard Worker
252*333d2b36SAndroid Build Coastguard WorkerFor a **global reference** of the "//_scope_:_name_" form, Soong verifies there
253*333d2b36SAndroid Build Coastguard Workeris a namespace called "_scope_", then verifies it contains a "_name_" module and
254*333d2b36SAndroid Build Coastguard Workeruses it. Soong verifies there is only one "_name_" in "_scope_" at the beginning
255*333d2b36SAndroid Build Coastguard Workerwhen it parses Android.bp files.
256*333d2b36SAndroid Build Coastguard Worker
257*333d2b36SAndroid Build Coastguard WorkerA **local reference** has "_name_" form, and resolving it involves looking for a
258*333d2b36SAndroid Build Coastguard Workermodule "_name_" in one or more namespaces. By default only the global namespace
259*333d2b36SAndroid Build Coastguard Workeris searched for "_name_" (in other words, only the modules not belonging to an
260*333d2b36SAndroid Build Coastguard Workerexplicitly defined scope are considered). The `imports` attribute of the
261*333d2b36SAndroid Build Coastguard Worker`soong_namespaces` allows to specify where to look for modules . For instance,
262*333d2b36SAndroid Build Coastguard Workerwith `device/google/bonito/Android.bp` containing
263*333d2b36SAndroid Build Coastguard Worker
264*333d2b36SAndroid Build Coastguard Worker```
265*333d2b36SAndroid Build Coastguard Workersoong_namespace {
266*333d2b36SAndroid Build Coastguard Worker    imports: [
267*333d2b36SAndroid Build Coastguard Worker        "hardware/google/interfaces",
268*333d2b36SAndroid Build Coastguard Worker        "hardware/google/pixel",
269*333d2b36SAndroid Build Coastguard Worker        "hardware/qcom/bootctrl",
270*333d2b36SAndroid Build Coastguard Worker    ],
271*333d2b36SAndroid Build Coastguard Worker}
272*333d2b36SAndroid Build Coastguard Worker```
273*333d2b36SAndroid Build Coastguard Worker
274*333d2b36SAndroid Build Coastguard Workera reference to `"libpixelstats"` will resolve to the module defined in
275*333d2b36SAndroid Build Coastguard Worker`hardware/google/pixel/pixelstats/Android.bp` because this module is in
276*333d2b36SAndroid Build Coastguard Worker`hardware/google/pixel` namespace.
277*333d2b36SAndroid Build Coastguard Worker
278*333d2b36SAndroid Build Coastguard Worker**TODO**: Conventionally, languages with similar concepts provide separate
279*333d2b36SAndroid Build Coastguard Workerconstructs for namespace definition and name resolution (`namespace` and `using`
280*333d2b36SAndroid Build Coastguard Workerin C++, for instance). Should Soong do that, too?
281*333d2b36SAndroid Build Coastguard Worker
282*333d2b36SAndroid Build Coastguard Worker#### Referencing modules in makefiles
283*333d2b36SAndroid Build Coastguard Worker
284*333d2b36SAndroid Build Coastguard WorkerWhile we are gradually converting makefiles to Android.bp files, Android build
285*333d2b36SAndroid Build Coastguard Workeris described by a mixture of Android.bp and Android.mk files, and a module
286*333d2b36SAndroid Build Coastguard Workerdefined in an Android.mk file can reference a module defined in Android.bp file.
287*333d2b36SAndroid Build Coastguard WorkerFor instance, a binary still defined in an Android.mk file may have a library
288*333d2b36SAndroid Build Coastguard Workerdefined in already converted Android.bp as a dependency.
289*333d2b36SAndroid Build Coastguard Worker
290*333d2b36SAndroid Build Coastguard WorkerA module defined in an Android.bp file and belonging to the global namespace can
291*333d2b36SAndroid Build Coastguard Workerbe referenced from a makefile without additional effort. If a module belongs to
292*333d2b36SAndroid Build Coastguard Workeran explicit namespace, it can be referenced from a makefile only after after the
293*333d2b36SAndroid Build Coastguard Workername of the namespace has been added to the value of PRODUCT_SOONG_NAMESPACES
294*333d2b36SAndroid Build Coastguard Workervariable.
295*333d2b36SAndroid Build Coastguard Worker
296*333d2b36SAndroid Build Coastguard WorkerNote that makefiles have no notion of namespaces and exposing namespaces with
297*333d2b36SAndroid Build Coastguard Workerthe same modules via PRODUCT_SOONG_NAMESPACES may cause Make failure. For
298*333d2b36SAndroid Build Coastguard Workerinstance, exposing both `device/google/bonito` and `device/google/coral`
299*333d2b36SAndroid Build Coastguard Workernamespaces will cause Make failure because it will see two targets for the
300*333d2b36SAndroid Build Coastguard Worker`pixelstats-vendor` module.
301*333d2b36SAndroid Build Coastguard Worker
302*333d2b36SAndroid Build Coastguard Worker### Visibility
303*333d2b36SAndroid Build Coastguard Worker
304*333d2b36SAndroid Build Coastguard WorkerThe `visibility` property on a module controls whether the module can be
305*333d2b36SAndroid Build Coastguard Workerused by other packages. Modules are always visible to other modules declared
306*333d2b36SAndroid Build Coastguard Workerin the same package. This is based on the Bazel visibility mechanism.
307*333d2b36SAndroid Build Coastguard Worker
308*333d2b36SAndroid Build Coastguard WorkerIf specified the `visibility` property must contain at least one rule.
309*333d2b36SAndroid Build Coastguard Worker
310*333d2b36SAndroid Build Coastguard WorkerEach rule in the property must be in one of the following forms:
311*333d2b36SAndroid Build Coastguard Worker* `["//visibility:public"]`: Anyone can use this module.
312*333d2b36SAndroid Build Coastguard Worker* `["//visibility:private"]`: Only rules in the module's package (not its
313*333d2b36SAndroid Build Coastguard Workersubpackages) can use this module.
314*333d2b36SAndroid Build Coastguard Worker* `["//visibility:override"]`: Discards any rules inherited from defaults or a
315*333d2b36SAndroid Build Coastguard Workercreating module. Can only be used at the beginning of a list of visibility
316*333d2b36SAndroid Build Coastguard Workerrules.
317*333d2b36SAndroid Build Coastguard Worker* `["//visibility:any_partition"]`: Any modules of type android_filesystem
318*333d2b36SAndroid Build Coastguard Workeror android_system_image can use this module. Intended for modules that no one
319*333d2b36SAndroid Build Coastguard Workershould link against, but should still be included in soong-built partitions.
320*333d2b36SAndroid Build Coastguard Worker* `["//some/package:__pkg__", "//other/package:__pkg__"]`: Only modules in
321*333d2b36SAndroid Build Coastguard Worker`some/package` and `other/package` (defined in `some/package/*.bp` and
322*333d2b36SAndroid Build Coastguard Worker`other/package/*.bp`) have access to this module. Note that sub-packages do not
323*333d2b36SAndroid Build Coastguard Workerhave access to the rule; for example, `//some/package/foo:bar` or
324*333d2b36SAndroid Build Coastguard Worker`//other/package/testing:bla` wouldn't have access. `__pkg__` is a special
325*333d2b36SAndroid Build Coastguard Workermodule and must be used verbatim. It represents all of the modules in the
326*333d2b36SAndroid Build Coastguard Workerpackage.
327*333d2b36SAndroid Build Coastguard Worker* `["//project:__subpackages__", "//other:__subpackages__"]`: Only modules in
328*333d2b36SAndroid Build Coastguard Workerpackages `project` or `other` or in one of their sub-packages have access to
329*333d2b36SAndroid Build Coastguard Workerthis module. For example, `//project:rule`, `//project/library:lib` or
330*333d2b36SAndroid Build Coastguard Worker`//other/testing/internal:munge` are allowed to depend on this rule (but not
331*333d2b36SAndroid Build Coastguard Worker`//independent:evil`)
332*333d2b36SAndroid Build Coastguard Worker* `["//project"]`: This is shorthand for `["//project:__pkg__"]`
333*333d2b36SAndroid Build Coastguard Worker* `[":__subpackages__"]`: This is shorthand for `["//project:__subpackages__"]`
334*333d2b36SAndroid Build Coastguard Workerwhere `//project` is the module's package, e.g. using `[":__subpackages__"]` in
335*333d2b36SAndroid Build Coastguard Worker`packages/apps/Settings/Android.bp` is equivalent to
336*333d2b36SAndroid Build Coastguard Worker`//packages/apps/Settings:__subpackages__`.
337*333d2b36SAndroid Build Coastguard Worker* `["//visibility:legacy_public"]`: The default visibility, behaves as
338*333d2b36SAndroid Build Coastguard Worker`//visibility:public` for now. It is an error if it is used in a module.
339*333d2b36SAndroid Build Coastguard Worker
340*333d2b36SAndroid Build Coastguard WorkerThe visibility rules of `//visibility:public` and `//visibility:private` cannot
341*333d2b36SAndroid Build Coastguard Workerbe combined with any other visibility specifications, except
342*333d2b36SAndroid Build Coastguard Worker`//visibility:public` is allowed to override visibility specifications imported
343*333d2b36SAndroid Build Coastguard Workerthrough the `defaults` property.
344*333d2b36SAndroid Build Coastguard Worker
345*333d2b36SAndroid Build Coastguard WorkerPackages outside `vendor/` cannot make themselves visible to specific packages
346*333d2b36SAndroid Build Coastguard Workerin `vendor/`, e.g. a module in `libcore` cannot declare that it is visible to
347*333d2b36SAndroid Build Coastguard Workersay `vendor/google`, instead it must make itself visible to all packages within
348*333d2b36SAndroid Build Coastguard Worker`vendor/` using `//vendor:__subpackages__`.
349*333d2b36SAndroid Build Coastguard Worker
350*333d2b36SAndroid Build Coastguard WorkerIf a module does not specify the `visibility` property then it uses the
351*333d2b36SAndroid Build Coastguard Worker`default_visibility` property of the `package` module in the module's package.
352*333d2b36SAndroid Build Coastguard Worker
353*333d2b36SAndroid Build Coastguard WorkerIf the `default_visibility` property is not set for the module's package then
354*333d2b36SAndroid Build Coastguard Workerit will use the `default_visibility` of its closest ancestor package for which
355*333d2b36SAndroid Build Coastguard Workera `default_visibility` property is specified.
356*333d2b36SAndroid Build Coastguard Worker
357*333d2b36SAndroid Build Coastguard WorkerIf no `default_visibility` property can be found then the module uses the
358*333d2b36SAndroid Build Coastguard Workerglobal default of `//visibility:legacy_public`.
359*333d2b36SAndroid Build Coastguard Worker
360*333d2b36SAndroid Build Coastguard WorkerThe `visibility` property has no effect on a defaults module although it does
361*333d2b36SAndroid Build Coastguard Workerapply to any non-defaults module that uses it. To set the visibility of a
362*333d2b36SAndroid Build Coastguard Workerdefaults module, use the `defaults_visibility` property on the defaults module;
363*333d2b36SAndroid Build Coastguard Workernot to be confused with the `default_visibility` property on the package module.
364*333d2b36SAndroid Build Coastguard Worker
365*333d2b36SAndroid Build Coastguard WorkerOnce the build has been completely switched over to soong it is possible that a
366*333d2b36SAndroid Build Coastguard Workerglobal refactoring will be done to change this to `//visibility:private` at
367*333d2b36SAndroid Build Coastguard Workerwhich point all packages that do not currently specify a `default_visibility`
368*333d2b36SAndroid Build Coastguard Workerproperty will be updated to have
369*333d2b36SAndroid Build Coastguard Worker`default_visibility = [//visibility:legacy_public]` added. It will then be the
370*333d2b36SAndroid Build Coastguard Workerowner's responsibility to replace that with a more appropriate visibility.
371*333d2b36SAndroid Build Coastguard Worker
372*333d2b36SAndroid Build Coastguard Worker### Formatter
373*333d2b36SAndroid Build Coastguard Worker
374*333d2b36SAndroid Build Coastguard WorkerSoong includes a canonical formatter for Android.bp files, similar to
375*333d2b36SAndroid Build Coastguard Worker[gofmt](https://golang.org/cmd/gofmt/).  To recursively reformat all Android.bp files
376*333d2b36SAndroid Build Coastguard Workerin the current directory:
377*333d2b36SAndroid Build Coastguard Worker```
378*333d2b36SAndroid Build Coastguard Workerbpfmt -w .
379*333d2b36SAndroid Build Coastguard Worker```
380*333d2b36SAndroid Build Coastguard Worker
381*333d2b36SAndroid Build Coastguard WorkerThe canonical format includes 4 space indents, newlines after every element of a
382*333d2b36SAndroid Build Coastguard Workermulti-element list, and always includes a trailing comma in lists and maps.
383*333d2b36SAndroid Build Coastguard Worker
384*333d2b36SAndroid Build Coastguard Worker### Convert Android.mk files
385*333d2b36SAndroid Build Coastguard Worker
386*333d2b36SAndroid Build Coastguard WorkerSoong includes a tool perform a first pass at converting Android.mk files
387*333d2b36SAndroid Build Coastguard Workerto Android.bp files:
388*333d2b36SAndroid Build Coastguard Worker
389*333d2b36SAndroid Build Coastguard Worker```
390*333d2b36SAndroid Build Coastguard Workerandroidmk Android.mk > Android.bp
391*333d2b36SAndroid Build Coastguard Worker```
392*333d2b36SAndroid Build Coastguard Worker
393*333d2b36SAndroid Build Coastguard WorkerThe tool converts variables, modules, comments, and some conditionals, but any
394*333d2b36SAndroid Build Coastguard Workercustom Makefile rules, complex conditionals or extra includes must be converted
395*333d2b36SAndroid Build Coastguard Workerby hand.
396*333d2b36SAndroid Build Coastguard Worker
397*333d2b36SAndroid Build Coastguard Worker#### Differences between Android.mk and Android.bp
398*333d2b36SAndroid Build Coastguard Worker
399*333d2b36SAndroid Build Coastguard Worker* Android.mk files often have multiple modules with the same name (for example
400*333d2b36SAndroid Build Coastguard Workerfor static and shared version of a library, or for host and device versions).
401*333d2b36SAndroid Build Coastguard WorkerAndroid.bp files require unique names for every module, but a single module can
402*333d2b36SAndroid Build Coastguard Workerbe built in multiple variants, for example by adding `host_supported: true`.
403*333d2b36SAndroid Build Coastguard WorkerThe androidmk converter will produce multiple conflicting modules, which must
404*333d2b36SAndroid Build Coastguard Workerbe resolved by hand to a single module with any differences inside
405*333d2b36SAndroid Build Coastguard Worker`target: { android: { }, host: { } }` blocks.
406*333d2b36SAndroid Build Coastguard Worker
407*333d2b36SAndroid Build Coastguard Worker### Conditionals
408*333d2b36SAndroid Build Coastguard Worker
409*333d2b36SAndroid Build Coastguard WorkerSoong deliberately does not support most conditionals in Android.bp files.  We
410*333d2b36SAndroid Build Coastguard Workersuggest removing most conditionals from the build.  See
411*333d2b36SAndroid Build Coastguard Worker[Best Practices](docs/best_practices.md#removing-conditionals) for some
412*333d2b36SAndroid Build Coastguard Workerexamples on how to remove conditionals.
413*333d2b36SAndroid Build Coastguard Worker
414*333d2b36SAndroid Build Coastguard WorkerMost conditionals supported natively by Soong are converted to a map
415*333d2b36SAndroid Build Coastguard Workerproperty.  When building the module one of the properties in the map will be
416*333d2b36SAndroid Build Coastguard Workerselected, and its values appended to the property with the same name at the
417*333d2b36SAndroid Build Coastguard Workertop level of the module.
418*333d2b36SAndroid Build Coastguard Worker
419*333d2b36SAndroid Build Coastguard WorkerFor example, to support architecture specific files:
420*333d2b36SAndroid Build Coastguard Worker```
421*333d2b36SAndroid Build Coastguard Workercc_library {
422*333d2b36SAndroid Build Coastguard Worker    ...
423*333d2b36SAndroid Build Coastguard Worker    srcs: ["generic.cpp"],
424*333d2b36SAndroid Build Coastguard Worker    arch: {
425*333d2b36SAndroid Build Coastguard Worker        arm: {
426*333d2b36SAndroid Build Coastguard Worker            srcs: ["arm.cpp"],
427*333d2b36SAndroid Build Coastguard Worker        },
428*333d2b36SAndroid Build Coastguard Worker        x86: {
429*333d2b36SAndroid Build Coastguard Worker            srcs: ["x86.cpp"],
430*333d2b36SAndroid Build Coastguard Worker        },
431*333d2b36SAndroid Build Coastguard Worker    },
432*333d2b36SAndroid Build Coastguard Worker}
433*333d2b36SAndroid Build Coastguard Worker```
434*333d2b36SAndroid Build Coastguard Worker
435*333d2b36SAndroid Build Coastguard WorkerWhen building the module for arm the `generic.cpp` and `arm.cpp` sources will
436*333d2b36SAndroid Build Coastguard Workerbe built.  When building for x86 the `generic.cpp` and 'x86.cpp' sources will
437*333d2b36SAndroid Build Coastguard Workerbe built.
438*333d2b36SAndroid Build Coastguard Worker
439*333d2b36SAndroid Build Coastguard Worker#### Soong Config Variables
440*333d2b36SAndroid Build Coastguard Worker
441*333d2b36SAndroid Build Coastguard WorkerWhen converting vendor modules that contain conditionals, simple conditionals
442*333d2b36SAndroid Build Coastguard Workercan be supported through Soong config variables using `soong_config_*`
443*333d2b36SAndroid Build Coastguard Workermodules that describe the module types, variables and possible values:
444*333d2b36SAndroid Build Coastguard Worker
445*333d2b36SAndroid Build Coastguard Worker```
446*333d2b36SAndroid Build Coastguard Workersoong_config_module_type {
447*333d2b36SAndroid Build Coastguard Worker    name: "acme_cc_defaults",
448*333d2b36SAndroid Build Coastguard Worker    module_type: "cc_defaults",
449*333d2b36SAndroid Build Coastguard Worker    config_namespace: "acme",
450*333d2b36SAndroid Build Coastguard Worker    variables: ["board"],
451*333d2b36SAndroid Build Coastguard Worker    bool_variables: ["feature"],
452*333d2b36SAndroid Build Coastguard Worker    list_variables: ["impl"],
453*333d2b36SAndroid Build Coastguard Worker    value_variables: ["width"],
454*333d2b36SAndroid Build Coastguard Worker    properties: ["cflags", "srcs"],
455*333d2b36SAndroid Build Coastguard Worker}
456*333d2b36SAndroid Build Coastguard Worker
457*333d2b36SAndroid Build Coastguard Workersoong_config_string_variable {
458*333d2b36SAndroid Build Coastguard Worker    name: "board",
459*333d2b36SAndroid Build Coastguard Worker    values: ["soc_a", "soc_b", "soc_c"],
460*333d2b36SAndroid Build Coastguard Worker}
461*333d2b36SAndroid Build Coastguard Worker```
462*333d2b36SAndroid Build Coastguard Worker
463*333d2b36SAndroid Build Coastguard WorkerThis example describes a new `acme_cc_defaults` module type that extends the
464*333d2b36SAndroid Build Coastguard Worker`cc_defaults` module type, with four additional conditionals based on variables
465*333d2b36SAndroid Build Coastguard Worker`board`, `feature`, `impl` and `width` which can affect properties `cflags` and
466*333d2b36SAndroid Build Coastguard Worker`srcs`. The four types of soong variables control properties in the following
467*333d2b36SAndroid Build Coastguard Workerways.
468*333d2b36SAndroid Build Coastguard Worker
469*333d2b36SAndroid Build Coastguard Worker* bool variable (e.g. `feature`): Properties are applied if set to `true`.
470*333d2b36SAndroid Build Coastguard Worker* list variable (e.g. `impl`): (lists of strings properties only) Properties are
471*333d2b36SAndroid Build Coastguard Worker  applied for each value in the list, using `%s` substitution. For example, if
472*333d2b36SAndroid Build Coastguard Worker  the property is `["%s.cpp", "%s.h"]` and the list value is `foo bar`,
473*333d2b36SAndroid Build Coastguard Worker  the result is `["foo.cpp", "foo.h", "bar.cpp", "bar.h"]`.
474*333d2b36SAndroid Build Coastguard Worker* value variable (e.g. `width`): (strings or lists of strings) The value are
475*333d2b36SAndroid Build Coastguard Worker  directly substituted into properties using `%s`.
476*333d2b36SAndroid Build Coastguard Worker* string variable (e.g. `board`): Properties are applied only if they match the
477*333d2b36SAndroid Build Coastguard Worker  variable's value.
478*333d2b36SAndroid Build Coastguard Worker
479*333d2b36SAndroid Build Coastguard WorkerAdditionally, each conditional containing a `conditions_default` property can
480*333d2b36SAndroid Build Coastguard Workeraffect `cflags` and `srcs` in the following conditions:
481*333d2b36SAndroid Build Coastguard Worker
482*333d2b36SAndroid Build Coastguard Worker* bool variable (e.g. `feature`): the variable is unspecified or not set to
483*333d2b36SAndroid Build Coastguard Worker  `true`
484*333d2b36SAndroid Build Coastguard Worker* list variable (e.g. `impl`): the variable is unspecified
485*333d2b36SAndroid Build Coastguard Worker* value variable (e.g. `width`): the variable is unspecified
486*333d2b36SAndroid Build Coastguard Worker* string variable (e.g. `board`): the variable is unspecified or the variable is
487*333d2b36SAndroid Build Coastguard Worker  set to a string unused in the given module. For example, with `board`, if the
488*333d2b36SAndroid Build Coastguard Worker  `board` conditional contains the properties `soc_a` and `conditions_default`,
489*333d2b36SAndroid Build Coastguard Worker  when `board` is `soc_b`, the `cflags` and `srcs` values under
490*333d2b36SAndroid Build Coastguard Worker  `conditions_default` is used. To specify that no properties should be amended
491*333d2b36SAndroid Build Coastguard Worker  for `soc_b`, you can set `soc_b: {},`.
492*333d2b36SAndroid Build Coastguard Worker
493*333d2b36SAndroid Build Coastguard WorkerThe values of the variables can be set from a product's `BoardConfig.mk` file:
494*333d2b36SAndroid Build Coastguard Worker```
495*333d2b36SAndroid Build Coastguard Worker$(call soong_config_set,acme,board,soc_a)
496*333d2b36SAndroid Build Coastguard Worker$(call soong_config_set,acme,feature,true)
497*333d2b36SAndroid Build Coastguard Worker$(call soong_config_set,acme,impl,foo.cpp bar.cpp)
498*333d2b36SAndroid Build Coastguard Worker$(call soong_config_set,acme,width,200)
499*333d2b36SAndroid Build Coastguard Worker```
500*333d2b36SAndroid Build Coastguard Worker
501*333d2b36SAndroid Build Coastguard WorkerThe `acme_cc_defaults` module type can be used anywhere after the definition in
502*333d2b36SAndroid Build Coastguard Workerthe file where it is defined, or can be imported into another file with:
503*333d2b36SAndroid Build Coastguard Worker```
504*333d2b36SAndroid Build Coastguard Workersoong_config_module_type_import {
505*333d2b36SAndroid Build Coastguard Worker    from: "device/acme/Android.bp",
506*333d2b36SAndroid Build Coastguard Worker    module_types: ["acme_cc_defaults"],
507*333d2b36SAndroid Build Coastguard Worker}
508*333d2b36SAndroid Build Coastguard Worker```
509*333d2b36SAndroid Build Coastguard Worker
510*333d2b36SAndroid Build Coastguard WorkerIt can used like any other module type:
511*333d2b36SAndroid Build Coastguard Worker```
512*333d2b36SAndroid Build Coastguard Workeracme_cc_defaults {
513*333d2b36SAndroid Build Coastguard Worker    name: "acme_defaults",
514*333d2b36SAndroid Build Coastguard Worker    cflags: ["-DGENERIC"],
515*333d2b36SAndroid Build Coastguard Worker    soong_config_variables: {
516*333d2b36SAndroid Build Coastguard Worker        board: {
517*333d2b36SAndroid Build Coastguard Worker            soc_a: {
518*333d2b36SAndroid Build Coastguard Worker                cflags: ["-DSOC_A"],
519*333d2b36SAndroid Build Coastguard Worker            },
520*333d2b36SAndroid Build Coastguard Worker            soc_b: {
521*333d2b36SAndroid Build Coastguard Worker                cflags: ["-DSOC_B"],
522*333d2b36SAndroid Build Coastguard Worker            },
523*333d2b36SAndroid Build Coastguard Worker            conditions_default: {
524*333d2b36SAndroid Build Coastguard Worker                cflags: ["-DSOC_DEFAULT"],
525*333d2b36SAndroid Build Coastguard Worker            },
526*333d2b36SAndroid Build Coastguard Worker        },
527*333d2b36SAndroid Build Coastguard Worker        feature: {
528*333d2b36SAndroid Build Coastguard Worker            cflags: ["-DFEATURE"],
529*333d2b36SAndroid Build Coastguard Worker            conditions_default: {
530*333d2b36SAndroid Build Coastguard Worker                cflags: ["-DFEATURE_DEFAULT"],
531*333d2b36SAndroid Build Coastguard Worker            },
532*333d2b36SAndroid Build Coastguard Worker        },
533*333d2b36SAndroid Build Coastguard Worker        width: {
534*333d2b36SAndroid Build Coastguard Worker            cflags: ["-DWIDTH=%s"],
535*333d2b36SAndroid Build Coastguard Worker            conditions_default: {
536*333d2b36SAndroid Build Coastguard Worker                cflags: ["-DWIDTH=DEFAULT"],
537*333d2b36SAndroid Build Coastguard Worker            },
538*333d2b36SAndroid Build Coastguard Worker        },
539*333d2b36SAndroid Build Coastguard Worker        impl: {
540*333d2b36SAndroid Build Coastguard Worker            srcs: ["impl/%s"],
541*333d2b36SAndroid Build Coastguard Worker            conditions_default: {
542*333d2b36SAndroid Build Coastguard Worker                srcs: ["impl/default.cpp"],
543*333d2b36SAndroid Build Coastguard Worker            },
544*333d2b36SAndroid Build Coastguard Worker        },
545*333d2b36SAndroid Build Coastguard Worker    },
546*333d2b36SAndroid Build Coastguard Worker}
547*333d2b36SAndroid Build Coastguard Worker
548*333d2b36SAndroid Build Coastguard Workercc_library {
549*333d2b36SAndroid Build Coastguard Worker    name: "libacme_foo",
550*333d2b36SAndroid Build Coastguard Worker    defaults: ["acme_defaults"],
551*333d2b36SAndroid Build Coastguard Worker    srcs: ["*.cpp"],
552*333d2b36SAndroid Build Coastguard Worker}
553*333d2b36SAndroid Build Coastguard Worker```
554*333d2b36SAndroid Build Coastguard Worker
555*333d2b36SAndroid Build Coastguard WorkerWith the `BoardConfig.mk` snippet above, `libacme_foo` would build with
556*333d2b36SAndroid Build Coastguard Worker`cflags: "-DGENERIC -DSOC_A -DFEATURE -DWIDTH=200"` and
557*333d2b36SAndroid Build Coastguard Worker`srcs: ["*.cpp", "impl/foo.cpp", "impl/bar.cpp"]`.
558*333d2b36SAndroid Build Coastguard Worker
559*333d2b36SAndroid Build Coastguard WorkerAlternatively, with `DefaultBoardConfig.mk`:
560*333d2b36SAndroid Build Coastguard Worker
561*333d2b36SAndroid Build Coastguard Worker```
562*333d2b36SAndroid Build Coastguard WorkerSOONG_CONFIG_NAMESPACES += acme
563*333d2b36SAndroid Build Coastguard WorkerSOONG_CONFIG_acme += \
564*333d2b36SAndroid Build Coastguard Worker    board \
565*333d2b36SAndroid Build Coastguard Worker    feature \
566*333d2b36SAndroid Build Coastguard Worker    impl \
567*333d2b36SAndroid Build Coastguard Worker    width \
568*333d2b36SAndroid Build Coastguard Worker
569*333d2b36SAndroid Build Coastguard WorkerSOONG_CONFIG_acme_feature := false
570*333d2b36SAndroid Build Coastguard Worker```
571*333d2b36SAndroid Build Coastguard Worker
572*333d2b36SAndroid Build Coastguard Workerthen `libacme_foo` would build with `cflags: "-DGENERIC -DSOC_DEFAULT -DFEATURE_DEFAULT -DSIZE=DEFAULT"`
573*333d2b36SAndroid Build Coastguard Workerand `srcs: ["*.cpp", "impl/default.cpp"]`.
574*333d2b36SAndroid Build Coastguard Worker
575*333d2b36SAndroid Build Coastguard WorkerAlternatively, with `DefaultBoardConfig.mk`:
576*333d2b36SAndroid Build Coastguard Worker
577*333d2b36SAndroid Build Coastguard Worker```
578*333d2b36SAndroid Build Coastguard WorkerSOONG_CONFIG_NAMESPACES += acme
579*333d2b36SAndroid Build Coastguard WorkerSOONG_CONFIG_acme += \
580*333d2b36SAndroid Build Coastguard Worker    board \
581*333d2b36SAndroid Build Coastguard Worker    feature \
582*333d2b36SAndroid Build Coastguard Worker    impl \
583*333d2b36SAndroid Build Coastguard Worker    width \
584*333d2b36SAndroid Build Coastguard Worker
585*333d2b36SAndroid Build Coastguard WorkerSOONG_CONFIG_acme_board := soc_c
586*333d2b36SAndroid Build Coastguard WorkerSOONG_CONFIG_acme_impl := baz
587*333d2b36SAndroid Build Coastguard Worker```
588*333d2b36SAndroid Build Coastguard Worker
589*333d2b36SAndroid Build Coastguard Workerthen `libacme_foo` would build with `cflags: "-DGENERIC -DSOC_DEFAULT
590*333d2b36SAndroid Build Coastguard Worker-DFEATURE_DEFAULT -DSIZE=DEFAULT"` and `srcs: ["*.cpp", "impl/baz.cpp"]`.
591*333d2b36SAndroid Build Coastguard Worker
592*333d2b36SAndroid Build Coastguard Worker`soong_config_module_type` modules will work best when used to wrap defaults
593*333d2b36SAndroid Build Coastguard Workermodules (`cc_defaults`, `java_defaults`, etc.), which can then be referenced
594*333d2b36SAndroid Build Coastguard Workerby all of the vendor's other modules using the normal namespace and visibility
595*333d2b36SAndroid Build Coastguard Workerrules.
596*333d2b36SAndroid Build Coastguard Worker
597*333d2b36SAndroid Build Coastguard Worker## Build logic
598*333d2b36SAndroid Build Coastguard Worker
599*333d2b36SAndroid Build Coastguard WorkerThe build logic is written in Go using the
600*333d2b36SAndroid Build Coastguard Worker[blueprint](https://android.googlesource.com/platform/build/blueprint)
601*333d2b36SAndroid Build Coastguard Workerframework.  Build logic receives module definitions parsed into Go structures
602*333d2b36SAndroid Build Coastguard Workerusing reflection and produces build rules.  The build rules are collected by
603*333d2b36SAndroid Build Coastguard Workerblueprint and written to a [ninja](http://ninja-build.org) build file.
604*333d2b36SAndroid Build Coastguard Worker
605*333d2b36SAndroid Build Coastguard Worker## Environment Variables Config File
606*333d2b36SAndroid Build Coastguard Worker
607*333d2b36SAndroid Build Coastguard WorkerSoong can optionally load environment variables from a pre-specified
608*333d2b36SAndroid Build Coastguard Workerconfiguration file during startup. These environment variables can be used
609*333d2b36SAndroid Build Coastguard Workerto control the behavior of the build. For example, these variables can determine
610*333d2b36SAndroid Build Coastguard Workerwhether remote-execution should be used for the build or not.
611*333d2b36SAndroid Build Coastguard Worker
612*333d2b36SAndroid Build Coastguard WorkerThe `ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR` environment variable specifies the
613*333d2b36SAndroid Build Coastguard Workerdirectory in which the config file should be searched for. The
614*333d2b36SAndroid Build Coastguard Worker`ANDROID_BUILD_ENVIRONMENT_CONFIG` variable determines the name of the config
615*333d2b36SAndroid Build Coastguard Workerfile to be searched for within the config directory. For example, the following
616*333d2b36SAndroid Build Coastguard Workerbuild comand will load `ENV_VAR_1` and `ENV_VAR_2` environment variables from
617*333d2b36SAndroid Build Coastguard Workerthe `example_config.json` file inside the `build/soong` directory.
618*333d2b36SAndroid Build Coastguard Worker
619*333d2b36SAndroid Build Coastguard Worker```
620*333d2b36SAndroid Build Coastguard WorkerANDROID_BUILD_ENVIRONMENT_CONFIG_DIR=build/soong \
621*333d2b36SAndroid Build Coastguard Worker  ANDROID_BUILD_ENVIRONMENT_CONFIG=example_config \
622*333d2b36SAndroid Build Coastguard Worker  build/soong/soong_ui.bash
623*333d2b36SAndroid Build Coastguard Worker```
624*333d2b36SAndroid Build Coastguard Worker
625*333d2b36SAndroid Build Coastguard Worker## Other documentation
626*333d2b36SAndroid Build Coastguard Worker
627*333d2b36SAndroid Build Coastguard Worker* [Best Practices](docs/best_practices.md)
628*333d2b36SAndroid Build Coastguard Worker* [Build Performance](docs/perf.md)
629*333d2b36SAndroid Build Coastguard Worker* [Generating CLion Projects](docs/clion.md)
630*333d2b36SAndroid Build Coastguard Worker* [Generating YouCompleteMe/VSCode compile\_commands.json file](docs/compdb.md)
631*333d2b36SAndroid Build Coastguard Worker* Make-specific documentation: [build/make/README.md](https://android.googlesource.com/platform/build/+/main/README.md)
632*333d2b36SAndroid Build Coastguard Worker
633*333d2b36SAndroid Build Coastguard Worker## Developing for Soong
634*333d2b36SAndroid Build Coastguard Worker
635*333d2b36SAndroid Build Coastguard WorkerTo load the code of Soong in IntelliJ:
636*333d2b36SAndroid Build Coastguard Worker
637*333d2b36SAndroid Build Coastguard Worker* File -> Open, open the `build/soong` directory. It will be opened as a new
638*333d2b36SAndroid Build Coastguard Worker  project.
639*333d2b36SAndroid Build Coastguard Worker* File -> Settings, then Languages & Frameworks -> Go -> GOROOT, then set it to
640*333d2b36SAndroid Build Coastguard Worker  `prebuilts/go/linux-x86`
641*333d2b36SAndroid Build Coastguard Worker* File -> Project Structure, then, Project Settings -> Modules, then Add
642*333d2b36SAndroid Build Coastguard Worker  Content Root, then add the `build/blueprint` directory.
643*333d2b36SAndroid Build Coastguard Worker* Optional: also add the `external/golang-protobuf` directory. In practice,
644*333d2b36SAndroid Build Coastguard Worker  IntelliJ seems to work well enough without this, too.
645*333d2b36SAndroid Build Coastguard Worker
646*333d2b36SAndroid Build Coastguard Worker### Running Soong in a debugger
647*333d2b36SAndroid Build Coastguard Worker
648*333d2b36SAndroid Build Coastguard WorkerBoth the Android build driver (`soong_ui`) and Soong proper (`soong_build`) are
649*333d2b36SAndroid Build Coastguard WorkerGo applications and can be debugged with the help of the standard Go debugger
650*333d2b36SAndroid Build Coastguard Workercalled Delve. A client (e.g., IntelliJ IDEA) communicates with Delve via IP port
651*333d2b36SAndroid Build Coastguard Workerthat Delve listens to (the port number is passed to it on invocation).
652*333d2b36SAndroid Build Coastguard Worker
653*333d2b36SAndroid Build Coastguard Worker#### Debugging Android Build Driver ####
654*333d2b36SAndroid Build Coastguard WorkerTo make `soong_ui` wait for a debugger connection, use the `SOONG_UI_DELVE`
655*333d2b36SAndroid Build Coastguard Workervariable:
656*333d2b36SAndroid Build Coastguard Worker
657*333d2b36SAndroid Build Coastguard Worker```
658*333d2b36SAndroid Build Coastguard WorkerSOONG_UI_DELVE=5006 m nothing
659*333d2b36SAndroid Build Coastguard Worker```
660*333d2b36SAndroid Build Coastguard Worker
661*333d2b36SAndroid Build Coastguard Worker#### Debugging Soong Proper ####
662*333d2b36SAndroid Build Coastguard Worker
663*333d2b36SAndroid Build Coastguard WorkerTo make `soong_build` wait for a debugger connection, install `dlv` and then
664*333d2b36SAndroid Build Coastguard Workerstart the build with `SOONG_DELVE=<listen addr>` in the environment.
665*333d2b36SAndroid Build Coastguard WorkerFor example:
666*333d2b36SAndroid Build Coastguard Worker```bash
667*333d2b36SAndroid Build Coastguard WorkerSOONG_DELVE=5006 m nothing
668*333d2b36SAndroid Build Coastguard Worker```
669*333d2b36SAndroid Build Coastguard WorkerAndroid build driver invokes `soong_build` multiple times, and by default each
670*333d2b36SAndroid Build Coastguard Workerinvocation is run in the debugger. Setting `SOONG_DELVE_STEPS` controls which
671*333d2b36SAndroid Build Coastguard Workerinvocations are run in the debugger, e.g., running
672*333d2b36SAndroid Build Coastguard Worker```bash
673*333d2b36SAndroid Build Coastguard WorkerSOONG_DELVE=2345 SOONG_DELVE_STEPS='build,modulegraph' m
674*333d2b36SAndroid Build Coastguard Worker```
675*333d2b36SAndroid Build Coastguard Workerresults in only `build` (main build step) and `modulegraph` being run in the debugger.
676*333d2b36SAndroid Build Coastguard WorkerThe allowed step names are `build`, `soong_docs`.
677*333d2b36SAndroid Build Coastguard Worker
678*333d2b36SAndroid Build Coastguard WorkerNote setting or unsetting `SOONG_DELVE` causes a recompilation of `soong_build`. This
679*333d2b36SAndroid Build Coastguard Workeris because in order to debug the binary, it needs to be built with debug
680*333d2b36SAndroid Build Coastguard Workersymbols.
681*333d2b36SAndroid Build Coastguard Worker#### Delve Troubleshooting ####
682*333d2b36SAndroid Build Coastguard WorkerTo test the debugger connection, run this command:
683*333d2b36SAndroid Build Coastguard Worker
684*333d2b36SAndroid Build Coastguard Worker```
685*333d2b36SAndroid Build Coastguard Workerdlv connect :5006
686*333d2b36SAndroid Build Coastguard Worker```
687*333d2b36SAndroid Build Coastguard Worker
688*333d2b36SAndroid Build Coastguard WorkerIf you see an error:
689*333d2b36SAndroid Build Coastguard Worker```
690*333d2b36SAndroid Build Coastguard WorkerCould not attach to pid 593: this could be caused by a kernel
691*333d2b36SAndroid Build Coastguard Workersecurity setting, try writing "0" to /proc/sys/kernel/yama/ptrace_scope
692*333d2b36SAndroid Build Coastguard Worker```
693*333d2b36SAndroid Build Coastguard Workeryou can temporarily disable
694*333d2b36SAndroid Build Coastguard Worker[Yama's ptrace protection](https://www.kernel.org/doc/Documentation/security/Yama.txt)
695*333d2b36SAndroid Build Coastguard Workerusing:
696*333d2b36SAndroid Build Coastguard Worker```bash
697*333d2b36SAndroid Build Coastguard Workersudo sysctl -w kernel.yama.ptrace_scope=0
698*333d2b36SAndroid Build Coastguard Worker```
699*333d2b36SAndroid Build Coastguard Worker
700*333d2b36SAndroid Build Coastguard Worker#### IntelliJ Setup ####
701*333d2b36SAndroid Build Coastguard WorkerTo connect to the process using IntelliJ:
702*333d2b36SAndroid Build Coastguard Worker
703*333d2b36SAndroid Build Coastguard Worker* Run -> Edit Configurations...
704*333d2b36SAndroid Build Coastguard Worker* Choose "Go Remote" on the left
705*333d2b36SAndroid Build Coastguard Worker* Click on the "+" buttion on the top-left
706*333d2b36SAndroid Build Coastguard Worker* Give it a nice _name_ and set "Host" to `localhost` and "Port" to the port in the
707*333d2b36SAndroid Build Coastguard Worker  environment variable (`SOONG_UI_DELVE` for `soong_ui`, `SOONG_DELVE` for
708*333d2b36SAndroid Build Coastguard Worker  `soong_build`)
709*333d2b36SAndroid Build Coastguard Worker* Set the breakpoints where you want application to stop
710*333d2b36SAndroid Build Coastguard Worker* Run the build from the command line
711*333d2b36SAndroid Build Coastguard Worker* In IntelliJ, click Run -> Debug _name_
712*333d2b36SAndroid Build Coastguard Worker* Observe _Connecting..._ message in the debugger pane. It changes to
713*333d2b36SAndroid Build Coastguard Worker  _Connected_ once the communication with the debugger has been established; the
714*333d2b36SAndroid Build Coastguard Worker  terminal window where the build started will display
715*333d2b36SAndroid Build Coastguard Worker  `API server listening at ...` message
716*333d2b36SAndroid Build Coastguard Worker
717*333d2b36SAndroid Build Coastguard Worker
718*333d2b36SAndroid Build Coastguard WorkerSometimes the `dlv` process hangs on connection. A symptom of this is `dlv`
719*333d2b36SAndroid Build Coastguard Workerspinning a core or two. In that case, `kill -9` `dlv` and try again.
720*333d2b36SAndroid Build Coastguard WorkerAnecdotally, it _feels_ like waiting a minute after the start of `soong_build`
721*333d2b36SAndroid Build Coastguard Workerhelps.
722*333d2b36SAndroid Build Coastguard Worker
723*333d2b36SAndroid Build Coastguard Worker## Contact
724*333d2b36SAndroid Build Coastguard Worker
725*333d2b36SAndroid Build Coastguard WorkerEmail [email protected] (external) for any questions, or see
726*333d2b36SAndroid Build Coastguard Worker[go/soong](http://go/soong) (internal).
727