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