1#-------------------------------------------------------------------------
2# drawElements CMake utilities
3# ----------------------------
4#
5# Copyright 2016 The Android Open Source Project
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11#      http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19#-------------------------------------------------------------------------
20
21# Platform defines.
22set(CMAKE_SYSTEM_NAME Linux)
23
24set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
25
26set(CMAKE_CROSSCOMPILING 1)
27
28# NDK installation path
29if (NOT DEFINED ANDROID_NDK_PATH)
30	message(FATAL_ERROR "Please provide ANDROID_NDK_PATH")
31endif ()
32
33# Host os (for toolchain binaries)
34if (NOT DEFINED ANDROID_NDK_HOST_OS)
35	message(STATUS "Warning: ANDROID_NDK_HOST_OS is not set")
36	if (WIN32)
37		set(ANDROID_NDK_HOST_OS "windows")
38	elseif (UNIX)
39		set(ANDROID_NDK_HOST_OS "linux-86")
40	endif ()
41endif ()
42
43# Compile target
44set(ANDROID_ABI			"armeabi-v7a"			CACHE STRING "Android ABI")
45set(ANDROID_NDK_TARGET	"android-${DE_ANDROID_API}")
46
47# dE defines
48set(DE_OS		"DE_OS_ANDROID")
49set(DE_COMPILER	"DE_COMPILER_GCC")
50if (NOT DEFINED DE_ANDROID_API)
51	set(DE_ANDROID_API 9)
52endif ()
53
54set(COMMON_C_FLAGS		"-mandroid -D__STDC_INT64__")
55set(COMMON_CXX_FLAGS	"${COMMON_C_FLAGS} -frtti -fexceptions")
56set(COMMON_LINKER_FLAGS	"")
57set(ARM_C_FLAGS			"-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ ")
58
59# ABI-dependent bits
60if (ANDROID_ABI STREQUAL "x86")
61	set(DE_CPU					"DE_CPU_X86")
62	set(CMAKE_SYSTEM_PROCESSOR	i686-android-linux)
63
64	set(ANDROID_CC_PATH			"${ANDROID_NDK_PATH}/toolchains/x86-4.9/prebuilt/${ANDROID_NDK_HOST_OS}/")
65	set(CROSS_COMPILE			"${ANDROID_CC_PATH}bin/i686-linux-android-")
66	set(ANDROID_SYSROOT			"${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-x86")
67	set(LIBGCC					"${ANDROID_CC_PATH}lib/gcc/i686-linux-android/4.9/libgcc.a")
68
69	set(CMAKE_FIND_ROOT_PATH
70		"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}"
71		"${ANDROID_CC_PATH}i686-linux-android"
72		"${ANDROID_CC_PATH}lib/gcc/i686-linux-android/4.9"
73		${ANDROID_SYSROOT}
74		)
75
76	set(TARGET_C_FLAGS			"-march=i686 -msse3 -mstackrealign -mfpmath=sse")
77	set(TARGET_LINKER_FLAGS		"")
78
79elseif (ANDROID_ABI STREQUAL "armeabi" OR
80		ANDROID_ABI STREQUAL "armeabi-v7a")
81	set(DE_CPU					"DE_CPU_ARM")
82	set(CMAKE_SYSTEM_PROCESSOR	arm-linux-androideabi)
83
84	set(ANDROID_CC_PATH	"${ANDROID_NDK_PATH}/toolchains/arm-linux-androideabi-4.9/prebuilt/${ANDROID_NDK_HOST_OS}/")
85	set(CROSS_COMPILE	"${ANDROID_CC_PATH}bin/arm-linux-androideabi-")
86	set(ANDROID_SYSROOT	"${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-arm")
87
88	if (ANDROID_ABI STREQUAL "armeabi-v7a")
89		set(TARGET_C_FLAGS		"${ARM_C_FLAGS} -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp")
90		set(TARGET_LINKER_FLAGS	"-Wl,--fix-cortex-a8 -march=armv7-a")
91		set(LIBGCC				"${ANDROID_CC_PATH}lib/gcc/arm-linux-androideabi/4.9/armv7-a/libgcc.a")
92
93	else () # armeabi
94		set(TARGET_C_FLAGS		"${ARM_C_FLAGS} -march=armv5te -mfloat-abi=softfp")
95		set(TARGET_LINKER_FLAGS	"-Wl,--fix-cortex-a8 -march=armv5te")
96		set(LIBGCC				"${ANDROID_CC_PATH}lib/gcc/arm-linux-androideabi/4.9/libgcc.a")
97	endif ()
98
99	set(CMAKE_FIND_ROOT_PATH
100		"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}"
101		"${ANDROID_CC_PATH}arm-linux-androideabi"
102		${ANDROID_SYSROOT}
103		)
104
105elseif (ANDROID_ABI STREQUAL "arm64-v8a")
106	set(DE_CPU					"DE_CPU_ARM_64")
107	set(CMAKE_SYSTEM_PROCESSOR	aarch64-linux-android)
108	set(CMAKE_SIZEOF_VOID_P		8)
109
110	set(ANDROID_CC_PATH	"${ANDROID_NDK_PATH}/toolchains/aarch64-linux-android-4.9/prebuilt/${ANDROID_NDK_HOST_OS}/")
111	set(CROSS_COMPILE	"${ANDROID_CC_PATH}bin/aarch64-linux-android-")
112	set(ANDROID_SYSROOT	"${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-arm64")
113	set(LIBGCC			"${ANDROID_CC_PATH}lib/gcc/aarch64-linux-android/4.9/libgcc.a")
114
115	set(CMAKE_FIND_ROOT_PATH
116		"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}"
117		"${ANDROID_CC_PATH}arm-linux-androideabi"
118		${ANDROID_SYSROOT}
119		)
120
121	set(TARGET_C_FLAGS		"-march=armv8-a -mabi=lp64")
122	set(TARGET_LINKER_FLAGS	"-march=armv8-a")
123
124else ()
125	message(FATAL_ERROR "Unknown ABI \"${ANDROID_ABI}\"")
126endif ()
127
128# crtbegin_so.o & crtend_so.o \todo [pyry] Is there some special CMake variable for these?
129if (DE_ANDROID_API GREATER 8)
130	set(CRTBEGIN_SO	"${ANDROID_SYSROOT}/usr/lib/crtbegin_so.o")
131	set(CRTEND_SO	"${ANDROID_SYSROOT}/usr/lib/crtend_so.o")
132endif ()
133
134# C++ library
135set(LIBCPP "${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}/libgnustl_static.a")
136
137include(CMakeForceCompiler)
138
139if (ANDROID_NDK_HOST_OS STREQUAL "linux-x86" OR
140	ANDROID_NDK_HOST_OS STREQUAL "linux-x86_64" OR
141	ANDROID_NDK_HOST_OS STREQUAL "darwin-x86")
142	cmake_force_c_compiler("${CROSS_COMPILE}gcc"		GNU)
143	cmake_force_cxx_compiler("${CROSS_COMPILE}g++"		GNU)
144elseif (ANDROID_NDK_HOST_OS STREQUAL "windows")
145	cmake_force_c_compiler("${CROSS_COMPILE}gcc.exe"	GNU)
146	cmake_force_cxx_compiler("${CROSS_COMPILE}g++.exe"	GNU)
147else ()
148	message(FATAL_ERROR "Unknown ANDROID_NDK_HOST_OS")
149endif ()
150
151set(CMAKE_SHARED_LIBRARY_C_FLAGS	"")
152set(CMAKE_SHARED_LIBRARY_CXX_FLAGS	"")
153
154set(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>")
155set(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}")
156set(CMAKE_CXX_LINK_EXECUTABLE		"<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> -o <TARGET> <OBJECTS> <LINK_LIBRARIES> ${LIBCPP} ${LIBGCC}")
157
158set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
159set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
160set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
161
162# \note Without CACHE STRING FORCE cmake ignores these.
163set(CMAKE_C_FLAGS				"--sysroot=${ANDROID_SYSROOT} ${COMMON_C_FLAGS} ${TARGET_C_FLAGS}" CACHE STRING "" FORCE)
164set(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)
165set(CMAKE_SHARED_LINKER_FLAGS	"-nostdlib -Wl,-shared,-Bsymbolic -Wl,--no-undefined ${COMMON_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE)
166set(CMAKE_EXE_LINKER_FLAGS		"${COMMON_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE)
167