Merge branch 'lua_versions' into main/rendor-staging
[ryzomcore.git] / CMakeModules / AndroidToolChain.cmake
blob4ca7022958d3d4d582ce8f681fc5db6fb4f4d800
1 IF(DEFINED CMAKE_CROSSCOMPILING)
2   # subsequent toolchain loading is not really needed
3   RETURN()
4 ENDIF()
6 # Standard settings
7 SET(CMAKE_SYSTEM_NAME Linux)
8 SET(CMAKE_SYSTEM_VERSION 1) # TODO: determine target Linux version
9 SET(UNIX ON)
10 SET(LINUX ON)
11 SET(ANDROID ON)
13 IF(NOT NDK_ROOT)
14   SET(NDK_ROOT $ENV{NDK_ROOT})
16   IF(CMAKE_HOST_WIN32)
17     FILE(TO_CMAKE_PATH ${NDK_ROOT} NDK_ROOT)
18   ENDIF()
19 ENDIF()
21 IF(NOT TARGET_CPU)
22   SET(TARGET_CPU "armv7")
23 ENDIF()
25 IF(TARGET_CPU STREQUAL "armv7")
26   SET(LIBRARY_ARCHITECTURE "armeabi-v7a")
27   SET(CMAKE_SYSTEM_PROCESSOR "armv7")
28   SET(TOOLCHAIN_ARCH "arm")
29   SET(GCC_TOOLCHAIN_PREFIX "arm-linux-androideabi")
30   SET(TOOLCHAIN_BIN_PREFIX "arm-linux-androideabi")
31   SET(MINIMUM_NDK_TARGET 9)
32 ELSEIF(TARGET_CPU STREQUAL "armv5")
33   SET(LIBRARY_ARCHITECTURE "armeabi")
34   SET(CMAKE_SYSTEM_PROCESSOR "armv5")
35   SET(TOOLCHAIN_ARCH "arm")
36   SET(GCC_TOOLCHAIN_PREFIX "arm-linux-androideabi")
37   SET(TOOLCHAIN_BIN_PREFIX "arm-linux-androideabi")
38   SET(MINIMUM_NDK_TARGET 9)
39 ELSEIF(TARGET_CPU STREQUAL "arm64")
40   SET(LIBRARY_ARCHITECTURE "arm64-v8a")
41   SET(CMAKE_SYSTEM_PROCESSOR "arm64")
42   SET(TOOLCHAIN_ARCH "arm64")
43   SET(GCC_TOOLCHAIN_PREFIX "aarch64-linux-android")
44   SET(TOOLCHAIN_BIN_PREFIX "aarch64-linux-android")
45   SET(MINIMUM_NDK_TARGET 21)
46 ELSEIF(TARGET_CPU STREQUAL "x86")
47   SET(LIBRARY_ARCHITECTURE "x86")
48   SET(CMAKE_SYSTEM_PROCESSOR "x86")
49   SET(TOOLCHAIN_ARCH "x86")
50   SET(GCC_TOOLCHAIN_PREFIX "x86")
51   SET(TOOLCHAIN_BIN_PREFIX "i686-linux-android")
52   SET(MINIMUM_NDK_TARGET 9)
53 ELSEIF(TARGET_CPU STREQUAL "x86_64")
54   SET(LIBRARY_ARCHITECTURE "x86_64")
55   SET(CMAKE_SYSTEM_PROCESSOR "x86_64")
56   SET(TOOLCHAIN_ARCH "x86_64")
57   SET(GCC_TOOLCHAIN_PREFIX "x86_64")
58   SET(TOOLCHAIN_BIN_PREFIX "x86_64-linux-android")
59   SET(MINIMUM_NDK_TARGET 21)
60 ELSEIF(TARGET_CPU STREQUAL "mips")
61   SET(LIBRARY_ARCHITECTURE "mips")
62   SET(CMAKE_SYSTEM_PROCESSOR "mips")
63   SET(TOOLCHAIN_ARCH "mips")
64   SET(GCC_TOOLCHAIN_PREFIX "mipsel-linux-android")
65   SET(TOOLCHAIN_BIN_PREFIX "mipsel-linux-android")
66   SET(MINIMUM_NDK_TARGET 9)
67 ELSEIF(TARGET_CPU STREQUAL "mips64")
68   SET(LIBRARY_ARCHITECTURE "mips64")
69   SET(CMAKE_SYSTEM_PROCESSOR "mips64")
70   SET(TOOLCHAIN_ARCH "mips64")
71   SET(GCC_TOOLCHAIN_PREFIX "mips64el-linux-android")
72   SET(TOOLCHAIN_BIN_PREFIX "mips64el-linux-android")
73   SET(MINIMUM_NDK_TARGET 21)
74 ELSE()
75   MESSAGE(FATAL_ERROR "Unable to process TARGET_CPU ${TARGET_CPU}")
76 ENDIF()
78 SET(CLANG_TOOLCHAIN_PREFIX "llvm")
79 SET(ANDROID_COMPILER "clang")
80 SET(ANDROID_COMPILER "clang")
81 SET(CLANG ON)
83 IF(NOT NDK_TARGET)
84   SET(NDK_TARGET ${MINIMUM_NDK_TARGET})
85 ELSE()
86   IF(NDK_TARGET LESS MINIMUM_NDK_TARGET)
87     SET(NDK_TARGET ${MINIMUM_NDK_TARGET})
88   ENDIF()
89 ENDIF()
91 IF(CMAKE_HOST_WIN32)
92   SET(TOOLCHAIN_HOST "windows-x86_64")
93   SET(TOOLCHAIN_BIN_SUFFIX ".exe")
94 ELSEIF(CMAKE_HOST_APPLE)
95   SET(TOOLCHAIN_HOST "apple")
96   SET(TOOLCHAIN_BIN_SUFFIX "")
97 ELSEIF(CMAKE_HOST_UNIX)
98   SET(TOOLCHAIN_HOST "linux-x86_64")
99   SET(TOOLCHAIN_BIN_SUFFIX "")
100 ENDIF()
102 # clang
103 SET(CLANG_TOOLCHAIN_ROOT "${NDK_ROOT}/toolchains/${CLANG_TOOLCHAIN_PREFIX}/prebuilt/${TOOLCHAIN_HOST}")
105 IF(EXISTS ${CLANG_TOOLCHAIN_ROOT})
106   MESSAGE(STATUS "Found LLVM toolchain in ${CLANG_TOOLCHAIN_ROOT}")
107 ELSE()
108   MESSAGE(FATAL_ERROR "No LLVM toolchain found in default search path ${CLANG_TOOLCHAIN_ROOT}")
109 ENDIF()
111 # gcc
112 SET(GCC_TOOLCHAIN_VERSIONS)
113 FILE(GLOB _TOOLCHAIN_VERSIONS "${NDK_ROOT}/toolchains/${GCC_TOOLCHAIN_PREFIX}-*")
114 IF(_TOOLCHAIN_VERSIONS)
115   LIST(SORT _TOOLCHAIN_VERSIONS)
116   LIST(REVERSE _TOOLCHAIN_VERSIONS)
117   FOREACH(_TOOLCHAIN_VERSION ${_TOOLCHAIN_VERSIONS})
118     STRING(REGEX REPLACE ".+${_PREFIX}-([0-9.]+)" "\\1" _TOOLCHAIN_VERSION "${_TOOLCHAIN_VERSION}")
119     IF(_TOOLCHAIN_VERSION MATCHES "^([0-9.]+)$")
120       LIST(APPEND GCC_TOOLCHAIN_VERSIONS ${_TOOLCHAIN_VERSION})
121     ENDIF()
122   ENDFOREACH()
123 ENDIF()
125 IF(NOT GCC_TOOLCHAIN_VERSIONS)
126   MESSAGE(FATAL_ERROR "No GCC version found in default search path ${NDK_ROOT}/toolchains")
127 ENDIF()
129 IF(GCC_TOOLCHAIN_VERSIONS)
130   LIST(FIND GCC_TOOLCHAIN_VERSIONS "${GCC_TOOLCHAIN_VERSION}" _INDEX)
131   IF(_INDEX EQUAL -1)
132     LIST(GET GCC_TOOLCHAIN_VERSIONS 0 GCC_TOOLCHAIN_VERSION)
133   ENDIF()
134 ELSE()
135   LIST(GET GCC_TOOLCHAIN_VERSIONS 0 GCC_TOOLCHAIN_VERSION)
136 ENDIF()
138 SET(GCC_TOOLCHAIN_ROOT "${NDK_ROOT}/toolchains/${GCC_TOOLCHAIN_PREFIX}-${GCC_TOOLCHAIN_VERSION}/prebuilt/${TOOLCHAIN_HOST}")
140 IF(NOT EXISTS "${GCC_TOOLCHAIN_ROOT}")
141   FILE(GLOB _TOOLCHAIN_PREFIXES "${GCC_TOOLCHAIN_ROOT}*")
142   IF(_TOOLCHAIN_PREFIXES)
143     LIST(GET _TOOLCHAIN_PREFIXES 0 GCC_TOOLCHAIN_ROOT)
144   ENDIF()
145 ENDIF()
147 IF(EXISTS "${GCC_TOOLCHAIN_ROOT}")
148   MESSAGE(STATUS "Found GCC toolchain in ${GCC_TOOLCHAIN_ROOT}")
149 ELSE()
150   MESSAGE(FATAL_ERROR "No GCC toolchain found in default search path ${GCC_TOOLCHAIN_ROOT}")
151 ENDIF()
153 # NDK
154 SET(PLATFORM_ROOT "${NDK_ROOT}/platforms/android-${NDK_TARGET}/arch-${TOOLCHAIN_ARCH}")
156 MESSAGE(STATUS "Target Android NDK ${NDK_TARGET} found in ${PLATFORM_ROOT}")
158 # include dirs
159 SET(PLATFORM_INCLUDE_DIR "${PLATFORM_ROOT}/usr/include")
160 SET(STL_DIR "${NDK_ROOT}/sources/cxx-stl/gnu-libstdc++")
162 IF(EXISTS "${STL_DIR}/${GCC_TOOLCHAIN_VERSION}")
163   # NDK version >= 8b
164   SET(STL_DIR "${STL_DIR}/${GCC_TOOLCHAIN_VERSION}")
165 ENDIF()
167 SET(STL_INCLUDE_DIR "${STL_DIR}/include")
168 SET(STL_LIBRARY_DIR "${STL_DIR}/libs/${LIBRARY_ARCHITECTURE}")
169 SET(STL_INCLUDE_CPU_DIR "${STL_LIBRARY_DIR}/include")
170 SET(STL_LIBRARY "${STL_LIBRARY_DIR}/libgnustl_static.a")
172 MESSAGE(STATUS "STL include dir: ${STL_INCLUDE_DIR}")
173 MESSAGE(STATUS "STL library dir: ${STL_LIBRARY_DIR}")
175 SET(CMAKE_FIND_ROOT_PATH ${CLANG_TOOLCHAIN_ROOT} ${GCC_TOOLCHAIN_ROOT} ${PLATFORM_ROOT}/usr ${CMAKE_PREFIX_PATH} ${CMAKE_INSTALL_PREFIX} $ENV{EXTERNAL_ANDROID_PATH} CACHE string  "Android find search path root")
177 SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
178 SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
179 SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
181 MACRO(SET_TOOLCHAIN_BINARY_LLVM _NAME _BINARY)
182   SET(${_NAME} ${CLANG_TOOLCHAIN_ROOT}/bin/${_BINARY}${TOOLCHAIN_BIN_SUFFIX} CACHE PATH "" FORCE)
183 ENDMACRO()
185 MACRO(SET_TOOLCHAIN_BINARY_GCC _NAME _BINARY)
186   SET(${_NAME} ${GCC_TOOLCHAIN_ROOT}/bin/${TOOLCHAIN_BIN_PREFIX}-${_BINARY}${TOOLCHAIN_BIN_SUFFIX} CACHE PATH "" FORCE)
187 ENDMACRO()
189 SET(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
191 SET_TOOLCHAIN_BINARY_LLVM(CMAKE_C_COMPILER clang)
192 SET_TOOLCHAIN_BINARY_LLVM(CMAKE_CXX_COMPILER clang++)
194 SET(CMAKE_C_COMPILER ${CMAKE_C_COMPILER})
195 SET(CMAKE_C_COMPILER_TARGET ${TOOLCHAIN_BIN_PREFIX})
196 SET(CMAKE_C_COMPILER_FORCED TRUE)
198 SET(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER})
199 SET(CMAKE_CXX_COMPILER_TARGET ${TOOLCHAIN_BIN_PREFIX})
200 SET(CMAKE_CXX_COMPILER_FORCED TRUE)
202 SET_TOOLCHAIN_BINARY_LLVM(CMAKE_ASM_COMPILER llvm-as)
203 SET_TOOLCHAIN_BINARY_LLVM(CMAKE_AR llvm-ar)
204 SET_TOOLCHAIN_BINARY_LLVM(CMAKE_LINKER clang++)
206 IF(NOT EXISTS "${CMAKE_ASM_COMPILER}")
207   SET_TOOLCHAIN_BINARY_GCC(CMAKE_ASM_COMPILER as)
208 ENDIF()
210 IF(NOT EXISTS "${CMAKE_AR}")
211   SET_TOOLCHAIN_BINARY_GCC(CMAKE_AR ar)
212 ENDIF()
214 SET_TOOLCHAIN_BINARY_GCC(CMAKE_STRIP strip)
215 SET_TOOLCHAIN_BINARY_GCC(CMAKE_NM nm)
216 SET_TOOLCHAIN_BINARY_GCC(CMAKE_OBJCOPY objcopy)
217 SET_TOOLCHAIN_BINARY_GCC(CMAKE_OBJDUMP objdump)
218 SET_TOOLCHAIN_BINARY_GCC(CMAKE_RANLIB ranlib)