1 # Platform defines. 2 set(CMAKE_SYSTEM_NAME Linux) 3 4 set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE) 5 6 set(CMAKE_CROSSCOMPILING 1) 7 8 # NDK installation path 9 if (NOT DEFINED ANDROID_NDK_PATH) 10 message(FATAL_ERROR "Please provide ANDROID_NDK_PATH") 11 endif () 12 13 # Host os (for toolchain binaries) 14 if (NOT DEFINED ANDROID_NDK_HOST_OS) 15 message(STATUS "Warning: ANDROID_NDK_HOST_OS is not set") 16 if (WIN32) 17 set(ANDROID_NDK_HOST_OS "windows") 18 elseif (UNIX) 19 set(ANDROID_NDK_HOST_OS "linux-86") 20 endif () 21 endif () 22 23 # Compile target 24 set(ANDROID_ABI "armeabi-v7a" CACHE STRING "Android ABI") 25 set(ANDROID_NDK_TARGET "android-${DE_ANDROID_API}") 26 27 # dE defines 28 set(DE_OS "DE_OS_ANDROID") 29 30 if (NOT DEFINED DE_COMPILER) 31 set(DE_COMPILER "DE_COMPILER_CLANG") 32 endif () 33 34 if (NOT DEFINED DE_ANDROID_API) 35 set(DE_ANDROID_API 9) 36 endif () 37 38 set(COMMON_C_FLAGS "-D__STDC_INT64__") 39 set(COMMON_CXX_FLAGS "${COMMON_C_FLAGS} -frtti -fexceptions") 40 set(COMMON_LINKER_FLAGS "") 41 set(ARM_C_FLAGS "-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ ") 42 43 # ABI-dependent bits 44 if (ANDROID_ABI STREQUAL "x86") 45 set(DE_CPU "DE_CPU_X86") 46 set(CMAKE_SYSTEM_PROCESSOR i686-android-linux) 47 48 set(ANDROID_CC_PATH "${ANDROID_NDK_PATH}/toolchains/x86-4.9/prebuilt/${ANDROID_NDK_HOST_OS}/") 49 set(CROSS_COMPILE "${ANDROID_CC_PATH}bin/i686-linux-android-") 50 set(ANDROID_SYSROOT "${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-x86") 51 52 set(CMAKE_FIND_ROOT_PATH 53 "${ANDROID_CC_PATH}i686-linux-android" 54 "${ANDROID_CC_PATH}lib/gcc/i686-linux-android/4.9" 55 ) 56 57 set(TARGET_C_FLAGS "-march=i686 -msse3 -mstackrealign -mfpmath=sse") 58 set(TARGET_LINKER_FLAGS "") 59 set(LLVM_TRIPLE "i686-none-linux-android") 60 61 elseif (ANDROID_ABI STREQUAL "armeabi" OR 62 ANDROID_ABI STREQUAL "armeabi-v7a") 63 set(DE_CPU "DE_CPU_ARM") 64 set(CMAKE_SYSTEM_PROCESSOR arm-linux-androideabi) 65 66 set(ANDROID_CC_PATH "${ANDROID_NDK_PATH}/toolchains/arm-linux-androideabi-4.9/prebuilt/${ANDROID_NDK_HOST_OS}/") 67 set(CROSS_COMPILE "${ANDROID_CC_PATH}bin/arm-linux-androideabi-") 68 set(ANDROID_SYSROOT "${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-arm") 69 70 if (ANDROID_ABI STREQUAL "armeabi-v7a") 71 set(TARGET_C_FLAGS "${ARM_C_FLAGS} -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp") 72 set(TARGET_LINKER_FLAGS "-Wl,--fix-cortex-a8 -march=armv7-a") 73 set(LLVM_TRIPLE "armv7-none-linux-androideabi") 74 75 else () # armeabi 76 set(TARGET_C_FLAGS "${ARM_C_FLAGS} -march=armv5te -mfloat-abi=softfp") 77 set(TARGET_LINKER_FLAGS "-Wl,--fix-cortex-a8 -march=armv5te") 78 set(LLVM_TRIPLE "armv5te-none-linux-androideabi") 79 endif () 80 81 set(CMAKE_FIND_ROOT_PATH 82 "${ANDROID_CC_PATH}arm-linux-androideabi" 83 ) 84 85 elseif (ANDROID_ABI STREQUAL "arm64-v8a") 86 set(DE_CPU "DE_CPU_ARM_64") 87 set(CMAKE_SYSTEM_PROCESSOR aarch64-linux-android) 88 set(CMAKE_SIZEOF_VOID_P 8) 89 90 set(ANDROID_CC_PATH "${ANDROID_NDK_PATH}/toolchains/aarch64-linux-android-4.9/prebuilt/${ANDROID_NDK_HOST_OS}/") 91 set(CROSS_COMPILE "${ANDROID_CC_PATH}bin/aarch64-linux-android-") 92 set(ANDROID_SYSROOT "${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-arm64") 93 94 set(CMAKE_FIND_ROOT_PATH 95 "${ANDROID_CC_PATH}arm-linux-androideabi" 96 ) 97 98 set(TARGET_C_FLAGS "-march=armv8-a") 99 set(TARGET_LINKER_FLAGS "-Wl,--fix-cortex-a53-835769 -Wl,--fix-cortex-a53-835769 -march=armv8-a") 100 set(LLVM_TRIPLE "aarch64-none-linux-android") 101 102 if (DE_COMPILER STREQUAL "DE_COMPILER_GCC") 103 set(TARGET_C_FLAGS "${TARGET_C_FLAGS} -mabi=lp64") 104 endif () 105 106 else () 107 message(FATAL_ERROR "Unknown ABI \"${ANDROID_ABI}\"") 108 endif () 109 110 # Use LLVM libc++ for full C++11 support 111 set(ANDROID_CXX_LIBRARY "${ANDROID_NDK_PATH}/sources/cxx-stl/llvm-libc++/libs/${ANDROID_ABI}/libc++_static.a") 112 set(CXX_INCLUDES "-I${ANDROID_NDK_PATH}/sources/cxx-stl/llvm-libc++/libcxx/include") 113 set(CMAKE_FIND_ROOT_PATH "" ${CMAKE_FIND_ROOT_PATH}) 114 115 set(CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH} ${ANDROID_SYSROOT}) 116 117 include(CMakeForceCompiler) 118 119 if (DE_COMPILER STREQUAL "DE_COMPILER_GCC") 120 if (ANDROID_NDK_HOST_OS STREQUAL "linux-x86" OR 121 ANDROID_NDK_HOST_OS STREQUAL "linux-x86_64" OR 122 ANDROID_NDK_HOST_OS STREQUAL "darwin-x86") 123 cmake_force_c_compiler("${CROSS_COMPILE}gcc" GNU) 124 cmake_force_cxx_compiler("${CROSS_COMPILE}g++" GNU) 125 elseif (ANDROID_NDK_HOST_OS STREQUAL "windows") 126 cmake_force_c_compiler("${CROSS_COMPILE}gcc.exe" GNU) 127 cmake_force_cxx_compiler("${CROSS_COMPILE}g++.exe" GNU) 128 else () 129 message(FATAL_ERROR "Unknown ANDROID_NDK_HOST_OS") 130 endif () 131 132 set(TARGET_C_FLAGS "-mandroid ${TARGET_C_FLAGS}") 133 134 elseif (DE_COMPILER STREQUAL "DE_COMPILER_CLANG") 135 set(LLVM_PATH "${ANDROID_NDK_PATH}/toolchains/llvm/prebuilt/${ANDROID_NDK_HOST_OS}/") 136 137 if (ANDROID_NDK_HOST_OS STREQUAL "linux-x86" OR 138 ANDROID_NDK_HOST_OS STREQUAL "linux-x86_64" OR 139 ANDROID_NDK_HOST_OS STREQUAL "darwin-x86" OR 140 ANDROID_NDK_HOST_OS STREQUAL "darwin-x86_64") 141 cmake_force_c_compiler("${LLVM_PATH}bin/clang" Clang) 142 cmake_force_cxx_compiler("${LLVM_PATH}bin/clang++" Clang) 143 set(CMAKE_AR "${CROSS_COMPILE}ar" CACHE FILEPATH "Archiver") 144 set(CMAKE_RANLIB "${CROSS_COMPILE}ranlib" CACHE FILEPATH "Indexer") 145 146 elseif (ANDROID_NDK_HOST_OS STREQUAL "windows" OR 147 ANDROID_NDK_HOST_OS STREQUAL "windows-x86_64") 148 cmake_force_c_compiler("${LLVM_PATH}bin/clang.exe" Clang) 149 cmake_force_cxx_compiler("${LLVM_PATH}bin/clang++.exe" Clang) 150 set(CMAKE_AR "${CROSS_COMPILE}ar.exe" CACHE FILEPATH "Archiver") 151 set(CMAKE_RANLIB "${CROSS_COMPILE}ranlib.exe" CACHE FILEPATH "Indexer") 152 153 else () 154 message(FATAL_ERROR "Unknown ANDROID_NDK_HOST_OS") 155 endif () 156 157 set(TARGET_C_FLAGS "-target ${LLVM_TRIPLE} -gcc-toolchain ${ANDROID_CC_PATH} ${TARGET_C_FLAGS}") 158 set(TARGET_LINKER_FLAGS "-target ${LLVM_TRIPLE} -gcc-toolchain ${ANDROID_CC_PATH} ${TARGET_LINKER_FLAGS}") 159 160 endif () 161 162 set(CMAKE_SHARED_LIBRARY_C_FLAGS "") 163 set(CMAKE_SHARED_LIBRARY_CXX_FLAGS "") 164 165 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 166 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 167 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 168 169 # \note Without CACHE STRING FORCE cmake ignores these. 170 set(CMAKE_C_FLAGS "--sysroot=${ANDROID_SYSROOT} ${COMMON_C_FLAGS} ${TARGET_C_FLAGS}" CACHE STRING "" FORCE) 171 set(CMAKE_CXX_FLAGS "--sysroot=${ANDROID_SYSROOT} ${COMMON_CXX_FLAGS} ${TARGET_C_FLAGS} ${CXX_INCLUDES} -I${ANDROID_NDK_PATH}/sources/android/support/include" CACHE STRING "" FORCE) 172 set(CMAKE_SHARED_LINKER_FLAGS "-nodefaultlibs -Wl,-shared,-Bsymbolic -Wl,--no-undefined ${COMMON_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE) 173 set(CMAKE_EXE_LINKER_FLAGS "-nodefaultlibs ${COMMON_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE) 174