| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 1 | Unified Headers |
| 2 | =============== |
| 3 | |
| 4 | [Issue #120](https://github.com/android-ndk/ndk/issues/120) |
| 5 | |
| Dan Albert | 06914ae | 2017-05-17 14:33:22 -0700 | [diff] [blame] | 6 | Before NDK r14, we had a set of libc headers for each API version. In many cases |
| 7 | these headers were incorrect. Many exposed APIs that didn't exist, and others |
| 8 | didn't expose APIs that did. |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 9 | |
| Dan Albert | 06914ae | 2017-05-17 14:33:22 -0700 | [diff] [blame] | 10 | In NDK r14 (as an opt in feature) we unified these into a single set of headers. |
| 11 | In r15 these are used by default. This single header path is used for *every* |
| 12 | platform level. API level guards are handled with `#ifdef`. These headers can |
| 13 | be found in [prebuilts/ndk/headers]. |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 14 | |
| Dan Albert | 06914ae | 2017-05-17 14:33:22 -0700 | [diff] [blame] | 15 | Unified headers are built directly from the Android platform, so they be up to |
| 16 | date and correct (or at the very least, any bugs in the NDK headers will also be |
| 17 | a bug in the platform headers, which means we're much more likely to find them). |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 18 | |
| Dan Albert | b7513db | 2017-05-23 10:46:05 -0700 | [diff] [blame] | 19 | [prebuilts/ndk/headers]: https://android.googlesource.com/platform/prebuilts/ndk/+/dev/platform/sysroot/usr/include |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | Known Issues |
| 23 | ------------ |
| 24 | |
| Dan Albert | 06914ae | 2017-05-17 14:33:22 -0700 | [diff] [blame] | 25 | * Some third-party projects have incorrect feature checks for things like |
| 26 | `epoll_create1`. These are not bugs in the NDK, but rather need to be fixed |
| 27 | in those projects. Boost and libev are projects that we know are affected by |
| 28 | this. We'll be sending some patches to those projects soon. See [bug 302] and |
| 29 | [bug 394]. |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 30 | * Standalone toolchains using GCC are not supported out of the box. To use GCC, |
| Dan Albert | 06914ae | 2017-05-17 14:33:22 -0700 | [diff] [blame] | 31 | pass `-D__ANDROID_API__=$API` when compiling. Note: this is not something we |
| 32 | will be fixing. |
| 33 | |
| 34 | [bug 302]: https://github.com/android-ndk/ndk/issues/302 |
| 35 | [bug 394]: https://github.com/android-ndk/ndk/issues/394 |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 36 | |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 37 | |
| 38 | Using Unified Headers |
| 39 | --------------------- |
| 40 | |
| Dan Albert | c02e422 | 2017-01-31 14:35:57 -0800 | [diff] [blame] | 41 | Unified headers are enabled by default starting with NDK r15. If your build is |
| 42 | not yet compatible with unified headers, you can revert to the deprecated |
| 43 | headers. The way to do so depends on your build system. |
| 44 | |
| 45 | **Warning:** The deprecated headers will be removed from the NDK in r16. If you |
| 46 | need to revert to the deprecated headers, make sure you're working on fixing |
| 47 | your build or filing bugs. |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 48 | |
| 49 | ### ndk-build |
| 50 | |
| Dan Albert | ce6f19a | 2017-08-09 22:33:52 -0700 | [diff] [blame] | 51 | Add the following to your Application.mk: |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 52 | |
| 53 | ```makefile |
| Dan Albert | c02e422 | 2017-01-31 14:35:57 -0800 | [diff] [blame] | 54 | APP_DEPRECATED_HEADERS := true |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 55 | ``` |
| 56 | |
| 57 | ### CMake |
| 58 | |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 59 | ```bash |
| Dan Albert | c02e422 | 2017-01-31 14:35:57 -0800 | [diff] [blame] | 60 | cmake -DANDROID_DEPRECATED_HEADERS=ON ... |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 61 | ``` |
| 62 | |
| 63 | ### Standalone Toolchains |
| 64 | |
| 65 | ```bash |
| Dan Albert | c02e422 | 2017-01-31 14:35:57 -0800 | [diff] [blame] | 66 | $NDK/build/tools/make_standalone_toolchain.py --deprecated-headers ... |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 67 | ``` |
| 68 | |
| Dan Albert | fc2eccc | 2016-12-28 12:42:01 -0800 | [diff] [blame] | 69 | For general standalone toolchain documentation, see |
| 70 | https://developer.android.com/ndk/guides/standalone_toolchain.html |
| 71 | |
| Dan Albert | 9578029 | 2017-04-17 17:32:14 -0700 | [diff] [blame] | 72 | ### Gradle Experimental Plugin |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 73 | |
| Dan Albert | 9578029 | 2017-04-17 17:32:14 -0700 | [diff] [blame] | 74 | ```gradle |
| 75 | model { |
| 76 | android { |
| 77 | ndk { |
| 78 | useUnifiedHeaders false |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | ``` |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 83 | |
| 84 | |
| Elliott Hughes | 3a6ea50 | 2017-01-30 13:33:03 -0800 | [diff] [blame] | 85 | Using unified headers with `configure && make` |
| 86 | ---------------------------------------------- |
| 87 | |
| 88 | If you're trying to build a traditional open-source `configure && make` |
| 89 | style project with the NDK, unified headers are your best choice. The |
| 90 | basic steps look like this (here for 32-bit ARM): |
| 91 | |
| 92 | ```bash |
| 93 | $ $NDK/build/tools/make_standalone_toolchain.py --unified-headers \ |
| 94 | --arch arm --api 21 --install-dir /tmp/ndk-arm-21 |
| 95 | $ cd <project> |
| 96 | $ export CC=/tmp/ndk-arm-21/arm-linux-androideabi-clang |
| 97 | $ export LDFLAGS="-pie" |
| 98 | $ ./configure --host=arm-linux-androideabi |
| 99 | $ make |
| 100 | ``` |
| 101 | |
| 102 | Replace "arm" and "arm-linux-androideabi" with the appropriate pair for |
| 103 | the architecture you actually want to build for, and 21 with the API |
| 104 | level you want to target. |
| 105 | |
| 106 | |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 107 | Supporting Unified Headers in Your Build System |
| 108 | ----------------------------------------------- |
| 109 | |
| 110 | App developers can stop reading here. The following information is only |
| 111 | relevant to build system engineers. |
| 112 | |
| Dan Albert | c02e422 | 2017-01-31 14:35:57 -0800 | [diff] [blame] | 113 | Unified headers require only a few changes compared to using the deprecated NDK |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 114 | headers. For reference, this patch added support to ndk-build: |
| 115 | https://android-review.googlesource.com/c/239934/ |
| 116 | |
| 117 | 1. The compile time sysroot is now `$NDK/sysroot`. Previously this was |
| 118 | `$NDK/platforms/android-$API/arch-$ARCH`. |
| 119 | |
| 120 | 2. Pass `-isystem $NDK/sysroot/usr/include/$TRIPLE` when compiling. The triple |
| 121 | has the following mapping: |
| 122 | |
| 123 | Arch | Triple |
| 124 | --------|------------------------- |
| 125 | ARM | `arm-linux-androideabi` |
| 126 | ARM64 | `aarch64-linux-android` |
| 127 | MIPS | `mipsel-linux-android` |
| 128 | MIPS64 | `mips64el-linux-android` |
| Dan Albert | fe42e8f | 2016-12-12 14:17:44 -0800 | [diff] [blame] | 129 | x86 | `i686-linux-android` |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 130 | x86\_64 | `x86_64-linux-android` |
| 131 | |
| 132 | This is needed for architecture specific headers such as those in `asm/` and |
| 133 | `machine/`. We plan to teach Clang's driver to automatically search the |
| 134 | architecture specific include directory, but that has yet to be done. |
| 135 | |
| 136 | 3. Pass `-D__ANDROID_API__=$API` when compiling. This define used to be provided |
| 137 | by `<android/api-level.h>`, but with only one set of headers this is no |
| 138 | longer possible. In the future we will look in to adding `-mandroid-version` |
| 139 | or similar to Clang so this is automatic. |
| 140 | |
| 141 | 4. At link time, change nothing. All link time build behavior should match the |
| Dan Albert | c02e422 | 2017-01-31 14:35:57 -0800 | [diff] [blame] | 142 | deprecated headers behavior. `--sysroot` should still point to |
| Dan Albert | ab5740b | 2016-11-03 16:02:16 -0700 | [diff] [blame] | 143 | `$NDK/platforms/android-$API/arch-$ARCH/`. |