Android-Determine.cmake 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # When CMAKE_SYSTEM_NAME is "Android", CMakeDetermineSystem loads this module.
  4. # This module detects platform-wide information about the Android target
  5. # in order to store it in "CMakeSystem.cmake".
  6. # Include the NDK hook.
  7. # It can be used by NDK to inject necessary fixes for an earlier cmake.
  8. if(CMAKE_ANDROID_NDK)
  9. include(${CMAKE_ANDROID_NDK}/build/cmake/hooks/pre/Android-Determine.cmake OPTIONAL)
  10. endif()
  11. # Support for NVIDIA Nsight Tegra Visual Studio Edition was previously
  12. # implemented in the CMake VS IDE generators. Avoid interfering with
  13. # that functionality for now.
  14. if(CMAKE_GENERATOR_PLATFORM STREQUAL "Tegra-Android")
  15. return()
  16. endif()
  17. # Commonly used Android toolchain files that pre-date CMake upstream support
  18. # set CMAKE_SYSTEM_VERSION to 1. Avoid interfering with them.
  19. if(CMAKE_SYSTEM_VERSION EQUAL 1)
  20. return()
  21. endif()
  22. # Natively compiling on an Android host doesn't use the NDK cross-compilation
  23. # tools.
  24. if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Android")
  25. return()
  26. endif()
  27. cmake_policy(PUSH)
  28. cmake_policy(SET CMP0057 NEW) # if IN_LIST
  29. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  30. # If using Android tools for Visual Studio, compile a sample project to get the
  31. # NDK path and set the processor from the generator platform.
  32. if(CMAKE_GENERATOR MATCHES "Visual Studio")
  33. if(NOT CMAKE_ANDROID_ARCH_ABI AND NOT CMAKE_SYSTEM_PROCESSOR)
  34. if(CMAKE_GENERATOR_PLATFORM STREQUAL "ARM")
  35. set(CMAKE_SYSTEM_PROCESSOR "armv7-a")
  36. elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "ARM64")
  37. set(CMAKE_SYSTEM_PROCESSOR "aarch64")
  38. elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "x86")
  39. set(CMAKE_SYSTEM_PROCESSOR "i686")
  40. elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "x64")
  41. set(CMAKE_SYSTEM_PROCESSOR "x86_64")
  42. else()
  43. message(FATAL_ERROR "Unhandled generator platform, please choose ARM, ARM64, x86 or x86_64 using -A")
  44. endif()
  45. endif()
  46. if(NOT CMAKE_ANDROID_NDK)
  47. set(vcx_platform ${CMAKE_GENERATOR_PLATFORM})
  48. if(CMAKE_GENERATOR MATCHES "Visual Studio 14")
  49. set(vcx_revision "2.0")
  50. elseif(CMAKE_GENERATOR MATCHES "Visual Studio 1[567]")
  51. set(vcx_revision "3.0")
  52. else()
  53. set(vcx_revision "")
  54. endif()
  55. configure_file(${CMAKE_ROOT}/Modules/Platform/Android/VCXProjInspect.vcxproj.in
  56. ${CMAKE_PLATFORM_INFO_DIR}/VCXProjInspect.vcxproj @ONLY)
  57. cmake_host_system_information(RESULT _msbuild QUERY VS_MSBUILD_COMMAND) # undocumented query
  58. execute_process(
  59. COMMAND "${_msbuild}" "VCXProjInspect.vcxproj"
  60. "/p:Configuration=Debug" "/p:Platform=${vcx_platform}"
  61. WORKING_DIRECTORY ${CMAKE_PLATFORM_INFO_DIR}
  62. OUTPUT_VARIABLE VCXPROJ_INSPECT_OUTPUT
  63. ERROR_VARIABLE VCXPROJ_INSPECT_OUTPUT
  64. RESULT_VARIABLE VCXPROJ_INSPECT_RESULT
  65. )
  66. unset(_msbuild)
  67. if(VCXPROJ_INSPECT_OUTPUT MATCHES "CMAKE_ANDROID_NDK=([^%\r\n]+)[\r\n]")
  68. # Strip VS diagnostic output from the end of the line.
  69. string(REGEX REPLACE " \\(TaskId:[0-9]*\\)$" "" _ndk "${CMAKE_MATCH_1}")
  70. if(EXISTS "${_ndk}")
  71. file(TO_CMAKE_PATH "${_ndk}" CMAKE_ANDROID_NDK)
  72. endif()
  73. endif()
  74. if(VCXPROJ_INSPECT_RESULT)
  75. message(CONFIGURE_LOG
  76. "Determining the Android NDK failed from msbuild failed.
  77. The output was:
  78. ${VCXPROJ_INSPECT_RESULT}
  79. ${VCXPROJ_INSPECT_OUTPUT}
  80. ")
  81. else()
  82. message(CONFIGURE_LOG
  83. "Determining the Android NDK succeeded.
  84. The output was:
  85. ${VCXPROJ_INSPECT_RESULT}
  86. ${VCXPROJ_INSPECT_OUTPUT}
  87. ")
  88. endif()
  89. endif()
  90. if(NOT CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION)
  91. set(CMAKE_ANDROID_NDK_TOOLCHAIN_VERSION "clang")
  92. endif()
  93. endif()
  94. # If the user provided CMAKE_SYSROOT for us, extract information from it.
  95. set(_ANDROID_SYSROOT_NDK "")
  96. set(_ANDROID_SYSROOT_API "")
  97. set(_ANDROID_SYSROOT_ARCH "")
  98. set(_ANDROID_SYSROOT_STANDALONE_TOOLCHAIN "")
  99. if(CMAKE_SYSROOT)
  100. if(NOT IS_DIRECTORY "${CMAKE_SYSROOT}")
  101. message(FATAL_ERROR
  102. "Android: The specified CMAKE_SYSROOT:\n"
  103. " ${CMAKE_SYSROOT}\n"
  104. "is not an existing directory."
  105. )
  106. endif()
  107. if(CMAKE_SYSROOT MATCHES "^([^\\\n]*)/platforms/android-([0-9]+)/arch-([a-z0-9_]+)$")
  108. set(_ANDROID_SYSROOT_NDK "${CMAKE_MATCH_1}")
  109. set(_ANDROID_SYSROOT_API "${CMAKE_MATCH_2}")
  110. set(_ANDROID_SYSROOT_ARCH "${CMAKE_MATCH_3}")
  111. elseif(CMAKE_SYSROOT MATCHES "^([^\\\n]*)/sysroot$")
  112. set(_ANDROID_SYSROOT_STANDALONE_TOOLCHAIN "${CMAKE_MATCH_1}")
  113. else()
  114. message(FATAL_ERROR
  115. "The value of CMAKE_SYSROOT:\n"
  116. " ${CMAKE_SYSROOT}\n"
  117. "does not match any of the forms:\n"
  118. " <ndk>/platforms/android-<api>/arch-<arch>\n"
  119. " <standalone-toolchain>/sysroot\n"
  120. "where:\n"
  121. " <ndk> = Android NDK directory (with forward slashes)\n"
  122. " <api> = Android API version number (decimal digits)\n"
  123. " <arch> = Android ARCH name (lower case)\n"
  124. " <standalone-toolchain> = Path to standalone toolchain prefix\n"
  125. )
  126. endif()
  127. endif()
  128. # Find the Android NDK.
  129. if(CMAKE_ANDROID_NDK)
  130. if(NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK}")
  131. message(FATAL_ERROR
  132. "Android: The NDK root directory specified by CMAKE_ANDROID_NDK:\n"
  133. " ${CMAKE_ANDROID_NDK}\n"
  134. "does not exist."
  135. )
  136. endif()
  137. elseif(CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  138. if(NOT IS_DIRECTORY "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}")
  139. message(FATAL_ERROR
  140. "Android: The standalone toolchain directory specified by CMAKE_ANDROID_STANDALONE_TOOLCHAIN:\n"
  141. " ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}\n"
  142. "does not exist."
  143. )
  144. endif()
  145. if(NOT EXISTS "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h")
  146. message(FATAL_ERROR
  147. "Android: The standalone toolchain directory specified by CMAKE_ANDROID_STANDALONE_TOOLCHAIN:\n"
  148. " ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}\n"
  149. "does not contain a sysroot with a known layout. The file:\n"
  150. " ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h\n"
  151. "does not exist."
  152. )
  153. endif()
  154. else()
  155. if(IS_DIRECTORY "${_ANDROID_SYSROOT_NDK}")
  156. set(CMAKE_ANDROID_NDK "${_ANDROID_SYSROOT_NDK}")
  157. elseif(IS_DIRECTORY "${_ANDROID_SYSROOT_STANDALONE_TOOLCHAIN}")
  158. set(CMAKE_ANDROID_STANDALONE_TOOLCHAIN "${_ANDROID_SYSROOT_STANDALONE_TOOLCHAIN}")
  159. elseif(IS_DIRECTORY "${ANDROID_NDK}")
  160. file(TO_CMAKE_PATH "${ANDROID_NDK}" CMAKE_ANDROID_NDK)
  161. elseif(IS_DIRECTORY "${ANDROID_STANDALONE_TOOLCHAIN}")
  162. file(TO_CMAKE_PATH "${ANDROID_STANDALONE_TOOLCHAIN}" CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  163. elseif(IS_DIRECTORY "$ENV{ANDROID_NDK_ROOT}")
  164. file(TO_CMAKE_PATH "$ENV{ANDROID_NDK_ROOT}" CMAKE_ANDROID_NDK)
  165. elseif(IS_DIRECTORY "$ENV{ANDROID_NDK}")
  166. file(TO_CMAKE_PATH "$ENV{ANDROID_NDK}" CMAKE_ANDROID_NDK)
  167. elseif(IS_DIRECTORY "$ENV{ANDROID_STANDALONE_TOOLCHAIN}")
  168. file(TO_CMAKE_PATH "$ENV{ANDROID_STANDALONE_TOOLCHAIN}" CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  169. endif()
  170. # TODO: Search harder for the NDK or standalone toolchain.
  171. endif()
  172. set(_ANDROID_STANDALONE_TOOLCHAIN_API "")
  173. if(CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  174. # Try to read the API level from the toolchain launcher.
  175. if(EXISTS "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/bin/clang")
  176. set(_ANDROID_API_LEVEL_CLANG_REGEX "__ANDROID_API__=([0-9]+)")
  177. file(STRINGS "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/bin/clang" _ANDROID_STANDALONE_TOOLCHAIN_BIN_CLANG
  178. REGEX "${_ANDROID_API_LEVEL_CLANG_REGEX}" LIMIT_COUNT 1 LIMIT_INPUT 65536)
  179. if(_ANDROID_STANDALONE_TOOLCHAIN_BIN_CLANG MATCHES "${_ANDROID_API_LEVEL_CLANG_REGEX}")
  180. set(_ANDROID_STANDALONE_TOOLCHAIN_API "${CMAKE_MATCH_1}")
  181. endif()
  182. unset(_ANDROID_STANDALONE_TOOLCHAIN_BIN_CLANG)
  183. unset(_ANDROID_API_LEVEL_CLANG_REGEX)
  184. endif()
  185. if(NOT _ANDROID_STANDALONE_TOOLCHAIN_API)
  186. # The compiler launcher does not know __ANDROID_API__. Assume this
  187. # is not unified headers and look for it in the api-level.h header.
  188. set(_ANDROID_API_LEVEL_H_REGEX "^[\t ]*#[\t ]*define[\t ]+__ANDROID_API__[\t ]+([0-9]+)")
  189. file(STRINGS "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h"
  190. _ANDROID_API_LEVEL_H_CONTENT REGEX "${_ANDROID_API_LEVEL_H_REGEX}")
  191. if(_ANDROID_API_LEVEL_H_CONTENT MATCHES "${_ANDROID_API_LEVEL_H_REGEX}")
  192. set(_ANDROID_STANDALONE_TOOLCHAIN_API "${CMAKE_MATCH_1}")
  193. endif()
  194. endif()
  195. if(NOT _ANDROID_STANDALONE_TOOLCHAIN_API)
  196. message(WARNING
  197. "Android: Did not detect API level from\n"
  198. " ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/bin/clang\n"
  199. "or\n"
  200. " ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h\n"
  201. )
  202. endif()
  203. endif()
  204. if(NOT CMAKE_ANDROID_NDK AND NOT CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  205. message(FATAL_ERROR "Android: Neither the NDK or a standalone toolchain was found.")
  206. endif()
  207. if(CMAKE_ANDROID_NDK)
  208. # NDK >= 18 has platforms.cmake. It provides:
  209. # NDK_MIN_PLATFORM_LEVEL
  210. # NDK_MAX_PLATFORM_LEVEL
  211. include("${CMAKE_ANDROID_NDK}/build/cmake/platforms.cmake" OPTIONAL RESULT_VARIABLE _INCLUDED_PLATFORMS)
  212. # NDK >= 18 has abis.cmake. It provides:
  213. # NDK_KNOWN_DEVICE_ABI32S
  214. # NDK_KNOWN_DEVICE_ABI64S
  215. # NDK >= 23 also provides:
  216. # NDK_KNOWN_DEVICE_ABIS
  217. # NDK_ABI_<abi>_PROC
  218. # NDK_ABI_<abi>_ARCH
  219. # NDK_ABI_<abi>_TRIPLE
  220. # NDK_ABI_<abi>_LLVM_TRIPLE
  221. # NDK_PROC_<processor>_ABI
  222. # NDK_ARCH_<arch>_ABI
  223. include("${CMAKE_ANDROID_NDK}/build/cmake/abis.cmake" OPTIONAL RESULT_VARIABLE _INCLUDED_ABIS)
  224. endif()
  225. if(CMAKE_ANDROID_NDK AND EXISTS "${CMAKE_ANDROID_NDK}/source.properties")
  226. # Android NDK revision
  227. # Possible formats:
  228. # * r16, build 1234: 16.0.1234
  229. # * r16b, build 1234: 16.1.1234
  230. # * r16 beta 1, build 1234: 16.0.1234-beta1
  231. #
  232. # Canary builds are not specially marked.
  233. file(READ "${CMAKE_ANDROID_NDK}/source.properties" _ANDROID_NDK_SOURCE_PROPERTIES)
  234. set(_ANDROID_NDK_REVISION_REGEX
  235. "^Pkg\\.Desc = Android NDK\nPkg\\.Revision = ([0-9]+)\\.([0-9]+)\\.([0-9]+)(-beta([0-9]+))?")
  236. if(NOT _ANDROID_NDK_SOURCE_PROPERTIES MATCHES "${_ANDROID_NDK_REVISION_REGEX}")
  237. string(REPLACE "\n" "\n " _ANDROID_NDK_SOURCE_PROPERTIES "${_ANDROID_NDK_SOURCE_PROPERTIES}")
  238. message(FATAL_ERROR
  239. "Android: Failed to parse NDK revision from:\n"
  240. " ${CMAKE_ANDROID_NDK}/source.properties\n"
  241. "with content:\n"
  242. " ${_ANDROID_NDK_SOURCE_PROPERTIES}")
  243. endif()
  244. set(_ANDROID_NDK_MAJOR "${CMAKE_MATCH_1}")
  245. set(_ANDROID_NDK_MINOR "${CMAKE_MATCH_2}")
  246. set(_ANDROID_NDK_BUILD "${CMAKE_MATCH_3}")
  247. set(_ANDROID_NDK_BETA "${CMAKE_MATCH_5}")
  248. if(_ANDROID_NDK_BETA STREQUAL "")
  249. set(_ANDROID_NDK_BETA "0")
  250. endif()
  251. set(CMAKE_ANDROID_NDK_VERSION "${_ANDROID_NDK_MAJOR}.${_ANDROID_NDK_MINOR}")
  252. unset(_ANDROID_NDK_SOURCE_PROPERTIES)
  253. unset(_ANDROID_NDK_REVISION_REGEX)
  254. unset(_ANDROID_NDK_MAJOR)
  255. unset(_ANDROID_NDK_MINOR)
  256. unset(_ANDROID_NDK_BUILD)
  257. unset(_ANDROID_NDK_BETA)
  258. endif()
  259. if(CMAKE_ANDROID_NDK)
  260. # Identify the host platform.
  261. if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
  262. if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "64")
  263. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "darwin-x86_64")
  264. else()
  265. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "darwin-x86")
  266. endif()
  267. elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
  268. if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
  269. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "linux-x86_64")
  270. else()
  271. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "linux-x86")
  272. endif()
  273. elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
  274. if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "AMD64")
  275. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "windows-x86_64")
  276. else()
  277. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "windows")
  278. endif()
  279. else()
  280. message(FATAL_ERROR "Android: Builds hosted on '${CMAKE_HOST_SYSTEM_NAME}' not supported.")
  281. endif()
  282. # Look for a unified toolchain/sysroot provided with the NDK.
  283. set(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED "${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/${CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG}")
  284. if(NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/sysroot")
  285. set(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED "")
  286. endif()
  287. else()
  288. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "")
  289. set(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED "")
  290. endif()
  291. if(_INCLUDED_ABIS)
  292. if(NDK_KNOWN_DEVICE_ABIS)
  293. set(_ANDROID_KNOWN_ABIS ${NDK_KNOWN_DEVICE_ABIS})
  294. else()
  295. set(_ANDROID_KNOWN_ABIS ${NDK_KNOWN_DEVICE_ABI32S} ${NDK_KNOWN_DEVICE_ABI64S})
  296. endif()
  297. endif()
  298. if(NOT DEFINED NDK_KNOWN_DEVICE_ABIS)
  299. # The NDK is not new enough to provide ABI information.
  300. # https://developer.android.com/ndk/guides/abis.html
  301. set(NDK_ABI_arm64-v8a_PROC "aarch64")
  302. set(NDK_ABI_arm64-v8a_ARCH "arm64")
  303. set(NDK_ABI_arm64-v8a_TRIPLE "aarch64-linux-android")
  304. set(NDK_ABI_arm64-v8a_LLVM_TRIPLE "aarch64-none-linux-android")
  305. set(NDK_ABI_armeabi-v7a_PROC "armv7-a")
  306. set(NDK_ABI_armeabi-v7a_ARCH "arm")
  307. set(NDK_ABI_armeabi-v7a_TRIPLE "arm-linux-androideabi")
  308. set(NDK_ABI_armeabi-v7a_LLVM_TRIPLE "armv7-none-linux-androideabi")
  309. set(NDK_ABI_armeabi-v6_PROC "armv6")
  310. set(NDK_ABI_armeabi-v6_ARCH "arm")
  311. set(NDK_ABI_armeabi-v6_TRIPLE "arm-linux-androideabi")
  312. set(NDK_ABI_armeabi-v6_LLVM_TRIPLE "armv6-none-linux-androideabi")
  313. set(NDK_ABI_armeabi_PROC "armv5te")
  314. set(NDK_ABI_armeabi_ARCH "arm")
  315. set(NDK_ABI_armeabi_TRIPLE "arm-linux-androideabi")
  316. set(NDK_ABI_armeabi_LLVM_TRIPLE "armv5te-none-linux-androideabi")
  317. set(NDK_ABI_mips_PROC "mips")
  318. set(NDK_ABI_mips_ARCH "mips")
  319. set(NDK_ABI_mips_TRIPLE "mipsel-linux-android")
  320. set(NDK_ABI_mips_LLVM_TRIPLE "mipsel-none-linux-android")
  321. set(NDK_ABI_mips64_PROC "mips64")
  322. set(NDK_ABI_mips64_ARCH "mips64")
  323. set(NDK_ABI_mips64_TRIPLE "mips64el-linux-android")
  324. set(NDK_ABI_mips64_LLVM_TRIPLE "mips64el-none-linux-android")
  325. set(NDK_ABI_x86_PROC "i686")
  326. set(NDK_ABI_x86_ARCH "x86")
  327. set(NDK_ABI_x86_TRIPLE "i686-linux-android")
  328. set(NDK_ABI_x86_LLVM_TRIPLE "i686-none-linux-android")
  329. set(NDK_ABI_x86_64_PROC "x86_64")
  330. set(NDK_ABI_x86_64_ARCH "x86_64")
  331. set(NDK_ABI_x86_64_TRIPLE "x86_64-linux-android")
  332. set(NDK_ABI_x86_64_LLVM_TRIPLE "x86_64-none-linux-android")
  333. set(NDK_PROC_aarch64_ABI "arm64-v8a")
  334. set(NDK_PROC_armv7-a_ABI "armeabi-v7a")
  335. set(NDK_PROC_armv6_ABI "armeabi-v6")
  336. set(NDK_PROC_armv5te_ABI "armeabi")
  337. set(NDK_PROC_i686_ABI "x86")
  338. set(NDK_PROC_mips_ABI "mips")
  339. set(NDK_PROC_mips64_ABI "mips64")
  340. set(NDK_PROC_x86_64_ABI "x86_64")
  341. set(NDK_ARCH_arm64_ABI "arm64-v8a")
  342. set(NDK_ARCH_arm_ABI "armeabi")
  343. set(NDK_ARCH_mips_ABI "mips")
  344. set(NDK_ARCH_mips64_ABI "mips64")
  345. set(NDK_ARCH_x86_ABI "x86")
  346. set(NDK_ARCH_x86_64_ABI "x86_64")
  347. endif()
  348. # Validate inputs.
  349. if(CMAKE_ANDROID_ARCH_ABI AND NOT DEFINED "NDK_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC")
  350. message(FATAL_ERROR "Android: Unknown ABI CMAKE_ANDROID_ARCH_ABI='${CMAKE_ANDROID_ARCH_ABI}'.")
  351. endif()
  352. # Select an ABI.
  353. if(NOT CMAKE_ANDROID_ARCH_ABI)
  354. if(CMAKE_SYSTEM_PROCESSOR)
  355. if(NOT DEFINED "NDK_PROC_${CMAKE_SYSTEM_PROCESSOR}_ABI")
  356. message(FATAL_ERROR "Android: Unknown processor CMAKE_SYSTEM_PROCESSOR='${CMAKE_SYSTEM_PROCESSOR}'.")
  357. endif()
  358. set(CMAKE_ANDROID_ARCH_ABI "${NDK_PROC_${CMAKE_SYSTEM_PROCESSOR}_ABI}")
  359. elseif(_ANDROID_SYSROOT_ARCH)
  360. if(NOT DEFINED "NDK_ARCH_${_ANDROID_SYSROOT_ARCH}_ABI")
  361. message(FATAL_ERROR
  362. "Android: Unknown architecture '${_ANDROID_SYSROOT_ARCH}' specified in CMAKE_SYSROOT:\n"
  363. " ${CMAKE_SYSROOT}"
  364. )
  365. endif()
  366. set(CMAKE_ANDROID_ARCH_ABI "${NDK_ARCH_${_ANDROID_SYSROOT_ARCH}_ABI}")
  367. elseif(_INCLUDED_ABIS)
  368. # Default to the oldest ARM ABI.
  369. foreach(abi armeabi armeabi-v7a arm64-v8a)
  370. if("${abi}" IN_LIST _ANDROID_KNOWN_ABIS)
  371. set(CMAKE_ANDROID_ARCH_ABI "${abi}")
  372. break()
  373. endif()
  374. endforeach()
  375. if(NOT CMAKE_ANDROID_ARCH_ABI)
  376. message(FATAL_ERROR
  377. "Android: Can not determine the default ABI. Please set CMAKE_ANDROID_ARCH_ABI."
  378. )
  379. endif()
  380. else()
  381. # https://developer.android.com/ndk/guides/application_mk.html
  382. # Default is the oldest ARM ABI.
  383. # Lookup the available ABIs among all toolchains.
  384. set(_ANDROID_ABIS "")
  385. file(GLOB _ANDROID_CONFIG_MKS
  386. "${CMAKE_ANDROID_NDK}/build/core/toolchains/*/config.mk"
  387. "${CMAKE_ANDROID_NDK}/toolchains/*/config.mk"
  388. )
  389. foreach(config_mk IN LISTS _ANDROID_CONFIG_MKS)
  390. file(STRINGS "${config_mk}" _ANDROID_TOOL_ABIS REGEX "^TOOLCHAIN_ABIS :=")
  391. string(REPLACE "TOOLCHAIN_ABIS :=" "" _ANDROID_TOOL_ABIS "${_ANDROID_TOOL_ABIS}")
  392. separate_arguments(_ANDROID_TOOL_ABIS UNIX_COMMAND "${_ANDROID_TOOL_ABIS}")
  393. list(APPEND _ANDROID_ABIS ${_ANDROID_TOOL_ABIS})
  394. unset(_ANDROID_TOOL_ABIS)
  395. endforeach()
  396. unset(_ANDROID_CONFIG_MKS)
  397. # Choose the oldest among the available arm ABIs.
  398. if(_ANDROID_ABIS)
  399. list(REMOVE_DUPLICATES _ANDROID_ABIS)
  400. foreach(abi armeabi armeabi-v7a arm64-v8a)
  401. if("${abi}" IN_LIST _ANDROID_ABIS)
  402. set(CMAKE_ANDROID_ARCH_ABI "${abi}")
  403. break()
  404. endif()
  405. endforeach()
  406. endif()
  407. unset(_ANDROID_ABIS)
  408. if(NOT CMAKE_ANDROID_ARCH_ABI)
  409. set(CMAKE_ANDROID_ARCH_ABI "armeabi")
  410. endif()
  411. endif()
  412. endif()
  413. if(_INCLUDED_ABIS AND NOT CMAKE_ANDROID_ARCH_ABI IN_LIST _ANDROID_KNOWN_ABIS)
  414. message(FATAL_ERROR
  415. "Android: ABI '${CMAKE_ANDROID_ARCH_ABI}' is not supported by the NDK.\n"
  416. "Supported ABIS: ${_ANDROID_KNOWN_ABIS}."
  417. )
  418. endif()
  419. set(CMAKE_ANDROID_ARCH "${NDK_ABI_${CMAKE_ANDROID_ARCH_ABI}_ARCH}")
  420. if(_ANDROID_SYSROOT_ARCH AND NOT "x${_ANDROID_SYSROOT_ARCH}" STREQUAL "x${CMAKE_ANDROID_ARCH}")
  421. message(FATAL_ERROR
  422. "Android: Architecture '${_ANDROID_SYSROOT_ARCH}' specified in CMAKE_SYSROOT:\n"
  423. " ${CMAKE_SYSROOT}\n"
  424. "does not match architecture '${CMAKE_ANDROID_ARCH}' for the ABI '${CMAKE_ANDROID_ARCH_ABI}'."
  425. )
  426. endif()
  427. set(CMAKE_ANDROID_ARCH_TRIPLE "${NDK_ABI_${CMAKE_ANDROID_ARCH_ABI}_TRIPLE}")
  428. set(CMAKE_ANDROID_ARCH_LLVM_TRIPLE
  429. "${NDK_ABI_${CMAKE_ANDROID_ARCH_ABI}_LLVM_TRIPLE}")
  430. # Select a processor.
  431. if(NOT CMAKE_SYSTEM_PROCESSOR)
  432. set(CMAKE_SYSTEM_PROCESSOR "${NDK_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC}")
  433. endif()
  434. # If the user specified both an ABI and a processor then they might not match.
  435. if(NOT NDK_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC STREQUAL CMAKE_SYSTEM_PROCESSOR)
  436. message(FATAL_ERROR "Android: The specified CMAKE_ANDROID_ARCH_ABI='${CMAKE_ANDROID_ARCH_ABI}' and CMAKE_SYSTEM_PROCESSOR='${CMAKE_SYSTEM_PROCESSOR}' is not a valid combination.")
  437. endif()
  438. # Select an API.
  439. if(CMAKE_SYSTEM_VERSION)
  440. set(_ANDROID_API_VAR CMAKE_SYSTEM_VERSION)
  441. elseif(CMAKE_ANDROID_API)
  442. set(CMAKE_SYSTEM_VERSION "${CMAKE_ANDROID_API}")
  443. set(_ANDROID_API_VAR CMAKE_ANDROID_API)
  444. elseif(_ANDROID_SYSROOT_API)
  445. set(CMAKE_SYSTEM_VERSION "${_ANDROID_SYSROOT_API}")
  446. set(_ANDROID_API_VAR CMAKE_SYSROOT)
  447. elseif(_ANDROID_STANDALONE_TOOLCHAIN_API)
  448. set(CMAKE_SYSTEM_VERSION "${_ANDROID_STANDALONE_TOOLCHAIN_API}")
  449. endif()
  450. if(CMAKE_SYSTEM_VERSION)
  451. if(CMAKE_ANDROID_API AND NOT "x${CMAKE_ANDROID_API}" STREQUAL "x${CMAKE_SYSTEM_VERSION}")
  452. message(FATAL_ERROR
  453. "Android: The API specified by CMAKE_ANDROID_API='${CMAKE_ANDROID_API}' is not consistent with CMAKE_SYSTEM_VERSION='${CMAKE_SYSTEM_VERSION}'."
  454. )
  455. endif()
  456. if(_ANDROID_SYSROOT_API)
  457. foreach(v CMAKE_ANDROID_API CMAKE_SYSTEM_VERSION)
  458. if(${v} AND NOT "x${_ANDROID_SYSROOT_API}" STREQUAL "x${${v}}")
  459. message(FATAL_ERROR
  460. "Android: The API specified by ${v}='${${v}}' is not consistent with CMAKE_SYSROOT:\n"
  461. " ${CMAKE_SYSROOT}"
  462. )
  463. endif()
  464. endforeach()
  465. endif()
  466. if(CMAKE_ANDROID_NDK)
  467. if (_INCLUDED_PLATFORMS)
  468. if(CMAKE_SYSTEM_VERSION GREATER NDK_MAX_PLATFORM_LEVEL OR
  469. CMAKE_SYSTEM_VERSION LESS NDK_MIN_PLATFORM_LEVEL)
  470. message(FATAL_ERROR
  471. "Android: The API level ${CMAKE_SYSTEM_VERSION} is not supported by the NDK.\n"
  472. "Choose one in the range of [${NDK_MIN_PLATFORM_LEVEL}, ${NDK_MAX_PLATFORM_LEVEL}]."
  473. )
  474. endif()
  475. else()
  476. if(NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}")
  477. message(FATAL_ERROR
  478. "Android: The API specified by ${_ANDROID_API_VAR}='${${_ANDROID_API_VAR}}' does not exist in the NDK. "
  479. "The directory:\n"
  480. " ${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}\n"
  481. "does not exist."
  482. )
  483. endif()
  484. endif()
  485. endif()
  486. elseif(CMAKE_ANDROID_NDK)
  487. if (_INCLUDED_PLATFORMS)
  488. set(CMAKE_SYSTEM_VERSION ${NDK_MIN_PLATFORM_LEVEL})
  489. # And for LP64 we need to pull up to 21. No diagnostic is provided here because
  490. # minSdkVersion < 21 is valid for the project even though it may not be for this
  491. # ABI.
  492. if(CMAKE_ANDROID_ARCH_ABI MATCHES "64(-v8a)?$" AND CMAKE_SYSTEM_VERSION LESS 21)
  493. set(CMAKE_SYSTEM_VERSION 21)
  494. endif()
  495. else()
  496. file(GLOB _ANDROID_APIS_1 RELATIVE "${CMAKE_ANDROID_NDK}/platforms" "${CMAKE_ANDROID_NDK}/platforms/android-[0-9]")
  497. file(GLOB _ANDROID_APIS_2 RELATIVE "${CMAKE_ANDROID_NDK}/platforms" "${CMAKE_ANDROID_NDK}/platforms/android-[0-9][0-9]")
  498. list(SORT _ANDROID_APIS_1)
  499. list(SORT _ANDROID_APIS_2)
  500. set(_ANDROID_APIS ${_ANDROID_APIS_1} ${_ANDROID_APIS_2})
  501. unset(_ANDROID_APIS_1)
  502. unset(_ANDROID_APIS_2)
  503. if(_ANDROID_APIS STREQUAL "")
  504. message(FATAL_ERROR
  505. "Android: No APIs found in the NDK. No\n"
  506. " ${CMAKE_ANDROID_NDK}/platforms/android-*\n"
  507. "directories exist."
  508. )
  509. endif()
  510. string(REPLACE "android-" "" _ANDROID_APIS "${_ANDROID_APIS}")
  511. list(REVERSE _ANDROID_APIS)
  512. list(GET _ANDROID_APIS 0 CMAKE_SYSTEM_VERSION)
  513. unset(_ANDROID_APIS)
  514. endif()
  515. endif()
  516. if(NOT CMAKE_SYSTEM_VERSION MATCHES "^[0-9]+$")
  517. message(FATAL_ERROR "Android: The API specified by CMAKE_SYSTEM_VERSION='${CMAKE_SYSTEM_VERSION}' is not an integer.")
  518. endif()
  519. if(CMAKE_ANDROID_NDK AND NOT DEFINED CMAKE_ANDROID_NDK_DEPRECATED_HEADERS)
  520. if(IS_DIRECTORY "${CMAKE_ANDROID_NDK}/sysroot/usr/include/${CMAKE_ANDROID_ARCH_TRIPLE}")
  521. # Unified headers exist so we use them by default.
  522. set(CMAKE_ANDROID_NDK_DEPRECATED_HEADERS 0)
  523. else()
  524. # Unified headers do not exist so use the deprecated headers.
  525. set(CMAKE_ANDROID_NDK_DEPRECATED_HEADERS 1)
  526. endif()
  527. endif()
  528. # Save the Android-specific information in CMakeSystem.cmake.
  529. set(CMAKE_SYSTEM_CUSTOM_CODE "
  530. set(CMAKE_ANDROID_NDK \"${CMAKE_ANDROID_NDK}\")
  531. set(CMAKE_ANDROID_STANDALONE_TOOLCHAIN \"${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}\")
  532. set(CMAKE_ANDROID_ARCH \"${CMAKE_ANDROID_ARCH}\")
  533. set(CMAKE_ANDROID_ARCH_ABI \"${CMAKE_ANDROID_ARCH_ABI}\")
  534. ")
  535. if(CMAKE_ANDROID_NDK)
  536. string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
  537. "set(CMAKE_ANDROID_ARCH_TRIPLE \"${CMAKE_ANDROID_ARCH_TRIPLE}\")\n"
  538. "set(CMAKE_ANDROID_ARCH_LLVM_TRIPLE \"${CMAKE_ANDROID_ARCH_LLVM_TRIPLE}\")\n"
  539. "set(CMAKE_ANDROID_NDK_VERSION \"${CMAKE_ANDROID_NDK_VERSION}\")\n"
  540. "set(CMAKE_ANDROID_NDK_DEPRECATED_HEADERS \"${CMAKE_ANDROID_NDK_DEPRECATED_HEADERS}\")\n"
  541. "set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG \"${CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG}\")\n"
  542. "set(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED \"${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}\")\n"
  543. )
  544. endif()
  545. # Select an ARM variant.
  546. if(CMAKE_ANDROID_ARCH_ABI MATCHES "^armeabi")
  547. if(CMAKE_ANDROID_ARM_MODE)
  548. set(CMAKE_ANDROID_ARM_MODE 1)
  549. else()
  550. set(CMAKE_ANDROID_ARM_MODE 0)
  551. endif()
  552. string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
  553. "set(CMAKE_ANDROID_ARM_MODE \"${CMAKE_ANDROID_ARM_MODE}\")\n"
  554. )
  555. elseif(DEFINED CMAKE_ANDROID_ARM_MODE)
  556. message(FATAL_ERROR "Android: CMAKE_ANDROID_ARM_MODE is set but is valid only for 'armeabi' architectures.")
  557. endif()
  558. if(CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a")
  559. if(CMAKE_ANDROID_ARM_NEON)
  560. set(CMAKE_ANDROID_ARM_NEON 1)
  561. else()
  562. set(CMAKE_ANDROID_ARM_NEON 0)
  563. endif()
  564. string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
  565. "set(CMAKE_ANDROID_ARM_NEON \"${CMAKE_ANDROID_ARM_NEON}\")\n"
  566. )
  567. elseif(DEFINED CMAKE_ANDROID_ARM_NEON)
  568. message(FATAL_ERROR "Android: CMAKE_ANDROID_ARM_NEON is set but is valid only for 'armeabi-v7a' architecture.")
  569. endif()
  570. # Report the chosen architecture.
  571. message(STATUS "Android: Targeting API '${CMAKE_SYSTEM_VERSION}' with architecture '${CMAKE_ANDROID_ARCH}', ABI '${CMAKE_ANDROID_ARCH_ABI}', and processor '${CMAKE_SYSTEM_PROCESSOR}'")
  572. cmake_policy(POP)
  573. # Include the NDK hook.
  574. # It can be used by NDK to inject necessary fixes for an earlier cmake.
  575. if(CMAKE_ANDROID_NDK)
  576. include(${CMAKE_ANDROID_NDK}/build/cmake/hooks/post/Android-Determine.cmake OPTIONAL)
  577. endif()