1 # Soong
2
3 Soong is the replacement for the old Android make-based build system. It
4 replaces Android.mk files with Android.bp files, which are JSON-like simple
5 declarative descriptions of modules to build.
6
7 ## Android.bp file format
8
9 By design, Android.bp files are very simple. There are no conditionals or
10 control flow statements - any complexity is handled in build logic written in
11 Go. The syntax and semantics of Android.bp files are intentionally similar
12 to [Bazel BUILD files](https://www.bazel.io/versions/master/docs/be/overview.html)
13 when possible.
14
15 ### Modules
16
17 A module in an Android.bp file starts with a module type, followed by a set of
18 properties in `name: value,` format:
19
20 ```
21 cc_binary {
22 name: "gzip",
23 srcs: ["src/test/minigzip.c"],
24 shared_libs: ["libz"],
25 stl: "none",
26 }
27 ```
28
29 Every module must have a `name` property, and the value must be unique across
30 all Android.bp files.
31
32 For a list of valid module types and their properties see
33 [$OUT_DIR/soong/.bootstrap/docs/soong_build.html](https://go/Android.bp).
34
35 ### Variables
36
37 An Android.bp file may contain top-level variable assignments:
38 ```
39 gzip_srcs = ["src/test/minigzip.c"],
40
41 cc_binary {
42 name: "gzip",
43 srcs: gzip_srcs,
44 shared_libs: ["libz"],
45 stl: "none",
46 }
47 ```
48
49 Variables are scoped to the remainder of the file they are declared in, as well
50 as any child blueprint files. Variables are immutable with one exception - they
51 can be appended to with a += assignment, but only before they have been
52 referenced.
53
54 ### Comments
55 Android.bp files can contain C-style multiline `/* */` and C++ style single-line
56 `//` comments.
57
58 ### Types
59
60 Variables and properties are strongly typed, variables dynamically based on the
61 first assignment, and properties statically by the module type. The supported
62 types are:
63 * Bool (`true` or `false`)
64 * Integers (`int`)
65 * Strings (`"string"`)
66 * Lists of strings (`["string1", "string2"]`)
67 * Maps (`{key1: "value1", key2: ["value2"]}`)
68
69 Maps may values of any type, including nested maps. Lists and maps may have
70 trailing commas after the last value.
71
72 ### Operators
73
74 Strings, lists of strings, and maps can be appended using the `+` operator.
75 Integers can be summed up using the `+` operator. Appending a map produces the
76 union of keys in both maps, appending the values of any keys that are present
77 in both maps.
78
79 ### Defaults modules
80
81 A defaults module can be used to repeat the same properties in multiple modules.
82 For example:
83
84 ```
85 cc_defaults {
86 name: "gzip_defaults",
87 shared_libs: ["libz"],
88 stl: "none",
89 }
90
91 cc_binary {
92 name: "gzip",
93 defaults: ["gzip_defaults"],
94 srcs: ["src/test/minigzip.c"],
95 }
96 ```
97
98 ### Name resolution
99
100 Soong provides the ability for modules in different directories to specify
101 the same name, as long as each module is declared within a separate namespace.
102 A namespace can be declared like this:
103
104 ```
105 soong_namespace {
106 imports: ["path/to/otherNamespace1", "path/to/otherNamespace2"],
107 }
108 ```
109
110 Each Soong module is assigned a namespace based on its location in the tree.
111 Each Soong module is considered to be in the namespace defined by the
112 soong_namespace found in an Android.bp in the current directory or closest
113 ancestor directory, unless no such soong_namespace module is found, in which
114 case the module is considered to be in the implicit root namespace.
115
116 When Soong attempts to resolve dependency D declared my module M in namespace
117 N which imports namespaces I1, I2, I3..., then if D is a fully-qualified name
118 of the form "//namespace:module", only the specified namespace will be searched
119 for the specified module name. Otherwise, Soong will first look for a module
120 named D declared in namespace N. If that module does not exist, Soong will look
121 for a module named D in namespaces I1, I2, I3... Lastly, Soong will look in the
122 root namespace.
123
124 Until we have fully converted from Make to Soong, it will be necessary for the
125 Make product config to specify a value of PRODUCT_SOONG_NAMESPACES. Its value
126 should be a space-separated list of namespaces that Soong export to Make to be
127 built by the `m` command. After we have fully converted from Make to Soong, the
128 details of enabling namespaces could potentially change.
129
130 ### Formatter
131
132 Soong includes a canonical formatter for blueprint files, similar to
133 [gofmt](https://golang.org/cmd/gofmt/). To recursively reformat all Android.bp files
134 in the current directory:
135 ```
136 bpfmt -w .
137 ```
138
139 The canonical format includes 4 space indents, newlines after every element of a
140 multi-element list, and always includes a trailing comma in lists and maps.
141
142 ### Convert Android.mk files
143
144 Soong includes a tool perform a first pass at converting Android.mk files
145 to Android.bp files:
146
147 ```
148 androidmk Android.mk > Android.bp
149 ```
150
151 The tool converts variables, modules, comments, and some conditionals, but any
152 custom Makefile rules, complex conditionals or extra includes must be converted
153 by hand.
154
155 #### Differences between Android.mk and Android.bp
156
157 * Android.mk files often have multiple modules with the same name (for example
158 for static and shared version of a library, or for host and device versions).
159 Android.bp files require unique names for every module, but a single module can
160 be built in multiple variants, for example by adding `host_supported: true`.
161 The androidmk converter will produce multiple conflicting modules, which must
162 be resolved by hand to a single module with any differences inside
163 `target: { android: { }, host: { } }` blocks.
164
165 ## Build logic
166
167 The build logic is written in Go using the
168 [blueprint](http://godoc.org/github.com/google/blueprint) framework. Build
169 logic receives module definitions parsed into Go structures using reflection
170 and produces build rules. The build rules are collected by blueprint and
171 written to a [ninja](http://ninja-build.org) build file.
172
173 ## Other documentation
174
175 * [Best Practices](docs/best_practices.md)
176 * [Build Performance](docs/perf.md)
177 * [Generating CLion Projects](docs/clion.md)
178 * Make-specific documentation: [build/make/README.md](https://android.googlesource.com/platform/build/+/master/README.md)
179
180 ## FAQ
181
182 ### How do I write conditionals?
183
184 Soong deliberately does not support conditionals in Android.bp files.
185 Instead, complexity in build rules that would require conditionals are handled
186 in Go, where high level language features can be used and implicit dependencies
187 introduced by conditionals can be tracked. Most conditionals are converted
188 to a map property, where one of the values in the map will be selected and
189 appended to the top level properties.
190
191 For example, to support architecture specific files:
192 ```
193 cc_library {
194 ...
195 srcs: ["generic.cpp"],
196 arch: {
197 arm: {
198 srcs: ["arm.cpp"],
199 },
200 x86: {
201 srcs: ["x86.cpp"],
202 },
203 },
204 }
205 ```
206
207 See [art/build/art.go](https://android.googlesource.com/platform/art/+/master/build/art.go)
208 or [external/llvm/soong/llvm.go](https://android.googlesource.com/platform/external/llvm/+/master/soong/llvm.go)
209 for examples of more complex conditionals on product variables or environment variables.
210
211 ## Contact
212
213 Email android-building (a] googlegroups.com (external) for any questions, or see
214 [go/soong](http://go/soong) (internal).
215