blob: 7f1846351defb1bba65996ec673d43aef51fa62d [file] [log] [blame] [view]
Colin Crosse10ce182016-07-18 10:58:47 -07001# Soong
2
Lukacs T. Berki84f8b982022-10-10 07:06:35 +00003Soong is one of the build systems used in Android. There are altogether three:
4* The legacy Make-based build system that is controlled by files called
5 `Android.mk`.
6* Soong, which is controlled by files called `Android.bp`.
7* The upcoming Bazel-based build system that is controlled by files called
8 `BUILD.bazel`.
9
10`Android.bp` file are JSON-like declarative descriptions of "modules" to build;
11a "module" is the basic unit of building that Soong understands, similarly to
12how "target" is the basic unit of building for Bazel (and Make, although the
13two kinds of "targets" are very different)
Colin Crosse10ce182016-07-18 10:58:47 -070014
Clay Murphy021e3762019-01-08 10:55:51 -080015See [Simple Build
16Configuration](https://source.android.com/compatibility/tests/development/blueprints)
17on source.android.com to read how Soong is configured for testing.
18
Lukacs T. Berki84f8b982022-10-10 07:06:35 +000019### Contributing
20
21Code reviews are handled through the usual code review system of Android,
22available [here](https://android-review.googlesource.com/dashboard/self).
23
24For simple changes (fixing typos, obvious optimizations, etc.), sending a code
25review request is enough. For more substantial changes, file a bug in our
26[bug tracker](https://issuetracker.google.com/issues/new?component=381517) or
27or write us at android-building@googlegroups.com .
28
29For Googlers, see our [internal documentation](http://go/soong).
30
Colin Crosse10ce182016-07-18 10:58:47 -070031## Android.bp file format
32
33By design, Android.bp files are very simple. There are no conditionals or
34control flow statements - any complexity is handled in build logic written in
Colin Cross068e0fe2016-12-13 15:23:47 -080035Go. The syntax and semantics of Android.bp files are intentionally similar
Yilin Ma03a9dfc2022-08-12 23:17:14 +000036to [Bazel BUILD files](https://bazel.build/concepts/build-files) when possible.
Colin Crosse10ce182016-07-18 10:58:47 -070037
38### Modules
39
40A module in an Android.bp file starts with a module type, followed by a set of
41properties in `name: value,` format:
42
43```
44cc_binary {
45 name: "gzip",
46 srcs: ["src/test/minigzip.c"],
47 shared_libs: ["libz"],
48 stl: "none",
49}
50```
51
52Every module must have a `name` property, and the value must be unique across
53all Android.bp files.
54
Lukacs T. Berkic6012f32021-09-06 18:31:46 +020055The list of valid module types and their properties can be generated by calling
56`m soong_docs`. It will be written to `$OUT_DIR/soong/docs/soong_build.html`.
57This 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).
58
Martin Stjernholm49531ab2019-11-22 15:52:14 +000059### File lists
Colin Cross645332d2018-04-25 15:06:01 -070060
Martin Stjernholm49531ab2019-11-22 15:52:14 +000061Properties that take a list of files can also take glob patterns and output path
62expansions.
63
64* Glob patterns can contain the normal Unix wildcard `*`, for example `"*.java"`.
65
66 Glob patterns can also contain a single `**` wildcard as a path element, which
67 will match zero or more path elements. For example, `java/**/*.java` will match
68 `java/Main.java` and `java/com/android/Main.java`.
69
70* Output path expansions take the format `:module` or `:module{.tag}`, where
71 `module` is the name of a module that produces output files, and it expands to
72 a list of those output files. With the optional `{.tag}` suffix, the module
73 may produce a different list of outputs according to `tag`.
74
75 For example, a `droiddoc` module with the name "my-docs" would return its
76 `.stubs.srcjar` output with `":my-docs"`, and its `.doc.zip` file with
77 `":my-docs{.doc.zip}"`.
78
79 This is commonly used to reference `filegroup` modules, whose output files
80 consist of their `srcs`.
Colin Cross645332d2018-04-25 15:06:01 -070081
Colin Crosse10ce182016-07-18 10:58:47 -070082### Variables
83
84An Android.bp file may contain top-level variable assignments:
85```
86gzip_srcs = ["src/test/minigzip.c"],
87
88cc_binary {
89 name: "gzip",
90 srcs: gzip_srcs,
91 shared_libs: ["libz"],
92 stl: "none",
93}
94```
95
96Variables are scoped to the remainder of the file they are declared in, as well
Sasha Smundakd42609e2019-11-21 13:23:44 -080097as any child Android.bp files. Variables are immutable with one exception - they
Colin Crosse10ce182016-07-18 10:58:47 -070098can be appended to with a += assignment, but only before they have been
99referenced.
100
101### Comments
Martin Stjernholm49531ab2019-11-22 15:52:14 +0000102
Colin Crosse10ce182016-07-18 10:58:47 -0700103Android.bp files can contain C-style multiline `/* */` and C++ style single-line
104`//` comments.
105
106### Types
107
108Variables and properties are strongly typed, variables dynamically based on the
109first assignment, and properties statically by the module type. The supported
110types are:
111* Bool (`true` or `false`)
Nan Zhang61eaedb2017-11-02 13:28:15 -0700112* Integers (`int`)
Colin Crosse10ce182016-07-18 10:58:47 -0700113* Strings (`"string"`)
114* Lists of strings (`["string1", "string2"]`)
115* Maps (`{key1: "value1", key2: ["value2"]}`)
116
117Maps may values of any type, including nested maps. Lists and maps may have
118trailing commas after the last value.
119
Colin Crosse4a05842019-05-28 10:17:14 -0700120Strings can contain double quotes using `\"`, for example `"cat \"a b\""`.
121
Colin Crosse10ce182016-07-18 10:58:47 -0700122### Operators
123
124Strings, lists of strings, and maps can be appended using the `+` operator.
Nan Zhang61eaedb2017-11-02 13:28:15 -0700125Integers can be summed up using the `+` operator. Appending a map produces the
126union of keys in both maps, appending the values of any keys that are present
127in both maps.
Colin Crosse10ce182016-07-18 10:58:47 -0700128
129### Defaults modules
130
131A defaults module can be used to repeat the same properties in multiple modules.
132For example:
133
134```
135cc_defaults {
136 name: "gzip_defaults",
137 shared_libs: ["libz"],
138 stl: "none",
139}
140
141cc_binary {
142 name: "gzip",
143 defaults: ["gzip_defaults"],
144 srcs: ["src/test/minigzip.c"],
145}
146```
147
Paul Duffin2e61fa62019-03-28 14:10:57 +0000148### Packages
149
150The build is organized into packages where each package is a collection of related files and a
151specification of the dependencies among them in the form of modules.
152
153A package is defined as a directory containing a file named `Android.bp`, residing beneath the
154top-level directory in the build and its name is its path relative to the top-level directory. A
155package includes all files in its directory, plus all subdirectories beneath it, except those which
156themselves contain an `Android.bp` file.
157
158The modules in a package's `Android.bp` and included files are part of the module.
159
160For example, in the following directory tree (where `.../android/` is the top-level Android
161directory) there are two packages, `my/app`, and the subpackage `my/app/tests`. Note that
162`my/app/data` is not a package, but a directory belonging to package `my/app`.
163
164 .../android/my/app/Android.bp
165 .../android/my/app/app.cc
166 .../android/my/app/data/input.txt
167 .../android/my/app/tests/Android.bp
168 .../android/my/app/tests/test.cc
169
170This is based on the Bazel package concept.
171
Paul Duffine2453c72019-05-31 14:00:04 +0100172The `package` module type allows information to be specified about a package. Only a single
173`package` module can be specified per package and in the case where there are multiple `.bp` files
174in the same package directory it is highly recommended that the `package` module (if required) is
175specified in the `Android.bp` file.
176
177Unlike most module type `package` does not have a `name` property. Instead the name is set to the
178name of the package, e.g. if the package is in `top/intermediate/package` then the package name is
179`//top/intermediate/package`.
180
Paul Duffine484f472019-06-20 16:38:08 +0100181E.g. The following will set the default visibility for all the modules defined in the package and
182any subpackages that do not set their own default visibility (irrespective of whether they are in
183the same `.bp` file as the `package` module) to be visible to all the subpackages by default.
Paul Duffine2453c72019-05-31 14:00:04 +0100184
185```
186package {
187 default_visibility: [":__subpackages"]
188}
189```
190
Sasha Smundakd42609e2019-11-21 13:23:44 -0800191### Referencing Modules
Jeff Gaston44c0cd82017-11-29 19:51:52 -0800192
Sasha Smundakd42609e2019-11-21 13:23:44 -0800193A module `libfoo` can be referenced by its name
Jeff Gaston44c0cd82017-11-29 19:51:52 -0800194
195```
Sasha Smundakd42609e2019-11-21 13:23:44 -0800196cc_binary {
197 name: "app",
198 shared_libs: ["libfoo"],
Jeff Gaston44c0cd82017-11-29 19:51:52 -0800199}
200```
201
Sasha Smundakd42609e2019-11-21 13:23:44 -0800202Obviously, this works only if there is only one `libfoo` module in the source
203tree. Ensuring such name uniqueness for larger trees may become problematic. We
204might also want to use the same name in multiple mutually exclusive subtrees
205(for example, implementing different devices) deliberately in order to describe
206a functionally equivalent module. Enter Soong namespaces.
Jeff Gaston44c0cd82017-11-29 19:51:52 -0800207
Sasha Smundakd42609e2019-11-21 13:23:44 -0800208#### Namespaces
Jeff Gaston44c0cd82017-11-29 19:51:52 -0800209
Usta Shresthac725f472022-01-11 02:44:21 -0500210The presence of the `soong_namespace {..}` in an Android.bp file defines a
Sasha Smundakd42609e2019-11-21 13:23:44 -0800211**namespace**. For instance, having
212
213```
214soong_namespace {
215 ...
216}
217...
218```
219
220in `device/google/bonito/Android.bp` informs Soong that within the
221`device/google/bonito` package the module names are unique, that is, all the
222modules defined in the Android.bp files in the `device/google/bonito/` tree have
223unique names. However, there may be modules with the same names outside
224`device/google/bonito` tree. Indeed, there is a module `"pixelstats-vendor"`
225both in `device/google/bonito/pixelstats` and in
226`device/google/coral/pixelstats`.
227
228The name of a namespace is the path of its directory. The name of the namespace
229in the example above is thus `device/google/bonito`.
230
231An implicit **global namespace** corresponds to the source tree as a whole. It
232has empty name.
233
234A module name's **scope** is the smallest namespace containing it. Suppose a
235source tree has `device/my` and `device/my/display` namespaces. If `libfoo`
Filip87112d62021-06-29 21:23:39 +0000236module is defined in `device/my/display/lib/Android.bp`, its namespace is
237`device/my/display`.
Sasha Smundakd42609e2019-11-21 13:23:44 -0800238
239The name uniqueness thus means that module's name is unique within its scope. In
240other words, "//_scope_:_name_" is globally unique module reference, e.g,
241`"//device/google/bonito:pixelstats-vendor"`. _Note_ that the name of the
242namespace for a module may be different from module's package name: `libfoo`
243belongs to `device/my/display` namespace but is contained in
244`device/my/display/lib` package.
245
246#### Name Resolution
247
248The form of a module reference determines how Soong locates the module.
249
250For a **global reference** of the "//_scope_:_name_" form, Soong verifies there
251is a namespace called "_scope_", then verifies it contains a "_name_" module and
252uses it. Soong verifies there is only one "_name_" in "_scope_" at the beginning
253when it parses Android.bp files.
254
255A **local reference** has "_name_" form, and resolving it involves looking for a
256module "_name_" in one or more namespaces. By default only the global namespace
257is searched for "_name_" (in other words, only the modules not belonging to an
258explicitly defined scope are considered). The `imports` attribute of the
259`soong_namespaces` allows to specify where to look for modules . For instance,
260with `device/google/bonito/Android.bp` containing
261
262```
263soong_namespace {
264 imports: [
265 "hardware/google/interfaces",
266 "hardware/google/pixel",
267 "hardware/qcom/bootctrl",
268 ],
269}
270```
271
272a reference to `"libpixelstats"` will resolve to the module defined in
273`hardware/google/pixel/pixelstats/Android.bp` because this module is in
274`hardware/google/pixel` namespace.
275
276**TODO**: Conventionally, languages with similar concepts provide separate
277constructs for namespace definition and name resolution (`namespace` and `using`
278in C++, for instance). Should Soong do that, too?
279
280#### Referencing modules in makefiles
281
282While we are gradually converting makefiles to Android.bp files, Android build
283is described by a mixture of Android.bp and Android.mk files, and a module
284defined in an Android.mk file can reference a module defined in Android.bp file.
285For instance, a binary still defined in an Android.mk file may have a library
286defined in already converted Android.bp as a dependency.
287
288A module defined in an Android.bp file and belonging to the global namespace can
289be referenced from a makefile without additional effort. If a module belongs to
290an explicit namespace, it can be referenced from a makefile only after after the
291name of the namespace has been added to the value of PRODUCT_SOONG_NAMESPACES
292variable.
293
294Note that makefiles have no notion of namespaces and exposing namespaces with
295the same modules via PRODUCT_SOONG_NAMESPACES may cause Make failure. For
296instance, exposing both `device/google/bonito` and `device/google/coral`
297namespaces will cause Make failure because it will see two targets for the
298`pixelstats-vendor` module.
Jeff Gaston44c0cd82017-11-29 19:51:52 -0800299
Paul Duffin2e61fa62019-03-28 14:10:57 +0000300### Visibility
301
302The `visibility` property on a module controls whether the module can be
303used by other packages. Modules are always visible to other modules declared
304in the same package. This is based on the Bazel visibility mechanism.
305
306If specified the `visibility` property must contain at least one rule.
307
308Each rule in the property must be in one of the following forms:
309* `["//visibility:public"]`: Anyone can use this module.
310* `["//visibility:private"]`: Only rules in the module's package (not its
311subpackages) can use this module.
Paul Duffin51084ff2020-05-05 19:19:22 +0100312* `["//visibility:override"]`: Discards any rules inherited from defaults or a
313creating module. Can only be used at the beginning of a list of visibility
314rules.
Paul Duffin2e61fa62019-03-28 14:10:57 +0000315* `["//some/package:__pkg__", "//other/package:__pkg__"]`: Only modules in
316`some/package` and `other/package` (defined in `some/package/*.bp` and
317`other/package/*.bp`) have access to this module. Note that sub-packages do not
318have access to the rule; for example, `//some/package/foo:bar` or
319`//other/package/testing:bla` wouldn't have access. `__pkg__` is a special
320module and must be used verbatim. It represents all of the modules in the
321package.
322* `["//project:__subpackages__", "//other:__subpackages__"]`: Only modules in
323packages `project` or `other` or in one of their sub-packages have access to
324this module. For example, `//project:rule`, `//project/library:lib` or
325`//other/testing/internal:munge` are allowed to depend on this rule (but not
326`//independent:evil`)
327* `["//project"]`: This is shorthand for `["//project:__pkg__"]`
328* `[":__subpackages__"]`: This is shorthand for `["//project:__subpackages__"]`
Paul Duffine2453c72019-05-31 14:00:04 +0100329where `//project` is the module's package, e.g. using `[":__subpackages__"]` in
Paul Duffin2e61fa62019-03-28 14:10:57 +0000330`packages/apps/Settings/Android.bp` is equivalent to
331`//packages/apps/Settings:__subpackages__`.
332* `["//visibility:legacy_public"]`: The default visibility, behaves as
333`//visibility:public` for now. It is an error if it is used in a module.
334
Paul Duffine2453c72019-05-31 14:00:04 +0100335The visibility rules of `//visibility:public` and `//visibility:private` cannot
Martin Stjernholm226b20d2019-05-17 22:42:02 +0100336be combined with any other visibility specifications, except
337`//visibility:public` is allowed to override visibility specifications imported
338through the `defaults` property.
Paul Duffin2e61fa62019-03-28 14:10:57 +0000339
340Packages outside `vendor/` cannot make themselves visible to specific packages
341in `vendor/`, e.g. a module in `libcore` cannot declare that it is visible to
342say `vendor/google`, instead it must make itself visible to all packages within
343`vendor/` using `//vendor:__subpackages__`.
344
Paul Duffine2453c72019-05-31 14:00:04 +0100345If a module does not specify the `visibility` property then it uses the
346`default_visibility` property of the `package` module in the module's package.
347
348If the `default_visibility` property is not set for the module's package then
Paul Duffine484f472019-06-20 16:38:08 +0100349it will use the `default_visibility` of its closest ancestor package for which
350a `default_visibility` property is specified.
351
352If no `default_visibility` property can be found then the module uses the
353global default of `//visibility:legacy_public`.
Paul Duffine2453c72019-05-31 14:00:04 +0100354
Paul Duffin95d53b52019-07-24 13:45:05 +0100355The `visibility` property has no effect on a defaults module although it does
356apply to any non-defaults module that uses it. To set the visibility of a
357defaults module, use the `defaults_visibility` property on the defaults module;
358not to be confused with the `default_visibility` property on the package module.
359
Paul Duffine2453c72019-05-31 14:00:04 +0100360Once the build has been completely switched over to soong it is possible that a
361global refactoring will be done to change this to `//visibility:private` at
362which point all packages that do not currently specify a `default_visibility`
363property will be updated to have
364`default_visibility = [//visibility:legacy_public]` added. It will then be the
365owner's responsibility to replace that with a more appropriate visibility.
Paul Duffin2e61fa62019-03-28 14:10:57 +0000366
Colin Crosse10ce182016-07-18 10:58:47 -0700367### Formatter
368
Sasha Smundakd42609e2019-11-21 13:23:44 -0800369Soong includes a canonical formatter for Android.bp files, similar to
Colin Crosse10ce182016-07-18 10:58:47 -0700370[gofmt](https://golang.org/cmd/gofmt/). To recursively reformat all Android.bp files
371in the current directory:
372```
373bpfmt -w .
374```
375
376The canonical format includes 4 space indents, newlines after every element of a
377multi-element list, and always includes a trailing comma in lists and maps.
378
379### Convert Android.mk files
380
381Soong includes a tool perform a first pass at converting Android.mk files
382to Android.bp files:
383
384```
385androidmk Android.mk > Android.bp
386```
387
388The tool converts variables, modules, comments, and some conditionals, but any
Colin Cross024c32e2016-09-26 15:42:42 -0700389custom Makefile rules, complex conditionals or extra includes must be converted
390by hand.
391
392#### Differences between Android.mk and Android.bp
393
394* Android.mk files often have multiple modules with the same name (for example
395for static and shared version of a library, or for host and device versions).
396Android.bp files require unique names for every module, but a single module can
397be built in multiple variants, for example by adding `host_supported: true`.
398The androidmk converter will produce multiple conflicting modules, which must
399be resolved by hand to a single module with any differences inside
400`target: { android: { }, host: { } }` blocks.
Colin Crosse10ce182016-07-18 10:58:47 -0700401
Colin Cross9d34f352019-11-22 16:03:51 -0800402### Conditionals
Colin Crosse10ce182016-07-18 10:58:47 -0700403
Colin Cross9d34f352019-11-22 16:03:51 -0800404Soong deliberately does not support most conditionals in Android.bp files. We
Colin Cross2322c4d2019-11-12 14:39:17 -0800405suggest removing most conditionals from the build. See
406[Best Practices](docs/best_practices.md#removing-conditionals) for some
407examples on how to remove conditionals.
408
Colin Cross9d34f352019-11-22 16:03:51 -0800409Most conditionals supported natively by Soong are converted to a map
Colin Cross2322c4d2019-11-12 14:39:17 -0800410property. When building the module one of the properties in the map will be
411selected, and its values appended to the property with the same name at the
412top level of the module.
Colin Crosse10ce182016-07-18 10:58:47 -0700413
414For example, to support architecture specific files:
415```
416cc_library {
417 ...
418 srcs: ["generic.cpp"],
419 arch: {
420 arm: {
421 srcs: ["arm.cpp"],
422 },
423 x86: {
424 srcs: ["x86.cpp"],
425 },
426 },
427}
428```
429
Colin Cross2322c4d2019-11-12 14:39:17 -0800430When building the module for arm the `generic.cpp` and `arm.cpp` sources will
431be built. When building for x86 the `generic.cpp` and 'x86.cpp' sources will
432be built.
Colin Cross1a01e832017-01-13 18:00:19 -0800433
Colin Cross9d34f352019-11-22 16:03:51 -0800434#### Soong Config Variables
435
436When converting vendor modules that contain conditionals, simple conditionals
437can be supported through Soong config variables using `soong_config_*`
438modules that describe the module types, variables and possible values:
439
440```
441soong_config_module_type {
442 name: "acme_cc_defaults",
443 module_type: "cc_defaults",
444 config_namespace: "acme",
Dan Willemsen2b8b89c2020-03-23 19:39:34 -0700445 variables: ["board"],
446 bool_variables: ["feature"],
Dan Willemsenb0935db2020-03-23 19:42:18 -0700447 value_variables: ["width"],
Colin Cross9d34f352019-11-22 16:03:51 -0800448 properties: ["cflags", "srcs"],
449}
450
451soong_config_string_variable {
452 name: "board",
Liz Kammer432bd592020-12-16 12:42:02 -0800453 values: ["soc_a", "soc_b", "soc_c"],
Colin Cross9d34f352019-11-22 16:03:51 -0800454}
Colin Cross9d34f352019-11-22 16:03:51 -0800455```
456
457This example describes a new `acme_cc_defaults` module type that extends the
Dan Willemsenb0935db2020-03-23 19:42:18 -0700458`cc_defaults` module type, with three additional conditionals based on
459variables `board`, `feature` and `width`, which can affect properties `cflags`
Liz Kammer432bd592020-12-16 12:42:02 -0800460and `srcs`. Additionally, each conditional will contain a `conditions_default`
461property can affect `cflags` and `srcs` in the following conditions:
462
463* bool variable (e.g. `feature`): the variable is unspecified or not set to a true value
464* value variable (e.g. `width`): the variable is unspecified
465* string variable (e.g. `board`): the variable is unspecified or the variable is set to a string unused in the
466given module. For example, with `board`, if the `board`
467conditional contains the properties `soc_a` and `conditions_default`, when
468board=soc_b, the `cflags` and `srcs` values under `conditions_default` will be
469used. To specify that no properties should be amended for `soc_b`, you can set
470`soc_b: {},`.
Colin Cross9d34f352019-11-22 16:03:51 -0800471
472The values of the variables can be set from a product's `BoardConfig.mk` file:
473```
Cole Faustd05d5f52022-01-13 17:18:57 -0800474$(call soong_config_set,acme,board,soc_a)
475$(call soong_config_set,acme,feature,true)
476$(call soong_config_set,acme,width,200)
Colin Cross9d34f352019-11-22 16:03:51 -0800477```
478
479The `acme_cc_defaults` module type can be used anywhere after the definition in
480the file where it is defined, or can be imported into another file with:
481```
482soong_config_module_type_import {
483 from: "device/acme/Android.bp",
484 module_types: ["acme_cc_defaults"],
485}
486```
487
488It can used like any other module type:
489```
490acme_cc_defaults {
491 name: "acme_defaults",
492 cflags: ["-DGENERIC"],
493 soong_config_variables: {
494 board: {
495 soc_a: {
496 cflags: ["-DSOC_A"],
497 },
498 soc_b: {
499 cflags: ["-DSOC_B"],
500 },
Liz Kammer432bd592020-12-16 12:42:02 -0800501 conditions_default: {
502 cflags: ["-DSOC_DEFAULT"],
503 },
Colin Cross9d34f352019-11-22 16:03:51 -0800504 },
505 feature: {
506 cflags: ["-DFEATURE"],
Liz Kammer432bd592020-12-16 12:42:02 -0800507 conditions_default: {
508 cflags: ["-DFEATURE_DEFAULT"],
509 },
Colin Cross9d34f352019-11-22 16:03:51 -0800510 },
Dan Willemsenb0935db2020-03-23 19:42:18 -0700511 width: {
512 cflags: ["-DWIDTH=%s"],
Liz Kammer432bd592020-12-16 12:42:02 -0800513 conditions_default: {
514 cflags: ["-DWIDTH=DEFAULT"],
515 },
Dan Willemsenb0935db2020-03-23 19:42:18 -0700516 },
Colin Cross9d34f352019-11-22 16:03:51 -0800517 },
518}
519
520cc_library {
521 name: "libacme_foo",
522 defaults: ["acme_defaults"],
523 srcs: ["*.cpp"],
524}
525```
526
Liz Kammer432bd592020-12-16 12:42:02 -0800527With the `BoardConfig.mk` snippet above, `libacme_foo` would build with
528`cflags: "-DGENERIC -DSOC_A -DFEATURE -DWIDTH=200"`.
529
530Alternatively, with `DefaultBoardConfig.mk`:
531
532```
533SOONG_CONFIG_NAMESPACES += acme
534SOONG_CONFIG_acme += \
535 board \
536 feature \
537 width \
538
539SOONG_CONFIG_acme_feature := false
540```
541
542then `libacme_foo` would build with `cflags: "-DGENERIC -DSOC_DEFAULT -DFEATURE_DEFAULT -DSIZE=DEFAULT"`.
543
544Alternatively, with `DefaultBoardConfig.mk`:
545
546```
547SOONG_CONFIG_NAMESPACES += acme
548SOONG_CONFIG_acme += \
549 board \
550 feature \
551 width \
552
553SOONG_CONFIG_acme_board := soc_c
554```
555
556then `libacme_foo` would build with `cflags: "-DGENERIC -DSOC_DEFAULT
557-DFEATURE_DEFAULT -DSIZE=DEFAULT"`.
Colin Cross9d34f352019-11-22 16:03:51 -0800558
559`soong_config_module_type` modules will work best when used to wrap defaults
560modules (`cc_defaults`, `java_defaults`, etc.), which can then be referenced
561by all of the vendor's other modules using the normal namespace and visibility
562rules.
563
564## Build logic
565
566The build logic is written in Go using the
567[blueprint](http://godoc.org/github.com/google/blueprint) framework. Build
568logic receives module definitions parsed into Go structures using reflection
569and produces build rules. The build rules are collected by blueprint and
570written to a [ninja](http://ninja-build.org) build file.
571
Kousik Kumard13fcd82022-02-08 23:02:29 -0500572## Environment Variables Config File
573
574Soong can optionally load environment variables from a pre-specified
575configuration file during startup. These environment variables can be used
576to control the behavior of the build. For example, these variables can determine
577whether remote-execution should be used for the build or not.
578
579The `ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR` environment variable specifies the
580directory in which the config file should be searched for. The
581`ANDROID_BUILD_ENVIRONMENT_CONFIG` variable determines the name of the config
582file to be searched for within the config directory. For example, the following
583build comand will load `ENV_VAR_1` and `ENV_VAR_2` environment variables from
584the `example_config.json` file inside the `build/soong` directory.
585
586```
587ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR=build/soong \
588 ANDROID_BUILD_ENVIRONMENT_CONFIG=example_config \
589 build/soong/soong_ui.bash
590```
591
Colin Cross9d34f352019-11-22 16:03:51 -0800592## Other documentation
593
594* [Best Practices](docs/best_practices.md)
595* [Build Performance](docs/perf.md)
596* [Generating CLion Projects](docs/clion.md)
597* [Generating YouCompleteMe/VSCode compile\_commands.json file](docs/compdb.md)
598* Make-specific documentation: [build/make/README.md](https://android.googlesource.com/platform/build/+/master/README.md)
599
Colin Crossaa070b42018-07-09 09:44:41 -0700600## Developing for Soong
601
Lukacs T. Berki9699c522021-09-30 12:54:34 +0200602To load the code of Soong in IntelliJ:
Colin Crossaa070b42018-07-09 09:44:41 -0700603
Lukacs T. Berki9699c522021-09-30 12:54:34 +0200604* File -> Open, open the `build/soong` directory. It will be opened as a new
605 project.
606* File -> Settings, then Languages & Frameworks -> Go -> GOROOT, then set it to
607 `prebuilts/go/linux-x86`
608* File -> Project Structure, then, Project Settings -> Modules, then Add
609 Content Root, then add the `build/blueprint` directory.
610* Optional: also add the `external/golang-protobuf` directory. In practice,
611 IntelliJ seems to work well enough without this, too.
Sasha Smundak4cbe83a2022-11-28 17:02:40 -0800612
Colin Crossaa812d12019-06-19 13:33:24 -0700613### Running Soong in a debugger
614
Sasha Smundak4cbe83a2022-11-28 17:02:40 -0800615Both the Android build driver (`soong_ui`) and Soong proper (`soong_build`) are
616Go applications and can be debugged with the help of the standard Go debugger
617called Delve. A client (e.g., IntelliJ IDEA) communicates with Delve via IP port
618that Delve listens to (the port number is passed to it on invocation).
Lukacs T. Berki9699c522021-09-30 12:54:34 +0200619
Sasha Smundak4cbe83a2022-11-28 17:02:40 -0800620#### Debugging Android Build Driver ####
Lukacs T. Berki9699c522021-09-30 12:54:34 +0200621To make `soong_ui` wait for a debugger connection, use the `SOONG_UI_DELVE`
622variable:
623
Colin Crossaa812d12019-06-19 13:33:24 -0700624```
usta406015b2022-08-16 16:50:48 -0400625SOONG_UI_DELVE=5006 m nothing
Lukacs T. Berki9699c522021-09-30 12:54:34 +0200626```
627
Sasha Smundak4cbe83a2022-11-28 17:02:40 -0800628#### Debugging Soong Proper ####
Lukacs T. Berki9699c522021-09-30 12:54:34 +0200629
Sasha Smundak4cbe83a2022-11-28 17:02:40 -0800630To make `soong_build` wait for a debugger connection, install `dlv` and then
631start the build with `SOONG_DELVE=<listen addr>` in the environment.
632For example:
633```bash
634SOONG_DELVE=5006 m nothing
635```
636Android build driver invokes `soong_build` multiple times, and by default each
637invocation is run in the debugger. Setting `SOONG_DELVE_STEPS` controls which
638invocations are run in the debugger, e.g., running
639```bash
640SOONG_DELVE=2345 SOONG_DELVE_STEPS='build,modulegraph' m
641```
642results in only `build` (main build step) and `modulegraph` being run in the debugger.
643The allowed step names are `api_bp2build`, `bp2build_files`, `bp2build_workspace`,
644`build`, `modulegraph`, `queryview`, `soong_docs`.
645
646Note setting or unsetting `SOONG_DELVE` causes a recompilation of `soong_build`. This
Lukacs T. Berki9699c522021-09-30 12:54:34 +0200647is because in order to debug the binary, it needs to be built with debug
648symbols.
Sasha Smundak4cbe83a2022-11-28 17:02:40 -0800649#### Delve Troubleshooting ####
Lukacs T. Berki9699c522021-09-30 12:54:34 +0200650To test the debugger connection, run this command:
651
652```
653dlv connect :5006
Colin Crossaa812d12019-06-19 13:33:24 -0700654```
655
Colin Cross8baf29f2019-07-29 16:43:11 -0700656If you see an error:
657```
658Could not attach to pid 593: this could be caused by a kernel
659security setting, try writing "0" to /proc/sys/kernel/yama/ptrace_scope
660```
661you can temporarily disable
662[Yama's ptrace protection](https://www.kernel.org/doc/Documentation/security/Yama.txt)
663using:
664```bash
665sudo sysctl -w kernel.yama.ptrace_scope=0
666```
667
Sasha Smundak4cbe83a2022-11-28 17:02:40 -0800668#### IntelliJ Setup ####
Lukacs T. Berki9699c522021-09-30 12:54:34 +0200669To connect to the process using IntelliJ:
670
671* Run -> Edit Configurations...
672* Choose "Go Remote" on the left
673* Click on the "+" buttion on the top-left
Sasha Smundak4cbe83a2022-11-28 17:02:40 -0800674* Give it a nice _name_ and set "Host" to `localhost` and "Port" to the port in the
675 environment variable (`SOONG_UI_DELVE` for `soong_ui`, `SOONG_DELVE` for
676 `soong_build`)
677* Set the breakpoints where you want application to stop
678* Run the build from the command line
679* In IntelliJ, click Run -> Debug _name_
680* Observe _Connecting..._ message in the debugger pane. It changes to
681 _Connected_ once the communication with the debugger has been established; the
682 terminal window where the build started will display
683 `API server listening at ...` message
Lukacs T. Berki9699c522021-09-30 12:54:34 +0200684
Lukacs T. Berki9699c522021-09-30 12:54:34 +0200685
686Sometimes the `dlv` process hangs on connection. A symptom of this is `dlv`
687spinning a core or two. In that case, `kill -9` `dlv` and try again.
688Anecdotally, it _feels_ like waiting a minute after the start of `soong_build`
689helps.
690
Colin Crosse10ce182016-07-18 10:58:47 -0700691## Contact
692
693Email android-building@googlegroups.com (external) for any questions, or see
694[go/soong](http://go/soong) (internal).