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