toolchain-android.cmake 9.71 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 19 20 21 22 23
############################################################################
# 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
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
############################################################################

set(CMAKE_ANDROID_API 14)
24
set(CMAKE_ANDROID_STL_TYPE gnustl_shared)
Ghislain MARY's avatar
Ghislain MARY committed
25

26 27 28 29 30 31 32
set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_POSITION_INDEPENDENT_CODE YES)

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

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

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

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

51
find_file(CLANG_EXECUTABLE "clang"
Ghislain MARY's avatar
Ghislain MARY committed
52
	PATHS
53 54 55 56
	"${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
57
)
58
if(CLANG_EXECUTABLE)
Ghislain MARY's avatar
Ghislain MARY committed
59

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

66 67 68 69
	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}"
		"${ANDROID_NDK_PATH}/toolchains/${CMAKE_SYSTEM_PROCESSOR}-${GCC_VERSION}/prebuilt/linux-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}"
70 71
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}.x${GCC_LIBRARY_ADDITIONAL_DIR}"
		"${ANDROID_NDK_PATH}/toolchains/${CMAKE_SYSTEM_PROCESSOR}-${GCC_VERSION}/prebuilt/linux-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}.x"
72 73
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/darwin-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}${GCC_LIBRARY_ADDITIONAL_DIR}"
		"${ANDROID_NDK_PATH}/toolchains/${CMAKE_SYSTEM_PROCESSOR}-${GCC_VERSION}/prebuilt/darwin-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}"
74 75
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/darwin-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}.x${GCC_LIBRARY_ADDITIONAL_DIR}"
		"${ANDROID_NDK_PATH}/toolchains/${CMAKE_SYSTEM_PROCESSOR}-${GCC_VERSION}/prebuilt/darwin-x86_64/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}.x"
76 77
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}${GCC_LIBRARY_ADDITIONAL_DIR}"
		"${ANDROID_NDK_PATH}/toolchains/${CMAKE_SYSTEM_PROCESSOR}-${GCC_VERSION}/prebuilt/linux-x86/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}"
78 79
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}.x${GCC_LIBRARY_ADDITIONAL_DIR}"
		"${ANDROID_NDK_PATH}/toolchains/${CMAKE_SYSTEM_PROCESSOR}-${GCC_VERSION}/prebuilt/linux-x86/lib/gcc/${COMPILER_PREFIX}/${GCC_VERSION}.x"
80 81 82 83 84 85
		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
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100
	find_file(GCC_EXECUTABLE "${COMPILER_PREFIX}-gcc"
		PATHS
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86_64/bin"
		"${ANDROID_NDK_PATH}/toolchains/${CMAKE_SYSTEM_PROCESSOR}-${GCC_VERSION}/prebuilt/linux-x86_64/bin"
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/darwin-x86_64/bin"
		"${ANDROID_NDK_PATH}/toolchains/${CMAKE_SYSTEM_PROCESSOR}-${GCC_VERSION}/prebuilt/darwin-x86_64/bin"
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86/bin"
		"${ANDROID_NDK_PATH}/toolchains/${CMAKE_SYSTEM_PROCESSOR}-${GCC_VERSION}/prebuilt/linux-x86/bin"
		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
101

102 103 104 105 106 107 108 109
	#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
110

111 112
	set(TOOLCHAIN_PREFIX "${EXTERNAL_TOOLCHAIN_PATH}/${COMPILER_PREFIX}-")
	set(TOOLCHAIN_STRIP "${TOOLCHAIN_PREFIX}strip")
113

114
else()
Ghislain MARY's avatar
Ghislain MARY committed
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	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"
		"${ANDROID_NDK_PATH}/toolchains/${CMAKE_SYSTEM_PROCESSOR}-${GCC_VERSION}/prebuilt/linux-x86_64/bin"
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/darwin-x86_64/bin"
		"${ANDROID_NDK_PATH}/toolchains/${CMAKE_SYSTEM_PROCESSOR}-${GCC_VERSION}/prebuilt/darwin-x86_64/bin"
		"${ANDROID_NDK_PATH}/toolchains/${COMPILER_PREFIX}-${GCC_VERSION}/prebuilt/linux-x86/bin"
		"${ANDROID_NDK_PATH}/toolchains/${CMAKE_SYSTEM_PROCESSOR}-${GCC_VERSION}/prebuilt/linux-x86/bin"
		NO_DEFAULT_PATH
	)
	if(NOT GCC_EXECUTABLE)
		message(FATAL_ERROR "Cannot find the compiler")
	endif()
131
	get_filename_component(TOOLCHAIN_PATH "${GCC_EXECUTABLE}" DIRECTORY)
132

133 134 135 136 137 138 139 140
	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")
141 142

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

146 147 148
endif()

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

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

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

# 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)

163
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/${CMAKE_SYSTEM_PROCESSOR}/include")
164 165 166 167 168 169 170
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
171
include_directories(SYSTEM ${ANDROID_CPU_FEATURES_INCLUDE_DIRS} ${ANDROID_STL_INCLUDE_DIRS} ${ANDROID_SUPPORT_INCLUDE_DIRS})
172
add_definitions("-DANDROID")
173 174 175
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
176 177 178
	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)
179
endif()
180 181 182 183 184 185 186
set(GNUSTL_PATH "${ANDROID_NDK_PATH}/sources/cxx-stl/gnu-libstdc++/${GCC_VERSION}/libs/${CMAKE_SYSTEM_PROCESSOR}")
#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)
link_libraries("${GNUSTL_PATH}/libgnustl_shared.so")
Ghislain MARY's avatar
Ghislain MARY committed
187
link_libraries("log")
188
link_libraries("atomic")