blob: e356109cda483bceb4c9a31d91e1f61f38421a02 [file] [log] [blame] [view]
Dan Willemsen77338622017-11-08 16:39:18 -08001# Build System Changes for Android.mk Writers
2
Dan Willemsenf2646902019-01-25 16:54:37 -08003## Windows cross-compiles no longer supported in Android.mk
4
5Modules that build for Windows (our only `HOST_CROSS` OS currently) must now be
6defined in `Android.bp` files.
7
Dan Willemsen9569ddd2019-01-22 19:38:56 -08008## `LOCAL_MODULE_TAGS := eng debug` deprecation {#LOCAL_MODULE_TAGS}
9
10`LOCAL_MODULE_TAGS` value `eng` and `debug` are being deprecated. They allowed
11modules to specify that they should always be installed on `-eng`, or `-eng`
12and `-userdebug` builds. This conflicted with the ability for products to
13specify which modules should be installed, effectively making it impossible to
14build a stripped down product configuration that did not include those modules.
15
16For the equivalent functionality, specify the modules in `PRODUCT_PACKAGES_ENG`
17or `PRODUCT_PACKAGES_DEBUG` in the appropriate product makefiles.
18
19Core android packages like `su` got added to the list in
20`build/make/target/product/base_system.mk`, but for device-specific modules
21there are often better base product makefiles to use instead.
22
Dan Willemsen06364282019-01-02 14:32:54 -080023## `USER` deprecation {#USER}
24
25`USER` will soon be `nobody` in many cases due to the addition of a sandbox
26around the Android build. Most of the time you shouldn't need to know the
27identity of the user running the build, but if you do, it's available in the
28make variable `BUILD_USERNAME` for now.
29
30Similarly, the `hostname` tool will also be returning a more consistent value
31of `android-build`. The real value is available as `BUILD_HOSTNAME`.
32
Dan Willemsen6dbb33d2018-10-21 19:41:49 -070033## `BUILD_NUMBER` removal from Android.mk {#BUILD_NUMBER}
34
35`BUILD_NUMBER` should not be used directly in Android.mk files, as it would
36trigger them to be re-read every time the `BUILD_NUMBER` changes (which it does
37on every build server build). If possible, just remove the use so that your
38builds are more reproducible. If you do need it, use `BUILD_NUMBER_FROM_FILE`:
39
40``` make
41$(LOCAL_BUILT_MODULE):
42 mytool --build_number $(BUILD_NUMBER_FROM_FILE) -o $@
43```
44
45That will expand out to a subshell that will read the current `BUILD_NUMBER`
46whenever it's run. It will not re-run your command if the build number has
47changed, so incremental builds will have the build number from the last time
48the particular output was rebuilt.
49
Dan Willemsen78c40be2018-10-17 16:50:49 -070050## `DIST_DIR`, `dist_goal`, and `dist-for-goals` {#dist}
51
52`DIST_DIR` and `dist_goal` are no longer available when reading Android.mk
53files (or other build tasks). Always use `dist-for-goals` instead, which takes
54a PHONY goal, and a list of files to copy to `$DIST_DIR`. Whenever `dist` is
55specified, and the goal would be built (either explicitly on the command line,
56or as a dependency of something on the command line), that file will be copied
57into `$DIST_DIR`. For example,
58
59``` make
60$(call dist-for-goals,foo,bar/baz)
61```
62
63will copy `bar/baz` into `$DIST_DIR/baz` when `m foo dist` is run.
64
65#### Renames during copy
66
67Instead of specifying just a file, a destination name can be specified,
68including subdirectories:
69
70``` make
71$(call dist-for-goals,foo,bar/baz:logs/foo.log)
72```
73
74will copy `bar/baz` into `$DIST_DIR/logs/foo.log` when `m foo dist` is run.
75
Dan Willemsen5fb16a62018-09-04 16:23:14 -070076## `.PHONY` rule enforcement {#phony_targets}
77
78There are several new warnings/errors meant to ensure the proper use of
79`.PHONY` targets in order to improve the speed and reliability of incremental
80builds.
81
82`.PHONY`-marked targets are often used as shortcuts to provide "friendly" names
83for real files to be built, but any target marked with `.PHONY` is also always
84considered dirty, needing to be rebuilt every build. This isn't a problem for
85aliases or one-off user-requested operations, but if real builds steps depend
86on a `.PHONY` target, it can get quite expensive for what should be a tiny
87build.
88
89``` make
90...mk:42: warning: PHONY target "out/.../foo" looks like a real file (contains a "/")
91```
92
93Between this warning and the next, we're requiring that `.PHONY` targets do not
94have "/" in them, and real file targets do have a "/". This makes it more
95obvious when reading makefiles what is happening, and will help the build
96system differentiate these in the future too.
97
98``` make
99...mk:42: warning: writing to readonly directory: "kernel-modules"
100```
101
102This warning will show up for one of two reasons:
103
1041. The target isn't intended to be a real file, and should be marked with
105 `.PHONY`. This would be the case for this example.
1062. The target is a real file, but it's outside the output directories. All
107 outputs from the build system should be within the output directory,
108 otherwise `m clean` is unable to clean the build, and future builds may not
109 work properly.
110
111``` make
112...mk:42: warning: real file "out/.../foo" depends on PHONY target "buildbins"
113```
114
115If the first target isn't intended to be a real file, then it should be marked
116with `.PHONY`, which will satisfy this warning. This isn't the case for this
117example, as we require `.PHONY` targets not to have '/' in them.
118
119If the second (PHONY) target is a real file, it may unnecessarily be marked
120with `.PHONY`.
121
122### `.PHONY` and calling other build systems
123
124One common pattern (mostly outside AOSP) that we've seen hit these warning is
125when building with external build systems (firmware, bootloader, kernel, etc).
126Those are often marked as `.PHONY` because the Android build system doesn't
127have enough dependencies to know when to run the other build system again
128during an incremental build.
129
130We recommend to build these outside of Android, and deliver prebuilts into the
131Android tree instead of decreasing the speed and reliability of the incremental
132Android build.
133
134In cases where that's not desired, to preserve the speed of Android
135incrementals, over-specifying dependencies is likely a better option than
136marking it with `.PHONY`:
137
138``` make
139out/target/.../zImage: $(sort $(shell find -L $(KERNEL_SRCDIR)))
140 ...
141```
142
143For reliability, many of these other build systems do not guarantee the same
144level of incremental build assurances as the Android Build is attempting to do
145-- without custom checks, Make doesn't rebuild objects when CFLAGS change, etc.
146In order to fix this, our recommendation is to do clean builds for each of
147these external build systems every time anything they rely on changes. For
148relatively smaller builds (like the kernel), this may be reasonable as long as
149you're not trying to actively debug the kernel.
150
151## `export` and `unexport` deprecation {#export_keyword}
Dan Willemsen8b9c3cc2018-02-27 02:15:32 -0800152
153The `export` and `unexport` keywords have been deprecated, and will throw
154warnings or errors depending on where they are used.
155
156Early in the make system, during product configuration and BoardConfig.mk
157reading: these will throw a warnings, and will be an error in the future.
158Device specific configuration should not be able to affect common core build
159steps -- we're looking at triggering build steps to be invalidated if the set
160of environment variables they can access changes. If device specific
161configuration is allowed to change those, switching devices with the same
162output directory could become significantly more expensive than it already can
163be.
164
165Later, during Android.mk files, and later tasks: these will throw errors, since
166it is increasingly likely that they are being used incorrectly, attempting to
167change the environment for a single build step, and instead setting it for
168hundreds of thousands.
169
170It is not recommended to just move the environment variable setting outside of
171the build (in vendorsetup.sh, or some other configuration script or wrapper).
172We expect to limit the environment variables that the build respects in the
173future, others will be cleared. (There will be methods to get custom variables
174into the build, just not to every build step)
175
176Instead, write the export commands into the rule command lines themselves:
177
178``` make
179$(intermediates)/generated_output.img:
180 rm -rf $@
181 export MY_ENV_A="$(MY_A)"; make ...
182```
183
184If you want to set many environment variables, and/or use them many times,
185write them out to a script and source the script:
186
187``` make
188envsh := $(intermediates)/env.sh
189$(envsh):
190 rm -rf $@
191 echo 'export MY_ENV_A="$(MY_A)"' >$@
192 echo 'export MY_ENV_B="$(MY_B)"' >>$@
193
194$(intermediates)/generated_output.img: PRIVATE_ENV := $(envsh)
195$(intermediates)/generated_output.img: $(envsh) a/b/c/package.sh
196 rm -rf $@
197 source $(PRIVATE_ENV); make ...
198 source $(PRIVATE_ENV); a/b/c/package.sh ...
199```
200
Dan Willemsen5f76fc02018-06-21 21:42:29 -0700201## Implicit make rules are obsolete {#implicit_rules}
Dan Willemsen62db0f02018-06-16 09:37:13 -0700202
203Implicit rules look something like the following:
204
205``` make
206$(TARGET_OUT_SHARED_LIBRARIES)/%_vendor.so: $(TARGET_OUT_SHARED_LIBRARIES)/%.so
207 ...
208
209%.o : %.foo
210 ...
211```
212
Dan Willemsen5f76fc02018-06-21 21:42:29 -0700213These can have wide ranging effects across unrelated modules, so they're now obsolete. Instead, use static pattern rules, which are similar, but explicitly match the specified outputs:
Dan Willemsen62db0f02018-06-16 09:37:13 -0700214
215``` make
216libs := $(foreach lib,libfoo libbar,$(TARGET_OUT_SHARED_LIBRARIES)/$(lib)_vendor.so)
217$(libs): %_vendor.so: %.so
218 ...
219
220files := $(wildcard $(LOCAL_PATH)/*.foo)
221gen := $(patsubst $(LOCAL_PATH)/%.foo,$(intermediates)/%.o,$(files))
222$(gen): %.o : %.foo
223 ...
224```
225
Dan Willemsenac926592018-06-11 22:28:00 -0700226## Removing '/' from Valid Module Names {#name_slash}
227
228The build system uses module names in path names in many places. Having an
229extra '/' or '../' being inserted can cause problems -- and not just build
230breaks, but stranger invalid behavior.
231
232In every case we've seen, the fix is relatively simple: move the directory into
233`LOCAL_MODULE_RELATIVE_PATH` (or `LOCAL_MODULE_PATH` if you're still using it).
234If this causes multiple modules to be named the same, use unique module names
235and `LOCAL_MODULE_STEM` to change the installed file name:
236
237``` make
238include $(CLEAR_VARS)
239LOCAL_MODULE := ver1/code.bin
240LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/firmware
241...
242include $(BUILD_PREBUILT)
243
244include $(CLEAR_VARS)
245LOCAL_MODULE := ver2/code.bin
246LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/firmware
247...
248include $(BUILD_PREBUILT)
249```
250
251Can be rewritten as:
252
253```
254include $(CLEAR_VARS)
255LOCAL_MODULE := ver1_code.bin
256LOCAL_MODULE_STEM := code.bin
257LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR)/firmware/ver1
258...
259include $(BUILD_PREBUILT)
260
261include $(CLEAR_VARS)
262LOCAL_MODULE := ver2_code.bin
263LOCAL_MODULE_STEM := code.bin
264LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR)/firmware/ver2
265...
266include $(BUILD_PREBUILT)
267```
268
269You just need to make sure that any other references (`PRODUCT_PACKAGES`,
270`LOCAL_REQUIRED_MODULES`, etc) are converted to the new names.
271
Dan Willemsenbbe6a022018-06-10 14:49:01 -0700272## Valid Module Names {#name}
273
274We've adopted lexical requirements very similar to [Bazel's
275requirements](https://docs.bazel.build/versions/master/build-ref.html#name) for
276target names. Valid characters are `a-z`, `A-Z`, `0-9`, and the special
Dan Willemsenac926592018-06-11 22:28:00 -0700277characters `_.+-=,@~`. This currently applies to `LOCAL_PACKAGE_NAME`,
Dan Willemsenbbe6a022018-06-10 14:49:01 -0700278`LOCAL_MODULE`, and `LOCAL_MODULE_SUFFIX`, and `LOCAL_MODULE_STEM*`.
279
280Many other characters already caused problems if you used them, so we don't
281expect this to have a large effect.
282
Dan Willemsen5039ef42018-05-18 11:00:17 -0700283## PATH Tools {#PATH_Tools}
284
285The build has started restricting the external host tools usable inside the
286build. This will help ensure that build results are reproducible across
287different machines, and catch mistakes before they become larger issues.
288
289To start with, this includes replacing the $PATH with our own directory of
290tools, mirroring that of the host PATH. The only difference so far is the
291removal of the host GCC tools. Anything that is not explicitly in the
292configuration as allowed will continue functioning, but will generate a log
293message. This is expected to become more restrictive over time.
294
295The configuration is located in build/soong/ui/build/paths/config.go, and
296contains all the common tools in use in many builds. Anything not in that list
297will currently print a warning in the `$OUT_DIR/soong.log` file, including the
298command and arguments used, and the process tree in order to help locate the
299usage.
300
301In order to fix any issues brought up by these checks, the best way to fix them
302is to use tools checked into the tree -- either as prebuilts, or building them
303as host tools during the build.
304
305As a temporary measure, you can set `TEMPORARY_DISABLE_PATH_RESTRICTIONS=true`
306in your environment to temporarily turn off the error checks and allow any tool
307to be used (with logging). Beware that GCC didn't work well with the interposer
308used for logging, so this may not help in all cases.
309
Dan Willemsen79fd6962017-11-28 22:32:05 -0800310## Deprecating / obsoleting envsetup.sh variables in Makefiles
Dan Willemsen77338622017-11-08 16:39:18 -0800311
312It is not required to source envsetup.sh before running a build. Many scripts,
313including a majority of our automated build systems, do not do so. Make will
314transparently make every environment variable available as a make variable.
315This means that relying on environment variables only set up in envsetup.sh will
316produce different output for local users and scripted users.
317
318Many of these variables also include absolute path names, which we'd like to
319keep out of the generated files, so that you don't need to do a full rebuild if
320you move the source tree.
321
322To fix this, we're marking the variables that are set in envsetup.sh as
323deprecated in the makefiles. This will trigger a warning every time one is read
Dan Willemsen79fd6962017-11-28 22:32:05 -0800324(or written) inside Kati. Once all the warnings have been removed for a
325particular variable, we'll switch it to obsolete, and any references will become
326errors.
Dan Willemsen77338622017-11-08 16:39:18 -0800327
328### envsetup.sh variables with make equivalents
329
330| instead of | use |
331|--------------------------------------------------------------|----------------------|
332| OUT {#OUT} | OUT_DIR |
333| ANDROID_HOST_OUT {#ANDROID_HOST_OUT} | HOST_OUT |
334| ANDROID_PRODUCT_OUT {#ANDROID_PRODUCT_OUT} | PRODUCT_OUT |
335| ANDROID_HOST_OUT_TESTCASES {#ANDROID_HOST_OUT_TESTCASES} | HOST_OUT_TESTCASES |
336| ANDROID_TARGET_OUT_TESTCASES {#ANDROID_TARGET_OUT_TESTCASES} | TARGET_OUT_TESTCASES |
337
338All of the make variables may be relative paths from the current directory, or
339absolute paths if the output directory was specified as an absolute path. If you
340need an absolute variable, convert it to absolute during a rule, so that it's
341not expanded into the generated ninja file:
342
343``` make
344$(PRODUCT_OUT)/gen.img: my/src/path/gen.sh
345 export PRODUCT_OUT=$$(cd $(PRODUCT_OUT); pwd); cd my/src/path; ./gen.sh -o $${PRODUCT_OUT}/gen.img
346```
347
348### ANDROID_BUILD_TOP {#ANDROID_BUILD_TOP}
349
350In Android.mk files, you can always assume that the current directory is the
351root of the source tree, so this can just be replaced with '.' (which is what
352$TOP is hardcoded to), or removed entirely. If you need an absolute path, see
353the instructions above.
354
355### Stop using PATH directly {#PATH}
356
357This isn't only set by envsetup.sh, but it is modified by it. Due to that it's
358rather easy for this to change between different shells, and it's not ideal to
359reread the makefiles every time this changes.
360
361In most cases, you shouldn't need to touch PATH at all. When you need to have a
362rule reference a particular binary that's part of the source tree or outputs,
363it's preferrable to just use the path to the file itself (since you should
364already be adding that as a dependency).
365
366Depending on the rule, passing the file path itself may not be feasible due to
367layers of unchangable scripts/binaries. In that case, be sure to add the
368dependency, but modify the PATH within the rule itself:
369
370``` make
371$(TARGET): myscript my/path/binary
372 PATH=my/path:$$PATH myscript -o $@
373```
374
375### Stop using PYTHONPATH directly {#PYTHONPATH}
376
377Like PATH, this isn't only set by envsetup.sh, but it is modified by it. Due to
378that it's rather easy for this to change between different shells, and it's not
379ideal to reread the makefiles every time.
380
381The best solution here is to start switching to Soong's python building support,
382which packages the python interpreter, libraries, and script all into one file
383that no longer needs PYTHONPATH. See fontchain_lint for examples of this:
384
385* [external/fonttools/Lib/fontTools/Android.bp] for python_library_host
386* [frameworks/base/Android.bp] for python_binary_host
387* [frameworks/base/data/fonts/Android.mk] to execute the python binary
388
389If you still need to use PYTHONPATH, do so within the rule itself, just like
390path:
391
392``` make
393$(TARGET): myscript.py $(sort $(shell find my/python/lib -name '*.py'))
394 PYTHONPATH=my/python/lib:$$PYTHONPATH myscript.py -o $@
395```
Yifan Hong97de88c2017-12-12 18:01:09 -0800396### Stop using PRODUCT_COMPATIBILITY_MATRIX_LEVEL_OVERRIDE directly {#PRODUCT_COMPATIBILITY_MATRIX_LEVEL_OVERRIDE}
397
398Specify Framework Compatibility Matrix Version in device manifest by adding a `target-level`
399attribute to the root element `<manifest>`. If `PRODUCT_COMPATIBILITY_MATRIX_LEVEL_OVERRIDE`
400is 26 or 27, you can add `"target-level"="1"` to your device manifest instead.
Dan Willemsen77338622017-11-08 16:39:18 -0800401
Stephen Hines178cf8e2018-01-11 11:54:48 -0800402### Stop using USE_CLANG_PLATFORM_BUILD {#USE_CLANG_PLATFORM_BUILD}
403
404Clang is the default and only supported Android compiler, so there is no reason
405for this option to exist.
406
Dan Willemsen77338622017-11-08 16:39:18 -0800407### Other envsetup.sh variables {#other_envsetup_variables}
408
409* ANDROID_TOOLCHAIN
410* ANDROID_TOOLCHAIN_2ND_ARCH
411* ANDROID_DEV_SCRIPTS
412* ANDROID_EMULATOR_PREBUILTS
413* ANDROID_PRE_BUILD_PATHS
414
415These are all exported from envsetup.sh, but don't have clear equivalents within
416the makefile system. If you need one of them, you'll have to set up your own
417version.
418
419
420[build/soong/Changes.md]: https://android.googlesource.com/platform/build/soong/+/master/Changes.md
421[external/fonttools/Lib/fontTools/Android.bp]: https://android.googlesource.com/platform/external/fonttools/+/master/Lib/fontTools/Android.bp
422[frameworks/base/Android.bp]: https://android.googlesource.com/platform/frameworks/base/+/master/Android.bp
423[frameworks/base/data/fonts/Android.mk]: https://android.googlesource.com/platform/frameworks/base/+/master/data/fonts/Android.mk