toolchain-android.cmake 9.89 KB
Newer Older
Ghislain MARY's avatar
Ghislain MARY committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
############################################################################
# toolchain-android.cmake
# Copyright (C) 2016  Belledonne Communications, Grenoble France
#
############################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Ghislain MARY's avatar
Ghislain MARY committed
20 21 22
#
############################################################################

Ghislain MARY's avatar
Ghislain MARY committed
23 24 25
if(NOT CMAKE_ANDROID_API)
	set(CMAKE_ANDROID_API 14)
endif()
26
set(CMAKE_ANDROID_STL_TYPE gnustl_shared)
Ghislain MARY's avatar
Ghislain MARY committed
27

28 29 30 31 32 33 34
set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_POSITION_INDEPENDENT_CODE YES)

# Define name of the target system
set(CMAKE_SYSTEM_NAME "Linux")
set(ANDROID True)

35 36
find_program(ANDROID_NDK_BUILD_PROGRAM ndk-build)
if(NOT ANDROID_NDK_BUILD_PROGRAM)
Ghislain MARY's avatar
Ghislain MARY committed
37 38
	message(FATAL_ERROR "Cannot find 'ndk-build', make sure you installed the NDK and added it to your PATH")
endif()
39
get_filename_component(ANDROID_NDK_PATH "${ANDROID_NDK_BUILD_PROGRAM}" DIRECTORY)
Ghislain MARY's avatar
Ghislain MARY committed
40

41 42
find_program(ANDROID_ANDROID_PROGRAM android)
if(NOT ANDROID_ANDROID_PROGRAM)
Ghislain MARY's avatar
Ghislain MARY committed
43 44
	message(FATAL_ERROR "Cannot find 'android', make sure you installed the SDK and added it to your PATH")
endif()
45
get_filename_component(ANDROID_SDK_PATH "${ANDROID_ANDROID_PROGRAM}" DIRECTORY)
Ghislain MARY's avatar
Ghislain MARY committed
46

47 48
find_program(ANDROID_ADB_PROGRAM adb)
if(NOT ANDROID_ADB_PROGRAM)
Ghislain MARY's avatar
Ghislain MARY committed
49 50
	message(FATAL_ERROR "Cannot find 'adb', make sure you installed the SDK platform tools and added it to your PATH")
endif()
51
get_filename_component(ANDROID_SDK_PLATFORM_TOOLS_PATH "${ANDROID_ADB_PROGRAM}" DIRECTORY)
Ghislain MARY's avatar
Ghislain MARY committed
52

53
find_file(CLANG_EXECUTABLE "clang"
Ghislain MARY's avatar
Ghislain MARY committed
54
	PATHS
55 56 57 58
	"${ANDROID_NDK_PATH}/toolchains/llvm/prebuilt/linux-x86_64/bin"
	"${ANDROID_NDK_PATH}/toolchains/llvm/prebuilt/darwin-x86_64/bin"
	"${ANDROID_NDK_PATH}/toolchains/llvm/prebuilt/linux-x86/bin"
	NO_DEFAULT_PATH
Ghislain MARY's avatar
Ghislain MARY committed
59
)
60
if(CLANG_EXECUTABLE)
Ghislain MARY's avatar
Ghislain MARY committed
61

62
	get_filename_component(TOOLCHAIN_PATH "${CLANG_EXECUTABLE}" DIRECTORY)
63
	set(GCC_VERSION "4.9")
Ghislain MARY's avatar
Ghislain MARY committed
64
	if(NDK_ARCHITECTURE STREQUAL "armeabi-v7a")
65 66
		set(GCC_LIBRARY_ADDITIONAL_DIR "/armv7-a")
	endif()
Ghislain MARY's avatar
Ghislain MARY committed
67

68 69 70
	find_file(GCC_LIBRARY "libgcc.a"
		PATHS
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}${GCC_LIBRARY_ADDITIONAL_DIR}"
Ghislain MARY's avatar
Ghislain MARY committed
71
		"${ANDROID_NDK_PATH}/toolchains/${NDK_ARCHITECTURE}-${GCC_VERSION}/prebuilt/linux-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}"
72
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}.x${GCC_LIBRARY_ADDITIONAL_DIR}"
Ghislain MARY's avatar
Ghislain MARY committed
73
		"${ANDROID_NDK_PATH}/toolchains/${NDK_ARCHITECTURE}-${GCC_VERSION}/prebuilt/linux-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}.x"
74
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/darwin-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}${GCC_LIBRARY_ADDITIONAL_DIR}"
Ghislain MARY's avatar
Ghislain MARY committed
75
		"${ANDROID_NDK_PATH}/toolchains/${NDK_ARCHITECTURE}-${GCC_VERSION}/prebuilt/darwin-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}"
76
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/darwin-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}.x${GCC_LIBRARY_ADDITIONAL_DIR}"
Ghislain MARY's avatar
Ghislain MARY committed
77
		"${ANDROID_NDK_PATH}/toolchains/${NDK_ARCHITECTURE}-${GCC_VERSION}/prebuilt/darwin-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}.x"
78
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}${GCC_LIBRARY_ADDITIONAL_DIR}"
Ghislain MARY's avatar
Ghislain MARY committed
79
		"${ANDROID_NDK_PATH}/toolchains/${NDK_ARCHITECTURE}-${GCC_VERSION}/prebuilt/linux-x86/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}"
80
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}.x${GCC_LIBRARY_ADDITIONAL_DIR}"
Ghislain MARY's avatar
Ghislain MARY committed
81
		"${ANDROID_NDK_PATH}/toolchains/${NDK_ARCHITECTURE}-${GCC_VERSION}/prebuilt/linux-x86/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}.x"
82 83 84 85 86 87
		NO_DEFAULT_PATH
	)
	if(NOT GCC_LIBRARY)
		message(FATAL_ERROR "Cannot find libgcc.a")
	endif()
	get_filename_component(GCC_LIBRARY_PATH "${GCC_LIBRARY}" DIRECTORY)
Ghislain MARY's avatar
Ghislain MARY committed
88

89 90 91
	find_file(GCC_EXECUTABLE "${COMPILER_PREFIX}-gcc"
		PATHS
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86_64/bin"
Ghislain MARY's avatar
Ghislain MARY committed
92
		"${ANDROID_NDK_PATH}/toolchains/${NDK_ARCHITECTURE}-${GCC_VERSION}/prebuilt/linux-x86_64/bin"
93
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/darwin-x86_64/bin"
Ghislain MARY's avatar
Ghislain MARY committed
94
		"${ANDROID_NDK_PATH}/toolchains/${NDK_ARCHITECTURE}-${GCC_VERSION}/prebuilt/darwin-x86_64/bin"
95
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86/bin"
Ghislain MARY's avatar
Ghislain MARY committed
96
		"${ANDROID_NDK_PATH}/toolchains/${NDK_ARCHITECTURE}-${GCC_VERSION}/prebuilt/linux-x86/bin"
97 98 99 100 101 102
		NO_DEFAULT_PATH
	)
	if(NOT GCC_EXECUTABLE)
		message(FATAL_ERROR "Cannot find gcc")
	endif()
	get_filename_component(EXTERNAL_TOOLCHAIN_PATH "${GCC_EXECUTABLE}" DIRECTORY)
Ghislain MARY's avatar
Ghislain MARY committed
103

104 105 106 107 108 109 110 111
	#Define the compiler
	set(_CMAKE_TOOLCHAIN_PREFIX "${COMPILER_PREFIX}-")
	set(CMAKE_C_COMPILER "${CLANG_EXECUTABLE}")
	set(CMAKE_C_COMPILER_TARGET "${CLANG_TARGET}")
	set(CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN "${EXTERNAL_TOOLCHAIN_PATH}/..")
	set(CMAKE_CXX_COMPILER "${CLANG_EXECUTABLE}++")
	set(CMAKE_CXX_COMPILER_TARGET "${CLANG_TARGET}")
	set(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN "${EXTERNAL_TOOLCHAIN_PATH}/..")
Ghislain MARY's avatar
Ghislain MARY committed
112

113 114
	set(TOOLCHAIN_PREFIX "${EXTERNAL_TOOLCHAIN_PATH}/${COMPILER_PREFIX}-")
	set(TOOLCHAIN_STRIP "${TOOLCHAIN_PREFIX}strip")
115

116
else()
Ghislain MARY's avatar
Ghislain MARY committed
117

118 119 120 121 122
	set(GCC_VERSION "4.8")

	find_file(GCC_EXECUTABLE "${COMPILER_PREFIX}-gcc"
		PATHS
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86_64/bin"
Ghislain MARY's avatar
Ghislain MARY committed
123
		"${ANDROID_NDK_PATH}/toolchains/${NDK_ARCHITECTURE}-${GCC_VERSION}/prebuilt/linux-x86_64/bin"
124
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/darwin-x86_64/bin"
Ghislain MARY's avatar
Ghislain MARY committed
125
		"${ANDROID_NDK_PATH}/toolchains/${NDK_ARCHITECTURE}-${GCC_VERSION}/prebuilt/darwin-x86_64/bin"
126
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86/bin"
Ghislain MARY's avatar
Ghislain MARY committed
127
		"${ANDROID_NDK_PATH}/toolchains/${NDK_ARCHITECTURE}-${GCC_VERSION}/prebuilt/linux-x86/bin"
128 129 130 131 132
		NO_DEFAULT_PATH
	)
	if(NOT GCC_EXECUTABLE)
		message(FATAL_ERROR "Cannot find the compiler")
	endif()
133
	get_filename_component(TOOLCHAIN_PATH "${GCC_EXECUTABLE}" DIRECTORY)
134

135 136 137 138 139 140 141 142
	set(TOOLCHAIN_PREFIX "${TOOLCHAIN_PATH}/${COMPILER_PREFIX}-")
	set(TOOLCHAIN_CC "${TOOLCHAIN_PREFIX}gcc")
	set(TOOLCHAIN_CXX "${TOOLCHAIN_PREFIX}g++")
	set(TOOLCHAIN_LD "${TOOLCHAIN_PREFIX}ld")
	set(TOOLCHAIN_AR "${TOOLCHAIN_PREFIX}ar")
	set(TOOLCHAIN_RANLIB "${TOOLCHAIN_PREFIX}ranlib")
	set(TOOLCHAIN_STRIP "${TOOLCHAIN_PREFIX}strip")
	set(TOOLCHAIN_NM "${TOOLCHAIN_PREFIX}nm")
143 144

	# Define the compiler
145 146
	set(CMAKE_C_COMPILER "${TOOLCHAIN_CC}")
	set(CMAKE_CXX_COMPILER "${TOOLCHAIN_CXX}")
Ghislain MARY's avatar
Ghislain MARY committed
147

148 149 150
endif()

set(CMAKE_SYSROOT "${ANDROID_NDK_PATH}/platforms/android-${CMAKE_ANDROID_API}/arch-${ARCHITECTURE}")
Ghislain MARY's avatar
Ghislain MARY committed
151 152
set(ANDROID_SUPPORT "${ANDROID_NDK_PATH}/sources/android/support")
set(ANDROID_SUPPORT_INCLUDE_DIRS "${ANDROID_SUPPORT}/include")
153 154

message(STATUS "Using sysroot path: ${CMAKE_SYSROOT}")
Ghislain MARY's avatar
Ghislain MARY committed
155

Ghislain MARY's avatar
Ghislain MARY committed
156
set(CMAKE_FIND_ROOT_PATH "${CMAKE_SYSROOT}" "${CMAKE_INSTALL_PREFIX}" "${ANDROID_SUPPORT}")
Ghislain MARY's avatar
Ghislain MARY committed
157 158 159 160 161 162 163 164

# search for programs in the build host directories
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

Ghislain MARY's avatar
Ghislain MARY committed
165
set(ANDROID_STL_INCLUDE_DIRS "${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/${GCC_VERSION}/include" "${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/${GCC_VERSION}/libs/${NDK_ARCHITECTURE}/include")
166 167 168 169 170 171 172
if(EXISTS "${ANDROID_NDK_PATH}/sources/android/cpufeatures/cpu-features.c")
	set(ANDROID_CPU_FEATURES_INCLUDE_DIRS "${ANDROID_NDK_PATH}/sources/android/cpufeatures")
elseif(EXISTS "${ANDROID_NDK_PATH}/sources/cpufeatures/cpu-features.c")
	set(ANDROID_CPU_FEATURES_INCLUDE_DIRS "${ANDROID_NDK_PATH}/sources/cpufeatures")
else()
	message(FATAL_ERROR "Cannot find cpu-features.c")
endif()
Ghislain MARY's avatar
Ghislain MARY committed
173
include_directories(SYSTEM ${ANDROID_CPU_FEATURES_INCLUDE_DIRS} ${ANDROID_STL_INCLUDE_DIRS} ${ANDROID_SUPPORT_INCLUDE_DIRS})
174
add_definitions("-DANDROID")
175 176 177
if(GCC_LIBRARY_PATH)
	#link_directories("${GCC_LIBRARY_PATH}")
	# link_directories has no effet for external projects so add the GCC library path to the compiler flags
178 179 180
	set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L${GCC_LIBRARY_PATH}" CACHE STRING "linker flags" FORCE)
	set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L${GCC_LIBRARY_PATH}" CACHE STRING "linker flags" FORCE)
	set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -L${GCC_LIBRARY_PATH}" CACHE STRING "linker flags" FORCE)
181
endif()
Ghislain MARY's avatar
Ghislain MARY committed
182
set(GNUSTL_PATH "${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/${GCC_VERSION}/libs/${NDK_ARCHITECTURE}")
183 184 185 186 187
#link_directories("${GNUSTL_PATH}")
# link_directories has no effet for external projects so add the gnustl library path to the compiler flags
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L${GNUSTL_PATH}" CACHE STRING "linker flags" FORCE)
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L${GNUSTL_PATH}" CACHE STRING "linker flags" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -L${GNUSTL_PATH}" CACHE STRING "linker flags" FORCE)
188 189 190 191 192 193

set(CMAKE_CXX_FLAGS_RELEASE             "-Os -DNDEBUG")
set(CMAKE_C_FLAGS_RELEASE               "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO      "-Os -g -DNDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO        "-Os -g -DNDEBUG")

194
link_libraries("${GNUSTL_PATH}/libgnustl_shared.so")
Ghislain MARY's avatar
Ghislain MARY committed
195
link_libraries("log")
196
link_libraries("atomic")