Android-Determine.cmake 24 KB

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