-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Cross Compilation of CGAL for Android
- Requirements
- Generating a Standalone Toolchain
- Building GMP
- Building MPFR
- Configuring CGAL
- Build an Example
- GMP sources
- MPFR sources
- Boost headers
- Android NDK
- CGAL sources
- For Windows, MSYS2 with
gcc
,make
,autoconf
andautomake
, installed with the command
> pacman -S gcc make autoconf automake
The first step for cross-compiling a library to Android is to create a toolchain from the NDK. A python script is given to do so:
> ${ANDROID_NDK}/build/tools/make_standalone_toolchain.py --arch ${ARCH} --install-dir ${TOOLCHAIN_PATH}
-
${ANDROID_NDK}
is the root directory of your NDK. -
${ARCH}
is the architecture you are targeting. For example: arm, arm64, ... -
${TOOLCHAIN_PATH}
is the directory where the toolchain will be installed.
This will generate all the files you need in ${TOOLCHAIN_PATH}
.
We will now build a version of GMP for Android, using the previously generated toolchain. In the GMP sources dir, do the following:
> export CC=${TOOLCHAIN_PATH}/bin/clang
> export PATH="${TOOLCHAIN_PATH}:${PATH}"
> ./configure --prefix=${GMP_PATH} --enable-shared --host=${TARGET_TRIPLET}
> make
> make install
-
${TARGET_TRIPLET}
represents the machine you are targeting. It is of the form cpu-company-system. For example,-
aarch64-linux-android
for a 64bit arm Android device, or -
arm-linux-androideabihf
for a 32bit arm Android device (ARMv7 or later, with a hard floating-points unit).
-
This is very similar to building GMP. In the MPFR sources dir, do the following:
> export CC=${TOOLCHAIN_PATH}/bin/clang
> export PATH="${TOOLCHAIN_PATH}:${PATH}"
> ./configure --prefix=${MPFR_PATH} --enable-shared --host=${TARGET_TRIPLET} --with-gmp=${GMP_PATH}
> make
> make install
Now that we have the dependencies built, we can configure CGAL.
First we will create a toolchain.cmake
file, containing the paths needed by CMake.
Create this file at ${TOOLCHAIN_FILE_PATH}:
#Target system
set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_SYSTEM_VERSION 21)
set(CMAKE_ANDROID_ARCH_ABI arm64-v8a) # Change arm64-v8a to armeabi-v7a for 32bit phones
set(CMAKE_ANDROID_NDK /home/gimeno/Android/android-ndk-r15c)
set(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION clang)
set(GMP_INCLUDE_DIR /home/gimeno/Android/3rdPartyLibs/gmp-arm64/include)
set(GMP_LIBRARIES /home/gimeno/Android/3rdPartyLibs/gmp-arm64/lib/libgmp.so)
set(MPFR_INCLUDE_DIR /home/gimeno/Android/3rdPartyLibs/mpfr-arm64/include)
set(MPFR_LIBRARIES /home/gimeno/Android/3rdPartyLibs/mpfr-arm64/lib/libmpfr.so)
set(Boost_INCLUDE_DIR /home/gimeno/Android/3rdPartyLibs/boost/include)
This is an example and you obviously have to use the right paths instead of those.
Once we have this, we call cmake:
> cmake -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE_PATH} -DWITH_CGAL_Core=FALSE -DCMAKE_CXX_FLAGS=-std=c++11 -DCGAL_test_cpp_version_RUN_RES=0 -DCGAL_HEADER_ONLY=TRUE -DWITH_CGAL_Qt5=FALSE /path/to/cgal/sources
CGAL is now ready to be used with an Android Application.
Notes:
- You have to run cmake twice, because of a bug of CGAL CMake scripts in case of a cross-compilation.
- On Windows, we add the option
-G "Unix Makefiles"
. - The options
-DCMAKE_CXX_FLAGS=-std=c++11 -DCGAL_test_cpp_version_RUN_RES=0
allow to use the C++11 standard. When CGAL is compiled with a C++11 compiler, the library Boost.Thread is no longer required, and Boost libraries can be used header-only. There is one exception:CGAL_Core
still requires Boost.Thread. That is why we use the cmake option-DWITH_CGAL_Core=FALSE
, to disableCGAL_Core
. If you needCGAL_Core
, then you will have to cross-compile Boost, and then set the appropriate CMake variables. Some packages have more dependencies. If you wish to use them, you will have to cross-compile them too. - With the option
-DCGAL_HEADER_ONLY=TRUE
, CGAL is used header-only.
Since Android 5.0 (API level 21), an executable needs to be PIE (Position independent executable) to work. To make your executable PIE, you need to add -fPIE
to the CXX flags, and -fPIE -pie
to the linker flags
> cmake -DCMAKE_CXX_FLAGS="-std=c++11 -fPIE" -DCMAKE_EXE_LINKER_FLAGS="-fPIE -pie" .
General Information
- Information for New Developers
- Developing with Git
- Structure of a CGAL Package
- Building
- Concurrency in CGAL
- License
- Documentation Guidelines
- Reviewing Process
- Testing
- Miscellaneous
- Tools
- Scripts
- Libraries
- Infrastructure
- Releases
- Miscellaneous