• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# android-cmake
2
3CMake is great, and so is Android. This is a collection of CMake scripts that may be useful to the Android NDK community. It is based on experience from porting OpenCV library to Android: http://opencv.org/platforms/android.html
4
5Main goal is to share these scripts so that devs that use CMake as their build system may easily compile native code for Android.
6
7## TL;DR
8
9    cmake -DCMAKE_TOOLCHAIN_FILE=android.toolchain.cmake \
10          -DANDROID_NDK=<ndk_path>                       \
11          -DCMAKE_BUILD_TYPE=Release                     \
12          -DANDROID_ABI="armeabi-v7a with NEON"          \
13          <source_path>
14    cmake --build .
15
16One-liner:
17
18    cmake -DCMAKE_TOOLCHAIN_FILE=android.toolchain.cmake -DANDROID_NDK=<ndk_path> -DCMAKE_BUILD_TYPE=Release -DANDROID_ABI="armeabi-v7a with NEON" <source_path> && cmake --build .
19
20_android-cmake_ will search for your NDK install in the following order:
21
221. Value of `ANDROID_NDK` CMake variable;
231. Value of `ANDROID_NDK` environment variable;
241. Search under paths from `ANDROID_NDK_SEARCH_PATHS` CMake variable;
251. Search platform specific locations (home folder, Windows "Program Files", etc).
26
27So if you have installed the NDK as `~/android-ndk-r10d` then _android-cmake_ will locate it automatically.
28
29## Getting started
30
31To build a cmake-based C/C++ project for Android you need:
32
33* Android NDK (>= r5) http://developer.android.com/tools/sdk/ndk/index.html
34* CMake (>= v2.6.3, >= v2.8.9 recommended) http://www.cmake.org/download
35
36The _android-cmake_ is also capable to build with NDK from AOSP or Linaro Android source tree, but you may be required to manually specify path to `libm` binary to link with.
37
38## Difference from traditional CMake
39
40Folowing the _ndk-build_ the _android-cmake_ supports **only two build targets**:
41
42* `-DCMAKE_BUILD_TYPE=Release`
43* `-DCMAKE_BUILD_TYPE=Debug`
44
45So don't even try other targets that can be found in CMake documentation and don't forget to explicitly specify `Release` or `Debug` because CMake builds without a build configuration by default.
46
47## Difference from _ndk-build_
48
49* Latest GCC available in NDK is used as the default compiler;
50* `Release` builds with `-O3` instead of `-Os`;
51* `Release` builds without debug info (without `-g`) (because _ndk-build_ always creates a stripped version but cmake delays this for `install/strip` target);
52* `-fsigned-char` is added to compiler flags to make `char` signed by default as it is on x86/x86_64;
53* GCC's stack protector is not used neither in `Debug` nor `Release` configurations;
54* No builds for multiple platforms (e.g. building for both arm and x86 require to run cmake twice with different parameters);
55* No file level Neon via `.neon` suffix;
56
57The following features of _ndk-build_ are not supported by the _android-cmake_ yet:
58
59* `armeabi-v7a-hard` ABI
60* `libc++_static`/`libc++_shared` STL runtime
61
62## Basic options
63
64Similarly to the NDK build system _android-cmake_ allows to select between several compiler toolchains and target platforms. Most of the options can be set either as cmake arguments: `-D<NAME>=<VALUE>` or as environment variables:
65
66* **ANDROID_NDK** - path to the Android NDK. If not set then _android-cmake_ will search for the most recent version of supported NDK in commonly used locations;
67* **ANDROID_ABI** - specifies the target Application Binary Interface (ABI). This option nearly matches to the APP_ABI variable used by ndk-build tool from Android NDK. If not specified then set to `armeabi-v7a`. Possible target names are:
68    * `armeabi` - ARMv5TE based CPU with software floating point operations;
69    * **`armeabi-v7a`** - ARMv7 based devices with hardware FPU instructions (VFPv3_D16);
70    * `armeabi-v7a with NEON` - same as armeabi-v7a, but sets NEON as floating-point unit;
71    * `armeabi-v7a with VFPV3` - same as armeabi-v7a, but sets VFPv3_D32 as floating-point unit;
72    * `armeabi-v6 with VFP` - tuned for ARMv6 processors having VFP;
73    * `x86` - IA-32 instruction set
74    * `mips` - MIPS32 instruction set
75    * `arm64-v8a` - ARMv8 AArch64 instruction set - only for NDK r10 and newer
76    * `x86_64` - Intel64 instruction set (r1) - only for NDK r10 and newer
77    * `mips64` - MIPS64 instruction set (r6) - only for NDK r10 and newer
78* **ANDROID_NATIVE_API_LEVEL** - level of android API to build for. Can be set either to full name (example: `android-8`) or a numeric value (example: `17`). The default API level depends on the target ABI:
79    * `android-8` for ARM;
80    * `android-9` for x86 and MIPS;
81    * `android-21` for 64-bit ABIs.
82
83    Building for `android-L` is possible only when it is explicitly selected.
84* **ANDROID_TOOLCHAIN_NAME** - the name of compiler toolchain to be used. This option allows to select between different GCC and Clang versions. The list of possible values depends on the NDK version and will be printed by toolchain file if an invalid value is set. By default _android-cmake_ selects the most recent version of GCC which can build for specified `ANDROID_ABI`.
85
86    Example values are:
87    * `aarch64-linux-android-4.9`
88    * `aarch64-linux-android-clang3.5`
89    * `arm-linux-androideabi-4.8`
90    * `arm-linux-androideabi-4.9`
91    * `arm-linux-androideabi-clang3.5`
92    * `mips64el-linux-android-4.9`
93    * `mipsel-linux-android-4.8`
94    * `x86-4.9`
95    * `x86_64-4.9`
96    * etc.
97* **ANDROID_STL** - the name of C++ runtime to use. The default is `gnustl_static`.
98    * `none` - do not configure the runtime.
99    * `system` - use the default minimal system C++ runtime library.
100        * Implies `-fno-rtti -fno-exceptions`.
101    * `system_re` - use the default minimal system C++ runtime library.
102        * Implies `-frtti -fexceptions`.
103    * `gabi++_static` - use the GAbi++ runtime as a static library.
104        * Implies `-frtti -fno-exceptions`.
105        * Available for NDK r7 and newer.
106    * `gabi++_shared` - use the GAbi++ runtime as a shared library.
107        * Implies `-frtti -fno-exceptions`.
108        * Available for NDK r7 and newer.
109    * `stlport_static` - use the STLport runtime as a static library.
110        * Implies `-fno-rtti -fno-exceptions` for NDK before r7.
111        * Implies `-frtti -fno-exceptions` for NDK r7 and newer.
112    * `stlport_shared` - use the STLport runtime as a shared library.
113        * Implies `-fno-rtti -fno-exceptions` for NDK before r7.
114        * Implies `-frtti -fno-exceptions` for NDK r7 and newer.
115    * **`gnustl_static`** - use the GNU STL as a static library.
116        * Implies `-frtti -fexceptions`.
117    * `gnustl_shared` - use the GNU STL as a shared library.
118        * Implies `-frtti -fno-exceptions`.
119        * Available for NDK r7b and newer.
120        * Silently degrades to `gnustl_static` if not available.
121* **NDK_CCACHE** - path to `ccache` executable. If not set then initialized from `NDK_CCACHE` environment variable.
122
123## Advanced _android-cmake_ options
124
125Normally _android-cmake_ users are not supposed to touch these variables but they might be useful to workaround some build issues:
126
127* **ANDROID_FORCE_ARM_BUILD** = `OFF` - generate 32-bit ARM instructions instead of Thumb. Applicable only for arm ABIs and is forced to be `ON` for `armeabi-v6 with VFP`;
128* **ANDROID_NO_UNDEFINED** = `ON` - show all undefined symbols as linker errors;
129* **ANDROID_SO_UNDEFINED** = `OFF` - allow undefined symbols in shared libraries;
130    * actually it is turned `ON` by default for NDK older than `r7`
131* **ANDROID_STL_FORCE_FEATURES** = `ON` - automatically configure rtti and exceptions support based on C++ runtime;
132* **ANDROID_NDK_LAYOUT** = `RELEASE` - inner layout of Android NDK, should be detected automatically. Possible values are:
133    * `RELEASE` - public releases from Google;
134    * `LINARO` - NDK from Linaro project;
135    * `ANDROID` - NDK from AOSP.
136* **ANDROID_FUNCTION_LEVEL_LINKING** = `ON` - enables saparate putting each function and data items into separate sections and enable garbage collection of unused input sections at link time (`-fdata-sections -ffunction-sections -Wl,--gc-sections`);
137* **ANDROID_GOLD_LINKER** = `ON` - use gold linker with GCC 4.6 for NDK r8b and newer (only for ARM and x86);
138* **ANDROID_NOEXECSTACK** = `ON` - enables or disables stack execution protection code (`-Wl,-z,noexecstack`);
139* **ANDROID_RELRO** = `ON` - Enables RELRO - a memory corruption mitigation technique (`-Wl,-z,relro -Wl,-z,now`);
140* **ANDROID_LIBM_PATH** - path to `libm.so` (set to something like `$(TOP)/out/target/product/<product_name>/obj/lib/libm.so`) to workaround unresolved `sincos`.
141
142## Fine-tuning `CMakeLists.txt` for _android-cmake_
143
144### Recognizing Android build
145
146_android-cmake_ defines `ANDROID` CMake variable which can be used to add Android-specific stuff:
147
148    if (ANDROID)
149        message(STATUS "Hello from Android build!")
150    endif()
151
152The recommended way to identify ARM/MIPS/x86 architecture is examining `CMAKE_SYSTEM_PROCESSOR` which is set to the appropriate value:
153
154* `armv5te` - for `armeabi` ABI
155* `armv6` - for `armeabi-v6 with VFP` ABI
156* `armv7-a` - for `armeabi-v7a`, `armeabi-v7a with VFPV3` and `armeabi-v7a with NEON` ABIs
157* `aarch64` - for `arm64-v8a` ABI
158* `i686` - for `x86` ABI
159* `x86_64` - for `x86_64` ABI
160* `mips` - for `mips` ABI
161* `mips64` - for `mips64` ABI
162
163Other variables that are set by _android-cmake_ and can be used for the fine-grained build configuration are:
164
165* `NEON` - set if target ABI supports Neon;
166* `ANDROID_NATIVE_API_LEVEL` - native Android API level we are building for (note: Java part of Andoid application can be built for another API level)
167* `ANDROID_NDK_RELEASE` - version of the Android NDK
168* `ANDROID_NDK_HOST_SYSTEM_NAME` - "windows", "linux-x86" or "darwin-x86" depending on the host platform
169* `ANDROID_RTTI` - set if rtti is enabled by the runtime
170* `ANDROID_EXCEPTIONS` - set if exceptions are enabled by the runtime
171
172### Finding packages
173
174When crosscompiling CMake `find_*` commands are normally expected to find libraries and packages belonging to the same build target. So _android-cmake_ configures CMake to search in Android-specific paths only and ignore your host system locations. So
175
176    find_package(ZLIB)
177
178will surely find libz.so within the Android NDK.
179
180However sometimes you need to locate a host package even when cross-compiling. For example you can be searching for your documentation generator. The _android-cmake_ recommends you to use `find_host_package` and `find_host_program` macro defined in the `android.toolchain.cmake`:
181
182    find_host_package(Doxygen)
183    find_host_program(PDFLATEX pdflatex)
184
185However this will break regular builds so instead of wrapping package search into platform-specific logic you can copy the following snippet into your project (put it after your top-level `project()` command):
186
187    # Search packages for host system instead of packages for target system
188    # in case of cross compilation these macro should be defined by toolchain file
189    if(NOT COMMAND find_host_package)
190      macro(find_host_package)
191        find_package(${ARGN})
192      endmacro()
193    endif()
194    if(NOT COMMAND find_host_program)
195      macro(find_host_program)
196        find_program(${ARGN})
197      endmacro()
198    endif()
199
200### Compiler flags recycling
201
202Make sure to do the following in your scripts:
203
204    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${my_cxx_flags}")
205    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${my_cxx_flags}")
206
207The flags will be prepopulated with critical flags, so don't loose them. Also be aware that _android-cmake_ also sets configuration-specific compiler and linker flags.
208
209## Troubleshooting
210
211### Building on Windows
212
213First of all `cygwin` builds are **NOT supported** and will not be supported by _android-cmake_. To build natively on Windows you need a port of make but I recommend http://martine.github.io/ninja/ instead.
214
215To build with Ninja you need:
216
217* Ensure you are using CMake newer than 2.8.9;
218* Download the latest Ninja from https://github.com/martine/ninja/releases;
219* Put the `ninja.exe` into your PATH (or add path to `ninja.exe` to your PATH environment variable);
220* Pass `-GNinja` to `cmake` alongside with other arguments (or choose Ninja generator in `cmake-gui`).
221* Enjoy the fast native multithreaded build :)
222
223But if you still want to stick to old make then:
224
225* Get a Windows port of GNU Make:
226    * Android NDK r7 (and newer) already has `make.exe` on board;
227    * `mingw-make` should work as fine;
228    * Download some other port. For example, this one: http://gnuwin32.sourceforge.net/packages/make.htm.
229* Add path to your `make.exe` to system PATH or always use full path;
230* Pass `-G"MinGW Makefiles"` and `-DCMAKE_MAKE_PROGRAM="<full/path/to/>make.exe"`
231    * It must be `MinGW Makefiles` and not `Unix Makefiles` even if your `make.exe` is not a MinGW's make.
232* Run `make.exe` or `cmake --build .` for single-threaded build.
233
234### Projects with assembler files
235
236The _android-cmake_ should correctly handle projects with assembler sources (`*.s` or `*.S`). But if you still facing problems with assembler then try to upgrade your CMake to version newer than 2.8.5
237
238## Copying
239
240_android-cmake_ is distributed under the terms of [BSD 3-Clause License](http://opensource.org/licenses/BSD-3-Clause)