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