Home | History | Annotate | Download | only in cmake
      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 # C++ library
    111 if (ANDROID_ABI STREQUAL "x86")
    112 	# Use gnu-libstc++, since libc++ is currently broken on x86, bug #22124988
    113 	set(ANDROID_CXX_LIBRARY 	"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}/libgnustl_static.a")
    114 	set(CXX_INCLUDES			"-I${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/include -I${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}/include")
    115 	set(CMAKE_FIND_ROOT_PATH	"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}" ${CMAKE_FIND_ROOT_PATH})
    116 else ()
    117 	# Use LLVM libc++ for full C++11 support
    118 	set(ANDROID_CXX_LIBRARY		"${ANDROID_NDK_PATH}/sources/cxx-stl/llvm-libc++/libs/${ANDROID_ABI}/libc++_static.a")
    119 	set(CXX_INCLUDES			"-I${ANDROID_NDK_PATH}/sources/cxx-stl/llvm-libc++/libcxx/include")
    120 	set(CMAKE_FIND_ROOT_PATH	"" ${CMAKE_FIND_ROOT_PATH})
    121 endif ()
    122 
    123 set(CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH} ${ANDROID_SYSROOT})
    124 
    125 include(CMakeForceCompiler)
    126 
    127 if (DE_COMPILER STREQUAL "DE_COMPILER_GCC")
    128 	if (ANDROID_NDK_HOST_OS STREQUAL "linux-x86" OR
    129 		ANDROID_NDK_HOST_OS STREQUAL "linux-x86_64" OR
    130 		ANDROID_NDK_HOST_OS STREQUAL "darwin-x86")
    131 		cmake_force_c_compiler("${CROSS_COMPILE}gcc"		GNU)
    132 		cmake_force_cxx_compiler("${CROSS_COMPILE}g++"		GNU)
    133 	elseif (ANDROID_NDK_HOST_OS STREQUAL "windows")
    134 		cmake_force_c_compiler("${CROSS_COMPILE}gcc.exe"	GNU)
    135 		cmake_force_cxx_compiler("${CROSS_COMPILE}g++.exe"	GNU)
    136 	else ()
    137 		message(FATAL_ERROR "Unknown ANDROID_NDK_HOST_OS")
    138 	endif ()
    139 
    140 	set(TARGET_C_FLAGS		"-mandroid ${TARGET_C_FLAGS}")
    141 
    142 elseif (DE_COMPILER STREQUAL "DE_COMPILER_CLANG")
    143 	if (NOT DEFINED LLVM_VERSION)
    144 		if (ANDROID_NDK_HOST_OS STREQUAL "windows" OR
    145 			ANDROID_NDK_HOST_OS STREQUAL "windows-x86_64")
    146 			# Windows NDK prebuilts don't include llvm-ar tool in version 3.6
    147 			set(LLVM_VERSION "3.5")
    148 		else ()
    149 			set(LLVM_VERSION "3.6")
    150 		endif ()
    151 	endif ()
    152 
    153 	set(LLVM_PATH "${ANDROID_NDK_PATH}/toolchains/llvm-${LLVM_VERSION}/prebuilt/${ANDROID_NDK_HOST_OS}/")
    154 
    155 	if (ANDROID_NDK_HOST_OS STREQUAL "linux-x86" OR
    156 		ANDROID_NDK_HOST_OS STREQUAL "linux-x86_64" OR
    157 		ANDROID_NDK_HOST_OS STREQUAL "darwin-x86" OR
    158 		ANDROID_NDK_HOST_OS STREQUAL "darwin-x86_64")
    159 		cmake_force_c_compiler("${LLVM_PATH}bin/clang"			Clang)
    160 		cmake_force_cxx_compiler("${LLVM_PATH}bin/clang++"		Clang)
    161 		set(CMAKE_AR "${LLVM_PATH}/bin/llvm-ar" CACHE FILEPATH "Archiver")
    162 		set(CMAKE_RANLIB "${CROSS_COMPILE}ranlib" CACHE FILEPATH "Indexer")
    163 	elseif (ANDROID_NDK_HOST_OS STREQUAL "windows" OR
    164 			ANDROID_NDK_HOST_OS STREQUAL "windows-x86_64")
    165 		cmake_force_c_compiler("${LLVM_PATH}bin/clang.exe"		Clang)
    166 		cmake_force_cxx_compiler("${LLVM_PATH}bin/clang++.exe"	Clang)
    167 		set(CMAKE_AR "${LLVM_PATH}bin/llvm-ar.exe" CACHE FILEPATH "Archiver")
    168 	else ()
    169 		message(FATAL_ERROR "Unknown ANDROID_NDK_HOST_OS")
    170 	endif ()
    171 
    172 	set(TARGET_C_FLAGS		"-target ${LLVM_TRIPLE} -gcc-toolchain ${ANDROID_CC_PATH} ${TARGET_C_FLAGS}")
    173 	set(TARGET_LINKER_FLAGS	"-target ${LLVM_TRIPLE} -gcc-toolchain ${ANDROID_CC_PATH} ${TARGET_LINKER_FLAGS}")
    174 
    175 endif ()
    176 
    177 set(CMAKE_SHARED_LIBRARY_C_FLAGS	"")
    178 set(CMAKE_SHARED_LIBRARY_CXX_FLAGS	"")
    179 
    180 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    181 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    182 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    183 
    184 # \note Without CACHE STRING FORCE cmake ignores these.
    185 set(CMAKE_C_FLAGS				"--sysroot=${ANDROID_SYSROOT} ${COMMON_C_FLAGS} ${TARGET_C_FLAGS}" CACHE STRING "" FORCE)
    186 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)
    187 set(CMAKE_SHARED_LINKER_FLAGS	"-nodefaultlibs -Wl,-shared,-Bsymbolic -Wl,--no-undefined ${COMMON_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE)
    188 set(CMAKE_EXE_LINKER_FLAGS		"-nodefaultlibs ${COMMON_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE)
    189