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