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