| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 1 | # Soong |
| 2 | |
| Lukacs T. Berki | 84f8b98 | 2022-10-10 07:06:35 +0000 | [diff] [blame] | 3 | Soong 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; |
| 11 | a "module" is the basic unit of building that Soong understands, similarly to |
| 12 | how "target" is the basic unit of building for Bazel (and Make, although the |
| 13 | two kinds of "targets" are very different) |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 14 | |
| Clay Murphy | 021e376 | 2019-01-08 10:55:51 -0800 | [diff] [blame] | 15 | See [Simple Build |
| 16 | Configuration](https://source.android.com/compatibility/tests/development/blueprints) |
| 17 | on source.android.com to read how Soong is configured for testing. |
| 18 | |
| Lukacs T. Berki | 84f8b98 | 2022-10-10 07:06:35 +0000 | [diff] [blame] | 19 | ### Contributing |
| 20 | |
| 21 | Code reviews are handled through the usual code review system of Android, |
| 22 | available [here](https://android-review.googlesource.com/dashboard/self). |
| 23 | |
| 24 | For simple changes (fixing typos, obvious optimizations, etc.), sending a code |
| 25 | review request is enough. For more substantial changes, file a bug in our |
| 26 | [bug tracker](https://issuetracker.google.com/issues/new?component=381517) or |
| 27 | or write us at android-building@googlegroups.com . |
| 28 | |
| 29 | For Googlers, see our [internal documentation](http://go/soong). |
| 30 | |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 31 | ## Android.bp file format |
| 32 | |
| 33 | By design, Android.bp files are very simple. There are no conditionals or |
| 34 | control flow statements - any complexity is handled in build logic written in |
| Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 35 | Go. The syntax and semantics of Android.bp files are intentionally similar |
| Yilin Ma | 03a9dfc | 2022-08-12 23:17:14 +0000 | [diff] [blame] | 36 | to [Bazel BUILD files](https://bazel.build/concepts/build-files) when possible. |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 37 | |
| 38 | ### Modules |
| 39 | |
| 40 | A module in an Android.bp file starts with a module type, followed by a set of |
| 41 | properties in `name: value,` format: |
| 42 | |
| 43 | ``` |
| 44 | cc_binary { |
| 45 | name: "gzip", |
| 46 | srcs: ["src/test/minigzip.c"], |
| 47 | shared_libs: ["libz"], |
| 48 | stl: "none", |
| 49 | } |
| 50 | ``` |
| 51 | |
| 52 | Every module must have a `name` property, and the value must be unique across |
| 53 | all Android.bp files. |
| 54 | |
| Lukacs T. Berki | c6012f3 | 2021-09-06 18:31:46 +0200 | [diff] [blame] | 55 | The 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`. |
| 57 | This 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 Stjernholm | 49531ab | 2019-11-22 15:52:14 +0000 | [diff] [blame] | 59 | ### File lists |
| Colin Cross | 645332d | 2018-04-25 15:06:01 -0700 | [diff] [blame] | 60 | |
| Martin Stjernholm | 49531ab | 2019-11-22 15:52:14 +0000 | [diff] [blame] | 61 | Properties that take a list of files can also take glob patterns and output path |
| 62 | expansions. |
| 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 Cross | 645332d | 2018-04-25 15:06:01 -0700 | [diff] [blame] | 81 | |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 82 | ### Variables |
| 83 | |
| 84 | An Android.bp file may contain top-level variable assignments: |
| 85 | ``` |
| 86 | gzip_srcs = ["src/test/minigzip.c"], |
| 87 | |
| 88 | cc_binary { |
| 89 | name: "gzip", |
| 90 | srcs: gzip_srcs, |
| 91 | shared_libs: ["libz"], |
| 92 | stl: "none", |
| 93 | } |
| 94 | ``` |
| 95 | |
| 96 | Variables are scoped to the remainder of the file they are declared in, as well |
| Sasha Smundak | d42609e | 2019-11-21 13:23:44 -0800 | [diff] [blame] | 97 | as any child Android.bp files. Variables are immutable with one exception - they |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 98 | can be appended to with a += assignment, but only before they have been |
| 99 | referenced. |
| 100 | |
| 101 | ### Comments |
| Martin Stjernholm | 49531ab | 2019-11-22 15:52:14 +0000 | [diff] [blame] | 102 | |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 103 | Android.bp files can contain C-style multiline `/* */` and C++ style single-line |
| 104 | `//` comments. |
| 105 | |
| 106 | ### Types |
| 107 | |
| 108 | Variables and properties are strongly typed, variables dynamically based on the |
| 109 | first assignment, and properties statically by the module type. The supported |
| 110 | types are: |
| 111 | * Bool (`true` or `false`) |
| Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 112 | * Integers (`int`) |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 113 | * Strings (`"string"`) |
| 114 | * Lists of strings (`["string1", "string2"]`) |
| 115 | * Maps (`{key1: "value1", key2: ["value2"]}`) |
| 116 | |
| 117 | Maps may values of any type, including nested maps. Lists and maps may have |
| 118 | trailing commas after the last value. |
| 119 | |
| Colin Cross | e4a0584 | 2019-05-28 10:17:14 -0700 | [diff] [blame] | 120 | Strings can contain double quotes using `\"`, for example `"cat \"a b\""`. |
| 121 | |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 122 | ### Operators |
| 123 | |
| 124 | Strings, lists of strings, and maps can be appended using the `+` operator. |
| Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 125 | Integers can be summed up using the `+` operator. Appending a map produces the |
| 126 | union of keys in both maps, appending the values of any keys that are present |
| 127 | in both maps. |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 128 | |
| 129 | ### Defaults modules |
| 130 | |
| 131 | A defaults module can be used to repeat the same properties in multiple modules. |
| 132 | For example: |
| 133 | |
| 134 | ``` |
| 135 | cc_defaults { |
| 136 | name: "gzip_defaults", |
| 137 | shared_libs: ["libz"], |
| 138 | stl: "none", |
| 139 | } |
| 140 | |
| 141 | cc_binary { |
| 142 | name: "gzip", |
| 143 | defaults: ["gzip_defaults"], |
| 144 | srcs: ["src/test/minigzip.c"], |
| 145 | } |
| 146 | ``` |
| 147 | |
| Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 148 | ### Packages |
| 149 | |
| 150 | The build is organized into packages where each package is a collection of related files and a |
| 151 | specification of the dependencies among them in the form of modules. |
| 152 | |
| 153 | A package is defined as a directory containing a file named `Android.bp`, residing beneath the |
| 154 | top-level directory in the build and its name is its path relative to the top-level directory. A |
| 155 | package includes all files in its directory, plus all subdirectories beneath it, except those which |
| 156 | themselves contain an `Android.bp` file. |
| 157 | |
| 158 | The modules in a package's `Android.bp` and included files are part of the module. |
| 159 | |
| 160 | For example, in the following directory tree (where `.../android/` is the top-level Android |
| 161 | directory) 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 | |
| 170 | This is based on the Bazel package concept. |
| 171 | |
| Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 172 | The `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 |
| 174 | in the same package directory it is highly recommended that the `package` module (if required) is |
| 175 | specified in the `Android.bp` file. |
| 176 | |
| 177 | Unlike most module type `package` does not have a `name` property. Instead the name is set to the |
| 178 | name of the package, e.g. if the package is in `top/intermediate/package` then the package name is |
| 179 | `//top/intermediate/package`. |
| 180 | |
| Paul Duffin | e484f47 | 2019-06-20 16:38:08 +0100 | [diff] [blame] | 181 | E.g. The following will set the default visibility for all the modules defined in the package and |
| 182 | any subpackages that do not set their own default visibility (irrespective of whether they are in |
| 183 | the same `.bp` file as the `package` module) to be visible to all the subpackages by default. |
| Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 184 | |
| 185 | ``` |
| 186 | package { |
| 187 | default_visibility: [":__subpackages"] |
| 188 | } |
| 189 | ``` |
| 190 | |
| Sasha Smundak | d42609e | 2019-11-21 13:23:44 -0800 | [diff] [blame] | 191 | ### Referencing Modules |
| Jeff Gaston | 44c0cd8 | 2017-11-29 19:51:52 -0800 | [diff] [blame] | 192 | |
| Sasha Smundak | d42609e | 2019-11-21 13:23:44 -0800 | [diff] [blame] | 193 | A module `libfoo` can be referenced by its name |
| Jeff Gaston | 44c0cd8 | 2017-11-29 19:51:52 -0800 | [diff] [blame] | 194 | |
| 195 | ``` |
| Sasha Smundak | d42609e | 2019-11-21 13:23:44 -0800 | [diff] [blame] | 196 | cc_binary { |
| 197 | name: "app", |
| 198 | shared_libs: ["libfoo"], |
| Jeff Gaston | 44c0cd8 | 2017-11-29 19:51:52 -0800 | [diff] [blame] | 199 | } |
| 200 | ``` |
| 201 | |
| Sasha Smundak | d42609e | 2019-11-21 13:23:44 -0800 | [diff] [blame] | 202 | Obviously, this works only if there is only one `libfoo` module in the source |
| 203 | tree. Ensuring such name uniqueness for larger trees may become problematic. We |
| 204 | might also want to use the same name in multiple mutually exclusive subtrees |
| 205 | (for example, implementing different devices) deliberately in order to describe |
| 206 | a functionally equivalent module. Enter Soong namespaces. |
| Jeff Gaston | 44c0cd8 | 2017-11-29 19:51:52 -0800 | [diff] [blame] | 207 | |
| Sasha Smundak | d42609e | 2019-11-21 13:23:44 -0800 | [diff] [blame] | 208 | #### Namespaces |
| Jeff Gaston | 44c0cd8 | 2017-11-29 19:51:52 -0800 | [diff] [blame] | 209 | |
| Usta Shrestha | c725f47 | 2022-01-11 02:44:21 -0500 | [diff] [blame] | 210 | The presence of the `soong_namespace {..}` in an Android.bp file defines a |
| Sasha Smundak | d42609e | 2019-11-21 13:23:44 -0800 | [diff] [blame] | 211 | **namespace**. For instance, having |
| 212 | |
| 213 | ``` |
| 214 | soong_namespace { |
| 215 | ... |
| 216 | } |
| 217 | ... |
| 218 | ``` |
| 219 | |
| 220 | in `device/google/bonito/Android.bp` informs Soong that within the |
| 221 | `device/google/bonito` package the module names are unique, that is, all the |
| 222 | modules defined in the Android.bp files in the `device/google/bonito/` tree have |
| 223 | unique names. However, there may be modules with the same names outside |
| 224 | `device/google/bonito` tree. Indeed, there is a module `"pixelstats-vendor"` |
| 225 | both in `device/google/bonito/pixelstats` and in |
| 226 | `device/google/coral/pixelstats`. |
| 227 | |
| 228 | The name of a namespace is the path of its directory. The name of the namespace |
| 229 | in the example above is thus `device/google/bonito`. |
| 230 | |
| 231 | An implicit **global namespace** corresponds to the source tree as a whole. It |
| 232 | has empty name. |
| 233 | |
| 234 | A module name's **scope** is the smallest namespace containing it. Suppose a |
| 235 | source tree has `device/my` and `device/my/display` namespaces. If `libfoo` |
| Filip | 87112d6 | 2021-06-29 21:23:39 +0000 | [diff] [blame] | 236 | module is defined in `device/my/display/lib/Android.bp`, its namespace is |
| 237 | `device/my/display`. |
| Sasha Smundak | d42609e | 2019-11-21 13:23:44 -0800 | [diff] [blame] | 238 | |
| 239 | The name uniqueness thus means that module's name is unique within its scope. In |
| 240 | other words, "//_scope_:_name_" is globally unique module reference, e.g, |
| 241 | `"//device/google/bonito:pixelstats-vendor"`. _Note_ that the name of the |
| 242 | namespace for a module may be different from module's package name: `libfoo` |
| 243 | belongs to `device/my/display` namespace but is contained in |
| 244 | `device/my/display/lib` package. |
| 245 | |
| 246 | #### Name Resolution |
| 247 | |
| 248 | The form of a module reference determines how Soong locates the module. |
| 249 | |
| 250 | For a **global reference** of the "//_scope_:_name_" form, Soong verifies there |
| 251 | is a namespace called "_scope_", then verifies it contains a "_name_" module and |
| 252 | uses it. Soong verifies there is only one "_name_" in "_scope_" at the beginning |
| 253 | when it parses Android.bp files. |
| 254 | |
| 255 | A **local reference** has "_name_" form, and resolving it involves looking for a |
| 256 | module "_name_" in one or more namespaces. By default only the global namespace |
| 257 | is searched for "_name_" (in other words, only the modules not belonging to an |
| 258 | explicitly defined scope are considered). The `imports` attribute of the |
| 259 | `soong_namespaces` allows to specify where to look for modules . For instance, |
| 260 | with `device/google/bonito/Android.bp` containing |
| 261 | |
| 262 | ``` |
| 263 | soong_namespace { |
| 264 | imports: [ |
| 265 | "hardware/google/interfaces", |
| 266 | "hardware/google/pixel", |
| 267 | "hardware/qcom/bootctrl", |
| 268 | ], |
| 269 | } |
| 270 | ``` |
| 271 | |
| 272 | a 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 |
| 277 | constructs for namespace definition and name resolution (`namespace` and `using` |
| 278 | in C++, for instance). Should Soong do that, too? |
| 279 | |
| 280 | #### Referencing modules in makefiles |
| 281 | |
| 282 | While we are gradually converting makefiles to Android.bp files, Android build |
| 283 | is described by a mixture of Android.bp and Android.mk files, and a module |
| 284 | defined in an Android.mk file can reference a module defined in Android.bp file. |
| 285 | For instance, a binary still defined in an Android.mk file may have a library |
| 286 | defined in already converted Android.bp as a dependency. |
| 287 | |
| 288 | A module defined in an Android.bp file and belonging to the global namespace can |
| 289 | be referenced from a makefile without additional effort. If a module belongs to |
| 290 | an explicit namespace, it can be referenced from a makefile only after after the |
| 291 | name of the namespace has been added to the value of PRODUCT_SOONG_NAMESPACES |
| 292 | variable. |
| 293 | |
| 294 | Note that makefiles have no notion of namespaces and exposing namespaces with |
| 295 | the same modules via PRODUCT_SOONG_NAMESPACES may cause Make failure. For |
| 296 | instance, exposing both `device/google/bonito` and `device/google/coral` |
| 297 | namespaces will cause Make failure because it will see two targets for the |
| 298 | `pixelstats-vendor` module. |
| Jeff Gaston | 44c0cd8 | 2017-11-29 19:51:52 -0800 | [diff] [blame] | 299 | |
| Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 300 | ### Visibility |
| 301 | |
| 302 | The `visibility` property on a module controls whether the module can be |
| 303 | used by other packages. Modules are always visible to other modules declared |
| 304 | in the same package. This is based on the Bazel visibility mechanism. |
| 305 | |
| 306 | If specified the `visibility` property must contain at least one rule. |
| 307 | |
| 308 | Each 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 |
| 311 | subpackages) can use this module. |
| Paul Duffin | 51084ff | 2020-05-05 19:19:22 +0100 | [diff] [blame] | 312 | * `["//visibility:override"]`: Discards any rules inherited from defaults or a |
| 313 | creating module. Can only be used at the beginning of a list of visibility |
| 314 | rules. |
| Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 315 | * `["//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 |
| 318 | have access to the rule; for example, `//some/package/foo:bar` or |
| 319 | `//other/package/testing:bla` wouldn't have access. `__pkg__` is a special |
| 320 | module and must be used verbatim. It represents all of the modules in the |
| 321 | package. |
| 322 | * `["//project:__subpackages__", "//other:__subpackages__"]`: Only modules in |
| 323 | packages `project` or `other` or in one of their sub-packages have access to |
| 324 | this 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 Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 329 | where `//project` is the module's package, e.g. using `[":__subpackages__"]` in |
| Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 330 | `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 Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 335 | The visibility rules of `//visibility:public` and `//visibility:private` cannot |
| Martin Stjernholm | 226b20d | 2019-05-17 22:42:02 +0100 | [diff] [blame] | 336 | be combined with any other visibility specifications, except |
| 337 | `//visibility:public` is allowed to override visibility specifications imported |
| 338 | through the `defaults` property. |
| Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 339 | |
| 340 | Packages outside `vendor/` cannot make themselves visible to specific packages |
| 341 | in `vendor/`, e.g. a module in `libcore` cannot declare that it is visible to |
| 342 | say `vendor/google`, instead it must make itself visible to all packages within |
| 343 | `vendor/` using `//vendor:__subpackages__`. |
| 344 | |
| Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 345 | If 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 | |
| 348 | If the `default_visibility` property is not set for the module's package then |
| Paul Duffin | e484f47 | 2019-06-20 16:38:08 +0100 | [diff] [blame] | 349 | it will use the `default_visibility` of its closest ancestor package for which |
| 350 | a `default_visibility` property is specified. |
| 351 | |
| 352 | If no `default_visibility` property can be found then the module uses the |
| 353 | global default of `//visibility:legacy_public`. |
| Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 354 | |
| Paul Duffin | 95d53b5 | 2019-07-24 13:45:05 +0100 | [diff] [blame] | 355 | The `visibility` property has no effect on a defaults module although it does |
| 356 | apply to any non-defaults module that uses it. To set the visibility of a |
| 357 | defaults module, use the `defaults_visibility` property on the defaults module; |
| 358 | not to be confused with the `default_visibility` property on the package module. |
| 359 | |
| Paul Duffin | e2453c7 | 2019-05-31 14:00:04 +0100 | [diff] [blame] | 360 | Once the build has been completely switched over to soong it is possible that a |
| 361 | global refactoring will be done to change this to `//visibility:private` at |
| 362 | which point all packages that do not currently specify a `default_visibility` |
| 363 | property will be updated to have |
| 364 | `default_visibility = [//visibility:legacy_public]` added. It will then be the |
| 365 | owner's responsibility to replace that with a more appropriate visibility. |
| Paul Duffin | 2e61fa6 | 2019-03-28 14:10:57 +0000 | [diff] [blame] | 366 | |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 367 | ### Formatter |
| 368 | |
| Sasha Smundak | d42609e | 2019-11-21 13:23:44 -0800 | [diff] [blame] | 369 | Soong includes a canonical formatter for Android.bp files, similar to |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 370 | [gofmt](https://golang.org/cmd/gofmt/). To recursively reformat all Android.bp files |
| 371 | in the current directory: |
| 372 | ``` |
| 373 | bpfmt -w . |
| 374 | ``` |
| 375 | |
| 376 | The canonical format includes 4 space indents, newlines after every element of a |
| 377 | multi-element list, and always includes a trailing comma in lists and maps. |
| 378 | |
| 379 | ### Convert Android.mk files |
| 380 | |
| 381 | Soong includes a tool perform a first pass at converting Android.mk files |
| 382 | to Android.bp files: |
| 383 | |
| 384 | ``` |
| 385 | androidmk Android.mk > Android.bp |
| 386 | ``` |
| 387 | |
| 388 | The tool converts variables, modules, comments, and some conditionals, but any |
| Colin Cross | 024c32e | 2016-09-26 15:42:42 -0700 | [diff] [blame] | 389 | custom Makefile rules, complex conditionals or extra includes must be converted |
| 390 | by 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 |
| 395 | for static and shared version of a library, or for host and device versions). |
| 396 | Android.bp files require unique names for every module, but a single module can |
| 397 | be built in multiple variants, for example by adding `host_supported: true`. |
| 398 | The androidmk converter will produce multiple conflicting modules, which must |
| 399 | be resolved by hand to a single module with any differences inside |
| 400 | `target: { android: { }, host: { } }` blocks. |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 401 | |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 402 | ### Conditionals |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 403 | |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 404 | Soong deliberately does not support most conditionals in Android.bp files. We |
| Colin Cross | 2322c4d | 2019-11-12 14:39:17 -0800 | [diff] [blame] | 405 | suggest removing most conditionals from the build. See |
| 406 | [Best Practices](docs/best_practices.md#removing-conditionals) for some |
| 407 | examples on how to remove conditionals. |
| 408 | |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 409 | Most conditionals supported natively by Soong are converted to a map |
| Colin Cross | 2322c4d | 2019-11-12 14:39:17 -0800 | [diff] [blame] | 410 | property. When building the module one of the properties in the map will be |
| 411 | selected, and its values appended to the property with the same name at the |
| 412 | top level of the module. |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 413 | |
| 414 | For example, to support architecture specific files: |
| 415 | ``` |
| 416 | cc_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 Cross | 2322c4d | 2019-11-12 14:39:17 -0800 | [diff] [blame] | 430 | When building the module for arm the `generic.cpp` and `arm.cpp` sources will |
| 431 | be built. When building for x86 the `generic.cpp` and 'x86.cpp' sources will |
| 432 | be built. |
| Colin Cross | 1a01e83 | 2017-01-13 18:00:19 -0800 | [diff] [blame] | 433 | |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 434 | #### Soong Config Variables |
| 435 | |
| 436 | When converting vendor modules that contain conditionals, simple conditionals |
| 437 | can be supported through Soong config variables using `soong_config_*` |
| 438 | modules that describe the module types, variables and possible values: |
| 439 | |
| 440 | ``` |
| 441 | soong_config_module_type { |
| 442 | name: "acme_cc_defaults", |
| 443 | module_type: "cc_defaults", |
| 444 | config_namespace: "acme", |
| Dan Willemsen | 2b8b89c | 2020-03-23 19:39:34 -0700 | [diff] [blame] | 445 | variables: ["board"], |
| 446 | bool_variables: ["feature"], |
| Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 447 | value_variables: ["width"], |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 448 | properties: ["cflags", "srcs"], |
| 449 | } |
| 450 | |
| 451 | soong_config_string_variable { |
| 452 | name: "board", |
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 453 | values: ["soc_a", "soc_b", "soc_c"], |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 454 | } |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 455 | ``` |
| 456 | |
| 457 | This example describes a new `acme_cc_defaults` module type that extends the |
| Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 458 | `cc_defaults` module type, with three additional conditionals based on |
| 459 | variables `board`, `feature` and `width`, which can affect properties `cflags` |
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 460 | and `srcs`. Additionally, each conditional will contain a `conditions_default` |
| 461 | property 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 |
| 466 | given module. For example, with `board`, if the `board` |
| 467 | conditional contains the properties `soc_a` and `conditions_default`, when |
| 468 | board=soc_b, the `cflags` and `srcs` values under `conditions_default` will be |
| 469 | used. To specify that no properties should be amended for `soc_b`, you can set |
| 470 | `soc_b: {},`. |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 471 | |
| 472 | The values of the variables can be set from a product's `BoardConfig.mk` file: |
| 473 | ``` |
| Cole Faust | d05d5f5 | 2022-01-13 17:18:57 -0800 | [diff] [blame] | 474 | $(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 Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 477 | ``` |
| 478 | |
| 479 | The `acme_cc_defaults` module type can be used anywhere after the definition in |
| 480 | the file where it is defined, or can be imported into another file with: |
| 481 | ``` |
| 482 | soong_config_module_type_import { |
| 483 | from: "device/acme/Android.bp", |
| 484 | module_types: ["acme_cc_defaults"], |
| 485 | } |
| 486 | ``` |
| 487 | |
| 488 | It can used like any other module type: |
| 489 | ``` |
| 490 | acme_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 Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 501 | conditions_default: { |
| 502 | cflags: ["-DSOC_DEFAULT"], |
| 503 | }, |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 504 | }, |
| 505 | feature: { |
| 506 | cflags: ["-DFEATURE"], |
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 507 | conditions_default: { |
| 508 | cflags: ["-DFEATURE_DEFAULT"], |
| 509 | }, |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 510 | }, |
| Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 511 | width: { |
| 512 | cflags: ["-DWIDTH=%s"], |
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 513 | conditions_default: { |
| 514 | cflags: ["-DWIDTH=DEFAULT"], |
| 515 | }, |
| Dan Willemsen | b0935db | 2020-03-23 19:42:18 -0700 | [diff] [blame] | 516 | }, |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 517 | }, |
| 518 | } |
| 519 | |
| 520 | cc_library { |
| 521 | name: "libacme_foo", |
| 522 | defaults: ["acme_defaults"], |
| 523 | srcs: ["*.cpp"], |
| 524 | } |
| 525 | ``` |
| 526 | |
| Liz Kammer | 432bd59 | 2020-12-16 12:42:02 -0800 | [diff] [blame] | 527 | With the `BoardConfig.mk` snippet above, `libacme_foo` would build with |
| 528 | `cflags: "-DGENERIC -DSOC_A -DFEATURE -DWIDTH=200"`. |
| 529 | |
| 530 | Alternatively, with `DefaultBoardConfig.mk`: |
| 531 | |
| 532 | ``` |
| 533 | SOONG_CONFIG_NAMESPACES += acme |
| 534 | SOONG_CONFIG_acme += \ |
| 535 | board \ |
| 536 | feature \ |
| 537 | width \ |
| 538 | |
| 539 | SOONG_CONFIG_acme_feature := false |
| 540 | ``` |
| 541 | |
| 542 | then `libacme_foo` would build with `cflags: "-DGENERIC -DSOC_DEFAULT -DFEATURE_DEFAULT -DSIZE=DEFAULT"`. |
| 543 | |
| 544 | Alternatively, with `DefaultBoardConfig.mk`: |
| 545 | |
| 546 | ``` |
| 547 | SOONG_CONFIG_NAMESPACES += acme |
| 548 | SOONG_CONFIG_acme += \ |
| 549 | board \ |
| 550 | feature \ |
| 551 | width \ |
| 552 | |
| 553 | SOONG_CONFIG_acme_board := soc_c |
| 554 | ``` |
| 555 | |
| 556 | then `libacme_foo` would build with `cflags: "-DGENERIC -DSOC_DEFAULT |
| 557 | -DFEATURE_DEFAULT -DSIZE=DEFAULT"`. |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 558 | |
| 559 | `soong_config_module_type` modules will work best when used to wrap defaults |
| 560 | modules (`cc_defaults`, `java_defaults`, etc.), which can then be referenced |
| 561 | by all of the vendor's other modules using the normal namespace and visibility |
| 562 | rules. |
| 563 | |
| 564 | ## Build logic |
| 565 | |
| 566 | The build logic is written in Go using the |
| 567 | [blueprint](http://godoc.org/github.com/google/blueprint) framework. Build |
| 568 | logic receives module definitions parsed into Go structures using reflection |
| 569 | and produces build rules. The build rules are collected by blueprint and |
| 570 | written to a [ninja](http://ninja-build.org) build file. |
| 571 | |
| Kousik Kumar | d13fcd8 | 2022-02-08 23:02:29 -0500 | [diff] [blame] | 572 | ## Environment Variables Config File |
| 573 | |
| 574 | Soong can optionally load environment variables from a pre-specified |
| 575 | configuration file during startup. These environment variables can be used |
| 576 | to control the behavior of the build. For example, these variables can determine |
| 577 | whether remote-execution should be used for the build or not. |
| 578 | |
| 579 | The `ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR` environment variable specifies the |
| 580 | directory in which the config file should be searched for. The |
| 581 | `ANDROID_BUILD_ENVIRONMENT_CONFIG` variable determines the name of the config |
| 582 | file to be searched for within the config directory. For example, the following |
| 583 | build comand will load `ENV_VAR_1` and `ENV_VAR_2` environment variables from |
| 584 | the `example_config.json` file inside the `build/soong` directory. |
| 585 | |
| 586 | ``` |
| 587 | ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR=build/soong \ |
| 588 | ANDROID_BUILD_ENVIRONMENT_CONFIG=example_config \ |
| 589 | build/soong/soong_ui.bash |
| 590 | ``` |
| 591 | |
| Colin Cross | 9d34f35 | 2019-11-22 16:03:51 -0800 | [diff] [blame] | 592 | ## 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 Cross | aa070b4 | 2018-07-09 09:44:41 -0700 | [diff] [blame] | 600 | ## Developing for Soong |
| 601 | |
| Lukacs T. Berki | 9699c52 | 2021-09-30 12:54:34 +0200 | [diff] [blame] | 602 | To load the code of Soong in IntelliJ: |
| Colin Cross | aa070b4 | 2018-07-09 09:44:41 -0700 | [diff] [blame] | 603 | |
| Lukacs T. Berki | 9699c52 | 2021-09-30 12:54:34 +0200 | [diff] [blame] | 604 | * 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 Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 612 | |
| Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 613 | ### Running Soong in a debugger |
| 614 | |
| Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 615 | Both the Android build driver (`soong_ui`) and Soong proper (`soong_build`) are |
| 616 | Go applications and can be debugged with the help of the standard Go debugger |
| 617 | called Delve. A client (e.g., IntelliJ IDEA) communicates with Delve via IP port |
| 618 | that Delve listens to (the port number is passed to it on invocation). |
| Lukacs T. Berki | 9699c52 | 2021-09-30 12:54:34 +0200 | [diff] [blame] | 619 | |
| Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 620 | #### Debugging Android Build Driver #### |
| Lukacs T. Berki | 9699c52 | 2021-09-30 12:54:34 +0200 | [diff] [blame] | 621 | To make `soong_ui` wait for a debugger connection, use the `SOONG_UI_DELVE` |
| 622 | variable: |
| 623 | |
| Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 624 | ``` |
| usta | 406015b | 2022-08-16 16:50:48 -0400 | [diff] [blame] | 625 | SOONG_UI_DELVE=5006 m nothing |
| Lukacs T. Berki | 9699c52 | 2021-09-30 12:54:34 +0200 | [diff] [blame] | 626 | ``` |
| 627 | |
| Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 628 | #### Debugging Soong Proper #### |
| Lukacs T. Berki | 9699c52 | 2021-09-30 12:54:34 +0200 | [diff] [blame] | 629 | |
| Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 630 | To make `soong_build` wait for a debugger connection, install `dlv` and then |
| 631 | start the build with `SOONG_DELVE=<listen addr>` in the environment. |
| 632 | For example: |
| 633 | ```bash |
| 634 | SOONG_DELVE=5006 m nothing |
| 635 | ``` |
| 636 | Android build driver invokes `soong_build` multiple times, and by default each |
| 637 | invocation is run in the debugger. Setting `SOONG_DELVE_STEPS` controls which |
| 638 | invocations are run in the debugger, e.g., running |
| 639 | ```bash |
| 640 | SOONG_DELVE=2345 SOONG_DELVE_STEPS='build,modulegraph' m |
| 641 | ``` |
| 642 | results in only `build` (main build step) and `modulegraph` being run in the debugger. |
| 643 | The allowed step names are `api_bp2build`, `bp2build_files`, `bp2build_workspace`, |
| 644 | `build`, `modulegraph`, `queryview`, `soong_docs`. |
| 645 | |
| 646 | Note setting or unsetting `SOONG_DELVE` causes a recompilation of `soong_build`. This |
| Lukacs T. Berki | 9699c52 | 2021-09-30 12:54:34 +0200 | [diff] [blame] | 647 | is because in order to debug the binary, it needs to be built with debug |
| 648 | symbols. |
| Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 649 | #### Delve Troubleshooting #### |
| Lukacs T. Berki | 9699c52 | 2021-09-30 12:54:34 +0200 | [diff] [blame] | 650 | To test the debugger connection, run this command: |
| 651 | |
| 652 | ``` |
| 653 | dlv connect :5006 |
| Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 654 | ``` |
| 655 | |
| Colin Cross | 8baf29f | 2019-07-29 16:43:11 -0700 | [diff] [blame] | 656 | If you see an error: |
| 657 | ``` |
| 658 | Could not attach to pid 593: this could be caused by a kernel |
| 659 | security setting, try writing "0" to /proc/sys/kernel/yama/ptrace_scope |
| 660 | ``` |
| 661 | you can temporarily disable |
| 662 | [Yama's ptrace protection](https://www.kernel.org/doc/Documentation/security/Yama.txt) |
| 663 | using: |
| 664 | ```bash |
| 665 | sudo sysctl -w kernel.yama.ptrace_scope=0 |
| 666 | ``` |
| 667 | |
| Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 668 | #### IntelliJ Setup #### |
| Lukacs T. Berki | 9699c52 | 2021-09-30 12:54:34 +0200 | [diff] [blame] | 669 | To 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 Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 674 | * 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. Berki | 9699c52 | 2021-09-30 12:54:34 +0200 | [diff] [blame] | 684 | |
| Lukacs T. Berki | 9699c52 | 2021-09-30 12:54:34 +0200 | [diff] [blame] | 685 | |
| 686 | Sometimes the `dlv` process hangs on connection. A symptom of this is `dlv` |
| 687 | spinning a core or two. In that case, `kill -9` `dlv` and try again. |
| 688 | Anecdotally, it _feels_ like waiting a minute after the start of `soong_build` |
| 689 | helps. |
| 690 | |
| Colin Cross | e10ce18 | 2016-07-18 10:58:47 -0700 | [diff] [blame] | 691 | ## Contact |
| 692 | |
| 693 | Email android-building@googlegroups.com (external) for any questions, or see |
| 694 | [go/soong](http://go/soong) (internal). |