1*7594170eSAndroid Build Coastguard Worker# Android Build System Concepts 2*7594170eSAndroid Build Coastguard Worker 3*7594170eSAndroid Build Coastguard WorkerThis document provides high level explanations and mapping of the internal 4*7594170eSAndroid Build Coastguard Workerbuild system components and concepts of the Android build system and Bazel, 5*7594170eSAndroid Build Coastguard Workerand how components communicate with each other. 6*7594170eSAndroid Build Coastguard Worker 7*7594170eSAndroid Build Coastguard WorkerFor implementation concepts, see: 8*7594170eSAndroid Build Coastguard Workerhttps://android.googlesource.com/platform/build/bazel/+/refs/heads/master/docs/internal_concepts.md. 9*7594170eSAndroid Build Coastguard Worker 10*7594170eSAndroid Build Coastguard Worker## High level components 11*7594170eSAndroid Build Coastguard Worker 12*7594170eSAndroid Build Coastguard WorkerThis table provides a high level overview of the components in the current 13*7594170eSAndroid Build Coastguard WorkerAndroid Platform build system, and how each component maps to a concept in 14*7594170eSAndroid Build Coastguard WorkerBazel. 15*7594170eSAndroid Build Coastguard Worker 16*7594170eSAndroid Build Coastguard Worker|Android build system component|Description|Mapping to Bazel concepts| 17*7594170eSAndroid Build Coastguard Worker|---|---|---| 18*7594170eSAndroid Build Coastguard Worker|Kati|Make-compatible front-end. Encodes build logic in `.mk` scripts. Declares buildable units in `Android.mk`. Generates Ninja file directly.|Loading and analysis phase. Conceptually similar to `bazel build --nobuild`.| 19*7594170eSAndroid Build Coastguard Worker|Blueprint|Build definition syntax. Build syntax parser. Internal data structures like Modules/Variations/Context/Scope. Ninja file generator.|Starlark.| 20*7594170eSAndroid Build Coastguard Worker|Soong|Bazel-like front-end. Encodes build logic in Go. Declares build units in `Android.bp`, parsed by Blueprint. Uses Blueprint to generate Ninja file. Generates a `.mk` file with prebuilt module stubs to Kati.|Loading and analysis phase. Conceptually similar to `bazel build --nobuild command`.| 21*7594170eSAndroid Build Coastguard Worker|Ninja|Serialized command line action graph executor. Executes Ninja graph generated from Kati and Soong.|Bazel's execution phase.| 22*7594170eSAndroid Build Coastguard Worker|atest|Test executor and orchestrator.|Conceptually similar to `bazel test` command.| 23*7594170eSAndroid Build Coastguard Worker|Blueprint + Kati + Soong + Ninja + atest|The entire build pipeline for Android.|Conceptually similar to `bazel build` or `bazel test` commands.| 24*7594170eSAndroid Build Coastguard Worker|`<script>.sh`|Running arbitrary scripts in AOSP.|Conceptually similar to `bazel run` command.| 25*7594170eSAndroid Build Coastguard Worker|Make (replaced in-place by Kati)|No longer in use. Entire build system, replaced by the tools above.|Loading, analysis, execution phases. Conceptually similar to `bazel build` command.| 26*7594170eSAndroid Build Coastguard Worker|`Android.bp`|Build definition file for Soong.|`BUILD.bazel` or `BUILD`.| 27*7594170eSAndroid Build Coastguard Worker|`Android.mk`|Build definition file for Kati.|`BUILD.bazel` or `BUILD`.| 28*7594170eSAndroid Build Coastguard Worker 29*7594170eSAndroid Build Coastguard Worker## Communication between components 30*7594170eSAndroid Build Coastguard Worker 31*7594170eSAndroid Build Coastguard Worker* Kati product configuration component to generate config variables (config.mk, AndroidProducts.mk) 32*7594170eSAndroid Build Coastguard Worker * **“Kati-config” for the purpose of this document** 33*7594170eSAndroid Build Coastguard Worker* Kati component to generate build actions in Ninja files (main.mk, Android.mk files) 34*7594170eSAndroid Build Coastguard Worker * **“Kati-build” for the purpose of this document** 35*7594170eSAndroid Build Coastguard Worker* Kati component to generate packaging actions in Ninja files (packaging.mk file) 36*7594170eSAndroid Build Coastguard Worker * **“Kati-package” for the purpose of this document** 37*7594170eSAndroid Build Coastguard Worker* Kati component to generate cleaning actions in Ninja files (cleanbuild.mk, CleanSpec.mk files) 38*7594170eSAndroid Build Coastguard Worker * **"Kati-cleanspec" for the purpose of this document** 39*7594170eSAndroid Build Coastguard Worker* **soong\_build** (and **Blueprint**) component to generate build actions (Android.bp, Blueprints files) 40*7594170eSAndroid Build Coastguard Worker* **Ninja** component to execute actions from Kati-build, Kati-package and soong\_build 41*7594170eSAndroid Build Coastguard Worker* **Bazel** as the next generation of the entire build system, starting as a Ninja executor drop-in replacement 42*7594170eSAndroid Build Coastguard Worker* **soong\_ui** as the tool to orchestrate all of the above, and with auxiliary tools like finder, path\_interposer, soong\_env, minibp and bpglob. 43*7594170eSAndroid Build Coastguard Worker 44*7594170eSAndroid Build Coastguard WorkerThe current build system architecture primarily uses **files** as the medium 45*7594170eSAndroid Build Coastguard Workerfor inter-process communication (IPC), with one known case of unix socket 46*7594170eSAndroid Build Coastguard Workercommunication (e.g. 47*7594170eSAndroid Build Coastguard Worker[path\_interposer](https://cs.android.com/android/platform/superproject/+/master:build/soong/ui/build/paths/logs.go;l=112-133;drc=184901135cda8bdcc51cab4f16c401a28a510593)), 48*7594170eSAndroid Build Coastguard Workerand a fifo between Ninja and soong_ui for the Protobuf stream for build 49*7594170eSAndroid Build Coastguard Workerstatus reporting. 50*7594170eSAndroid Build Coastguard Worker 51*7594170eSAndroid Build Coastguard Worker## Component order 52*7594170eSAndroid Build Coastguard Worker 53*7594170eSAndroid Build Coastguard WorkerThe build system components run in the following order, orchestrated by soong\_ui: 54*7594170eSAndroid Build Coastguard Worker 55*7594170eSAndroid Build Coastguard Worker1. soong\_ui bootstraps itself with microfactory (`go build` replacement) and launches. 56*7594170eSAndroid Build Coastguard Worker1. soong\_ui runs auxiliary tools to aggregate files into filelists, for 57*7594170eSAndroid Build Coastguard WorkerAndroid.mk, Android.bp, AndroidProducts.mk and several others. 58*7594170eSAndroid Build Coastguard Worker1. soong\_ui runs Kati-config with 59*7594170eSAndroid Build Coastguard Worker [the config.mk entry point](https://cs.android.com/android/platform/superproject/+/master:build/soong/ui/build/dumpvars.go;l=89;drc=9f43597ff7349c4facd9e338e5b4b277e625e518). 60*7594170eSAndroid Build Coastguard Worker1. soong\_ui orchestrates 3 Blueprint/Soong phases to generate the main out/soong/build.ninja file: 61*7594170eSAndroid Build Coastguard Worker minibootstrap, bootstrap, and primary. 62*7594170eSAndroid Build Coastguard Worker 1. Minibootstrap phase uses Blueprint/Microfactory to build itself 63*7594170eSAndroid Build Coastguard Worker (minibp) so that Android.bp and Blueprint files can be used to define 64*7594170eSAndroid Build Coastguard Worker Soong. 65*7594170eSAndroid Build Coastguard Worker 1. Bootstrap phase runs Ninja on a build.ninja file that runs minibp to 66*7594170eSAndroid Build Coastguard Worker read all Android.bp files across the source tree that describes Soong and 67*7594170eSAndroid Build Coastguard Worker plugins, and builds soong\_build. 68*7594170eSAndroid Build Coastguard Worker 1. Primary phase runs Ninja on a build.ninja file that runs soong_build 69*7594170eSAndroid Build Coastguard Worker to generate the final out/soong/build.ninja file. 70*7594170eSAndroid Build Coastguard Worker 1. soong\_build also runs its own tests alongside generating 71*7594170eSAndroid Build Coastguard Worker out/soong/build.ninja, which can be skipped with the `--skip-soong-tests` 72*7594170eSAndroid Build Coastguard Worker argument. 73*7594170eSAndroid Build Coastguard Worker1. soong\_ui runs Kati-cleanspec with 74*7594170eSAndroid Build Coastguard Worker [the cleanbuild.mk entry point](https://cs.android.com/android/platform/superproject/+/master:build/soong/ui/build/kati.go;l=362;drc=b1d30d63c5d1b818ea38e77cd155da2016fe8b6c). 75*7594170eSAndroid Build Coastguard Worker1. soong\_ui runs Kati-build to generate a Ninja file, with 76*7594170eSAndroid Build Coastguard Worker [the main.mk entry point.](https://cs.android.com/android/platform/superproject/+/master:build/soong/ui/build/kati.go;l=202;drc=b1d30d63c5d1b818ea38e77cd155da2016fe8b6c) 77*7594170eSAndroid Build Coastguard Worker1. soong\_ui runs Kati-package to generate a Ninja file, with 78*7594170eSAndroid Build Coastguard Worker [the packaging/main.mk](https://cs.android.com/android/platform/superproject/+/master:build/soong/ui/build/kati.go;l=314;drc=b1d30d63c5d1b818ea38e77cd155da2016fe8b6c) 79*7594170eSAndroid Build Coastguard Worker entry point. 80*7594170eSAndroid Build Coastguard Worker1. soong\_ui generates a Ninja file to combine above Ninja files. 81*7594170eSAndroid Build Coastguard Worker1. soong\_ui runs either Ninja or Bazel to execute the build, with the 82*7594170eSAndroid Build Coastguard Workercombined Ninja file as entry point. 83*7594170eSAndroid Build Coastguard Worker 84*7594170eSAndroid Build Coastguard Workersoong\_ui has a --skip-make flag that will skip Kati-config, Kati cleanspec, 85*7594170eSAndroid Build Coastguard WorkerKati-build and Kati-package, used for Soong-only builds in NDK and some 86*7594170eSAndroid Build Coastguard WorkerMainline projects. 87*7594170eSAndroid Build Coastguard Worker 88*7594170eSAndroid Build Coastguard Worker### soong\_ui 89*7594170eSAndroid Build Coastguard Worker 90*7594170eSAndroid Build Coastguard Workersoong\_ui is primarily responsible for orchestrating the build, cleaning the 91*7594170eSAndroid Build Coastguard Workerbuild environment, and running auxiliary tools. These tools (minibp, 92*7594170eSAndroid Build Coastguard Workermicrofactory) can bootstrap other tools (soong\_build), aggregate file lists 93*7594170eSAndroid Build Coastguard Worker(finder.go), improve hermeticity (path\_interposer, nsjail) or perform checks 94*7594170eSAndroid Build Coastguard Workeragainst the environment (soong\_env). 95*7594170eSAndroid Build Coastguard Worker 96*7594170eSAndroid Build Coastguard Workersoong\_ui uses finder.go to generate <filename>.list files for other 97*7594170eSAndroid Build Coastguard Workertools. For example, it generates Android.mk.list for Kati-build, 98*7594170eSAndroid Build Coastguard WorkerAndroidProducts.mk.list for Kati-config, and Android.bp.list for 99*7594170eSAndroid Build Coastguard Workersoong\_build. 100*7594170eSAndroid Build Coastguard Worker 101*7594170eSAndroid Build Coastguard Workersoong\_ui uses path\_interposer to prepare an hermetic $PATH with runtime 102*7594170eSAndroid Build Coastguard Workerchecks against allowlisted system tools. The $PATH contains these system 103*7594170eSAndroid Build Coastguard Workertools with checked-in prebuilts, and uses path\_interposer to intercept calls 104*7594170eSAndroid Build Coastguard Workerand error out whenever non-allowlisted tools are used (see out/.path for 105*7594170eSAndroid Build Coastguard Workerdirectory of intercepted tool symlinks). 106*7594170eSAndroid Build Coastguard Worker 107*7594170eSAndroid Build Coastguard Workersoong\_ui generates a Kati suffix to ensure that Kati-generated files are 108*7594170eSAndroid Build Coastguard Workerregenerated if inputs to Kati have changed between builds. 109*7594170eSAndroid Build Coastguard Worker 110*7594170eSAndroid Build Coastguard Workersoong\_ui calls Soong and Kati to generate Ninja files, and eventually 111*7594170eSAndroid Build Coastguard Workercreates another Ninja file (out/combined-<product>.ninja) to combine the 112*7594170eSAndroid Build Coastguard Workerothers, and executes either Ninja or Bazel to complete the build. 113*7594170eSAndroid Build Coastguard Worker 114*7594170eSAndroid Build Coastguard Workersoong\_ui sets up the sandbox and environment for the Ninja/Bazel process. 115*7594170eSAndroid Build Coastguard Worker 116*7594170eSAndroid Build Coastguard Worker## Kati-config 117*7594170eSAndroid Build Coastguard Worker 118*7594170eSAndroid Build Coastguard WorkerAs a product configuration tool, soong\_ui runs Kati-config in 119*7594170eSAndroid Build Coastguard Worker**[--dumpvars-mode](https://cs.android.com/android/platform/superproject/+/master:build/soong/cmd/soong_ui/main.go;l=298-305;drc=master)** 120*7594170eSAndroid Build Coastguard Workerto dump the values of specified Make variables at the end of an evaluation, 121*7594170eSAndroid Build Coastguard Workerwith build/make/core/config.mk as the entry point. During this phase, 122*7594170eSAndroid Build Coastguard WorkerKati-config eventually evaluates[ 123*7594170eSAndroid Build Coastguard Workersoong\_config.mk](https://cs.android.com/android/platform/superproject/+/master:build/make/core/soong_config.mk;l=2?q=soong.variables) 124*7594170eSAndroid Build Coastguard Workerto generate the **[soong.variables JSON 125*7594170eSAndroid Build Coastguard Workerfile](https://cs.android.com/android/platform/superproject/+/master:build/make/core/soong_config.mk;l=16-222;drc=341928ecc5da205401bcfd86f098662b0cee7857)**. 126*7594170eSAndroid Build Coastguard WorkerThis way, Kati-config can communicate product configuration to soong\_build, 127*7594170eSAndroid Build Coastguard Workeras soong\_build parses the dumped variables from the JSON on startup, and 128*7594170eSAndroid Build Coastguard Workerstores them into an in-memory Config object. 129*7594170eSAndroid Build Coastguard Worker 130*7594170eSAndroid Build Coastguard WorkerTo communicate 131*7594170eSAndroid Build Coastguard Worker[dexpreopt](https://cs.android.com/android/platform/superproject/+/master:build/soong/java/dexpreopt.go;l=115;drc=8cbc5d269b20b5743679bfb8684ed174dcf58a30) 132*7594170eSAndroid Build Coastguard Workervariables to soong\_build, [dexpreopt.config is also 133*7594170eSAndroid Build Coastguard Workergenerated](https://cs.android.com/android/platform/superproject/+/master:build/make/core/dex_preopt_config.mk;l=71-142;drc=f26015449f0747b9fdeceb5ce70e30ecd76e20e8) 134*7594170eSAndroid Build Coastguard Workeras a $(shell) action and [read by 135*7594170eSAndroid Build Coastguard Workersoong\_build](https://cs.android.com/android/platform/superproject/+/master:build/soong/dexpreopt/config.go;l=175-196;drc=1af783fae74715bcf1a94733bd75b2e6cc688e8c) 136*7594170eSAndroid Build Coastguard Workerin a similar way as Kati-config evaluates dex\_preopt\_config.mk included in 137*7594170eSAndroid Build Coastguard Workersoong\_config.mk. 138*7594170eSAndroid Build Coastguard Worker 139*7594170eSAndroid Build Coastguard Workersoong\_ui sets up a **KatiReader** to monitor Kati-config’s stdout/err for UI 140*7594170eSAndroid Build Coastguard Workerreporting and error handling purposes. 141*7594170eSAndroid Build Coastguard Worker 142*7594170eSAndroid Build Coastguard Worker## soong\_build 143*7594170eSAndroid Build Coastguard Worker 144*7594170eSAndroid Build Coastguard Workersoong\_build’s primary role is to evaluate all Android.bp files, run a series 145*7594170eSAndroid Build Coastguard Workerof mutators, and generate out/soong/build.ninja file. 146*7594170eSAndroid Build Coastguard Worker 147*7594170eSAndroid Build Coastguard Workersoong\_build communicates with Kati-build by generating Make Vars and running 148*7594170eSAndroid Build Coastguard Workerthe AndroidMk singleton to generate .mk files in the output directory 149*7594170eSAndroid Build Coastguard Worker(out/soong/{Android, late, make\_vars}-<product>.mk). 150*7594170eSAndroid Build Coastguard Worker 151*7594170eSAndroid Build Coastguard Worker* Android-<product>.mk contains Soong modules as Make modules so Make 152*7594170eSAndroid Build Coastguard Worker modules can depend on Soong modules. 153*7594170eSAndroid Build Coastguard Worker* make\_vars-<product>.mk contains Make variables for Kati-build, exported 154*7594170eSAndroid Build Coastguard Worker from Soong modules. There are also checks built into this .mk file to ensure that 155*7594170eSAndroid Build Coastguard Worker if a duplicate Make variable of the same name comes from another source, the Soong 156*7594170eSAndroid Build Coastguard Worker and Make variable values are identical. 157*7594170eSAndroid Build Coastguard Worker* late-<product>.mk contains Make variables that are not read while Kati-build 158*7594170eSAndroid Build Coastguard Worker parses the Android.mk file. (Late variables) 159*7594170eSAndroid Build Coastguard Worker * soong\_ui invokes Kati to parse make\_vars .mk file earlier than the Android.mk 160*7594170eSAndroid Build Coastguard Worker files,and late.mk after parsing the Android.mk files. 161*7594170eSAndroid Build Coastguard Worker * late.mk is used to define phony rules to take advantage of Make’s ability to 162*7594170eSAndroid Build Coastguard Worker add extra dependencies to an existing rule. late.mk is not strictly necessary to 163*7594170eSAndroid Build Coastguard Worker make this happen at this moment, since late.mk rules don’t currently depend on any 164*7594170eSAndroid Build Coastguard Worker variables defined during Android.mk processing (e.g. ALL\_MODULES$(module).INSTALLED). 165*7594170eSAndroid Build Coastguard Worker 166*7594170eSAndroid Build Coastguard Worker## Kati-build / Kati-package 167*7594170eSAndroid Build Coastguard Worker 168*7594170eSAndroid Build Coastguard WorkerKati-build’s primary role is to evaluate all Android.mk files with 169*7594170eSAndroid Build Coastguard Workerbuild/make/core/main.mk as entry point, and generate 170*7594170eSAndroid Build Coastguard Workerout/build-<product>.ninja. It also generates cleanspec.ninja for the 171*7594170eSAndroid Build Coastguard Workerproduct, containing statements on how to remove stale output files. 172*7594170eSAndroid Build Coastguard Worker 173*7594170eSAndroid Build Coastguard WorkerKati-build’s primary role is to evaluate all packaging .mk files with 174*7594170eSAndroid Build Coastguard Workerbuild/make/packaging/main.mk as entry point, including 175*7594170eSAndroid Build Coastguard Workerbuild/make/packaging/distdir.mk for dist-for-goals calls, and generate 176*7594170eSAndroid Build Coastguard Workerout/package-<product>.ninja. 177*7594170eSAndroid Build Coastguard Worker 178*7594170eSAndroid Build Coastguard WorkerKati-build/Kati-package’s stdout/stderr is monitored by soong\_ui’s 179*7594170eSAndroid Build Coastguard WorkerKatiReader to UI and error handling. 180*7594170eSAndroid Build Coastguard Worker 181*7594170eSAndroid Build Coastguard WorkerAs Kati-build/Kati-package generates Ninja files, they also generate 182*7594170eSAndroid Build Coastguard Workerout/ninja-<product>.sh and out/env-<product>.sh. These scripts are 183*7594170eSAndroid Build Coastguard Workerwrappers for soong\_ui to execute Ninja with the correct Ninja files, in a 184*7594170eSAndroid Build Coastguard Workercontrolled environment. 185*7594170eSAndroid Build Coastguard Worker 186*7594170eSAndroid Build Coastguard Worker## Ninja 187*7594170eSAndroid Build Coastguard Worker 188*7594170eSAndroid Build Coastguard WorkerAs Ninja executes files from Kati-build, Kati-package, soong\_build and other 189*7594170eSAndroid Build Coastguard Workerbootstrapping tools like Blueprint, it writes to a fifo in a proto front end 190*7594170eSAndroid Build Coastguard Workerthat soong\_ui monitors with NinjaReader. NinjaReader ensures that the user 191*7594170eSAndroid Build Coastguard Workerinterface for Ninja progress is consistent with the rest of the build. 192*7594170eSAndroid Build Coastguard Worker 193*7594170eSAndroid Build Coastguard Worker## Bazel 194*7594170eSAndroid Build Coastguard Worker 195*7594170eSAndroid Build Coastguard WorkerAs more Soong modules are converted to BUILD files, soong\_build serializes 196*7594170eSAndroid Build Coastguard Workerinformation about converted modules to BUILD/bzl files on disk. soong\_build 197*7594170eSAndroid Build Coastguard Workerthen consumes information about these targets from Bazel by directly calling 198*7594170eSAndroid Build Coastguard Workerthe Bazel client to issue `cquery` calls about these targets. 199