|
| 1 | +# Build System Changes for Android.mk Writers |
| 2 | + |
| 3 | +## Deprecating envsetup.sh variables in Makefiles |
| 4 | + |
| 5 | +It is not required to source envsetup.sh before running a build. Many scripts, |
| 6 | +including a majority of our automated build systems, do not do so. Make will |
| 7 | +transparently make every environment variable available as a make variable. |
| 8 | +This means that relying on environment variables only set up in envsetup.sh will |
| 9 | +produce different output for local users and scripted users. |
| 10 | + |
| 11 | +Many of these variables also include absolute path names, which we'd like to |
| 12 | +keep out of the generated files, so that you don't need to do a full rebuild if |
| 13 | +you move the source tree. |
| 14 | + |
| 15 | +To fix this, we're marking the variables that are set in envsetup.sh as |
| 16 | +deprecated in the makefiles. This will trigger a warning every time one is read |
| 17 | +(or written) inside Kati. Once all the warnings have been removed, we'll switch |
| 18 | +this to obsolete, and any references will become errors. |
| 19 | + |
| 20 | +### envsetup.sh variables with make equivalents |
| 21 | + |
| 22 | +| instead of | use | |
| 23 | +|--------------------------------------------------------------|----------------------| |
| 24 | +| OUT {#OUT} | OUT_DIR | |
| 25 | +| ANDROID_HOST_OUT {#ANDROID_HOST_OUT} | HOST_OUT | |
| 26 | +| ANDROID_PRODUCT_OUT {#ANDROID_PRODUCT_OUT} | PRODUCT_OUT | |
| 27 | +| ANDROID_HOST_OUT_TESTCASES {#ANDROID_HOST_OUT_TESTCASES} | HOST_OUT_TESTCASES | |
| 28 | +| ANDROID_TARGET_OUT_TESTCASES {#ANDROID_TARGET_OUT_TESTCASES} | TARGET_OUT_TESTCASES | |
| 29 | + |
| 30 | +All of the make variables may be relative paths from the current directory, or |
| 31 | +absolute paths if the output directory was specified as an absolute path. If you |
| 32 | +need an absolute variable, convert it to absolute during a rule, so that it's |
| 33 | +not expanded into the generated ninja file: |
| 34 | + |
| 35 | +``` make |
| 36 | +$(PRODUCT_OUT)/gen.img: my/src/path/gen.sh |
| 37 | + export PRODUCT_OUT=$$(cd $(PRODUCT_OUT); pwd); cd my/src/path; ./gen.sh -o $${PRODUCT_OUT}/gen.img |
| 38 | +``` |
| 39 | + |
| 40 | +### ANDROID_BUILD_TOP {#ANDROID_BUILD_TOP} |
| 41 | + |
| 42 | +In Android.mk files, you can always assume that the current directory is the |
| 43 | +root of the source tree, so this can just be replaced with '.' (which is what |
| 44 | +$TOP is hardcoded to), or removed entirely. If you need an absolute path, see |
| 45 | +the instructions above. |
| 46 | + |
| 47 | +### Stop using PATH directly {#PATH} |
| 48 | + |
| 49 | +This isn't only set by envsetup.sh, but it is modified by it. Due to that it's |
| 50 | +rather easy for this to change between different shells, and it's not ideal to |
| 51 | +reread the makefiles every time this changes. |
| 52 | + |
| 53 | +In most cases, you shouldn't need to touch PATH at all. When you need to have a |
| 54 | +rule reference a particular binary that's part of the source tree or outputs, |
| 55 | +it's preferrable to just use the path to the file itself (since you should |
| 56 | +already be adding that as a dependency). |
| 57 | + |
| 58 | +Depending on the rule, passing the file path itself may not be feasible due to |
| 59 | +layers of unchangable scripts/binaries. In that case, be sure to add the |
| 60 | +dependency, but modify the PATH within the rule itself: |
| 61 | + |
| 62 | +``` make |
| 63 | +$(TARGET): myscript my/path/binary |
| 64 | + PATH=my/path:$$PATH myscript -o $@ |
| 65 | +``` |
| 66 | + |
| 67 | +### Stop using PYTHONPATH directly {#PYTHONPATH} |
| 68 | + |
| 69 | +Like PATH, this isn't only set by envsetup.sh, but it is modified by it. Due to |
| 70 | +that it's rather easy for this to change between different shells, and it's not |
| 71 | +ideal to reread the makefiles every time. |
| 72 | + |
| 73 | +The best solution here is to start switching to Soong's python building support, |
| 74 | +which packages the python interpreter, libraries, and script all into one file |
| 75 | +that no longer needs PYTHONPATH. See fontchain_lint for examples of this: |
| 76 | + |
| 77 | +* [external/fonttools/Lib/fontTools/Android.bp] for python_library_host |
| 78 | +* [frameworks/base/Android.bp] for python_binary_host |
| 79 | +* [frameworks/base/data/fonts/Android.mk] to execute the python binary |
| 80 | + |
| 81 | +If you still need to use PYTHONPATH, do so within the rule itself, just like |
| 82 | +path: |
| 83 | + |
| 84 | +``` make |
| 85 | +$(TARGET): myscript.py $(sort $(shell find my/python/lib -name '*.py')) |
| 86 | + PYTHONPATH=my/python/lib:$$PYTHONPATH myscript.py -o $@ |
| 87 | +``` |
| 88 | + |
| 89 | +### Other envsetup.sh variables {#other_envsetup_variables} |
| 90 | + |
| 91 | +* ANDROID_TOOLCHAIN |
| 92 | +* ANDROID_TOOLCHAIN_2ND_ARCH |
| 93 | +* ANDROID_DEV_SCRIPTS |
| 94 | +* ANDROID_EMULATOR_PREBUILTS |
| 95 | +* ANDROID_PRE_BUILD_PATHS |
| 96 | + |
| 97 | +These are all exported from envsetup.sh, but don't have clear equivalents within |
| 98 | +the makefile system. If you need one of them, you'll have to set up your own |
| 99 | +version. |
| 100 | + |
| 101 | + |
| 102 | +[build/soong/Changes.md]: https://android.googlesource.com/platform/build/soong/+/master/Changes.md |
| 103 | +[external/fonttools/Lib/fontTools/Android.bp]: https://android.googlesource.com/platform/external/fonttools/+/master/Lib/fontTools/Android.bp |
| 104 | +[frameworks/base/Android.bp]: https://android.googlesource.com/platform/frameworks/base/+/master/Android.bp |
| 105 | +[frameworks/base/data/fonts/Android.mk]: https://android.googlesource.com/platform/frameworks/base/+/master/data/fonts/Android.mk |
0 commit comments