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_GCC")
30if (NOT DEFINED DE_ANDROID_API)
31	set(DE_ANDROID_API 9)
32endif ()
33
34set(COMMON_C_FLAGS		"-mandroid -D__STDC_INT64__")
35set(COMMON_CXX_FLAGS	"${COMMON_C_FLAGS} -frtti -fexceptions")
36set(COMMON_LINKER_FLAGS	"")
37set(ARM_C_FLAGS			"-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ ")
38
39# ABI-dependent bits
40if (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
59elseif (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
85elseif (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
104else ()
105	message(FATAL_ERROR "Unknown ABI \"${ANDROID_ABI}\"")
106endif ()
107
108# crtbegin_so.o & crtend_so.o \todo [pyry] Is there some special CMake variable for these?
109if (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")
112endif ()
113
114# C++ library
115set(LIBCPP "${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}/libgnustl_static.a")
116
117include(CMakeForceCompiler)
118
119if (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)
124elseif (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)
127else ()
128	message(FATAL_ERROR "Unknown ANDROID_NDK_HOST_OS")
129endif ()
130
131set(CMAKE_SHARED_LIBRARY_C_FLAGS	"")
132set(CMAKE_SHARED_LIBRARY_CXX_FLAGS	"")
133
134set(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>")
135set(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}")
136set(CMAKE_CXX_LINK_EXECUTABLE		"<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES> ${LIBCPP} ${LIBGCC}")
137
138set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
139set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
140set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
141
142# \note Without CACHE STRING FORCE cmake ignores these.
143set(CMAKE_C_FLAGS				"--sysroot=${ANDROID_SYSROOT} ${COMMON_C_FLAGS} ${TARGET_C_FLAGS}" CACHE STRING "" FORCE)
144set(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)
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