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 # dE defines
      9 set(DE_OS		"DE_OS_ANDROID")
     10 set(DE_COMPILER	"DE_COMPILER_GCC")
     11 if (NOT DEFINED DE_ANDROID_API)
     12 	set(DE_ANDROID_API 5)
     13 endif ()
     14 
     15 # NDK installation path
     16 set(ANDROID_NDK_PATH	"/opt/android-ndk-r8"	CACHE STRING "Android NDK installation path")
     17 set(ANDROID_NDK_HOST_OS	"linux-x86"				CACHE STRING "Android ndk host os")
     18 set(ANDROID_ABI			"armeabi-v7a"			CACHE STRING "Android ABI")
     19 set(ANDROID_NDK_TARGET	"android-${DE_ANDROID_API}")
     20 
     21 # Select cpu
     22 if (ANDROID_ABI STREQUAL "x86")
     23 	set(DE_CPU					"DE_CPU_X86")
     24 	set(CMAKE_SYSTEM_PROCESSOR	i686-android-linux)
     25 else ()
     26 	set(DE_CPU					"DE_CPU_ARM")
     27 	set(CMAKE_SYSTEM_PROCESSOR	arm-linux-androideabi)
     28 endif ()
     29 
     30 # Cross-compiler, search paths and sysroot
     31 if (ANDROID_ABI STREQUAL "x86")
     32 	set(ANDROID_CC_PATH	"${ANDROID_NDK_PATH}/toolchains/x86-4.4.3/prebuilt/${ANDROID_NDK_HOST_OS}/")
     33 	set(CROSS_COMPILE	"${ANDROID_CC_PATH}bin/i686-android-linux-")
     34 	set(ANDROID_SYSROOT	"${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-x86")
     35 
     36 	set(CMAKE_FIND_ROOT_PATH
     37 		"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/libs/${ANDROID_ABI}"
     38 		"${ANDROID_CC_PATH}i686-android-linux"
     39 		"${ANDROID_CC_PATH}lib/gcc/i686-android-linux/4.4.3"
     40 		${ANDROID_SYSROOT}
     41 		)
     42 
     43 else ()
     44 	set(ANDROID_CC_PATH	"${ANDROID_NDK_PATH}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/${ANDROID_NDK_HOST_OS}/")
     45 	set(CROSS_COMPILE	"${ANDROID_CC_PATH}bin/arm-linux-androideabi-")
     46 	set(ANDROID_SYSROOT	"${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-arm")
     47 
     48 	set(CMAKE_FIND_ROOT_PATH
     49 		"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/libs/${ANDROID_ABI}"
     50 		"${ANDROID_CC_PATH}arm-linux-androideabi"
     51 		${ANDROID_SYSROOT}
     52 		)
     53 
     54 endif ()
     55 
     56 # crtbegin_so.o & crtend_so.o \todo [pyry] Is there some special CMake variable for these?
     57 if (DE_ANDROID_API GREATER 8)
     58 	set(CRTBEGIN_SO	"${ANDROID_SYSROOT}/usr/lib/crtbegin_so.o")
     59 	set(CRTEND_SO	"${ANDROID_SYSROOT}/usr/lib/crtend_so.o")
     60 endif ()
     61 
     62 # libgcc
     63 if (ANDROID_ABI STREQUAL "armeabi-v7a")
     64 	set(LIBGCC "${ANDROID_CC_PATH}lib/gcc/arm-linux-androideabi/4.4.3/armv7-a/libgcc.a")
     65 elseif (ANDROID_ABI STREQUAL "armeabi")
     66 	set(LIBGCC "${ANDROID_CC_PATH}lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a")
     67 elseif (ANDROID_ABI STREQUAL "x86")
     68 	set(LIBGCC "${ANDROID_CC_PATH}lib/gcc/i686-android-linux/4.4.3/libgcc.a")
     69 else ()
     70 	message(FATAL_ERROR "Don't know where libgcc.a is")
     71 endif ()
     72 
     73 include(CMakeForceCompiler)
     74 
     75 if (ANDROID_NDK_HOST_OS STREQUAL "linux-x86" OR
     76 	ANDROID_NDK_HOST_OS STREQUAL "darwin-x86")
     77 	cmake_force_c_compiler("${CROSS_COMPILE}gcc"		GNU)
     78 	cmake_force_cxx_compiler("${CROSS_COMPILE}g++"		GNU)
     79 elseif (ANDROID_NDK_HOST_OS STREQUAL "windows")
     80 	cmake_force_c_compiler("${CROSS_COMPILE}gcc.exe"	GNU)
     81 	cmake_force_cxx_compiler("${CROSS_COMPILE}g++.exe"	GNU)
     82 else ()
     83 	message(FATAL_ERROR "Unknown ANDROID_NDK_HOST_OS")
     84 endif ()
     85 
     86 set(CMAKE_SHARED_LIBRARY_C_FLAGS "")
     87 set(CMAKE_SHARED_LIBRARY_CXX_FLAGS "")
     88 
     89 set(CMAKE_C_CREATE_SHARED_LIBRARY
     90   "<CMAKE_C_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_C_FLAGS> <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
     91 set(CMAKE_CXX_CREATE_SHARED_LIBRARY
     92   "<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> ${LIBGCC} ${CRTEND_SO}")
     93 
     94 set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
     95 set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
     96 set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
     97 
     98 set(COMMON_C_FLAGS		"--sysroot=${ANDROID_SYSROOT} -fpic -Os -DANDROID -D__ANDROID__ -D__STDC_INT64__")
     99 set(COMMON_LINKER_FLAGS	"-nostdlib -Wl,-shared,-Bsymbolic -Wl,--no-undefined")
    100 set(ARM_C_FLAGS			"-mfloat-abi=softfp -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ ")
    101 set(ARM_LINKER_FLAGS	"-Wl,--fix-cortex-a8")
    102 
    103 if (ANDROID_ABI STREQUAL "armeabi-v7a")
    104 	# For armeabi-v7a
    105 	set(TARGET_C_FLAGS		"${COMMON_C_FLAGS} ${ARM_C_FLAGS} -march=armv7-a -mfpu=vfpv3-d16")
    106 	set(TARGET_LINKER_FLAGS	"${COMMON_LINKER_FLAGS} ${ARM_LINKER_FLAGS} -march=armv7-a")
    107 
    108 elseif (ANDROID_ABI STREQUAL "armeabi")
    109 	# For armeabi (ARMv5TE)
    110 	set(TARGET_C_FLAGS		"${COMMON_C_FLAGS} ${ARM_C_FLAGS} -march=armv5te")
    111 	set(TARGET_LINKER_FLAGS	"${COMMON_LINKER_FLAGS} ${ARM_LINKER_FLAGS} -march=armv5te")
    112 
    113 elseif (ANDROID_ABI STREQUAL "x86")
    114 	set(TARGET_C_FLAGS		"${COMMON_C_FLAGS} -march=i686 -msse3 -mstackrealign -mfpmath=sse")
    115 	set(TARGET_LINKER_FLAGS	"${COMMON_LINKER_FLAGS}")
    116 
    117 else ()
    118 	message(FATAL_ERROR "Unknown Android ABI \"${ANDROID_ABI}\"")
    119 endif ()
    120 
    121 # \note Hacky workaround for flags...
    122 set(CMAKE_C_FLAGS	"${TARGET_C_FLAGS}" CACHE STRING "" FORCE)
    123 set(CMAKE_CXX_FLAGS	"${TARGET_C_FLAGS} -I${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/include -I${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/libs/${ANDROID_ABI}/include" CACHE STRING "" FORCE)
    124 set(CMAKE_SHARED_LINKER_FLAGS "${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE)
    125