blob: 385478f3a4d572765fef165d74b4568cac54a94e [file] [log] [blame] [view]
Dan Albertab5740b2016-11-03 16:02:16 -07001Unified Headers
2===============
3
4[Issue #120](https://github.com/android-ndk/ndk/issues/120)
5
Dan Albert06914ae2017-05-17 14:33:22 -07006Before NDK r14, we had a set of libc headers for each API version. In many cases
7these headers were incorrect. Many exposed APIs that didn't exist, and others
8didn't expose APIs that did.
Dan Albertab5740b2016-11-03 16:02:16 -07009
Dan Albert06914ae2017-05-17 14:33:22 -070010In NDK r14 (as an opt in feature) we unified these into a single set of headers.
11In r15 these are used by default. This single header path is used for *every*
12platform level. API level guards are handled with `#ifdef`. These headers can
13be found in [prebuilts/ndk/headers].
Dan Albertab5740b2016-11-03 16:02:16 -070014
Dan Albert06914ae2017-05-17 14:33:22 -070015Unified headers are built directly from the Android platform, so they be up to
16date and correct (or at the very least, any bugs in the NDK headers will also be
17a bug in the platform headers, which means we're much more likely to find them).
Dan Albertab5740b2016-11-03 16:02:16 -070018
Dan Albertb7513db2017-05-23 10:46:05 -070019[prebuilts/ndk/headers]: https://android.googlesource.com/platform/prebuilts/ndk/+/dev/platform/sysroot/usr/include
Dan Albertab5740b2016-11-03 16:02:16 -070020
21
22Known Issues
23------------
24
Dan Albert06914ae2017-05-17 14:33:22 -070025 * 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 Albertab5740b2016-11-03 16:02:16 -070030 * Standalone toolchains using GCC are not supported out of the box. To use GCC,
Dan Albert06914ae2017-05-17 14:33:22 -070031 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 Albertab5740b2016-11-03 16:02:16 -070036
Dan Albertab5740b2016-11-03 16:02:16 -070037
38Using Unified Headers
39---------------------
40
Dan Albertc02e4222017-01-31 14:35:57 -080041Unified headers are enabled by default starting with NDK r15. If your build is
42not yet compatible with unified headers, you can revert to the deprecated
43headers. 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
46need to revert to the deprecated headers, make sure you're working on fixing
47your build or filing bugs.
Dan Albertab5740b2016-11-03 16:02:16 -070048
49### ndk-build
50
Dan Albertce6f19a2017-08-09 22:33:52 -070051Add the following to your Application.mk:
Dan Albertab5740b2016-11-03 16:02:16 -070052
53```makefile
Dan Albertc02e4222017-01-31 14:35:57 -080054APP_DEPRECATED_HEADERS := true
Dan Albertab5740b2016-11-03 16:02:16 -070055```
56
57### CMake
58
Dan Albertab5740b2016-11-03 16:02:16 -070059```bash
Dan Albertc02e4222017-01-31 14:35:57 -080060cmake -DANDROID_DEPRECATED_HEADERS=ON ...
Dan Albertab5740b2016-11-03 16:02:16 -070061```
62
63### Standalone Toolchains
64
65```bash
Dan Albertc02e4222017-01-31 14:35:57 -080066$NDK/build/tools/make_standalone_toolchain.py --deprecated-headers ...
Dan Albertab5740b2016-11-03 16:02:16 -070067```
68
Dan Albertfc2eccc2016-12-28 12:42:01 -080069For general standalone toolchain documentation, see
70https://developer.android.com/ndk/guides/standalone_toolchain.html
71
Dan Albert95780292017-04-17 17:32:14 -070072### Gradle Experimental Plugin
Dan Albertab5740b2016-11-03 16:02:16 -070073
Dan Albert95780292017-04-17 17:32:14 -070074```gradle
75model {
76 android {
77 ndk {
78 useUnifiedHeaders false
79 }
80 }
81}
82```
Dan Albertab5740b2016-11-03 16:02:16 -070083
84
Elliott Hughes3a6ea502017-01-30 13:33:03 -080085Using unified headers with `configure && make`
86----------------------------------------------
87
88If you're trying to build a traditional open-source `configure && make`
89style project with the NDK, unified headers are your best choice. The
90basic 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
102Replace "arm" and "arm-linux-androideabi" with the appropriate pair for
103the architecture you actually want to build for, and 21 with the API
104level you want to target.
105
106
Dan Albertab5740b2016-11-03 16:02:16 -0700107Supporting Unified Headers in Your Build System
108-----------------------------------------------
109
110App developers can stop reading here. The following information is only
111relevant to build system engineers.
112
Dan Albertc02e4222017-01-31 14:35:57 -0800113Unified headers require only a few changes compared to using the deprecated NDK
Dan Albertab5740b2016-11-03 16:02:16 -0700114headers. For reference, this patch added support to ndk-build:
115https://android-review.googlesource.com/c/239934/
116
1171. The compile time sysroot is now `$NDK/sysroot`. Previously this was
118 `$NDK/platforms/android-$API/arch-$ARCH`.
119
1202. 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 Albertfe42e8f2016-12-12 14:17:44 -0800129 x86 | `i686-linux-android`
Dan Albertab5740b2016-11-03 16:02:16 -0700130 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
1363. 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
1414. At link time, change nothing. All link time build behavior should match the
Dan Albertc02e4222017-01-31 14:35:57 -0800142 deprecated headers behavior. `--sysroot` should still point to
Dan Albertab5740b2016-11-03 16:02:16 -0700143 `$NDK/platforms/android-$API/arch-$ARCH/`.