Android-Determine.cmake 24 KB

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