blob: 6d20173c199063706fa645dc58316aa685fd15ec [file] [log] [blame] [view]
Dan Willemsen77338622017-11-08 16:39:18 -08001# Build System Changes for Android.mk Writers
2
Liz Kammerbc4f48c2023-05-18 18:20:22 +00003## Perform validation of Soong plugins
4
5Each Soong plugin will require manual work to migrate to Bazel. In order to
6minimize the manual work outside of build/soong, we are restricting plugins to
7those that exist today and those in vendor or hardware directories.
8
9If you need to extend the build system via a plugin, please reach out to the
10build team via email android-building@googlegroups.com (external) for any
11questions, or see [go/soong](http://go/soong) (internal).
12
13To omit the validation, `BUILD_BROKEN_PLUGIN_VALIDATION` expects a list of
14plugins to omit from the validation.
15
Cole Faust189905b2022-09-09 19:41:47 -070016## Python 2 to 3 migration
17
18The path set when running builds now makes the `python` executable point to python 3,
19whereas on previous versions it pointed to python 2. If you still have python 2 scripts,
20you can change the shebang line to use `python2` explicitly. This only applies for
21scripts run directly from makefiles, or from soong genrules. This behavior can be
22temporarily overridden by setting the `BUILD_BROKEN_PYTHON_IS_PYTHON2` environment
23variable to `true`. It's only an environment variable and not a product config variable
24because product config sometimes calls python code.
25
26In addition, `python_*` soong modules no longer allow python 2. This can be temporarily
27overridden by setting the `BUILD_BROKEN_USES_SOONG_PYTHON2_MODULES` product configuration
28variable to `true`.
29
30Python 2 is slated for complete removal in V.
Trevor Radcliffec9e72662022-08-15 20:08:20 +000031## Stop referencing sysprop_library directly from cc modules
32
33For the migration to Bazel, we are no longer mapping sysprop_library targets
34to their generated `cc_library` counterparts when dependning on them from a
35cc module. Instead, directly depend on the generated module by prefixing the
36module name with `lib`. For example, depending on the following module:
37
38```
39sysprop_library {
40 name: "foo",
41 srcs: ["foo.sysprop"],
42}
43```
44
45from a module named `bar` can be done like so:
46
47```
48cc_library {
49 name: "bar",
50 srcs: ["bar.cc"],
51 deps: ["libfoo"],
52}
53```
54
55Failure to do this will result in an error about a missing variant.
56
Vinh Tran3fae6fe2022-06-21 12:00:02 -040057## Gensrcs starts disallowing depfile property
58
59To migrate all gensrcs to Bazel, we are restricting the use of depfile property
60because Bazel requires specifying the dependencies directly.
61
62To fix existing uses, remove depfile and directly specify all the dependencies
63in .bp files. For example:
64
65```
66gensrcs {
67 name: "framework-cppstream-protos",
68 tools: [
69 "aprotoc",
70 "protoc-gen-cppstream",
71 ],
72 cmd: "mkdir -p $(genDir)/$(in) " +
73 "&& $(location aprotoc) " +
74 " --plugin=$(location protoc-gen-cppstream) " +
75 " -I . " +
76 " $(in) ",
77 srcs: [
78 "bar.proto",
79 ],
80 output_extension: "srcjar",
81}
82```
83where `bar.proto` imports `external.proto` would become
84
85```
86gensrcs {
87 name: "framework-cppstream-protos",
88 tools: [
89 "aprotoc",
90 "protoc-gen-cpptream",
91 ],
92 tool_files: [
93 "external.proto",
94 ],
95 cmd: "mkdir -p $(genDir)/$(in) " +
96 "&& $(location aprotoc) " +
97 " --plugin=$(location protoc-gen-cppstream) " +
98 " $(in) ",
99 srcs: [
100 "bar.proto",
101 ],
102 output_extension: "srcjar",
103}
104```
105as in https://android-review.googlesource.com/c/platform/frameworks/base/+/2125692/.
106
107`BUILD_BROKEN_DEPFILE` can be used to allowlist usage of depfile in `gensrcs`.
108
109If `depfile` is needed for generating javastream proto, `java_library` with `proto.type`
110set `stream` is the alternative solution. Sees
111https://android-review.googlesource.com/c/platform/packages/modules/Permission/+/2118004/
112for an example.
113
Liz Kammer4065e5b2022-01-31 16:16:06 -0500114## Genrule starts disallowing directory inputs
115
116To better specify the inputs to the build, we are restricting use of directories
117as inputs to genrules.
118
119To fix existing uses, change inputs to specify the inputs and update the command
120accordingly. For example:
121
122```
123genrule: {
124 name: "foo",
125 srcs: ["bar"],
126 cmd: "cp $(location bar)/*.xml $(gendir)",
127 ...
128}
129```
130
131would become
132
133```
134genrule: {
135 name: "foo",
136 srcs: ["bar/*.xml"],
137 cmd: "cp $(in) $(gendir)",
138 ...
139}
Andrew Walbran9b255832022-03-09 14:51:51 +0000140```
Liz Kammer4065e5b2022-01-31 16:16:06 -0500141
142`BUILD_BROKEN_INPUT_DIR_MODULES` can be used to allowlist specific directories
143with genrules that have input directories.
144
Ulya Trafimovich4b4fd162021-03-31 11:49:53 +0100145## Dexpreopt starts enforcing `<uses-library>` checks (for Java modules)
146
147In order to construct correct class loader context for dexpreopt, build system
148needs to know about the shared library dependencies of Java modules listed in
149the `<uses-library>` tags in the manifest. Since the build system does not have
150access to the manifest contents, that information must be present in the build
151files. In simple cases Soong is able to infer it from its knowledge of Java SDK
152libraries and the `libs` property in Android.bp, but in more complex cases it is
153necessary to add the missing information in Android.bp/Android.mk manually.
154
155To specify a list of libraries for a given modules, use:
156
157* Android.bp properties: `uses_libs`, `optional_uses_libs`
158* Android.mk variables: `LOCAL_USES_LIBRARIES`, `LOCAL_OPTIONAL_USES_LIBRARIES`
159
160If a library is in `libs`, it usually should *not* be added to the above
161properties, and Soong should be able to infer the `<uses-library>` tag. But
162sometimes a library also needs additional information in its
163Android.bp/Android.mk file (e.g. when it is a `java_library` rather than a
164`java_sdk_library`, or when the library name is different from its module name,
165or when the module is defined in Android.mk rather than Android.bp). In such
166cases it is possible to tell the build system that the library provides a
167`<uses-library>` with a given name (however, this is discouraged and will be
168deprecated in the future, and it is recommended to fix the underlying problem):
169
170* Android.bp property: `provides_uses_lib`
171* Android.mk variable: `LOCAL_PROVIDES_USES_LIBRARY`
172
173It is possible to disable the check on a per-module basis. When doing that it is
174also recommended to disable dexpreopt, as disabling a failed check will result
175in incorrect class loader context recorded in the .odex file, which will cause
176class loader context mismatch and dexopt at first boot.
177
178* Android.bp property: `enforce_uses_lib`
179* Android.mk variable: `LOCAL_ENFORCE_USES_LIBRARIES`
180
181Finally, it is possible to globally disable the check:
182
183* For a given product: `PRODUCT_BROKEN_VERIFY_USES_LIBRARIES := true`
184* On the command line: `RELAX_USES_LIBRARY_CHECK=true`
185
186The environment variable overrides the product variable, so it is possible to
187disable the check for a product, but quickly re-enable it for a local build.
188
Yo Chiang64faf882020-07-15 18:35:12 +0800189## `LOCAL_REQUIRED_MODULES` requires listed modules to exist {#BUILD_BROKEN_MISSING_REQUIRED_MODULES}
190
191Modules listed in `LOCAL_REQUIRED_MODULES`, `LOCAL_HOST_REQUIRED_MODULES` and
192`LOCAL_TARGET_REQUIRED_MODULES` need to exist unless `ALLOW_MISSING_DEPENDENCIES`
193is set.
194
195To temporarily relax missing required modules check, use:
196
197`BUILD_BROKEN_MISSING_REQUIRED_MODULES := true`
198
Jiyong Park0b4fccb2020-06-26 17:38:00 +0900199## Changes in system properties settings
200
201### Product variables
202
203System properties for each of the partition is supposed to be set via following
204product config variables.
205
Donghyun Jo8c65ef62021-03-02 08:51:03 +0900206For system partition,
Jiyong Park0b4fccb2020-06-26 17:38:00 +0900207
Donghyun Jo8c65ef62021-03-02 08:51:03 +0900208* `PRODUCT_SYSTEM_PROPERTIES`
Jiyong Park0b4fccb2020-06-26 17:38:00 +0900209* `PRODUCT_SYSTEM_DEFAULT_PROPERTIES` is highly discouraged. Will be deprecated.
210
211For vendor partition,
212
213* `PRODUCT_VENDOR_PROPERTIES`
214* `PRODUCT_PROPERTY_OVERRIDES` is highly discouraged. Will be deprecated.
215* `PRODUCT_DEFAULT_PROPERTY_OVERRIDES` is also discouraged. Will be deprecated.
216
217For odm partition,
218
219* `PRODUCT_ODM_PROPERTIES`
220
221For system_ext partition,
222
223* `PRODUCT_SYSTEM_EXT_PROPERTIES`
224
225For product partition,
226
227* `PRODUCT_PRODUCT_PROPERTIES`
228
229### Duplication is not allowed within a partition
230
231For each partition, having multiple sysprop assignments for the same name is
232prohibited. For example, the following will now trigger an error:
233
234`PRODUCT_VENDOR_PROPERTIES += foo=true foo=false`
235
236Having duplication across partitions are still allowed. So, the following is
237not an error:
238
239`PRODUCT_VENDOR_PROPERTIES += foo=true`
240`PRODUCT_SYSTEM_PROPERTIES += foo=false`
241
242In that case, the final value is determined at runtime. The precedence is
243
244* product
245* odm
246* vendor
247* system_ext
248* system
249
250So, `foo` becomes `true` because vendor has higher priority than system.
251
252To temporarily turn the build-time restriction off, use
253
254`BUILD_BROKEN_DUP_SYSPROP := true`
255
256### Optional assignments
257
258System properties can now be set as optional using the new syntax:
259
260`name ?= value`
261
262Then the system property named `name` gets the value `value` only when there
263is no other non-optional assignments having the same name. For example, the
264following is allowed and `foo` gets `true`
265
266`PRODUCT_VENDOR_PROPERTIES += foo=true foo?=false`
267
268Note that the order between the optional and the non-optional assignments
269doesn't matter. The following gives the same result as above.
270
271`PRODUCT_VENDOR_PROPERTIES += foo?=false foo=true`
272
273Optional assignments can be duplicated and in that case their order matters.
274Specifically, the last one eclipses others.
275
276`PRODUCT_VENDOR_PROPERTIES += foo?=apple foo?=banana foo?=mango`
277
278With above, `foo` becomes `mango` since its the last one.
279
280Note that this behavior is different from the previous behavior of preferring
281the first one. To go back to the original behavior for compatability reason,
282use:
283
284`BUILD_BROKEN_DUP_SYSPROP := true`
285
Yo Chiangd4741812020-07-14 15:15:08 +0800286## ELF prebuilts in `PRODUCT_COPY_FILES` {#BUILD_BROKEN_ELF_PREBUILT_PRODUCT_COPY_FILES}
Yo Chiang73d31482020-04-07 15:59:59 +0800287
Yo Chiangd4741812020-07-14 15:15:08 +0800288ELF prebuilts in `PRODUCT_COPY_FILES` that are installed into these paths are an
Yo Chiang73d31482020-04-07 15:59:59 +0800289error:
290
291* `<partition>/bin/*`
292* `<partition>/lib/*`
293* `<partition>/lib64/*`
294
Yo Chiangd4741812020-07-14 15:15:08 +0800295Define prebuilt modules and add them to `PRODUCT_PACKAGES` instead.
Yo Chiang73d31482020-04-07 15:59:59 +0800296To temporarily relax this check and restore the behavior prior to this change,
297set `BUILD_BROKEN_ELF_PREBUILT_PRODUCT_COPY_FILES := true` in `BoardConfig.mk`.
298
Dan Willemsen66d21d42020-01-27 19:26:02 -0800299## COPY_HEADERS usage now produces warnings {#copy_headers}
300
301We've considered `BUILD_COPY_HEADERS`/`LOCAL_COPY_HEADERS` to be deprecated for
302a long time, and the places where it's been able to be used have shrinked over
303the last several releases. Equivalent functionality is not available in Soong.
304
305See the [build/soong/docs/best_practices.md#headers] for more information about
306how best to handle headers in Android.
307
Dan Willemsen2bfffb92020-01-14 10:56:53 -0800308## `m4` is not available on `$PATH`
309
310There is a prebuilt of it available in prebuilts/build-tools, and a make
311variable `M4` that contains the path.
312
313Beyond the direct usage, whenever you use bison or flex directly, they call m4
314behind the scene, so you must set the M4 environment variable (and depend upon
315it for incremental build correctness):
316
317```
318$(intermediates)/foo.c: .KATI_IMPLICIT_OUTPUTS := $(intermediates)/foo.h
319$(intermediates)/foo.c: $(LOCAL_PATH)/foo.y $(M4) $(BISON) $(BISON_DATA)
320 M4=$(M4) $(BISON) ...
321```
322
Dan Willemsen26076252020-01-02 20:08:10 -0800323## Rules executed within limited environment
324
325With `ALLOW_NINJA_ENV=false` (soon to be the default), ninja, and all the
326rules/actions executed within it will only have access to a limited number of
327environment variables. Ninja does not track when environment variables change
328in order to trigger rebuilds, so changing behavior based on arbitrary variables
329is not safe with incremental builds.
330
331Kati and Soong can safely use environment variables, so the expectation is that
332you'd embed any environment variables that you need to use within the command
333line generated by those tools. See the [export section](#export_keyword) below
334for examples.
335
336For a temporary workaround, you can set `ALLOW_NINJA_ENV=true` in your
337environment to restore the previous behavior, or set
338`BUILD_BROKEN_NINJA_USES_ENV_VAR := <var> <var2> ...` in your `BoardConfig.mk`
339to allow specific variables to be passed through until you've fixed the rules.
340
Dan Willemsen0cb422f2019-11-25 17:51:18 -0800341## LOCAL_C_INCLUDES outside the source/output trees are an error {#BUILD_BROKEN_OUTSIDE_INCLUDE_DIRS}
342
343Include directories are expected to be within the source tree (or in the output
344directory, generated during the build). This has been checked in some form
345since Oreo, but now has better checks.
346
347There's now a `BUILD_BROKEN_OUTSIDE_INCLUDE_DIRS` variable, that when set, will
348turn these errors into warnings temporarily. I don't expect this to last more
349than a release, since they're fairly easy to clean up.
350
351Neither of these cases are supported by Soong, and will produce errors when
352converting your module.
353
354### Absolute paths
355
356This has been checked since Oreo. The common reason to hit this is because a
357makefile is calculating a path, and ran abspath/realpath/etc. This is a problem
358because it makes your build non-reproducible. It's very unlikely that your
359source path is the same on every machine.
360
361### Using `../` to leave the source/output directories
362
363This is the new check that has been added. In every case I've found, this has
364been a mistake in the Android.mk -- assuming that `LOCAL_C_INCLUDES` (which is
365relative to the top of the source tree) acts like `LOCAL_SRC_FILES` (which is
366relative to `LOCAL_PATH`).
367
368Since this usually isn't a valid path, you can almost always just remove the
369offending line.
370
371
Dan Willemsen26076252020-01-02 20:08:10 -0800372## `BOARD_HAL_STATIC_LIBRARIES` and `LOCAL_HAL_STATIC_LIBRARIES` are obsolete {#BOARD_HAL_STATIC_LIBRARIES}
Yifan Hong88adfc62019-10-11 15:52:44 -0700373
374Define proper HIDL / Stable AIDL HAL instead.
375
376* For libhealthd, use health HAL. See instructions for implementing
377 health HAL:
378
379 * [hardware/interfaces/health/2.1/README.md] for health 2.1 HAL (recommended)
380 * [hardware/interfaces/health/1.0/README.md] for health 1.0 HAL
381
382* For libdumpstate, use at least Dumpstate HAL 1.0.
383
Tao Bao84f88a42019-05-28 16:30:18 -0700384## PRODUCT_STATIC_BOOT_CONTROL_HAL is obsolete {#PRODUCT_STATIC_BOOT_CONTROL_HAL}
385
386`PRODUCT_STATIC_BOOT_CONTROL_HAL` was the workaround to allow sideloading with
387statically linked boot control HAL, before shared library HALs were supported
388under recovery. Android Q has added such support (HALs will be loaded in
389passthrough mode), and the workarounds are being removed. Targets should build
390and install the recovery variant of boot control HAL modules into recovery
391image, similar to the ones installed for normal boot. See the change to
392crosshatch for example of this:
393
394* [device/google/crosshatch/bootctrl/Android.bp] for `bootctrl.sdm845` building
395 rules
396* [device/google/crosshatch/device.mk] for installing `bootctrl.sdm845.recovery`
397 and `android.hardware.boot@1.0-impl.recovery` into recovery image
398
399[device/google/crosshatch/bootctrl/Android.bp]: https://android.googlesource.com/device/google/crosshatch/+/master/bootctrl/Android.bp
400[device/google/crosshatch/device.mk]: https://android.googlesource.com/device/google/crosshatch/+/master/device.mk
401
Dan Willemsen695849e2019-04-17 12:25:09 -0700402## Deprecation of `BUILD_*` module types
403
404See [build/make/Deprecation.md](Deprecation.md) for the current status.
405
Dan Willemsen3d05a082019-02-22 23:06:03 +0000406## `PRODUCT_HOST_PACKAGES` split from `PRODUCT_PACKAGES` {#PRODUCT_HOST_PACKAGES}
407
408Previously, adding a module to `PRODUCT_PACKAGES` that supported both the host
409and the target (`host_supported` in Android.bp; two modules with the same name
410in Android.mk) would cause both to be built and installed. In many cases you
411only want either the host or target versions to be built/installed by default,
412and would be over-building with both. So `PRODUCT_PACKAGES` will be changing to
413just affect target modules, while `PRODUCT_HOST_PACKAGES` is being added for
414host modules.
415
416Functional differences between `PRODUCT_PACKAGES` and `PRODUCT_HOST_PACKAGES`:
417
418* `PRODUCT_HOST_PACKAGES` does not have `_ENG`/`_DEBUG` variants, as that's a
419 property of the target, not the host.
420* `PRODUCT_HOST_PACKAGES` does not support `LOCAL_MODULE_OVERRIDES`.
421* `PRODUCT_HOST_PACKAGES` requires listed modules to exist, and be host
422 modules. (Unless `ALLOW_MISSING_DEPENDENCIES` is set)
423
424This is still an active migration, so currently it still uses
425`PRODUCT_PACKAGES` to make installation decisions, but verifies that if we used
426`PRODUCT_HOST_PACKAGES`, it would trigger installation for all of the same host
427packages. This check ignores shared libraries, as those are not normally
428necessary in `PRODUCT_*PACKAGES`, and tended to be over-built (especially the
42932-bit variants).
430
431Future changes will switch installation decisions to `PRODUCT_HOST_PACKAGES`
432for host modules, error when there's a host-only module in `PRODUCT_PACKAGES`,
433and do some further cleanup where `LOCAL_REQUIRED_MODULES` are still merged
434between host and target modules with the same name.
435
Dan Willemsen46267cb2019-01-25 14:35:58 -0800436## `*.c.arm` / `*.cpp.arm` deprecation {#file_arm}
437
438In Android.mk files, you used to be able to change LOCAL_ARM_MODE for each
439source file by appending `.arm` to the end of the filename in
440`LOCAL_SRC_FILES`.
441
442Soong does not support this uncommonly used behavior, instead expecting those
443files to be split out into a separate static library that chooses `arm` over
444`thumb` for the entire library. This must now also be done in Android.mk files.
445
Dan Willemsenf2646902019-01-25 16:54:37 -0800446## Windows cross-compiles no longer supported in Android.mk
447
448Modules that build for Windows (our only `HOST_CROSS` OS currently) must now be
449defined in `Android.bp` files.
450
Dan Willemsene048f7e2019-02-09 18:58:36 -0800451## `LOCAL_MODULE_TAGS := eng debug` are obsolete {#LOCAL_MODULE_TAGS}
Dan Willemsen9569ddd2019-01-22 19:38:56 -0800452
Dan Willemsene048f7e2019-02-09 18:58:36 -0800453`LOCAL_MODULE_TAGS` value `eng` and `debug` are now obsolete. They allowed
Dan Willemsen9569ddd2019-01-22 19:38:56 -0800454modules to specify that they should always be installed on `-eng`, or `-eng`
455and `-userdebug` builds. This conflicted with the ability for products to
456specify which modules should be installed, effectively making it impossible to
457build a stripped down product configuration that did not include those modules.
458
459For the equivalent functionality, specify the modules in `PRODUCT_PACKAGES_ENG`
460or `PRODUCT_PACKAGES_DEBUG` in the appropriate product makefiles.
461
462Core android packages like `su` got added to the list in
463`build/make/target/product/base_system.mk`, but for device-specific modules
464there are often better base product makefiles to use instead.
465
Dan Willemsen06364282019-01-02 14:32:54 -0800466## `USER` deprecation {#USER}
467
468`USER` will soon be `nobody` in many cases due to the addition of a sandbox
469around the Android build. Most of the time you shouldn't need to know the
470identity of the user running the build, but if you do, it's available in the
471make variable `BUILD_USERNAME` for now.
472
473Similarly, the `hostname` tool will also be returning a more consistent value
474of `android-build`. The real value is available as `BUILD_HOSTNAME`.
475
Dan Willemsen6dbb33d2018-10-21 19:41:49 -0700476## `BUILD_NUMBER` removal from Android.mk {#BUILD_NUMBER}
477
478`BUILD_NUMBER` should not be used directly in Android.mk files, as it would
479trigger them to be re-read every time the `BUILD_NUMBER` changes (which it does
480on every build server build). If possible, just remove the use so that your
481builds are more reproducible. If you do need it, use `BUILD_NUMBER_FROM_FILE`:
482
483``` make
484$(LOCAL_BUILT_MODULE):
485 mytool --build_number $(BUILD_NUMBER_FROM_FILE) -o $@
486```
487
488That will expand out to a subshell that will read the current `BUILD_NUMBER`
489whenever it's run. It will not re-run your command if the build number has
490changed, so incremental builds will have the build number from the last time
491the particular output was rebuilt.
492
Dan Willemsen78c40be2018-10-17 16:50:49 -0700493## `DIST_DIR`, `dist_goal`, and `dist-for-goals` {#dist}
494
495`DIST_DIR` and `dist_goal` are no longer available when reading Android.mk
496files (or other build tasks). Always use `dist-for-goals` instead, which takes
497a PHONY goal, and a list of files to copy to `$DIST_DIR`. Whenever `dist` is
498specified, and the goal would be built (either explicitly on the command line,
499or as a dependency of something on the command line), that file will be copied
500into `$DIST_DIR`. For example,
501
502``` make
503$(call dist-for-goals,foo,bar/baz)
504```
505
506will copy `bar/baz` into `$DIST_DIR/baz` when `m foo dist` is run.
507
508#### Renames during copy
509
510Instead of specifying just a file, a destination name can be specified,
511including subdirectories:
512
513``` make
514$(call dist-for-goals,foo,bar/baz:logs/foo.log)
515```
516
517will copy `bar/baz` into `$DIST_DIR/logs/foo.log` when `m foo dist` is run.
518
Dan Willemsen5fb16a62018-09-04 16:23:14 -0700519## `.PHONY` rule enforcement {#phony_targets}
520
521There are several new warnings/errors meant to ensure the proper use of
522`.PHONY` targets in order to improve the speed and reliability of incremental
523builds.
524
525`.PHONY`-marked targets are often used as shortcuts to provide "friendly" names
526for real files to be built, but any target marked with `.PHONY` is also always
527considered dirty, needing to be rebuilt every build. This isn't a problem for
528aliases or one-off user-requested operations, but if real builds steps depend
529on a `.PHONY` target, it can get quite expensive for what should be a tiny
530build.
531
532``` make
533...mk:42: warning: PHONY target "out/.../foo" looks like a real file (contains a "/")
534```
535
536Between this warning and the next, we're requiring that `.PHONY` targets do not
537have "/" in them, and real file targets do have a "/". This makes it more
538obvious when reading makefiles what is happening, and will help the build
539system differentiate these in the future too.
540
541``` make
542...mk:42: warning: writing to readonly directory: "kernel-modules"
543```
544
545This warning will show up for one of two reasons:
546
5471. The target isn't intended to be a real file, and should be marked with
548 `.PHONY`. This would be the case for this example.
5492. The target is a real file, but it's outside the output directories. All
550 outputs from the build system should be within the output directory,
551 otherwise `m clean` is unable to clean the build, and future builds may not
552 work properly.
553
554``` make
555...mk:42: warning: real file "out/.../foo" depends on PHONY target "buildbins"
556```
557
558If the first target isn't intended to be a real file, then it should be marked
559with `.PHONY`, which will satisfy this warning. This isn't the case for this
560example, as we require `.PHONY` targets not to have '/' in them.
561
562If the second (PHONY) target is a real file, it may unnecessarily be marked
563with `.PHONY`.
564
565### `.PHONY` and calling other build systems
566
567One common pattern (mostly outside AOSP) that we've seen hit these warning is
568when building with external build systems (firmware, bootloader, kernel, etc).
569Those are often marked as `.PHONY` because the Android build system doesn't
570have enough dependencies to know when to run the other build system again
571during an incremental build.
572
573We recommend to build these outside of Android, and deliver prebuilts into the
574Android tree instead of decreasing the speed and reliability of the incremental
575Android build.
576
577In cases where that's not desired, to preserve the speed of Android
578incrementals, over-specifying dependencies is likely a better option than
579marking it with `.PHONY`:
580
581``` make
582out/target/.../zImage: $(sort $(shell find -L $(KERNEL_SRCDIR)))
583 ...
584```
585
586For reliability, many of these other build systems do not guarantee the same
587level of incremental build assurances as the Android Build is attempting to do
588-- without custom checks, Make doesn't rebuild objects when CFLAGS change, etc.
589In order to fix this, our recommendation is to do clean builds for each of
590these external build systems every time anything they rely on changes. For
591relatively smaller builds (like the kernel), this may be reasonable as long as
592you're not trying to actively debug the kernel.
593
594## `export` and `unexport` deprecation {#export_keyword}
Dan Willemsen8b9c3cc2018-02-27 02:15:32 -0800595
Dan Willemsen3a1072a2019-05-14 22:03:58 -0700596The `export` and `unexport` keywords are obsolete, and will throw errors when
597used.
Dan Willemsen8b9c3cc2018-02-27 02:15:32 -0800598
Dan Willemsen8b9c3cc2018-02-27 02:15:32 -0800599Device specific configuration should not be able to affect common core build
600steps -- we're looking at triggering build steps to be invalidated if the set
601of environment variables they can access changes. If device specific
602configuration is allowed to change those, switching devices with the same
603output directory could become significantly more expensive than it already can
604be.
605
Dan Willemsen3a1072a2019-05-14 22:03:58 -0700606If used during Android.mk files, and later tasks, it is increasingly likely
607that they are being used incorrectly. Attempting to change the environment for
608a single build step, and instead setting it for hundreds of thousands.
Dan Willemsen8b9c3cc2018-02-27 02:15:32 -0800609
610It is not recommended to just move the environment variable setting outside of
611the build (in vendorsetup.sh, or some other configuration script or wrapper).
612We expect to limit the environment variables that the build respects in the
613future, others will be cleared. (There will be methods to get custom variables
614into the build, just not to every build step)
615
616Instead, write the export commands into the rule command lines themselves:
617
618``` make
619$(intermediates)/generated_output.img:
620 rm -rf $@
621 export MY_ENV_A="$(MY_A)"; make ...
622```
623
624If you want to set many environment variables, and/or use them many times,
625write them out to a script and source the script:
626
627``` make
628envsh := $(intermediates)/env.sh
629$(envsh):
630 rm -rf $@
631 echo 'export MY_ENV_A="$(MY_A)"' >$@
632 echo 'export MY_ENV_B="$(MY_B)"' >>$@
633
634$(intermediates)/generated_output.img: PRIVATE_ENV := $(envsh)
635$(intermediates)/generated_output.img: $(envsh) a/b/c/package.sh
636 rm -rf $@
637 source $(PRIVATE_ENV); make ...
638 source $(PRIVATE_ENV); a/b/c/package.sh ...
639```
640
Dan Willemsen5f76fc02018-06-21 21:42:29 -0700641## Implicit make rules are obsolete {#implicit_rules}
Dan Willemsen62db0f02018-06-16 09:37:13 -0700642
643Implicit rules look something like the following:
644
645``` make
646$(TARGET_OUT_SHARED_LIBRARIES)/%_vendor.so: $(TARGET_OUT_SHARED_LIBRARIES)/%.so
647 ...
648
649%.o : %.foo
650 ...
651```
652
Dan Willemsen5f76fc02018-06-21 21:42:29 -0700653These 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 -0700654
655``` make
656libs := $(foreach lib,libfoo libbar,$(TARGET_OUT_SHARED_LIBRARIES)/$(lib)_vendor.so)
657$(libs): %_vendor.so: %.so
658 ...
659
660files := $(wildcard $(LOCAL_PATH)/*.foo)
661gen := $(patsubst $(LOCAL_PATH)/%.foo,$(intermediates)/%.o,$(files))
662$(gen): %.o : %.foo
663 ...
664```
665
Dan Willemsenac926592018-06-11 22:28:00 -0700666## Removing '/' from Valid Module Names {#name_slash}
667
668The build system uses module names in path names in many places. Having an
669extra '/' or '../' being inserted can cause problems -- and not just build
670breaks, but stranger invalid behavior.
671
672In every case we've seen, the fix is relatively simple: move the directory into
673`LOCAL_MODULE_RELATIVE_PATH` (or `LOCAL_MODULE_PATH` if you're still using it).
674If this causes multiple modules to be named the same, use unique module names
675and `LOCAL_MODULE_STEM` to change the installed file name:
676
677``` make
678include $(CLEAR_VARS)
679LOCAL_MODULE := ver1/code.bin
680LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/firmware
681...
682include $(BUILD_PREBUILT)
683
684include $(CLEAR_VARS)
685LOCAL_MODULE := ver2/code.bin
686LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/firmware
687...
688include $(BUILD_PREBUILT)
689```
690
691Can be rewritten as:
692
693```
694include $(CLEAR_VARS)
695LOCAL_MODULE := ver1_code.bin
696LOCAL_MODULE_STEM := code.bin
697LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR)/firmware/ver1
698...
699include $(BUILD_PREBUILT)
700
701include $(CLEAR_VARS)
702LOCAL_MODULE := ver2_code.bin
703LOCAL_MODULE_STEM := code.bin
704LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR)/firmware/ver2
705...
706include $(BUILD_PREBUILT)
707```
708
709You just need to make sure that any other references (`PRODUCT_PACKAGES`,
710`LOCAL_REQUIRED_MODULES`, etc) are converted to the new names.
711
Dan Willemsenbbe6a022018-06-10 14:49:01 -0700712## Valid Module Names {#name}
713
714We've adopted lexical requirements very similar to [Bazel's
715requirements](https://docs.bazel.build/versions/master/build-ref.html#name) for
716target names. Valid characters are `a-z`, `A-Z`, `0-9`, and the special
Dan Willemsenac926592018-06-11 22:28:00 -0700717characters `_.+-=,@~`. This currently applies to `LOCAL_PACKAGE_NAME`,
Dan Willemsenbbe6a022018-06-10 14:49:01 -0700718`LOCAL_MODULE`, and `LOCAL_MODULE_SUFFIX`, and `LOCAL_MODULE_STEM*`.
719
720Many other characters already caused problems if you used them, so we don't
721expect this to have a large effect.
722
Dan Willemsen5039ef42018-05-18 11:00:17 -0700723## PATH Tools {#PATH_Tools}
724
725The build has started restricting the external host tools usable inside the
726build. This will help ensure that build results are reproducible across
727different machines, and catch mistakes before they become larger issues.
728
729To start with, this includes replacing the $PATH with our own directory of
730tools, mirroring that of the host PATH. The only difference so far is the
731removal of the host GCC tools. Anything that is not explicitly in the
732configuration as allowed will continue functioning, but will generate a log
733message. This is expected to become more restrictive over time.
734
735The configuration is located in build/soong/ui/build/paths/config.go, and
736contains all the common tools in use in many builds. Anything not in that list
737will currently print a warning in the `$OUT_DIR/soong.log` file, including the
738command and arguments used, and the process tree in order to help locate the
739usage.
740
741In order to fix any issues brought up by these checks, the best way to fix them
742is to use tools checked into the tree -- either as prebuilts, or building them
743as host tools during the build.
744
745As a temporary measure, you can set `TEMPORARY_DISABLE_PATH_RESTRICTIONS=true`
746in your environment to temporarily turn off the error checks and allow any tool
747to be used (with logging). Beware that GCC didn't work well with the interposer
748used for logging, so this may not help in all cases.
749
Dan Willemsen79fd6962017-11-28 22:32:05 -0800750## Deprecating / obsoleting envsetup.sh variables in Makefiles
Dan Willemsen77338622017-11-08 16:39:18 -0800751
752It is not required to source envsetup.sh before running a build. Many scripts,
753including a majority of our automated build systems, do not do so. Make will
754transparently make every environment variable available as a make variable.
755This means that relying on environment variables only set up in envsetup.sh will
756produce different output for local users and scripted users.
757
758Many of these variables also include absolute path names, which we'd like to
759keep out of the generated files, so that you don't need to do a full rebuild if
760you move the source tree.
761
762To fix this, we're marking the variables that are set in envsetup.sh as
763deprecated in the makefiles. This will trigger a warning every time one is read
Dan Willemsen79fd6962017-11-28 22:32:05 -0800764(or written) inside Kati. Once all the warnings have been removed for a
765particular variable, we'll switch it to obsolete, and any references will become
766errors.
Dan Willemsen77338622017-11-08 16:39:18 -0800767
768### envsetup.sh variables with make equivalents
769
770| instead of | use |
771|--------------------------------------------------------------|----------------------|
Yasuhiro Kubotacd301f62018-10-09 15:51:23 +0900772| OUT {#OUT} | PRODUCT_OUT |
Dan Willemsen77338622017-11-08 16:39:18 -0800773| ANDROID_HOST_OUT {#ANDROID_HOST_OUT} | HOST_OUT |
774| ANDROID_PRODUCT_OUT {#ANDROID_PRODUCT_OUT} | PRODUCT_OUT |
775| ANDROID_HOST_OUT_TESTCASES {#ANDROID_HOST_OUT_TESTCASES} | HOST_OUT_TESTCASES |
776| ANDROID_TARGET_OUT_TESTCASES {#ANDROID_TARGET_OUT_TESTCASES} | TARGET_OUT_TESTCASES |
777
778All of the make variables may be relative paths from the current directory, or
779absolute paths if the output directory was specified as an absolute path. If you
780need an absolute variable, convert it to absolute during a rule, so that it's
781not expanded into the generated ninja file:
782
783``` make
784$(PRODUCT_OUT)/gen.img: my/src/path/gen.sh
785 export PRODUCT_OUT=$$(cd $(PRODUCT_OUT); pwd); cd my/src/path; ./gen.sh -o $${PRODUCT_OUT}/gen.img
786```
787
788### ANDROID_BUILD_TOP {#ANDROID_BUILD_TOP}
789
790In Android.mk files, you can always assume that the current directory is the
791root of the source tree, so this can just be replaced with '.' (which is what
792$TOP is hardcoded to), or removed entirely. If you need an absolute path, see
793the instructions above.
794
795### Stop using PATH directly {#PATH}
796
797This isn't only set by envsetup.sh, but it is modified by it. Due to that it's
798rather easy for this to change between different shells, and it's not ideal to
799reread the makefiles every time this changes.
800
801In most cases, you shouldn't need to touch PATH at all. When you need to have a
802rule reference a particular binary that's part of the source tree or outputs,
803it's preferrable to just use the path to the file itself (since you should
804already be adding that as a dependency).
805
806Depending on the rule, passing the file path itself may not be feasible due to
807layers of unchangable scripts/binaries. In that case, be sure to add the
808dependency, but modify the PATH within the rule itself:
809
810``` make
811$(TARGET): myscript my/path/binary
812 PATH=my/path:$$PATH myscript -o $@
813```
814
815### Stop using PYTHONPATH directly {#PYTHONPATH}
816
817Like PATH, this isn't only set by envsetup.sh, but it is modified by it. Due to
818that it's rather easy for this to change between different shells, and it's not
819ideal to reread the makefiles every time.
820
821The best solution here is to start switching to Soong's python building support,
822which packages the python interpreter, libraries, and script all into one file
823that no longer needs PYTHONPATH. See fontchain_lint for examples of this:
824
825* [external/fonttools/Lib/fontTools/Android.bp] for python_library_host
826* [frameworks/base/Android.bp] for python_binary_host
827* [frameworks/base/data/fonts/Android.mk] to execute the python binary
828
829If you still need to use PYTHONPATH, do so within the rule itself, just like
830path:
831
832``` make
833$(TARGET): myscript.py $(sort $(shell find my/python/lib -name '*.py'))
834 PYTHONPATH=my/python/lib:$$PYTHONPATH myscript.py -o $@
835```
Yifan Hong97de88c2017-12-12 18:01:09 -0800836### Stop using PRODUCT_COMPATIBILITY_MATRIX_LEVEL_OVERRIDE directly {#PRODUCT_COMPATIBILITY_MATRIX_LEVEL_OVERRIDE}
837
838Specify Framework Compatibility Matrix Version in device manifest by adding a `target-level`
839attribute to the root element `<manifest>`. If `PRODUCT_COMPATIBILITY_MATRIX_LEVEL_OVERRIDE`
840is 26 or 27, you can add `"target-level"="1"` to your device manifest instead.
Dan Willemsen77338622017-11-08 16:39:18 -0800841
Stephen Hines178cf8e2018-01-11 11:54:48 -0800842### Stop using USE_CLANG_PLATFORM_BUILD {#USE_CLANG_PLATFORM_BUILD}
843
844Clang is the default and only supported Android compiler, so there is no reason
845for this option to exist.
846
Alixa9324f02022-04-22 03:49:45 +0000847### Stop using clang property
848
Cole Faust189905b2022-09-09 19:41:47 -0700849The clang property has been deleted from Soong. To fix any build errors, remove the clang
Alixa9324f02022-04-22 03:49:45 +0000850property from affected Android.bp files using bpmodify.
851
852
853``` make
854go run bpmodify.go -w -m=module_name -remove-property=true -property=clang filepath
855```
856
Alix7a4d5392022-08-25 14:17:18 +0000857`BUILD_BROKEN_CLANG_PROPERTY` can be used as temporarily workaround
Alix Espino38e07f12022-09-14 19:10:51 +0000858
859
860### Stop using clang_cflags and clang_asflags
861
862clang_cflags and clang_asflags are deprecated.
863To fix any build errors, use bpmodify to either
864 - move the contents of clang_asflags/clang_cflags into asflags/cflags or
865 - delete clang_cflags/as_flags as necessary
866
867To Move the contents:
868``` make
869go run bpmodify.go -w -m=module_name -move-property=true -property=clang_cflags -new-location=cflags filepath
870```
871
872To Delete:
873``` make
874go run bpmodify.go -w -m=module_name -remove-property=true -property=clang_cflags filepath
875```
876
877`BUILD_BROKEN_CLANG_ASFLAGS` and `BUILD_BROKEN_CLANG_CFLAGS` can be used as temporarily workarounds
878
Dan Willemsen77338622017-11-08 16:39:18 -0800879### Other envsetup.sh variables {#other_envsetup_variables}
880
881* ANDROID_TOOLCHAIN
882* ANDROID_TOOLCHAIN_2ND_ARCH
883* ANDROID_DEV_SCRIPTS
884* ANDROID_EMULATOR_PREBUILTS
885* ANDROID_PRE_BUILD_PATHS
886
887These are all exported from envsetup.sh, but don't have clear equivalents within
888the makefile system. If you need one of them, you'll have to set up your own
889version.
890
Cole Faustcb30a802022-11-07 17:07:25 -0800891## Soong config variables
892
893### Soong config string variables must list all values they can be set to
894
895In order to facilitate the transition to bazel, all soong_config_string_variables
896must only be set to a value listed in their `values` property, or an empty string.
897It is a build error otherwise.
898
899Example Android.bp:
900```
901soong_config_string_variable {
902 name: "my_string_variable",
903 values: [
904 "foo",
905 "bar",
906 ],
907}
908
909soong_config_module_type {
910 name: "my_cc_defaults",
911 module_type: "cc_defaults",
912 config_namespace: "my_namespace",
913 variables: ["my_string_variable"],
914 properties: [
915 "shared_libs",
916 "static_libs",
917 ],
918}
919```
920Product config:
921```
922$(call soong_config_set,my_namespace,my_string_variable,baz) # Will be an error as baz is not listed in my_string_variable's values.
923```
Dan Willemsen77338622017-11-08 16:39:18 -0800924
925[build/soong/Changes.md]: https://android.googlesource.com/platform/build/soong/+/master/Changes.md
Dan Willemsen66d21d42020-01-27 19:26:02 -0800926[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 -0800927[external/fonttools/Lib/fontTools/Android.bp]: https://android.googlesource.com/platform/external/fonttools/+/master/Lib/fontTools/Android.bp
928[frameworks/base/Android.bp]: https://android.googlesource.com/platform/frameworks/base/+/master/Android.bp
929[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 -0700930[hardware/interfaces/health/1.0/README.md]: https://android.googlesource.com/platform/hardware/interfaces/+/master/health/1.0/README.md
931[hardware/interfaces/health/2.1/README.md]: https://android.googlesource.com/platform/hardware/interfaces/+/master/health/2.1/README.md