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")
49
50if (NOT DEFINED DE_COMPILER)
51	set(DE_COMPILER	"DE_COMPILER_CLANG")
52endif ()
53
54if (NOT DEFINED DE_ANDROID_API)
55	set(DE_ANDROID_API 9)
56endif ()
57
58set(COMMON_C_FLAGS		"-D__STDC_INT64__")
59set(COMMON_CXX_FLAGS	"${COMMON_C_FLAGS} -frtti -fexceptions")
60set(COMMON_LINKER_FLAGS	"")
61set(ARM_C_FLAGS			"-D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ ")
62
63# ABI-dependent bits
64if (ANDROID_ABI STREQUAL "x86")
65	set(DE_CPU					"DE_CPU_X86")
66	set(CMAKE_SYSTEM_PROCESSOR	i686-android-linux)
67
68	set(ANDROID_CC_PATH			"${ANDROID_NDK_PATH}/toolchains/x86-4.9/prebuilt/${ANDROID_NDK_HOST_OS}/")
69	set(CROSS_COMPILE			"${ANDROID_CC_PATH}bin/i686-linux-android-")
70	set(ANDROID_SYSROOT			"${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-x86")
71
72	set(CMAKE_FIND_ROOT_PATH
73		"${ANDROID_CC_PATH}i686-linux-android"
74		"${ANDROID_CC_PATH}lib/gcc/i686-linux-android/4.9"
75		)
76
77	set(TARGET_C_FLAGS			"-march=i686 -msse3 -mstackrealign -mfpmath=sse")
78	set(TARGET_LINKER_FLAGS		"")
79	set(LLVM_TRIPLE				"i686-none-linux-android")
80
81elseif (ANDROID_ABI STREQUAL "armeabi" OR
82		ANDROID_ABI STREQUAL "armeabi-v7a")
83	set(DE_CPU					"DE_CPU_ARM")
84	set(CMAKE_SYSTEM_PROCESSOR	arm-linux-androideabi)
85
86	set(ANDROID_CC_PATH	"${ANDROID_NDK_PATH}/toolchains/arm-linux-androideabi-4.9/prebuilt/${ANDROID_NDK_HOST_OS}/")
87	set(CROSS_COMPILE	"${ANDROID_CC_PATH}bin/arm-linux-androideabi-")
88	set(ANDROID_SYSROOT	"${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-arm")
89
90	if (ANDROID_ABI STREQUAL "armeabi-v7a")
91		set(TARGET_C_FLAGS		"${ARM_C_FLAGS} -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp")
92		set(TARGET_LINKER_FLAGS	"-Wl,--fix-cortex-a8 -march=armv7-a")
93		set(LLVM_TRIPLE			"armv7-none-linux-androideabi")
94
95	else () # armeabi
96		set(TARGET_C_FLAGS		"${ARM_C_FLAGS} -march=armv5te -mfloat-abi=softfp")
97		set(TARGET_LINKER_FLAGS	"-Wl,--fix-cortex-a8 -march=armv5te")
98		set(LLVM_TRIPLE			"armv5te-none-linux-androideabi")
99	endif ()
100
101	set(CMAKE_FIND_ROOT_PATH
102		"${ANDROID_CC_PATH}arm-linux-androideabi"
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
114	set(CMAKE_FIND_ROOT_PATH
115		"${ANDROID_CC_PATH}arm-linux-androideabi"
116		)
117
118	set(TARGET_C_FLAGS		"-march=armv8-a")
119	set(TARGET_LINKER_FLAGS	"-Wl,--fix-cortex-a53-835769 -Wl,--fix-cortex-a53-835769 -march=armv8-a")
120	set(LLVM_TRIPLE			"aarch64-none-linux-android")
121
122	if (DE_COMPILER STREQUAL "DE_COMPILER_GCC")
123		set(TARGET_C_FLAGS "${TARGET_C_FLAGS} -mabi=lp64")
124	endif ()
125
126elseif (ANDROID_ABI STREQUAL "x86_64")
127	set(DE_CPU					"DE_CPU_X86_64")
128	set(CMAKE_SYSTEM_PROCESSOR	x86_64-linux-android)
129	set(CMAKE_SIZEOF_VOID_P		8)
130
131	set(CMAKE_LIBRARY_PATH "/usr/lib64")
132
133	set(ANDROID_CC_PATH	"${ANDROID_NDK_PATH}/toolchains/x86_64-4.9/prebuilt/${ANDROID_NDK_HOST_OS}/")
134	set(CROSS_COMPILE	"${ANDROID_CC_PATH}bin/x86_64-linux-android-")
135	set(ANDROID_SYSROOT	"${ANDROID_NDK_PATH}/platforms/${ANDROID_NDK_TARGET}/arch-x86_64")
136
137	set(CMAKE_FIND_ROOT_PATH
138		"${ANDROID_CC_PATH}x86_64-linux-android"
139		)
140
141	set(LLVM_TRIPLE			"x86_64-none-linux-android")
142
143else ()
144	message(FATAL_ERROR "Unknown ABI \"${ANDROID_ABI}\"")
145endif ()
146
147# C++ library
148if (ANDROID_ABI STREQUAL "x86")
149	# Use gnu-libstc++, since libc++ is currently broken on x86, bug #22124988
150	set(ANDROID_CXX_LIBRARY		"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}/libgnustl_static.a")
151	set(CXX_INCLUDES			"-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")
152	set(CMAKE_FIND_ROOT_PATH	"${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/4.9/libs/${ANDROID_ABI}" ${CMAKE_FIND_ROOT_PATH})
153else ()
154	# Use LLVM libc++ for full C++11 support
155	set(ANDROID_CXX_LIBRARY		"${ANDROID_NDK_PATH}/sources/cxx-stl/llvm-libc++/libs/${ANDROID_ABI}/libc++_static.a")
156	set(CXX_INCLUDES			"-I${ANDROID_NDK_PATH}/sources/cxx-stl/llvm-libc++/libcxx/include")
157	set(CMAKE_FIND_ROOT_PATH	"" ${CMAKE_FIND_ROOT_PATH})
158endif ()
159
160set(CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH} ${ANDROID_SYSROOT})
161
162include(CMakeForceCompiler)
163
164if (DE_COMPILER STREQUAL "DE_COMPILER_GCC")
165	if (ANDROID_NDK_HOST_OS STREQUAL "linux-x86" OR
166		ANDROID_NDK_HOST_OS STREQUAL "linux-x86_64" OR
167		ANDROID_NDK_HOST_OS STREQUAL "darwin-x86")
168		cmake_force_c_compiler("${CROSS_COMPILE}gcc"		GNU)
169		cmake_force_cxx_compiler("${CROSS_COMPILE}g++"		GNU)
170	elseif (ANDROID_NDK_HOST_OS STREQUAL "windows")
171		cmake_force_c_compiler("${CROSS_COMPILE}gcc.exe"	GNU)
172		cmake_force_cxx_compiler("${CROSS_COMPILE}g++.exe"	GNU)
173	else ()
174		message(FATAL_ERROR "Unknown ANDROID_NDK_HOST_OS")
175	endif ()
176
177	set(TARGET_C_FLAGS		"-mandroid ${TARGET_C_FLAGS}")
178
179elseif (DE_COMPILER STREQUAL "DE_COMPILER_CLANG")
180	if (NOT DEFINED LLVM_VERSION)
181		if (ANDROID_NDK_HOST_OS STREQUAL "windows" OR
182			ANDROID_NDK_HOST_OS STREQUAL "windows-x86_64")
183			# Windows NDK prebuilts don't include llvm-ar tool in version 3.6
184			set(LLVM_VERSION "3.5")
185		else ()
186			set(LLVM_VERSION "3.6")
187		endif ()
188	endif ()
189
190	set(LLVM_PATH "${ANDROID_NDK_PATH}/toolchains/llvm-${LLVM_VERSION}/prebuilt/${ANDROID_NDK_HOST_OS}/")
191
192	if (ANDROID_NDK_HOST_OS STREQUAL "linux-x86" OR
193		ANDROID_NDK_HOST_OS STREQUAL "linux-x86_64" OR
194		ANDROID_NDK_HOST_OS STREQUAL "darwin-x86" OR
195		ANDROID_NDK_HOST_OS STREQUAL "darwin-x86_64")
196		cmake_force_c_compiler("${LLVM_PATH}bin/clang"			Clang)
197		cmake_force_cxx_compiler("${LLVM_PATH}bin/clang++"		Clang)
198		set(CMAKE_AR "${LLVM_PATH}/bin/llvm-ar" CACHE FILEPATH "Archiver")
199		set(CMAKE_RANLIB "${CROSS_COMPILE}ranlib" CACHE FILEPATH "Indexer")
200	elseif (ANDROID_NDK_HOST_OS STREQUAL "windows" OR
201			ANDROID_NDK_HOST_OS STREQUAL "windows-x86_64")
202		cmake_force_c_compiler("${LLVM_PATH}bin/clang.exe"		Clang)
203		cmake_force_cxx_compiler("${LLVM_PATH}bin/clang++.exe"	Clang)
204		set(CMAKE_AR "${LLVM_PATH}bin/llvm-ar.exe" CACHE FILEPATH "Archiver")
205	else ()
206		message(FATAL_ERROR "Unknown ANDROID_NDK_HOST_OS")
207	endif ()
208
209	set(TARGET_C_FLAGS		"-target ${LLVM_TRIPLE} -gcc-toolchain ${ANDROID_CC_PATH} ${TARGET_C_FLAGS}")
210	set(TARGET_LINKER_FLAGS	"-target ${LLVM_TRIPLE} -gcc-toolchain ${ANDROID_CC_PATH} ${TARGET_LINKER_FLAGS}")
211
212endif ()
213
214set(CMAKE_SHARED_LIBRARY_C_FLAGS	"")
215set(CMAKE_SHARED_LIBRARY_CXX_FLAGS	"")
216
217set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
218set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
219set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
220
221# \note Without CACHE STRING FORCE cmake ignores these.
222set(CMAKE_C_FLAGS				"--sysroot=${ANDROID_SYSROOT} ${COMMON_C_FLAGS} ${TARGET_C_FLAGS}" CACHE STRING "" FORCE)
223set(CMAKE_CXX_FLAGS				"--sysroot=${ANDROID_SYSROOT} ${COMMON_CXX_FLAGS} ${TARGET_C_FLAGS} ${CXX_INCLUDES} -I${ANDROID_NDK_PATH}/sources/android/support/include" CACHE STRING "" FORCE)
224set(CMAKE_SHARED_LINKER_FLAGS	"-nodefaultlibs -Wl,-shared,-Bsymbolic -Wl,--no-undefined ${COMMON_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE)
225set(CMAKE_EXE_LINKER_FLAGS		"-nodefaultlibs ${COMMON_LINKER_FLAGS} ${TARGET_LINKER_FLAGS}" CACHE STRING "" FORCE)
226