1# Platform defines.
2set(CMAKE_SYSTEM_NAME Linux)
3
4set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
5
6set(CMAKE_CROSSCOMPILING 1)
7
8# NDK installation path
9if (NOT DEFINED ANDROID_NDK_PATH)
10	message(FATAL_ERROR "Please provide ANDROID_NDK_PATH")
11endif ()
12
13# Host os (for toolchain binaries)
14if (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 ()
21endif ()
22
23# Compile target
24set(ANDROID_ABI			"armeabi-v7a"			CACHE STRING "Android ABI")
25set(ANDROID_NDK_TARGET	"android-${DE_ANDROID_API}")
26
27# dE defines
28set(DE_OS		"DE_OS_ANDROID")
29set(DE_COMPILER	"DE_COMPILER_CLANG")
30if (NOT DEFINED DE_ANDROID_API)
31	set(DE_ANDROID_API 5)
32endif ()
33
34# Select cpu
35if (ANDROID_ABI STREQUAL "x86")
36	set(DE_CPU					"DE_CPU_X86")
37	set(CMAKE_SYSTEM_PROCESSOR	i686-android-linux)
38else ()
39	set(DE_CPU					"DE_CPU_ARM")
40	set(CMAKE_SYSTEM_PROCESSOR	arm-linux-androideabi)
41endif ()
42
43# Path to LLVM
44set(LLVM_PATH	"${ANDROID_NDK_PATH}/toolchains/llvm-3.3/prebuilt/${ANDROID_NDK_HOST_OS}/")
45
46# Cross-compiler, search paths and sysroot
47if (ANDROID_ABI STREQUAL "x86")
48	set(ANDROID_CC_PATH	"${ANDROID_NDK_PATH}/toolchains/x86-4.8/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_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.8/libs/${ANDROID_ABI}"
54		"${ANDROID_CC_PATH}i686-linux-android"
55		"${ANDROID_CC_PATH}lib/gcc/i686-linux-android/4.8"
56		${ANDROID_SYSROOT}
57		)
58
59else ()
60	set(ANDROID_CC_PATH	"${ANDROID_NDK_PATH}/toolchains/arm-linux-androideabi-4.8/prebuilt/${ANDROID_NDK_HOST_OS}/")
61	set(CROSS_COMPILE	"${ANDROID_CC_PATH}bin/arm-linux-androideabi-")
62	set(ANDROID_SYSROOT	"${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-arm")
63
64	set(CMAKE_FIND_ROOT_PATH
65		"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.8/libs/${ANDROID_ABI}"
66		"${ANDROID_CC_PATH}arm-linux-androideabi"
67		${ANDROID_SYSROOT}
68		)
69
70endif ()
71
72# crtbegin_so.o & crtend_so.o \todo [pyry] Is there some special CMake variable for these?
73if (DE_ANDROID_API GREATER 8)
74	set(CRTBEGIN_SO	"${ANDROID_SYSROOT}/usr/lib/crtbegin_so.o")
75	set(CRTEND_SO	"${ANDROID_SYSROOT}/usr/lib/crtend_so.o")
76endif ()
77
78# libgcc
79if (ANDROID_ABI STREQUAL "armeabi-v7a")
80	set(LIBGCC "${ANDROID_CC_PATH}lib/gcc/arm-linux-androideabi/4.8/armv7-a/libgcc.a")
81elseif (ANDROID_ABI STREQUAL "armeabi")
82	set(LIBGCC "${ANDROID_CC_PATH}lib/gcc/arm-linux-androideabi/4.8/libgcc.a")
83elseif (ANDROID_ABI STREQUAL "x86")
84	set(LIBGCC "${ANDROID_CC_PATH}lib/gcc/i686-linux-android/4.8/libgcc.a")
85else ()
86	message(FATAL_ERROR "Don't know where libgcc.a is")
87endif ()
88
89# C++ library
90set(LIBCPP "${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.8/libs/${ANDROID_ABI}/libgnustl_static.a")
91
92include(CMakeForceCompiler)
93
94# Compiler binaries
95if (ANDROID_NDK_HOST_OS STREQUAL "linux-x86" OR
96	ANDROID_NDK_HOST_OS STREQUAL "darwin-x86")
97	cmake_force_c_compiler("${LLVM_PATH}bin/clang"			Clang)
98	cmake_force_cxx_compiler("${LLVM_PATH}bin/clang++"		Clang)
99	set(CMAKE_AR "${CROSS_COMPILE}ar" CACHE FILEPATH "Archiver")
100elseif (ANDROID_NDK_HOST_OS STREQUAL "windows")
101	cmake_force_c_compiler("${LLVM_PATH}bin/clang.exe"		Clang)
102	cmake_force_cxx_compiler("${LLVM_PATH}bin/clang++.exe"	Clang)
103	set(CMAKE_AR "${CROSS_COMPILE}ar.exe" CACHE FILEPATH "Archiver")
104else ()
105	message(FATAL_ERROR "Unknown ANDROID_NDK_HOST_OS")
106endif ()
107
108set(CMAKE_SHARED_LIBRARY_C_FLAGS	"")
109set(CMAKE_SHARED_LIBRARY_CXX_FLAGS	"")
110
111
112set(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>")
113set(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}")
114set(CMAKE_CXX_LINK_EXECUTABLE		"<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES> ${LIBCPP} ${LIBGCC}")
115
116set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
117set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
118set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
119
120set(COMMON_C_FLAGS		"-gcc-toolchain ${ANDROID_CC_PATH} --sysroot=${ANDROID_SYSROOT} -fpic -no-canonical-prefixes -DANDROID -D__ANDROID__ -D__STDC_INT64__")
121set(COMMON_LINKER_FLAGS	"")
122set(ARM_C_FLAGS			"-mfloat-abi=softfp -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ ")
123
124if (ANDROID_ABI STREQUAL "armeabi-v7a")
125	# For armeabi-v7a
126	set(TARGET_C_FLAGS		"${ARM_C_FLAGS} -target armv7-none-linux-androideabi -march=armv7-a -mfpu=vfpv3-d16")
127	set(TARGET_LINKER_FLAGS	"-Wl,--fix-cortex-a8 -march=armv7-a")
128
129elseif (ANDROID_ABI STREQUAL "armeabi")
130	# For armeabi (ARMv5TE)
131	set(TARGET_C_FLAGS		"${ARM_C_FLAGS} -target armv5te-none-linux-androideabi -march=armv5te")
132	set(TARGET_LINKER_FLAGS	"${ARM_LINKER_FLAGS} -march=armv5te")
133
134elseif (ANDROID_ABI STREQUAL "x86")
135	set(TARGET_C_FLAGS		"-target i686-none-linux-android -march=i686 -msse3 -mstackrealign")
136	set(TARGET_LINKER_FLAGS	"")
137
138else ()
139	message(FATAL_ERROR "Unknown Android ABI \"${ANDROID_ABI}\"")
140endif ()
141
142# \note Without CACHE STRING FORCE cmake ignores these.
143set(CMAKE_C_FLAGS				"${COMMON_C_FLAGS} ${TARGET_C_FLAGS}" CACHE STRING "" FORCE)
144set(CMAKE_CXX_FLAGS				"${COMMON_C_FLAGS} ${TARGET_C_FLAGS} -I${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.8/include -I${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.8/libs/${ANDROID_ABI}/include" CACHE STRING "" FORCE)
145set(CMAKE_SHARED_LINKER_FLAGS	"-nostdlib -Wl,-shared,-Bsymbolic -Wl,--no-undefined ${COMMON_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE)
146set(CMAKE_EXE_LINKER_FLAGS		"${COMMON_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE)
147