toolchain-android.cmake 9.45 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)

Ghislain MARY's avatar
Ghislain MARY committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
find_path(ANDROID_NDK_PATH ndk-build)
if(NOT ANDROID_NDK_PATH)
	message(FATAL_ERROR "Cannot find 'ndk-build', make sure you installed the NDK and added it to your PATH")
endif()

find_path(ANDROID_SDK_PATH android)
if(NOT ANDROID_SDK_PATH)
	message(FATAL_ERROR "Cannot find 'android', make sure you installed the SDK and added it to your PATH")
endif()

find_path(ANDROID_SDK_PLATFORM_TOOLS_PATH adb)
if(NOT ANDROID_SDK_PLATFORM_TOOLS_PATH)
	message(FATAL_ERROR "Cannot find 'adb', make sure you installed the SDK platform tools and added it to your PATH")
endif()

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

57
	get_filename_component(TOOLCHAIN_PATH "${CLANG_EXECUTABLE}" DIRECTORY)
58 59 60 61
	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
62

63 64 65 66
	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}"
67 68
		"${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"
69 70
		"${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}"
71 72
		"${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"
73 74
		"${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}"
75 76
		"${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"
77 78 79 80 81 82
		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
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97
	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
98

99 100 101 102 103 104 105 106
	#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
107

108 109
	set(TOOLCHAIN_PREFIX "${EXTERNAL_TOOLCHAIN_PATH}/${COMPILER_PREFIX}-")
	set(TOOLCHAIN_STRIP "${TOOLCHAIN_PREFIX}strip")
110

111
else()
Ghislain MARY's avatar
Ghislain MARY committed
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	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()
128
	get_filename_component(TOOLCHAIN_PATH "${GCC_EXECUTABLE}" DIRECTORY)
129

130 131 132 133 134 135 136 137
	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")
138 139

	# Define the compiler
140 141
	set(CMAKE_C_COMPILER "${TOOLCHAIN_CC}")
	set(CMAKE_CXX_COMPILER "${TOOLCHAIN_CXX}")
Ghislain MARY's avatar
Ghislain MARY committed
142

143 144 145
endif()

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

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

Ghislain MARY's avatar
Ghislain MARY committed
151
set(CMAKE_FIND_ROOT_PATH "${CMAKE_SYSROOT}" "${CMAKE_INSTALL_PREFIX}" "${ANDROID_SUPPORT}")
Ghislain MARY's avatar
Ghislain MARY committed
152 153 154 155 156 157 158 159

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

160
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")
161 162 163 164 165 166 167
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
168
include_directories(SYSTEM ${ANDROID_CPU_FEATURES_INCLUDE_DIRS} ${ANDROID_STL_INCLUDE_DIRS} ${ANDROID_SUPPORT_INCLUDE_DIRS})
169
add_definitions("-DANDROID")
170 171 172
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
173 174 175
	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)
176
endif()
177 178 179 180 181 182 183
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
184
link_libraries("log")
185
link_libraries("atomic")