1# Soong 2 3Soong is the replacement for the old Android make-based build system. It 4replaces Android.mk files with Android.bp files, which are JSON-like simple 5declarative descriptions of modules to build. 6 7See [Simple Build 8Configuration](https://source.android.com/compatibility/tests/development/blueprints) 9on source.android.com to read how Soong is configured for testing. 10 11## Android.bp file format 12 13By design, Android.bp files are very simple. There are no conditionals or 14control flow statements - any complexity is handled in build logic written in 15Go. The syntax and semantics of Android.bp files are intentionally similar 16to [Bazel BUILD files](https://www.bazel.io/versions/master/docs/be/overview.html) 17when possible. 18 19### Modules 20 21A module in an Android.bp file starts with a module type, followed by a set of 22properties in `name: value,` format: 23 24``` 25cc_binary { 26 name: "gzip", 27 srcs: ["src/test/minigzip.c"], 28 shared_libs: ["libz"], 29 stl: "none", 30} 31``` 32 33Every module must have a `name` property, and the value must be unique across 34all Android.bp files. 35 36For a list of valid module types and their properties see 37[$OUT_DIR/soong/docs/soong_build.html](https://ci.android.com/builds/latest/branches/aosp-build-tools/targets/linux/view/soong_build.html). 38 39### File lists 40 41Properties that take a list of files can also take glob patterns and output path 42expansions. 43 44* Glob patterns can contain the normal Unix wildcard `*`, for example `"*.java"`. 45 46 Glob patterns can also contain a single `**` wildcard as a path element, which 47 will match zero or more path elements. For example, `java/**/*.java` will match 48 `java/Main.java` and `java/com/android/Main.java`. 49 50* Output path expansions take the format `:module` or `:module{.tag}`, where 51 `module` is the name of a module that produces output files, and it expands to 52 a list of those output files. With the optional `{.tag}` suffix, the module 53 may produce a different list of outputs according to `tag`. 54 55 For example, a `droiddoc` module with the name "my-docs" would return its 56 `.stubs.srcjar` output with `":my-docs"`, and its `.doc.zip` file with 57 `":my-docs{.doc.zip}"`. 58 59 This is commonly used to reference `filegroup` modules, whose output files 60 consist of their `srcs`. 61 62### Variables 63 64An Android.bp file may contain top-level variable assignments: 65``` 66gzip_srcs = ["src/test/minigzip.c"], 67 68cc_binary { 69 name: "gzip", 70 srcs: gzip_srcs, 71 shared_libs: ["libz"], 72 stl: "none", 73} 74``` 75 76Variables are scoped to the remainder of the file they are declared in, as well 77as any child Android.bp files. Variables are immutable with one exception - they 78can be appended to with a += assignment, but only before they have been 79referenced. 80 81### Comments 82 83Android.bp files can contain C-style multiline `/* */` and C++ style single-line 84`//` comments. 85 86### Types 87 88Variables and properties are strongly typed, variables dynamically based on the 89first assignment, and properties statically by the module type. The supported 90types are: 91* Bool (`true` or `false`) 92* Integers (`int`) 93* Strings (`"string"`) 94* Lists of strings (`["string1", "string2"]`) 95* Maps (`{key1: "value1", key2: ["value2"]}`) 96 97Maps may values of any type, including nested maps. Lists and maps may have 98trailing commas after the last value. 99 100Strings can contain double quotes using `\"`, for example `"cat \"a b\""`. 101 102### Operators 103 104Strings, lists of strings, and maps can be appended using the `+` operator. 105Integers can be summed up using the `+` operator. Appending a map produces the 106union of keys in both maps, appending the values of any keys that are present 107in both maps. 108 109### Defaults modules 110 111A defaults module can be used to repeat the same properties in multiple modules. 112For example: 113 114``` 115cc_defaults { 116 name: "gzip_defaults", 117 shared_libs: ["libz"], 118 stl: "none", 119} 120 121cc_binary { 122 name: "gzip", 123 defaults: ["gzip_defaults"], 124 srcs: ["src/test/minigzip.c"], 125} 126``` 127 128### Packages 129 130The build is organized into packages where each package is a collection of related files and a 131specification of the dependencies among them in the form of modules. 132 133A package is defined as a directory containing a file named `Android.bp`, residing beneath the 134top-level directory in the build and its name is its path relative to the top-level directory. A 135package includes all files in its directory, plus all subdirectories beneath it, except those which 136themselves contain an `Android.bp` file. 137 138The modules in a package's `Android.bp` and included files are part of the module. 139 140For example, in the following directory tree (where `.../android/` is the top-level Android 141directory) there are two packages, `my/app`, and the subpackage `my/app/tests`. Note that 142`my/app/data` is not a package, but a directory belonging to package `my/app`. 143 144 .../android/my/app/Android.bp 145 .../android/my/app/app.cc 146 .../android/my/app/data/input.txt 147 .../android/my/app/tests/Android.bp 148 .../android/my/app/tests/test.cc 149 150This is based on the Bazel package concept. 151 152The `package` module type allows information to be specified about a package. Only a single 153`package` module can be specified per package and in the case where there are multiple `.bp` files 154in the same package directory it is highly recommended that the `package` module (if required) is 155specified in the `Android.bp` file. 156 157Unlike most module type `package` does not have a `name` property. Instead the name is set to the 158name of the package, e.g. if the package is in `top/intermediate/package` then the package name is 159`//top/intermediate/package`. 160 161E.g. The following will set the default visibility for all the modules defined in the package and 162any subpackages that do not set their own default visibility (irrespective of whether they are in 163the same `.bp` file as the `package` module) to be visible to all the subpackages by default. 164 165``` 166package { 167 default_visibility: [":__subpackages"] 168} 169``` 170 171### Referencing Modules 172 173A module `libfoo` can be referenced by its name 174 175``` 176cc_binary { 177 name: "app", 178 shared_libs: ["libfoo"], 179} 180``` 181 182Obviously, this works only if there is only one `libfoo` module in the source 183tree. Ensuring such name uniqueness for larger trees may become problematic. We 184might also want to use the same name in multiple mutually exclusive subtrees 185(for example, implementing different devices) deliberately in order to describe 186a functionally equivalent module. Enter Soong namespaces. 187 188#### Namespaces 189 190A presense of the `soong_namespace {..}` in an Android.bp file defines a 191**namespace**. For instance, having 192 193``` 194soong_namespace { 195 ... 196} 197... 198``` 199 200in `device/google/bonito/Android.bp` informs Soong that within the 201`device/google/bonito` package the module names are unique, that is, all the 202modules defined in the Android.bp files in the `device/google/bonito/` tree have 203unique names. However, there may be modules with the same names outside 204`device/google/bonito` tree. Indeed, there is a module `"pixelstats-vendor"` 205both in `device/google/bonito/pixelstats` and in 206`device/google/coral/pixelstats`. 207 208The name of a namespace is the path of its directory. The name of the namespace 209in the example above is thus `device/google/bonito`. 210 211An implicit **global namespace** corresponds to the source tree as a whole. It 212has empty name. 213 214A module name's **scope** is the smallest namespace containing it. Suppose a 215source tree has `device/my` and `device/my/display` namespaces. If `libfoo` 216module is defined in `device/co/display/lib/Android.bp`, its namespace is 217`device/co/display`. 218 219The name uniqueness thus means that module's name is unique within its scope. In 220other words, "//_scope_:_name_" is globally unique module reference, e.g, 221`"//device/google/bonito:pixelstats-vendor"`. _Note_ that the name of the 222namespace for a module may be different from module's package name: `libfoo` 223belongs to `device/my/display` namespace but is contained in 224`device/my/display/lib` package. 225 226#### Name Resolution 227 228The form of a module reference determines how Soong locates the module. 229 230For a **global reference** of the "//_scope_:_name_" form, Soong verifies there 231is a namespace called "_scope_", then verifies it contains a "_name_" module and 232uses it. Soong verifies there is only one "_name_" in "_scope_" at the beginning 233when it parses Android.bp files. 234 235A **local reference** has "_name_" form, and resolving it involves looking for a 236module "_name_" in one or more namespaces. By default only the global namespace 237is searched for "_name_" (in other words, only the modules not belonging to an 238explicitly defined scope are considered). The `imports` attribute of the 239`soong_namespaces` allows to specify where to look for modules . For instance, 240with `device/google/bonito/Android.bp` containing 241 242``` 243soong_namespace { 244 imports: [ 245 "hardware/google/interfaces", 246 "hardware/google/pixel", 247 "hardware/qcom/bootctrl", 248 ], 249} 250``` 251 252a reference to `"libpixelstats"` will resolve to the module defined in 253`hardware/google/pixel/pixelstats/Android.bp` because this module is in 254`hardware/google/pixel` namespace. 255 256**TODO**: Conventionally, languages with similar concepts provide separate 257constructs for namespace definition and name resolution (`namespace` and `using` 258in C++, for instance). Should Soong do that, too? 259 260#### Referencing modules in makefiles 261 262While we are gradually converting makefiles to Android.bp files, Android build 263is described by a mixture of Android.bp and Android.mk files, and a module 264defined in an Android.mk file can reference a module defined in Android.bp file. 265For instance, a binary still defined in an Android.mk file may have a library 266defined in already converted Android.bp as a dependency. 267 268A module defined in an Android.bp file and belonging to the global namespace can 269be referenced from a makefile without additional effort. If a module belongs to 270an explicit namespace, it can be referenced from a makefile only after after the 271name of the namespace has been added to the value of PRODUCT_SOONG_NAMESPACES 272variable. 273 274Note that makefiles have no notion of namespaces and exposing namespaces with 275the same modules via PRODUCT_SOONG_NAMESPACES may cause Make failure. For 276instance, exposing both `device/google/bonito` and `device/google/coral` 277namespaces will cause Make failure because it will see two targets for the 278`pixelstats-vendor` module. 279 280### Visibility 281 282The `visibility` property on a module controls whether the module can be 283used by other packages. Modules are always visible to other modules declared 284in the same package. This is based on the Bazel visibility mechanism. 285 286If specified the `visibility` property must contain at least one rule. 287 288Each rule in the property must be in one of the following forms: 289* `["//visibility:public"]`: Anyone can use this module. 290* `["//visibility:private"]`: Only rules in the module's package (not its 291subpackages) can use this module. 292* `["//visibility:override"]`: Discards any rules inherited from defaults or a 293creating module. Can only be used at the beginning of a list of visibility 294rules. 295* `["//some/package:__pkg__", "//other/package:__pkg__"]`: Only modules in 296`some/package` and `other/package` (defined in `some/package/*.bp` and 297`other/package/*.bp`) have access to this module. Note that sub-packages do not 298have access to the rule; for example, `//some/package/foo:bar` or 299`//other/package/testing:bla` wouldn't have access. `__pkg__` is a special 300module and must be used verbatim. It represents all of the modules in the 301package. 302* `["//project:__subpackages__", "//other:__subpackages__"]`: Only modules in 303packages `project` or `other` or in one of their sub-packages have access to 304this module. For example, `//project:rule`, `//project/library:lib` or 305`//other/testing/internal:munge` are allowed to depend on this rule (but not 306`//independent:evil`) 307* `["//project"]`: This is shorthand for `["//project:__pkg__"]` 308* `[":__subpackages__"]`: This is shorthand for `["//project:__subpackages__"]` 309where `//project` is the module's package, e.g. using `[":__subpackages__"]` in 310`packages/apps/Settings/Android.bp` is equivalent to 311`//packages/apps/Settings:__subpackages__`. 312* `["//visibility:legacy_public"]`: The default visibility, behaves as 313`//visibility:public` for now. It is an error if it is used in a module. 314 315The visibility rules of `//visibility:public` and `//visibility:private` cannot 316be combined with any other visibility specifications, except 317`//visibility:public` is allowed to override visibility specifications imported 318through the `defaults` property. 319 320Packages outside `vendor/` cannot make themselves visible to specific packages 321in `vendor/`, e.g. a module in `libcore` cannot declare that it is visible to 322say `vendor/google`, instead it must make itself visible to all packages within 323`vendor/` using `//vendor:__subpackages__`. 324 325If a module does not specify the `visibility` property then it uses the 326`default_visibility` property of the `package` module in the module's package. 327 328If the `default_visibility` property is not set for the module's package then 329it will use the `default_visibility` of its closest ancestor package for which 330a `default_visibility` property is specified. 331 332If no `default_visibility` property can be found then the module uses the 333global default of `//visibility:legacy_public`. 334 335The `visibility` property has no effect on a defaults module although it does 336apply to any non-defaults module that uses it. To set the visibility of a 337defaults module, use the `defaults_visibility` property on the defaults module; 338not to be confused with the `default_visibility` property on the package module. 339 340Once the build has been completely switched over to soong it is possible that a 341global refactoring will be done to change this to `//visibility:private` at 342which point all packages that do not currently specify a `default_visibility` 343property will be updated to have 344`default_visibility = [//visibility:legacy_public]` added. It will then be the 345owner's responsibility to replace that with a more appropriate visibility. 346 347### Formatter 348 349Soong includes a canonical formatter for Android.bp files, similar to 350[gofmt](https://golang.org/cmd/gofmt/). To recursively reformat all Android.bp files 351in the current directory: 352``` 353bpfmt -w . 354``` 355 356The canonical format includes 4 space indents, newlines after every element of a 357multi-element list, and always includes a trailing comma in lists and maps. 358 359### Convert Android.mk files 360 361Soong includes a tool perform a first pass at converting Android.mk files 362to Android.bp files: 363 364``` 365androidmk Android.mk > Android.bp 366``` 367 368The tool converts variables, modules, comments, and some conditionals, but any 369custom Makefile rules, complex conditionals or extra includes must be converted 370by hand. 371 372#### Differences between Android.mk and Android.bp 373 374* Android.mk files often have multiple modules with the same name (for example 375for static and shared version of a library, or for host and device versions). 376Android.bp files require unique names for every module, but a single module can 377be built in multiple variants, for example by adding `host_supported: true`. 378The androidmk converter will produce multiple conflicting modules, which must 379be resolved by hand to a single module with any differences inside 380`target: { android: { }, host: { } }` blocks. 381 382### Conditionals 383 384Soong deliberately does not support most conditionals in Android.bp files. We 385suggest removing most conditionals from the build. See 386[Best Practices](docs/best_practices.md#removing-conditionals) for some 387examples on how to remove conditionals. 388 389Most conditionals supported natively by Soong are converted to a map 390property. When building the module one of the properties in the map will be 391selected, and its values appended to the property with the same name at the 392top level of the module. 393 394For example, to support architecture specific files: 395``` 396cc_library { 397 ... 398 srcs: ["generic.cpp"], 399 arch: { 400 arm: { 401 srcs: ["arm.cpp"], 402 }, 403 x86: { 404 srcs: ["x86.cpp"], 405 }, 406 }, 407} 408``` 409 410When building the module for arm the `generic.cpp` and `arm.cpp` sources will 411be built. When building for x86 the `generic.cpp` and 'x86.cpp' sources will 412be built. 413 414#### Soong Config Variables 415 416When converting vendor modules that contain conditionals, simple conditionals 417can be supported through Soong config variables using `soong_config_*` 418modules that describe the module types, variables and possible values: 419 420``` 421soong_config_module_type { 422 name: "acme_cc_defaults", 423 module_type: "cc_defaults", 424 config_namespace: "acme", 425 variables: ["board"], 426 bool_variables: ["feature"], 427 value_variables: ["width"], 428 properties: ["cflags", "srcs"], 429} 430 431soong_config_string_variable { 432 name: "board", 433 values: ["soc_a", "soc_b"], 434} 435``` 436 437This example describes a new `acme_cc_defaults` module type that extends the 438`cc_defaults` module type, with three additional conditionals based on 439variables `board`, `feature` and `width`, which can affect properties `cflags` 440and `srcs`. 441 442The values of the variables can be set from a product's `BoardConfig.mk` file: 443``` 444SOONG_CONFIG_NAMESPACES += acme 445SOONG_CONFIG_acme += \ 446 board \ 447 feature \ 448 449SOONG_CONFIG_acme_board := soc_a 450SOONG_CONFIG_acme_feature := true 451SOONG_CONFIG_acme_width := 200 452``` 453 454The `acme_cc_defaults` module type can be used anywhere after the definition in 455the file where it is defined, or can be imported into another file with: 456``` 457soong_config_module_type_import { 458 from: "device/acme/Android.bp", 459 module_types: ["acme_cc_defaults"], 460} 461``` 462 463It can used like any other module type: 464``` 465acme_cc_defaults { 466 name: "acme_defaults", 467 cflags: ["-DGENERIC"], 468 soong_config_variables: { 469 board: { 470 soc_a: { 471 cflags: ["-DSOC_A"], 472 }, 473 soc_b: { 474 cflags: ["-DSOC_B"], 475 }, 476 }, 477 feature: { 478 cflags: ["-DFEATURE"], 479 }, 480 width: { 481 cflags: ["-DWIDTH=%s"], 482 }, 483 }, 484} 485 486cc_library { 487 name: "libacme_foo", 488 defaults: ["acme_defaults"], 489 srcs: ["*.cpp"], 490} 491``` 492 493With the `BoardConfig.mk` snippet above, libacme_foo would build with 494cflags "-DGENERIC -DSOC_A -DFEATURE -DWIDTH=200". 495 496`soong_config_module_type` modules will work best when used to wrap defaults 497modules (`cc_defaults`, `java_defaults`, etc.), which can then be referenced 498by all of the vendor's other modules using the normal namespace and visibility 499rules. 500 501## Build logic 502 503The build logic is written in Go using the 504[blueprint](http://godoc.org/github.com/google/blueprint) framework. Build 505logic receives module definitions parsed into Go structures using reflection 506and produces build rules. The build rules are collected by blueprint and 507written to a [ninja](http://ninja-build.org) build file. 508 509## Other documentation 510 511* [Best Practices](docs/best_practices.md) 512* [Build Performance](docs/perf.md) 513* [Generating CLion Projects](docs/clion.md) 514* [Generating YouCompleteMe/VSCode compile\_commands.json file](docs/compdb.md) 515* Make-specific documentation: [build/make/README.md](https://android.googlesource.com/platform/build/+/master/README.md) 516 517## Developing for Soong 518 519To load Soong code in a Go-aware IDE, create a directory outside your android tree and then: 520```bash 521apt install bindfs 522export GOPATH=<path to the directory you created> 523build/soong/scripts/setup_go_workspace_for_soong.sh 524``` 525 526This will bind mount the Soong source directories into the directory in the layout expected by 527the IDE. 528 529### Running Soong in a debugger 530 531To run the soong_build process in a debugger, install `dlv` and then start the build with 532`SOONG_DELVE=<listen addr>` in the environment. 533For example: 534```bash 535SOONG_DELVE=:1234 m nothing 536``` 537and then in another terminal: 538``` 539dlv connect :1234 540``` 541 542If you see an error: 543``` 544Could not attach to pid 593: this could be caused by a kernel 545security setting, try writing "0" to /proc/sys/kernel/yama/ptrace_scope 546``` 547you can temporarily disable 548[Yama's ptrace protection](https://www.kernel.org/doc/Documentation/security/Yama.txt) 549using: 550```bash 551sudo sysctl -w kernel.yama.ptrace_scope=0 552``` 553 554## Contact 555 556Email android-building@googlegroups.com (external) for any questions, or see 557[go/soong](http://go/soong) (internal). 558