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