blob: b0b61a8e94d3d228970b28bddb8bb831d3307518 [file] [log] [blame] [view]
Colin Crosse10ce182016-07-18 10:58:47 -07001# 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
Clay Murphy021e3762019-01-08 10:55:51 -08007See [Simple Build
8Configuration](https://source.android.com/compatibility/tests/development/blueprints)
9on source.android.com to read how Soong is configured for testing.
10
Colin Crosse10ce182016-07-18 10:58:47 -070011## 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
Colin Cross068e0fe2016-12-13 15:23:47 -080015Go. 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.
Colin Crosse10ce182016-07-18 10:58:47 -070018
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
Dan Willemsenb2fbbbb2019-03-01 13:13:20 -080037[$OUT_DIR/soong/docs/soong_build.html](https://ci.android.com/builds/latest/branches/aosp-build-tools/targets/linux/view/soong_build.html).
Colin Crosse10ce182016-07-18 10:58:47 -070038
Colin Cross645332d2018-04-25 15:06:01 -070039### Globs
40
41Properties that take a list of files can also take glob patterns. Glob
42patterns can contain the normal Unix wildcard `*`, for example "*.java". Glob
43patterns can also contain a single `**` wildcard as a path element, which will
44match zero or more path elements. For example, `java/**/*.java` will match
45`java/Main.java` and `java/com/android/Main.java`.
46
Colin Crosse10ce182016-07-18 10:58:47 -070047### Variables
48
49An Android.bp file may contain top-level variable assignments:
50```
51gzip_srcs = ["src/test/minigzip.c"],
52
53cc_binary {
54 name: "gzip",
55 srcs: gzip_srcs,
56 shared_libs: ["libz"],
57 stl: "none",
58}
59```
60
61Variables are scoped to the remainder of the file they are declared in, as well
62as any child blueprint files. Variables are immutable with one exception - they
63can be appended to with a += assignment, but only before they have been
64referenced.
65
66### Comments
67Android.bp files can contain C-style multiline `/* */` and C++ style single-line
68`//` comments.
69
70### Types
71
72Variables and properties are strongly typed, variables dynamically based on the
73first assignment, and properties statically by the module type. The supported
74types are:
75* Bool (`true` or `false`)
Nan Zhang61eaedb2017-11-02 13:28:15 -070076* Integers (`int`)
Colin Crosse10ce182016-07-18 10:58:47 -070077* Strings (`"string"`)
78* Lists of strings (`["string1", "string2"]`)
79* Maps (`{key1: "value1", key2: ["value2"]}`)
80
81Maps may values of any type, including nested maps. Lists and maps may have
82trailing commas after the last value.
83
84### Operators
85
86Strings, lists of strings, and maps can be appended using the `+` operator.
Nan Zhang61eaedb2017-11-02 13:28:15 -070087Integers can be summed up using the `+` operator. Appending a map produces the
88union of keys in both maps, appending the values of any keys that are present
89in both maps.
Colin Crosse10ce182016-07-18 10:58:47 -070090
91### Defaults modules
92
93A defaults module can be used to repeat the same properties in multiple modules.
94For example:
95
96```
97cc_defaults {
98 name: "gzip_defaults",
99 shared_libs: ["libz"],
100 stl: "none",
101}
102
103cc_binary {
104 name: "gzip",
105 defaults: ["gzip_defaults"],
106 srcs: ["src/test/minigzip.c"],
107}
108```
109
Paul Duffin2e61fa62019-03-28 14:10:57 +0000110### Packages
111
112The build is organized into packages where each package is a collection of related files and a
113specification of the dependencies among them in the form of modules.
114
115A package is defined as a directory containing a file named `Android.bp`, residing beneath the
116top-level directory in the build and its name is its path relative to the top-level directory. A
117package includes all files in its directory, plus all subdirectories beneath it, except those which
118themselves contain an `Android.bp` file.
119
120The modules in a package's `Android.bp` and included files are part of the module.
121
122For example, in the following directory tree (where `.../android/` is the top-level Android
123directory) there are two packages, `my/app`, and the subpackage `my/app/tests`. Note that
124`my/app/data` is not a package, but a directory belonging to package `my/app`.
125
126 .../android/my/app/Android.bp
127 .../android/my/app/app.cc
128 .../android/my/app/data/input.txt
129 .../android/my/app/tests/Android.bp
130 .../android/my/app/tests/test.cc
131
132This is based on the Bazel package concept.
133
Jeff Gaston44c0cd82017-11-29 19:51:52 -0800134### Name resolution
135
136Soong provides the ability for modules in different directories to specify
137the same name, as long as each module is declared within a separate namespace.
138A namespace can be declared like this:
139
140```
141soong_namespace {
142 imports: ["path/to/otherNamespace1", "path/to/otherNamespace2"],
143}
144```
145
146Each Soong module is assigned a namespace based on its location in the tree.
147Each Soong module is considered to be in the namespace defined by the
148soong_namespace found in an Android.bp in the current directory or closest
149ancestor directory, unless no such soong_namespace module is found, in which
150case the module is considered to be in the implicit root namespace.
151
152When Soong attempts to resolve dependency D declared my module M in namespace
153N which imports namespaces I1, I2, I3..., then if D is a fully-qualified name
154of the form "//namespace:module", only the specified namespace will be searched
155for the specified module name. Otherwise, Soong will first look for a module
156named D declared in namespace N. If that module does not exist, Soong will look
157for a module named D in namespaces I1, I2, I3... Lastly, Soong will look in the
158root namespace.
159
160Until we have fully converted from Make to Soong, it will be necessary for the
161Make product config to specify a value of PRODUCT_SOONG_NAMESPACES. Its value
162should be a space-separated list of namespaces that Soong export to Make to be
163built by the `m` command. After we have fully converted from Make to Soong, the
164details of enabling namespaces could potentially change.
165
Paul Duffin2e61fa62019-03-28 14:10:57 +0000166### Visibility
167
168The `visibility` property on a module controls whether the module can be
169used by other packages. Modules are always visible to other modules declared
170in the same package. This is based on the Bazel visibility mechanism.
171
172If specified the `visibility` property must contain at least one rule.
173
174Each rule in the property must be in one of the following forms:
175* `["//visibility:public"]`: Anyone can use this module.
176* `["//visibility:private"]`: Only rules in the module's package (not its
177subpackages) can use this module.
178* `["//some/package:__pkg__", "//other/package:__pkg__"]`: Only modules in
179`some/package` and `other/package` (defined in `some/package/*.bp` and
180`other/package/*.bp`) have access to this module. Note that sub-packages do not
181have access to the rule; for example, `//some/package/foo:bar` or
182`//other/package/testing:bla` wouldn't have access. `__pkg__` is a special
183module and must be used verbatim. It represents all of the modules in the
184package.
185* `["//project:__subpackages__", "//other:__subpackages__"]`: Only modules in
186packages `project` or `other` or in one of their sub-packages have access to
187this module. For example, `//project:rule`, `//project/library:lib` or
188`//other/testing/internal:munge` are allowed to depend on this rule (but not
189`//independent:evil`)
190* `["//project"]`: This is shorthand for `["//project:__pkg__"]`
191* `[":__subpackages__"]`: This is shorthand for `["//project:__subpackages__"]`
192where `//project` is the module's package. e.g. using `[":__subpackages__"]` in
193`packages/apps/Settings/Android.bp` is equivalent to
194`//packages/apps/Settings:__subpackages__`.
195* `["//visibility:legacy_public"]`: The default visibility, behaves as
196`//visibility:public` for now. It is an error if it is used in a module.
197
198The visibility rules of `//visibility:public` and `//visibility:private` can
199not be combined with any other visibility specifications.
200
201Packages outside `vendor/` cannot make themselves visible to specific packages
202in `vendor/`, e.g. a module in `libcore` cannot declare that it is visible to
203say `vendor/google`, instead it must make itself visible to all packages within
204`vendor/` using `//vendor:__subpackages__`.
205
206If a module does not specify the `visibility` property the module is
207`//visibility:legacy_public`. Once the build has been completely switched over to
208soong it is possible that a global refactoring will be done to change this to
209`//visibility:private` at which point all modules that do not currently specify
210a `visibility` property will be updated to have
211`visibility = [//visibility:legacy_public]` added. It will then be the owner's
212responsibility to replace that with a more appropriate visibility.
213
Colin Crosse10ce182016-07-18 10:58:47 -0700214### Formatter
215
216Soong includes a canonical formatter for blueprint files, similar to
217[gofmt](https://golang.org/cmd/gofmt/). To recursively reformat all Android.bp files
218in the current directory:
219```
220bpfmt -w .
221```
222
223The canonical format includes 4 space indents, newlines after every element of a
224multi-element list, and always includes a trailing comma in lists and maps.
225
226### Convert Android.mk files
227
228Soong includes a tool perform a first pass at converting Android.mk files
229to Android.bp files:
230
231```
232androidmk Android.mk > Android.bp
233```
234
235The tool converts variables, modules, comments, and some conditionals, but any
Colin Cross024c32e2016-09-26 15:42:42 -0700236custom Makefile rules, complex conditionals or extra includes must be converted
237by hand.
238
239#### Differences between Android.mk and Android.bp
240
241* Android.mk files often have multiple modules with the same name (for example
242for static and shared version of a library, or for host and device versions).
243Android.bp files require unique names for every module, but a single module can
244be built in multiple variants, for example by adding `host_supported: true`.
245The androidmk converter will produce multiple conflicting modules, which must
246be resolved by hand to a single module with any differences inside
247`target: { android: { }, host: { } }` blocks.
Colin Crosse10ce182016-07-18 10:58:47 -0700248
249## Build logic
250
251The build logic is written in Go using the
252[blueprint](http://godoc.org/github.com/google/blueprint) framework. Build
253logic receives module definitions parsed into Go structures using reflection
254and produces build rules. The build rules are collected by blueprint and
255written to a [ninja](http://ninja-build.org) build file.
256
Dan Willemsenbc203622018-01-22 20:56:10 -0800257## Other documentation
258
259* [Best Practices](docs/best_practices.md)
260* [Build Performance](docs/perf.md)
261* [Generating CLion Projects](docs/clion.md)
Alex Lightec868fc2018-04-17 16:50:48 -0700262* [Generating YouCompleteMe/VSCode compile\_commands.json file](docs/compdb.md)
Dan Willemsenbc203622018-01-22 20:56:10 -0800263* Make-specific documentation: [build/make/README.md](https://android.googlesource.com/platform/build/+/master/README.md)
264
Colin Crosse10ce182016-07-18 10:58:47 -0700265## FAQ
266
267### How do I write conditionals?
268
269Soong deliberately does not support conditionals in Android.bp files.
270Instead, complexity in build rules that would require conditionals are handled
271in Go, where high level language features can be used and implicit dependencies
272introduced by conditionals can be tracked. Most conditionals are converted
273to a map property, where one of the values in the map will be selected and
274appended to the top level properties.
275
276For example, to support architecture specific files:
277```
278cc_library {
279 ...
280 srcs: ["generic.cpp"],
281 arch: {
282 arm: {
283 srcs: ["arm.cpp"],
284 },
285 x86: {
286 srcs: ["x86.cpp"],
287 },
288 },
289}
290```
291
Colin Cross1a01e832017-01-13 18:00:19 -0800292See [art/build/art.go](https://android.googlesource.com/platform/art/+/master/build/art.go)
293or [external/llvm/soong/llvm.go](https://android.googlesource.com/platform/external/llvm/+/master/soong/llvm.go)
294for examples of more complex conditionals on product variables or environment variables.
295
Colin Crossaa070b42018-07-09 09:44:41 -0700296## Developing for Soong
297
298To load Soong code in a Go-aware IDE, create a directory outside your android tree and then:
299```bash
300apt install bindfs
301export GOPATH=<path to the directory you created>
302build/soong/scripts/setup_go_workspace_for_soong.sh
303```
304
305This will bind mount the Soong source directories into the directory in the layout expected by
306the IDE.
307
Colin Crosse10ce182016-07-18 10:58:47 -0700308## Contact
309
310Email android-building@googlegroups.com (external) for any questions, or see
311[go/soong](http://go/soong) (internal).