blob: 0a6adc4be647588f44e8ab1e37e502eccf9fa0bb [file] [log] [blame] [view]
Dan Willemsen77338622017-11-08 16:39:18 -08001# Build System Changes for Android.mk Writers
2
Yo Chiang64faf882020-07-15 18:35:12 +08003## `LOCAL_REQUIRED_MODULES` requires listed modules to exist {#BUILD_BROKEN_MISSING_REQUIRED_MODULES}
4
5Modules listed in `LOCAL_REQUIRED_MODULES`, `LOCAL_HOST_REQUIRED_MODULES` and
6`LOCAL_TARGET_REQUIRED_MODULES` need to exist unless `ALLOW_MISSING_DEPENDENCIES`
7is set.
8
9To temporarily relax missing required modules check, use:
10
11`BUILD_BROKEN_MISSING_REQUIRED_MODULES := true`
12
Jiyong Park0b4fccb2020-06-26 17:38:00 +090013## Changes in system properties settings
14
15### Product variables
16
17System properties for each of the partition is supposed to be set via following
18product config variables.
19
Donghyun Jo8c65ef62021-03-02 08:51:03 +090020For system partition,
Jiyong Park0b4fccb2020-06-26 17:38:00 +090021
Donghyun Jo8c65ef62021-03-02 08:51:03 +090022* `PRODUCT_SYSTEM_PROPERTIES`
Jiyong Park0b4fccb2020-06-26 17:38:00 +090023* `PRODUCT_SYSTEM_DEFAULT_PROPERTIES` is highly discouraged. Will be deprecated.
24
25For vendor partition,
26
27* `PRODUCT_VENDOR_PROPERTIES`
28* `PRODUCT_PROPERTY_OVERRIDES` is highly discouraged. Will be deprecated.
29* `PRODUCT_DEFAULT_PROPERTY_OVERRIDES` is also discouraged. Will be deprecated.
30
31For odm partition,
32
33* `PRODUCT_ODM_PROPERTIES`
34
35For system_ext partition,
36
37* `PRODUCT_SYSTEM_EXT_PROPERTIES`
38
39For product partition,
40
41* `PRODUCT_PRODUCT_PROPERTIES`
42
43### Duplication is not allowed within a partition
44
45For each partition, having multiple sysprop assignments for the same name is
46prohibited. For example, the following will now trigger an error:
47
48`PRODUCT_VENDOR_PROPERTIES += foo=true foo=false`
49
50Having duplication across partitions are still allowed. So, the following is
51not an error:
52
53`PRODUCT_VENDOR_PROPERTIES += foo=true`
54`PRODUCT_SYSTEM_PROPERTIES += foo=false`
55
56In that case, the final value is determined at runtime. The precedence is
57
58* product
59* odm
60* vendor
61* system_ext
62* system
63
64So, `foo` becomes `true` because vendor has higher priority than system.
65
66To temporarily turn the build-time restriction off, use
67
68`BUILD_BROKEN_DUP_SYSPROP := true`
69
70### Optional assignments
71
72System properties can now be set as optional using the new syntax:
73
74`name ?= value`
75
76Then the system property named `name` gets the value `value` only when there
77is no other non-optional assignments having the same name. For example, the
78following is allowed and `foo` gets `true`
79
80`PRODUCT_VENDOR_PROPERTIES += foo=true foo?=false`
81
82Note that the order between the optional and the non-optional assignments
83doesn't matter. The following gives the same result as above.
84
85`PRODUCT_VENDOR_PROPERTIES += foo?=false foo=true`
86
87Optional assignments can be duplicated and in that case their order matters.
88Specifically, the last one eclipses others.
89
90`PRODUCT_VENDOR_PROPERTIES += foo?=apple foo?=banana foo?=mango`
91
92With above, `foo` becomes `mango` since its the last one.
93
94Note that this behavior is different from the previous behavior of preferring
95the first one. To go back to the original behavior for compatability reason,
96use:
97
98`BUILD_BROKEN_DUP_SYSPROP := true`
99
Yo Chiangd4741812020-07-14 15:15:08 +0800100## ELF prebuilts in `PRODUCT_COPY_FILES` {#BUILD_BROKEN_ELF_PREBUILT_PRODUCT_COPY_FILES}
Yo Chiang73d31482020-04-07 15:59:59 +0800101
Yo Chiangd4741812020-07-14 15:15:08 +0800102ELF prebuilts in `PRODUCT_COPY_FILES` that are installed into these paths are an
Yo Chiang73d31482020-04-07 15:59:59 +0800103error:
104
105* `<partition>/bin/*`
106* `<partition>/lib/*`
107* `<partition>/lib64/*`
108
Yo Chiangd4741812020-07-14 15:15:08 +0800109Define prebuilt modules and add them to `PRODUCT_PACKAGES` instead.
Yo Chiang73d31482020-04-07 15:59:59 +0800110To temporarily relax this check and restore the behavior prior to this change,
111set `BUILD_BROKEN_ELF_PREBUILT_PRODUCT_COPY_FILES := true` in `BoardConfig.mk`.
112
Dan Willemsen66d21d42020-01-27 19:26:02 -0800113## COPY_HEADERS usage now produces warnings {#copy_headers}
114
115We've considered `BUILD_COPY_HEADERS`/`LOCAL_COPY_HEADERS` to be deprecated for
116a long time, and the places where it's been able to be used have shrinked over
117the last several releases. Equivalent functionality is not available in Soong.
118
119See the [build/soong/docs/best_practices.md#headers] for more information about
120how best to handle headers in Android.
121
Dan Willemsen2bfffb92020-01-14 10:56:53 -0800122## `m4` is not available on `$PATH`
123
124There is a prebuilt of it available in prebuilts/build-tools, and a make
125variable `M4` that contains the path.
126
127Beyond the direct usage, whenever you use bison or flex directly, they call m4
128behind the scene, so you must set the M4 environment variable (and depend upon
129it for incremental build correctness):
130
131```
132$(intermediates)/foo.c: .KATI_IMPLICIT_OUTPUTS := $(intermediates)/foo.h
133$(intermediates)/foo.c: $(LOCAL_PATH)/foo.y $(M4) $(BISON) $(BISON_DATA)
134 M4=$(M4) $(BISON) ...
135```
136
Dan Willemsen26076252020-01-02 20:08:10 -0800137## Rules executed within limited environment
138
139With `ALLOW_NINJA_ENV=false` (soon to be the default), ninja, and all the
140rules/actions executed within it will only have access to a limited number of
141environment variables. Ninja does not track when environment variables change
142in order to trigger rebuilds, so changing behavior based on arbitrary variables
143is not safe with incremental builds.
144
145Kati and Soong can safely use environment variables, so the expectation is that
146you'd embed any environment variables that you need to use within the command
147line generated by those tools. See the [export section](#export_keyword) below
148for examples.
149
150For a temporary workaround, you can set `ALLOW_NINJA_ENV=true` in your
151environment to restore the previous behavior, or set
152`BUILD_BROKEN_NINJA_USES_ENV_VAR := <var> <var2> ...` in your `BoardConfig.mk`
153to allow specific variables to be passed through until you've fixed the rules.
154
Dan Willemsen0cb422f2019-11-25 17:51:18 -0800155## LOCAL_C_INCLUDES outside the source/output trees are an error {#BUILD_BROKEN_OUTSIDE_INCLUDE_DIRS}
156
157Include directories are expected to be within the source tree (or in the output
158directory, generated during the build). This has been checked in some form
159since Oreo, but now has better checks.
160
161There's now a `BUILD_BROKEN_OUTSIDE_INCLUDE_DIRS` variable, that when set, will
162turn these errors into warnings temporarily. I don't expect this to last more
163than a release, since they're fairly easy to clean up.
164
165Neither of these cases are supported by Soong, and will produce errors when
166converting your module.
167
168### Absolute paths
169
170This has been checked since Oreo. The common reason to hit this is because a
171makefile is calculating a path, and ran abspath/realpath/etc. This is a problem
172because it makes your build non-reproducible. It's very unlikely that your
173source path is the same on every machine.
174
175### Using `../` to leave the source/output directories
176
177This is the new check that has been added. In every case I've found, this has
178been a mistake in the Android.mk -- assuming that `LOCAL_C_INCLUDES` (which is
179relative to the top of the source tree) acts like `LOCAL_SRC_FILES` (which is
180relative to `LOCAL_PATH`).
181
182Since this usually isn't a valid path, you can almost always just remove the
183offending line.
184
185
Dan Willemsen26076252020-01-02 20:08:10 -0800186## `BOARD_HAL_STATIC_LIBRARIES` and `LOCAL_HAL_STATIC_LIBRARIES` are obsolete {#BOARD_HAL_STATIC_LIBRARIES}
Yifan Hong88adfc62019-10-11 15:52:44 -0700187
188Define proper HIDL / Stable AIDL HAL instead.
189
190* For libhealthd, use health HAL. See instructions for implementing
191 health HAL:
192
193 * [hardware/interfaces/health/2.1/README.md] for health 2.1 HAL (recommended)
194 * [hardware/interfaces/health/1.0/README.md] for health 1.0 HAL
195
196* For libdumpstate, use at least Dumpstate HAL 1.0.
197
Tao Bao84f88a42019-05-28 16:30:18 -0700198## PRODUCT_STATIC_BOOT_CONTROL_HAL is obsolete {#PRODUCT_STATIC_BOOT_CONTROL_HAL}
199
200`PRODUCT_STATIC_BOOT_CONTROL_HAL` was the workaround to allow sideloading with
201statically linked boot control HAL, before shared library HALs were supported
202under recovery. Android Q has added such support (HALs will be loaded in
203passthrough mode), and the workarounds are being removed. Targets should build
204and install the recovery variant of boot control HAL modules into recovery
205image, similar to the ones installed for normal boot. See the change to
206crosshatch for example of this:
207
208* [device/google/crosshatch/bootctrl/Android.bp] for `bootctrl.sdm845` building
209 rules
210* [device/google/crosshatch/device.mk] for installing `bootctrl.sdm845.recovery`
211 and `android.hardware.boot@1.0-impl.recovery` into recovery image
212
213[device/google/crosshatch/bootctrl/Android.bp]: https://android.googlesource.com/device/google/crosshatch/+/master/bootctrl/Android.bp
214[device/google/crosshatch/device.mk]: https://android.googlesource.com/device/google/crosshatch/+/master/device.mk
215
Dan Willemsen695849e2019-04-17 12:25:09 -0700216## Deprecation of `BUILD_*` module types
217
218See [build/make/Deprecation.md](Deprecation.md) for the current status.
219
Dan Willemsen3d05a082019-02-22 23:06:03 +0000220## `PRODUCT_HOST_PACKAGES` split from `PRODUCT_PACKAGES` {#PRODUCT_HOST_PACKAGES}
221
222Previously, adding a module to `PRODUCT_PACKAGES` that supported both the host
223and the target (`host_supported` in Android.bp; two modules with the same name
224in Android.mk) would cause both to be built and installed. In many cases you
225only want either the host or target versions to be built/installed by default,
226and would be over-building with both. So `PRODUCT_PACKAGES` will be changing to
227just affect target modules, while `PRODUCT_HOST_PACKAGES` is being added for
228host modules.
229
230Functional differences between `PRODUCT_PACKAGES` and `PRODUCT_HOST_PACKAGES`:
231
232* `PRODUCT_HOST_PACKAGES` does not have `_ENG`/`_DEBUG` variants, as that's a
233 property of the target, not the host.
234* `PRODUCT_HOST_PACKAGES` does not support `LOCAL_MODULE_OVERRIDES`.
235* `PRODUCT_HOST_PACKAGES` requires listed modules to exist, and be host
236 modules. (Unless `ALLOW_MISSING_DEPENDENCIES` is set)
237
238This is still an active migration, so currently it still uses
239`PRODUCT_PACKAGES` to make installation decisions, but verifies that if we used
240`PRODUCT_HOST_PACKAGES`, it would trigger installation for all of the same host
241packages. This check ignores shared libraries, as those are not normally
242necessary in `PRODUCT_*PACKAGES`, and tended to be over-built (especially the
24332-bit variants).
244
245Future changes will switch installation decisions to `PRODUCT_HOST_PACKAGES`
246for host modules, error when there's a host-only module in `PRODUCT_PACKAGES`,
247and do some further cleanup where `LOCAL_REQUIRED_MODULES` are still merged
248between host and target modules with the same name.
249
Dan Willemsen46267cb2019-01-25 14:35:58 -0800250## `*.c.arm` / `*.cpp.arm` deprecation {#file_arm}
251
252In Android.mk files, you used to be able to change LOCAL_ARM_MODE for each
253source file by appending `.arm` to the end of the filename in
254`LOCAL_SRC_FILES`.
255
256Soong does not support this uncommonly used behavior, instead expecting those
257files to be split out into a separate static library that chooses `arm` over
258`thumb` for the entire library. This must now also be done in Android.mk files.
259
Dan Willemsenf2646902019-01-25 16:54:37 -0800260## Windows cross-compiles no longer supported in Android.mk
261
262Modules that build for Windows (our only `HOST_CROSS` OS currently) must now be
263defined in `Android.bp` files.
264
Dan Willemsene048f7e2019-02-09 18:58:36 -0800265## `LOCAL_MODULE_TAGS := eng debug` are obsolete {#LOCAL_MODULE_TAGS}
Dan Willemsen9569ddd2019-01-22 19:38:56 -0800266
Dan Willemsene048f7e2019-02-09 18:58:36 -0800267`LOCAL_MODULE_TAGS` value `eng` and `debug` are now obsolete. They allowed
Dan Willemsen9569ddd2019-01-22 19:38:56 -0800268modules to specify that they should always be installed on `-eng`, or `-eng`
269and `-userdebug` builds. This conflicted with the ability for products to
270specify which modules should be installed, effectively making it impossible to
271build a stripped down product configuration that did not include those modules.
272
273For the equivalent functionality, specify the modules in `PRODUCT_PACKAGES_ENG`
274or `PRODUCT_PACKAGES_DEBUG` in the appropriate product makefiles.
275
276Core android packages like `su` got added to the list in
277`build/make/target/product/base_system.mk`, but for device-specific modules
278there are often better base product makefiles to use instead.
279
Dan Willemsen06364282019-01-02 14:32:54 -0800280## `USER` deprecation {#USER}
281
282`USER` will soon be `nobody` in many cases due to the addition of a sandbox
283around the Android build. Most of the time you shouldn't need to know the
284identity of the user running the build, but if you do, it's available in the
285make variable `BUILD_USERNAME` for now.
286
287Similarly, the `hostname` tool will also be returning a more consistent value
288of `android-build`. The real value is available as `BUILD_HOSTNAME`.
289
Dan Willemsen6dbb33d2018-10-21 19:41:49 -0700290## `BUILD_NUMBER` removal from Android.mk {#BUILD_NUMBER}
291
292`BUILD_NUMBER` should not be used directly in Android.mk files, as it would
293trigger them to be re-read every time the `BUILD_NUMBER` changes (which it does
294on every build server build). If possible, just remove the use so that your
295builds are more reproducible. If you do need it, use `BUILD_NUMBER_FROM_FILE`:
296
297``` make
298$(LOCAL_BUILT_MODULE):
299 mytool --build_number $(BUILD_NUMBER_FROM_FILE) -o $@
300```
301
302That will expand out to a subshell that will read the current `BUILD_NUMBER`
303whenever it's run. It will not re-run your command if the build number has
304changed, so incremental builds will have the build number from the last time
305the particular output was rebuilt.
306
Dan Willemsen78c40be2018-10-17 16:50:49 -0700307## `DIST_DIR`, `dist_goal`, and `dist-for-goals` {#dist}
308
309`DIST_DIR` and `dist_goal` are no longer available when reading Android.mk
310files (or other build tasks). Always use `dist-for-goals` instead, which takes
311a PHONY goal, and a list of files to copy to `$DIST_DIR`. Whenever `dist` is
312specified, and the goal would be built (either explicitly on the command line,
313or as a dependency of something on the command line), that file will be copied
314into `$DIST_DIR`. For example,
315
316``` make
317$(call dist-for-goals,foo,bar/baz)
318```
319
320will copy `bar/baz` into `$DIST_DIR/baz` when `m foo dist` is run.
321
322#### Renames during copy
323
324Instead of specifying just a file, a destination name can be specified,
325including subdirectories:
326
327``` make
328$(call dist-for-goals,foo,bar/baz:logs/foo.log)
329```
330
331will copy `bar/baz` into `$DIST_DIR/logs/foo.log` when `m foo dist` is run.
332
Dan Willemsen5fb16a62018-09-04 16:23:14 -0700333## `.PHONY` rule enforcement {#phony_targets}
334
335There are several new warnings/errors meant to ensure the proper use of
336`.PHONY` targets in order to improve the speed and reliability of incremental
337builds.
338
339`.PHONY`-marked targets are often used as shortcuts to provide "friendly" names
340for real files to be built, but any target marked with `.PHONY` is also always
341considered dirty, needing to be rebuilt every build. This isn't a problem for
342aliases or one-off user-requested operations, but if real builds steps depend
343on a `.PHONY` target, it can get quite expensive for what should be a tiny
344build.
345
346``` make
347...mk:42: warning: PHONY target "out/.../foo" looks like a real file (contains a "/")
348```
349
350Between this warning and the next, we're requiring that `.PHONY` targets do not
351have "/" in them, and real file targets do have a "/". This makes it more
352obvious when reading makefiles what is happening, and will help the build
353system differentiate these in the future too.
354
355``` make
356...mk:42: warning: writing to readonly directory: "kernel-modules"
357```
358
359This warning will show up for one of two reasons:
360
3611. The target isn't intended to be a real file, and should be marked with
362 `.PHONY`. This would be the case for this example.
3632. The target is a real file, but it's outside the output directories. All
364 outputs from the build system should be within the output directory,
365 otherwise `m clean` is unable to clean the build, and future builds may not
366 work properly.
367
368``` make
369...mk:42: warning: real file "out/.../foo" depends on PHONY target "buildbins"
370```
371
372If the first target isn't intended to be a real file, then it should be marked
373with `.PHONY`, which will satisfy this warning. This isn't the case for this
374example, as we require `.PHONY` targets not to have '/' in them.
375
376If the second (PHONY) target is a real file, it may unnecessarily be marked
377with `.PHONY`.
378
379### `.PHONY` and calling other build systems
380
381One common pattern (mostly outside AOSP) that we've seen hit these warning is
382when building with external build systems (firmware, bootloader, kernel, etc).
383Those are often marked as `.PHONY` because the Android build system doesn't
384have enough dependencies to know when to run the other build system again
385during an incremental build.
386
387We recommend to build these outside of Android, and deliver prebuilts into the
388Android tree instead of decreasing the speed and reliability of the incremental
389Android build.
390
391In cases where that's not desired, to preserve the speed of Android
392incrementals, over-specifying dependencies is likely a better option than
393marking it with `.PHONY`:
394
395``` make
396out/target/.../zImage: $(sort $(shell find -L $(KERNEL_SRCDIR)))
397 ...
398```
399
400For reliability, many of these other build systems do not guarantee the same
401level of incremental build assurances as the Android Build is attempting to do
402-- without custom checks, Make doesn't rebuild objects when CFLAGS change, etc.
403In order to fix this, our recommendation is to do clean builds for each of
404these external build systems every time anything they rely on changes. For
405relatively smaller builds (like the kernel), this may be reasonable as long as
406you're not trying to actively debug the kernel.
407
408## `export` and `unexport` deprecation {#export_keyword}
Dan Willemsen8b9c3cc2018-02-27 02:15:32 -0800409
Dan Willemsen3a1072a2019-05-14 22:03:58 -0700410The `export` and `unexport` keywords are obsolete, and will throw errors when
411used.
Dan Willemsen8b9c3cc2018-02-27 02:15:32 -0800412
Dan Willemsen8b9c3cc2018-02-27 02:15:32 -0800413Device specific configuration should not be able to affect common core build
414steps -- we're looking at triggering build steps to be invalidated if the set
415of environment variables they can access changes. If device specific
416configuration is allowed to change those, switching devices with the same
417output directory could become significantly more expensive than it already can
418be.
419
Dan Willemsen3a1072a2019-05-14 22:03:58 -0700420If used during Android.mk files, and later tasks, it is increasingly likely
421that they are being used incorrectly. Attempting to change the environment for
422a single build step, and instead setting it for hundreds of thousands.
Dan Willemsen8b9c3cc2018-02-27 02:15:32 -0800423
424It is not recommended to just move the environment variable setting outside of
425the build (in vendorsetup.sh, or some other configuration script or wrapper).
426We expect to limit the environment variables that the build respects in the
427future, others will be cleared. (There will be methods to get custom variables
428into the build, just not to every build step)
429
430Instead, write the export commands into the rule command lines themselves:
431
432``` make
433$(intermediates)/generated_output.img:
434 rm -rf $@
435 export MY_ENV_A="$(MY_A)"; make ...
436```
437
438If you want to set many environment variables, and/or use them many times,
439write them out to a script and source the script:
440
441``` make
442envsh := $(intermediates)/env.sh
443$(envsh):
444 rm -rf $@
445 echo 'export MY_ENV_A="$(MY_A)"' >$@
446 echo 'export MY_ENV_B="$(MY_B)"' >>$@
447
448$(intermediates)/generated_output.img: PRIVATE_ENV := $(envsh)
449$(intermediates)/generated_output.img: $(envsh) a/b/c/package.sh
450 rm -rf $@
451 source $(PRIVATE_ENV); make ...
452 source $(PRIVATE_ENV); a/b/c/package.sh ...
453```
454
Dan Willemsen5f76fc02018-06-21 21:42:29 -0700455## Implicit make rules are obsolete {#implicit_rules}
Dan Willemsen62db0f02018-06-16 09:37:13 -0700456
457Implicit rules look something like the following:
458
459``` make
460$(TARGET_OUT_SHARED_LIBRARIES)/%_vendor.so: $(TARGET_OUT_SHARED_LIBRARIES)/%.so
461 ...
462
463%.o : %.foo
464 ...
465```
466
Dan Willemsen5f76fc02018-06-21 21:42:29 -0700467These 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 -0700468
469``` make
470libs := $(foreach lib,libfoo libbar,$(TARGET_OUT_SHARED_LIBRARIES)/$(lib)_vendor.so)
471$(libs): %_vendor.so: %.so
472 ...
473
474files := $(wildcard $(LOCAL_PATH)/*.foo)
475gen := $(patsubst $(LOCAL_PATH)/%.foo,$(intermediates)/%.o,$(files))
476$(gen): %.o : %.foo
477 ...
478```
479
Dan Willemsenac926592018-06-11 22:28:00 -0700480## Removing '/' from Valid Module Names {#name_slash}
481
482The build system uses module names in path names in many places. Having an
483extra '/' or '../' being inserted can cause problems -- and not just build
484breaks, but stranger invalid behavior.
485
486In every case we've seen, the fix is relatively simple: move the directory into
487`LOCAL_MODULE_RELATIVE_PATH` (or `LOCAL_MODULE_PATH` if you're still using it).
488If this causes multiple modules to be named the same, use unique module names
489and `LOCAL_MODULE_STEM` to change the installed file name:
490
491``` make
492include $(CLEAR_VARS)
493LOCAL_MODULE := ver1/code.bin
494LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/firmware
495...
496include $(BUILD_PREBUILT)
497
498include $(CLEAR_VARS)
499LOCAL_MODULE := ver2/code.bin
500LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/firmware
501...
502include $(BUILD_PREBUILT)
503```
504
505Can be rewritten as:
506
507```
508include $(CLEAR_VARS)
509LOCAL_MODULE := ver1_code.bin
510LOCAL_MODULE_STEM := code.bin
511LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR)/firmware/ver1
512...
513include $(BUILD_PREBUILT)
514
515include $(CLEAR_VARS)
516LOCAL_MODULE := ver2_code.bin
517LOCAL_MODULE_STEM := code.bin
518LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR)/firmware/ver2
519...
520include $(BUILD_PREBUILT)
521```
522
523You just need to make sure that any other references (`PRODUCT_PACKAGES`,
524`LOCAL_REQUIRED_MODULES`, etc) are converted to the new names.
525
Dan Willemsenbbe6a022018-06-10 14:49:01 -0700526## Valid Module Names {#name}
527
528We've adopted lexical requirements very similar to [Bazel's
529requirements](https://docs.bazel.build/versions/master/build-ref.html#name) for
530target names. Valid characters are `a-z`, `A-Z`, `0-9`, and the special
Dan Willemsenac926592018-06-11 22:28:00 -0700531characters `_.+-=,@~`. This currently applies to `LOCAL_PACKAGE_NAME`,
Dan Willemsenbbe6a022018-06-10 14:49:01 -0700532`LOCAL_MODULE`, and `LOCAL_MODULE_SUFFIX`, and `LOCAL_MODULE_STEM*`.
533
534Many other characters already caused problems if you used them, so we don't
535expect this to have a large effect.
536
Dan Willemsen5039ef42018-05-18 11:00:17 -0700537## PATH Tools {#PATH_Tools}
538
539The build has started restricting the external host tools usable inside the
540build. This will help ensure that build results are reproducible across
541different machines, and catch mistakes before they become larger issues.
542
543To start with, this includes replacing the $PATH with our own directory of
544tools, mirroring that of the host PATH. The only difference so far is the
545removal of the host GCC tools. Anything that is not explicitly in the
546configuration as allowed will continue functioning, but will generate a log
547message. This is expected to become more restrictive over time.
548
549The configuration is located in build/soong/ui/build/paths/config.go, and
550contains all the common tools in use in many builds. Anything not in that list
551will currently print a warning in the `$OUT_DIR/soong.log` file, including the
552command and arguments used, and the process tree in order to help locate the
553usage.
554
555In order to fix any issues brought up by these checks, the best way to fix them
556is to use tools checked into the tree -- either as prebuilts, or building them
557as host tools during the build.
558
559As a temporary measure, you can set `TEMPORARY_DISABLE_PATH_RESTRICTIONS=true`
560in your environment to temporarily turn off the error checks and allow any tool
561to be used (with logging). Beware that GCC didn't work well with the interposer
562used for logging, so this may not help in all cases.
563
Dan Willemsen79fd6962017-11-28 22:32:05 -0800564## Deprecating / obsoleting envsetup.sh variables in Makefiles
Dan Willemsen77338622017-11-08 16:39:18 -0800565
566It is not required to source envsetup.sh before running a build. Many scripts,
567including a majority of our automated build systems, do not do so. Make will
568transparently make every environment variable available as a make variable.
569This means that relying on environment variables only set up in envsetup.sh will
570produce different output for local users and scripted users.
571
572Many of these variables also include absolute path names, which we'd like to
573keep out of the generated files, so that you don't need to do a full rebuild if
574you move the source tree.
575
576To fix this, we're marking the variables that are set in envsetup.sh as
577deprecated in the makefiles. This will trigger a warning every time one is read
Dan Willemsen79fd6962017-11-28 22:32:05 -0800578(or written) inside Kati. Once all the warnings have been removed for a
579particular variable, we'll switch it to obsolete, and any references will become
580errors.
Dan Willemsen77338622017-11-08 16:39:18 -0800581
582### envsetup.sh variables with make equivalents
583
584| instead of | use |
585|--------------------------------------------------------------|----------------------|
Yasuhiro Kubotacd301f62018-10-09 15:51:23 +0900586| OUT {#OUT} | PRODUCT_OUT |
Dan Willemsen77338622017-11-08 16:39:18 -0800587| ANDROID_HOST_OUT {#ANDROID_HOST_OUT} | HOST_OUT |
588| ANDROID_PRODUCT_OUT {#ANDROID_PRODUCT_OUT} | PRODUCT_OUT |
589| ANDROID_HOST_OUT_TESTCASES {#ANDROID_HOST_OUT_TESTCASES} | HOST_OUT_TESTCASES |
590| ANDROID_TARGET_OUT_TESTCASES {#ANDROID_TARGET_OUT_TESTCASES} | TARGET_OUT_TESTCASES |
591
592All of the make variables may be relative paths from the current directory, or
593absolute paths if the output directory was specified as an absolute path. If you
594need an absolute variable, convert it to absolute during a rule, so that it's
595not expanded into the generated ninja file:
596
597``` make
598$(PRODUCT_OUT)/gen.img: my/src/path/gen.sh
599 export PRODUCT_OUT=$$(cd $(PRODUCT_OUT); pwd); cd my/src/path; ./gen.sh -o $${PRODUCT_OUT}/gen.img
600```
601
602### ANDROID_BUILD_TOP {#ANDROID_BUILD_TOP}
603
604In Android.mk files, you can always assume that the current directory is the
605root of the source tree, so this can just be replaced with '.' (which is what
606$TOP is hardcoded to), or removed entirely. If you need an absolute path, see
607the instructions above.
608
609### Stop using PATH directly {#PATH}
610
611This isn't only set by envsetup.sh, but it is modified by it. Due to that it's
612rather easy for this to change between different shells, and it's not ideal to
613reread the makefiles every time this changes.
614
615In most cases, you shouldn't need to touch PATH at all. When you need to have a
616rule reference a particular binary that's part of the source tree or outputs,
617it's preferrable to just use the path to the file itself (since you should
618already be adding that as a dependency).
619
620Depending on the rule, passing the file path itself may not be feasible due to
621layers of unchangable scripts/binaries. In that case, be sure to add the
622dependency, but modify the PATH within the rule itself:
623
624``` make
625$(TARGET): myscript my/path/binary
626 PATH=my/path:$$PATH myscript -o $@
627```
628
629### Stop using PYTHONPATH directly {#PYTHONPATH}
630
631Like PATH, this isn't only set by envsetup.sh, but it is modified by it. Due to
632that it's rather easy for this to change between different shells, and it's not
633ideal to reread the makefiles every time.
634
635The best solution here is to start switching to Soong's python building support,
636which packages the python interpreter, libraries, and script all into one file
637that no longer needs PYTHONPATH. See fontchain_lint for examples of this:
638
639* [external/fonttools/Lib/fontTools/Android.bp] for python_library_host
640* [frameworks/base/Android.bp] for python_binary_host
641* [frameworks/base/data/fonts/Android.mk] to execute the python binary
642
643If you still need to use PYTHONPATH, do so within the rule itself, just like
644path:
645
646``` make
647$(TARGET): myscript.py $(sort $(shell find my/python/lib -name '*.py'))
648 PYTHONPATH=my/python/lib:$$PYTHONPATH myscript.py -o $@
649```
Yifan Hong97de88c2017-12-12 18:01:09 -0800650### Stop using PRODUCT_COMPATIBILITY_MATRIX_LEVEL_OVERRIDE directly {#PRODUCT_COMPATIBILITY_MATRIX_LEVEL_OVERRIDE}
651
652Specify Framework Compatibility Matrix Version in device manifest by adding a `target-level`
653attribute to the root element `<manifest>`. If `PRODUCT_COMPATIBILITY_MATRIX_LEVEL_OVERRIDE`
654is 26 or 27, you can add `"target-level"="1"` to your device manifest instead.
Dan Willemsen77338622017-11-08 16:39:18 -0800655
Stephen Hines178cf8e2018-01-11 11:54:48 -0800656### Stop using USE_CLANG_PLATFORM_BUILD {#USE_CLANG_PLATFORM_BUILD}
657
658Clang is the default and only supported Android compiler, so there is no reason
659for this option to exist.
660
Dan Willemsen77338622017-11-08 16:39:18 -0800661### Other envsetup.sh variables {#other_envsetup_variables}
662
663* ANDROID_TOOLCHAIN
664* ANDROID_TOOLCHAIN_2ND_ARCH
665* ANDROID_DEV_SCRIPTS
666* ANDROID_EMULATOR_PREBUILTS
667* ANDROID_PRE_BUILD_PATHS
668
669These are all exported from envsetup.sh, but don't have clear equivalents within
670the makefile system. If you need one of them, you'll have to set up your own
671version.
672
673
674[build/soong/Changes.md]: https://android.googlesource.com/platform/build/soong/+/master/Changes.md
Dan Willemsen66d21d42020-01-27 19:26:02 -0800675[build/soong/docs/best_practices.md#headers]: https://android.googlesource.com/platform/build/soong/+/master/docs/best_practices.md#headers
Dan Willemsen77338622017-11-08 16:39:18 -0800676[external/fonttools/Lib/fontTools/Android.bp]: https://android.googlesource.com/platform/external/fonttools/+/master/Lib/fontTools/Android.bp
677[frameworks/base/Android.bp]: https://android.googlesource.com/platform/frameworks/base/+/master/Android.bp
678[frameworks/base/data/fonts/Android.mk]: https://android.googlesource.com/platform/frameworks/base/+/master/data/fonts/Android.mk
Yifan Hong88adfc62019-10-11 15:52:44 -0700679[hardware/interfaces/health/1.0/README.md]: https://android.googlesource.com/platform/hardware/interfaces/+/master/health/1.0/README.md
680[hardware/interfaces/health/2.1/README.md]: https://android.googlesource.com/platform/hardware/interfaces/+/master/health/2.1/README.md