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