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 set(DE_COMPILER	"DE_COMPILER_GCC")
     30 if (NOT DEFINED DE_ANDROID_API)
     31 	set(DE_ANDROID_API 9)
     32 endif ()
     33 
     34 set(COMMON_C_FLAGS		"-mandroid -D__STDC_INT64__")
     35 set(COMMON_CXX_FLAGS	"${COMMON_C_FLAGS} -frtti -fexceptions")
     36 set(COMMON_LINKER_FLAGS	"")
     37 set(ARM_C_FLAGS			"-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ ")
     38 
     39 # ABI-dependent bits
     40 if (ANDROID_ABI STREQUAL "x86")
     41 	set(DE_CPU					"DE_CPU_X86")
     42 	set(CMAKE_SYSTEM_PROCESSOR	i686-android-linux)
     43 
     44 	set(ANDROID_CC_PATH			"${ANDROID_NDK_PATH}/toolchains/x86-4.9/prebuilt/${ANDROID_NDK_HOST_OS}/")
     45 	set(CROSS_COMPILE			"${ANDROID_CC_PATH}bin/i686-linux-android-")
     46 	set(ANDROID_SYSROOT			"${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-x86")
     47 	set(LIBGCC					"${ANDROID_CC_PATH}lib/gcc/i686-linux-android/4.9/libgcc.a")
     48 
     49 	set(CMAKE_FIND_ROOT_PATH
     50 		"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}"
     51 		"${ANDROID_CC_PATH}i686-linux-android"
     52 		"${ANDROID_CC_PATH}lib/gcc/i686-linux-android/4.9"
     53 		${ANDROID_SYSROOT}
     54 		)
     55 
     56 	set(TARGET_C_FLAGS			"-march=i686 -msse3 -mstackrealign -mfpmath=sse")
     57 	set(TARGET_LINKER_FLAGS		"")
     58 
     59 elseif (ANDROID_ABI STREQUAL "armeabi" OR
     60 		ANDROID_ABI STREQUAL "armeabi-v7a")
     61 	set(DE_CPU					"DE_CPU_ARM")
     62 	set(CMAKE_SYSTEM_PROCESSOR	arm-linux-androideabi)
     63 
     64 	set(ANDROID_CC_PATH	"${ANDROID_NDK_PATH}/toolchains/arm-linux-androideabi-4.9/prebuilt/${ANDROID_NDK_HOST_OS}/")
     65 	set(CROSS_COMPILE	"${ANDROID_CC_PATH}bin/arm-linux-androideabi-")
     66 	set(ANDROID_SYSROOT	"${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-arm")
     67 
     68 	if (ANDROID_ABI STREQUAL "armeabi-v7a")
     69 		set(TARGET_C_FLAGS		"${ARM_C_FLAGS} -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp")
     70 		set(TARGET_LINKER_FLAGS	"-Wl,--fix-cortex-a8 -march=armv7-a")
     71 		set(LIBGCC				"${ANDROID_CC_PATH}lib/gcc/arm-linux-androideabi/4.9/armv7-a/libgcc.a")
     72 
     73 	else () # armeabi
     74 		set(TARGET_C_FLAGS		"${ARM_C_FLAGS} -march=armv5te -mfloat-abi=softfp")
     75 		set(TARGET_LINKER_FLAGS	"-Wl,--fix-cortex-a8 -march=armv5te")
     76 		set(LIBGCC				"${ANDROID_CC_PATH}lib/gcc/arm-linux-androideabi/4.9/libgcc.a")
     77 	endif ()
     78 
     79 	set(CMAKE_FIND_ROOT_PATH
     80 		"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}"
     81 		"${ANDROID_CC_PATH}arm-linux-androideabi"
     82 		${ANDROID_SYSROOT}
     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 	set(LIBGCC			"${ANDROID_CC_PATH}lib/gcc/aarch64-linux-android/4.9/libgcc.a")
     94 
     95 	set(CMAKE_FIND_ROOT_PATH
     96 		"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}"
     97 		"${ANDROID_CC_PATH}arm-linux-androideabi"
     98 		${ANDROID_SYSROOT}
     99 		)
    100 
    101 	set(TARGET_C_FLAGS		"-march=armv8-a -mabi=lp64")
    102 	set(TARGET_LINKER_FLAGS	"-march=armv8-a")
    103 
    104 else ()
    105 	message(FATAL_ERROR "Unknown ABI \"${ANDROID_ABI}\"")
    106 endif ()
    107 
    108 # crtbegin_so.o & crtend_so.o \todo [pyry] Is there some special CMake variable for these?
    109 if (DE_ANDROID_API GREATER 8)
    110 	set(CRTBEGIN_SO	"${ANDROID_SYSROOT}/usr/lib/crtbegin_so.o")
    111 	set(CRTEND_SO	"${ANDROID_SYSROOT}/usr/lib/crtend_so.o")
    112 endif ()
    113 
    114 # C++ library
    115 set(LIBCPP "${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}/libgnustl_static.a")
    116 
    117 include(CMakeForceCompiler)
    118 
    119 if (ANDROID_NDK_HOST_OS STREQUAL "linux-x86" OR
    120 	ANDROID_NDK_HOST_OS STREQUAL "linux-x86_64" OR
    121 	ANDROID_NDK_HOST_OS STREQUAL "darwin-x86")
    122 	cmake_force_c_compiler("${CROSS_COMPILE}gcc"		GNU)
    123 	cmake_force_cxx_compiler("${CROSS_COMPILE}g++"		GNU)
    124 elseif (ANDROID_NDK_HOST_OS STREQUAL "windows")
    125 	cmake_force_c_compiler("${CROSS_COMPILE}gcc.exe"	GNU)
    126 	cmake_force_cxx_compiler("${CROSS_COMPILE}g++.exe"	GNU)
    127 else ()
    128 	message(FATAL_ERROR "Unknown ANDROID_NDK_HOST_OS")
    129 endif ()
    130 
    131 set(CMAKE_SHARED_LIBRARY_C_FLAGS	"")
    132 set(CMAKE_SHARED_LIBRARY_CXX_FLAGS	"")
    133 
    134 set(CMAKE_C_CREATE_SHARED_LIBRARY	"<CMAKE_C_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
    135 set(CMAKE_CXX_CREATE_SHARED_LIBRARY	"<CMAKE_CXX_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_CXX_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -o <TARGET> ${CRTBEGIN_SO} <OBJECTS> <LINK_LIBRARIES> ${LIBCPP} ${LIBGCC} ${CRTEND_SO}")
    136 set(CMAKE_CXX_LINK_EXECUTABLE		"<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES> ${LIBCPP} ${LIBGCC}")
    137 
    138 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    139 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    140 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    141 
    142 # \note Without CACHE STRING FORCE cmake ignores these.
    143 set(CMAKE_C_FLAGS				"--sysroot=${ANDROID_SYSROOT} ${COMMON_C_FLAGS} ${TARGET_C_FLAGS}" CACHE STRING "" FORCE)
    144 set(CMAKE_CXX_FLAGS				"--sysroot=${ANDROID_SYSROOT} ${COMMON_CXX_FLAGS} ${TARGET_C_FLAGS} -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" CACHE STRING "" FORCE)
    145 set(CMAKE_SHARED_LINKER_FLAGS	"-nostdlib -Wl,-shared,-Bsymbolic -Wl,--no-undefined ${COMMON_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE)
    146 set(CMAKE_EXE_LINKER_FLAGS		"${COMMON_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE)
    147