Android-Determine.cmake 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. # Support for NVIDIA Nsight Tegra Visual Studio Edition was previously
  7. # implemented in the CMake VS IDE generators. Avoid interfering with
  8. # that functionality for now. Later we may try to integrate this.
  9. if(CMAKE_GENERATOR MATCHES "Visual Studio")
  10. return()
  11. endif()
  12. # Commonly used Android toolchain files that pre-date CMake upstream support
  13. # set CMAKE_SYSTEM_VERSION to 1. Avoid interfering with them.
  14. if(CMAKE_SYSTEM_VERSION EQUAL 1)
  15. return()
  16. endif()
  17. cmake_policy(PUSH)
  18. cmake_policy(SET CMP0057 NEW) # if IN_LIST
  19. # If the user provided CMAKE_SYSROOT for us, extract information from it.
  20. set(_ANDROID_SYSROOT_NDK "")
  21. set(_ANDROID_SYSROOT_API "")
  22. set(_ANDROID_SYSROOT_ARCH "")
  23. set(_ANDROID_SYSROOT_STANDALONE_TOOLCHAIN "")
  24. if(CMAKE_SYSROOT)
  25. if(NOT IS_DIRECTORY "${CMAKE_SYSROOT}")
  26. message(FATAL_ERROR
  27. "Android: The specified CMAKE_SYSROOT:\n"
  28. " ${CMAKE_SYSROOT}\n"
  29. "is not an existing directory."
  30. )
  31. endif()
  32. if(CMAKE_SYSROOT MATCHES "^([^\\\n]*)/platforms/android-([0-9]+)/arch-([a-z0-9_]+)$")
  33. set(_ANDROID_SYSROOT_NDK "${CMAKE_MATCH_1}")
  34. set(_ANDROID_SYSROOT_API "${CMAKE_MATCH_2}")
  35. set(_ANDROID_SYSROOT_ARCH "${CMAKE_MATCH_3}")
  36. elseif(CMAKE_SYSROOT MATCHES "^([^\\\n]*)/sysroot$")
  37. set(_ANDROID_SYSROOT_STANDALONE_TOOLCHAIN "${CMAKE_MATCH_1}")
  38. else()
  39. message(FATAL_ERROR
  40. "The value of CMAKE_SYSROOT:\n"
  41. " ${CMAKE_SYSROOT}\n"
  42. "does not match any of the forms:\n"
  43. " <ndk>/platforms/android-<api>/arch-<arch>\n"
  44. " <standalone-toolchain>/sysroot\n"
  45. "where:\n"
  46. " <ndk> = Android NDK directory (with forward slashes)\n"
  47. " <api> = Android API version number (decimal digits)\n"
  48. " <arch> = Android ARCH name (lower case)\n"
  49. " <standalone-toolchain> = Path to standalone toolchain prefix\n"
  50. )
  51. endif()
  52. endif()
  53. # Find the Android NDK.
  54. if(CMAKE_ANDROID_NDK)
  55. if(NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK}")
  56. message(FATAL_ERROR
  57. "Android: The NDK root directory specified by CMAKE_ANDROID_NDK:\n"
  58. " ${CMAKE_ANDROID_NDK}\n"
  59. "does not exist."
  60. )
  61. endif()
  62. elseif(CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  63. if(NOT IS_DIRECTORY "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}")
  64. message(FATAL_ERROR
  65. "Android: The standalone toolchain directory specified by CMAKE_ANDROID_STANDALONE_TOOLCHAIN:\n"
  66. " ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}\n"
  67. "does not exist."
  68. )
  69. endif()
  70. if(NOT EXISTS "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h")
  71. message(FATAL_ERROR
  72. "Android: The standalone toolchain directory specified by CMAKE_ANDROID_STANDALONE_TOOLCHAIN:\n"
  73. " ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}\n"
  74. "does not contain a sysroot with a known layout. The file:\n"
  75. " ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h\n"
  76. "does not exist."
  77. )
  78. endif()
  79. else()
  80. if(IS_DIRECTORY "${_ANDROID_SYSROOT_NDK}")
  81. set(CMAKE_ANDROID_NDK "${_ANDROID_SYSROOT_NDK}")
  82. elseif(IS_DIRECTORY "${_ANDROID_SYSROOT_STANDALONE_TOOLCHAIN}")
  83. set(CMAKE_ANDROID_STANDALONE_TOOLCHAIN "${_ANDROID_SYSROOT_STANDALONE_TOOLCHAIN}")
  84. elseif(IS_DIRECTORY "${ANDROID_NDK}")
  85. file(TO_CMAKE_PATH "${ANDROID_NDK}" CMAKE_ANDROID_NDK)
  86. elseif(IS_DIRECTORY "${ANDROID_STANDALONE_TOOLCHAIN}")
  87. file(TO_CMAKE_PATH "${ANDROID_STANDALONE_TOOLCHAIN}" CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  88. elseif(IS_DIRECTORY "$ENV{ANDROID_NDK_ROOT}")
  89. file(TO_CMAKE_PATH "$ENV{ANDROID_NDK_ROOT}" CMAKE_ANDROID_NDK)
  90. elseif(IS_DIRECTORY "$ENV{ANDROID_NDK}")
  91. file(TO_CMAKE_PATH "$ENV{ANDROID_NDK}" CMAKE_ANDROID_NDK)
  92. elseif(IS_DIRECTORY "$ENV{ANDROID_STANDALONE_TOOLCHAIN}")
  93. file(TO_CMAKE_PATH "$ENV{ANDROID_STANDALONE_TOOLCHAIN}" CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  94. endif()
  95. # TODO: Search harder for the NDK or standalone toolchain.
  96. endif()
  97. set(_ANDROID_STANDALONE_TOOLCHAIN_API "")
  98. if(CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  99. # Try to read the API level from the toolchain launcher.
  100. if(EXISTS "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/bin/clang")
  101. set(_ANDROID_API_LEVEL_CLANG_REGEX "__ANDROID_API__=([0-9]+)")
  102. file(STRINGS "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/bin/clang" _ANDROID_STANDALONE_TOOLCHAIN_BIN_CLANG
  103. REGEX "${_ANDROID_API_LEVEL_CLANG_REGEX}" LIMIT_COUNT 1 LIMIT_INPUT 65536)
  104. if(_ANDROID_STANDALONE_TOOLCHAIN_BIN_CLANG MATCHES "${_ANDROID_API_LEVEL_CLANG_REGEX}")
  105. set(_ANDROID_STANDALONE_TOOLCHAIN_API "${CMAKE_MATCH_1}")
  106. endif()
  107. unset(_ANDROID_STANDALONE_TOOLCHAIN_BIN_CLANG)
  108. unset(_ANDROID_API_LEVEL_CLANG_REGEX)
  109. endif()
  110. if(NOT _ANDROID_STANDALONE_TOOLCHAIN_API)
  111. # The compiler launcher does not know __ANDROID_API__. Assume this
  112. # is not unified headers and look for it in the api-level.h header.
  113. set(_ANDROID_API_LEVEL_H_REGEX "^[\t ]*#[\t ]*define[\t ]+__ANDROID_API__[\t ]+([0-9]+)")
  114. file(STRINGS "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h"
  115. _ANDROID_API_LEVEL_H_CONTENT REGEX "${_ANDROID_API_LEVEL_H_REGEX}")
  116. if(_ANDROID_API_LEVEL_H_CONTENT MATCHES "${_ANDROID_API_LEVEL_H_REGEX}")
  117. set(_ANDROID_STANDALONE_TOOLCHAIN_API "${CMAKE_MATCH_1}")
  118. endif()
  119. endif()
  120. if(NOT _ANDROID_STANDALONE_TOOLCHAIN_API)
  121. message(WARNING
  122. "Android: Did not detect API level from\n"
  123. " ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/bin/clang\n"
  124. "or\n"
  125. " ${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot/usr/include/android/api-level.h\n"
  126. )
  127. endif()
  128. endif()
  129. if(NOT CMAKE_ANDROID_NDK AND NOT CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  130. message(FATAL_ERROR "Android: Neither the NDK or a standalone toolchain was found.")
  131. endif()
  132. # Select an API.
  133. if(CMAKE_SYSTEM_VERSION)
  134. set(_ANDROID_API_VAR CMAKE_SYSTEM_VERSION)
  135. elseif(CMAKE_ANDROID_API)
  136. set(CMAKE_SYSTEM_VERSION "${CMAKE_ANDROID_API}")
  137. set(_ANDROID_API_VAR CMAKE_ANDROID_API)
  138. elseif(_ANDROID_SYSROOT_API)
  139. set(CMAKE_SYSTEM_VERSION "${_ANDROID_SYSROOT_API}")
  140. set(_ANDROID_API_VAR CMAKE_SYSROOT)
  141. elseif(_ANDROID_STANDALONE_TOOLCHAIN_API)
  142. set(CMAKE_SYSTEM_VERSION "${_ANDROID_STANDALONE_TOOLCHAIN_API}")
  143. endif()
  144. if(CMAKE_SYSTEM_VERSION)
  145. if(CMAKE_ANDROID_API AND NOT "x${CMAKE_ANDROID_API}" STREQUAL "x${CMAKE_SYSTEM_VERSION}")
  146. message(FATAL_ERROR
  147. "Android: The API specified by CMAKE_ANDROID_API='${CMAKE_ANDROID_API}' is not consistent with CMAKE_SYSTEM_VERSION='${CMAKE_SYSTEM_VERSION}'."
  148. )
  149. endif()
  150. if(_ANDROID_SYSROOT_API)
  151. foreach(v CMAKE_ANDROID_API CMAKE_SYSTEM_VERSION)
  152. if(${v} AND NOT "x${_ANDROID_SYSROOT_API}" STREQUAL "x${${v}}")
  153. message(FATAL_ERROR
  154. "Android: The API specified by ${v}='${${v}}' is not consistent with CMAKE_SYSROOT:\n"
  155. " ${CMAKE_SYSROOT}"
  156. )
  157. endif()
  158. endforeach()
  159. endif()
  160. if(CMAKE_ANDROID_NDK AND NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}")
  161. message(FATAL_ERROR
  162. "Android: The API specified by ${_ANDROID_API_VAR}='${${_ANDROID_API_VAR}}' does not exist in the NDK. "
  163. "The directory:\n"
  164. " ${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}\n"
  165. "does not exist."
  166. )
  167. endif()
  168. elseif(CMAKE_ANDROID_NDK)
  169. file(GLOB _ANDROID_APIS_1 RELATIVE "${CMAKE_ANDROID_NDK}/platforms" "${CMAKE_ANDROID_NDK}/platforms/android-[0-9]")
  170. file(GLOB _ANDROID_APIS_2 RELATIVE "${CMAKE_ANDROID_NDK}/platforms" "${CMAKE_ANDROID_NDK}/platforms/android-[0-9][0-9]")
  171. list(SORT _ANDROID_APIS_1)
  172. list(SORT _ANDROID_APIS_2)
  173. set(_ANDROID_APIS ${_ANDROID_APIS_1} ${_ANDROID_APIS_2})
  174. unset(_ANDROID_APIS_1)
  175. unset(_ANDROID_APIS_2)
  176. if(_ANDROID_APIS STREQUAL "")
  177. message(FATAL_ERROR
  178. "Android: No APIs found in the NDK. No\n"
  179. " ${CMAKE_ANDROID_NDK}/platforms/android-*\n"
  180. "directories exist."
  181. )
  182. endif()
  183. string(REPLACE "android-" "" _ANDROID_APIS "${_ANDROID_APIS}")
  184. list(REVERSE _ANDROID_APIS)
  185. list(GET _ANDROID_APIS 0 CMAKE_SYSTEM_VERSION)
  186. unset(_ANDROID_APIS)
  187. endif()
  188. if(NOT CMAKE_SYSTEM_VERSION MATCHES "^[0-9]+$")
  189. message(FATAL_ERROR "Android: The API specified by CMAKE_SYSTEM_VERSION='${CMAKE_SYSTEM_VERSION}' is not an integer.")
  190. endif()
  191. if(CMAKE_ANDROID_NDK)
  192. # Identify the host platform.
  193. if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
  194. if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
  195. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "darwin-x86_64")
  196. else()
  197. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "darwin-x86")
  198. endif()
  199. elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
  200. if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "x86_64")
  201. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "linux-x86_64")
  202. else()
  203. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "linux-x86")
  204. endif()
  205. elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
  206. if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "AMD64")
  207. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "windows-x86_64")
  208. else()
  209. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "windows")
  210. endif()
  211. else()
  212. message(FATAL_ERROR "Android: Builds hosted on '${CMAKE_HOST_SYSTEM_NAME}' not supported.")
  213. endif()
  214. # Look for a unified toolchain/sysroot provided with the NDK.
  215. set(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED "${CMAKE_ANDROID_NDK}/toolchains/llvm/prebuilt/${CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG}")
  216. if(NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/sysroot")
  217. set(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED "")
  218. endif()
  219. else()
  220. set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG "")
  221. set(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED "")
  222. endif()
  223. # https://developer.android.com/ndk/guides/abis.html
  224. set(_ANDROID_ABI_arm64-v8a_PROC "aarch64")
  225. set(_ANDROID_ABI_arm64-v8a_ARCH "arm64")
  226. set(_ANDROID_ABI_arm64-v8a_TRIPLE "aarch64-linux-android")
  227. set(_ANDROID_ABI_armeabi-v7a_PROC "armv7-a")
  228. set(_ANDROID_ABI_armeabi-v7a_ARCH "arm")
  229. set(_ANDROID_ABI_armeabi-v7a_TRIPLE "arm-linux-androideabi")
  230. set(_ANDROID_ABI_armeabi-v6_PROC "armv6")
  231. set(_ANDROID_ABI_armeabi-v6_ARCH "arm")
  232. set(_ANDROID_ABI_armeabi-v6_TRIPLE "arm-linux-androideabi")
  233. set(_ANDROID_ABI_armeabi_PROC "armv5te")
  234. set(_ANDROID_ABI_armeabi_ARCH "arm")
  235. set(_ANDROID_ABI_armeabi_TRIPLE "arm-linux-androideabi")
  236. set(_ANDROID_ABI_mips_PROC "mips")
  237. set(_ANDROID_ABI_mips_ARCH "mips")
  238. set(_ANDROID_ABI_mips_TRIPLE "mipsel-linux-android")
  239. set(_ANDROID_ABI_mips64_PROC "mips64")
  240. set(_ANDROID_ABI_mips64_ARCH "mips64")
  241. set(_ANDROID_ABI_mips64_TRIPLE "mips64el-linux-android")
  242. set(_ANDROID_ABI_x86_PROC "i686")
  243. set(_ANDROID_ABI_x86_ARCH "x86")
  244. set(_ANDROID_ABI_x86_TRIPLE "i686-linux-android")
  245. set(_ANDROID_ABI_x86_64_PROC "x86_64")
  246. set(_ANDROID_ABI_x86_64_ARCH "x86_64")
  247. set(_ANDROID_ABI_x86_64_TRIPLE "x86_64-linux-android")
  248. set(_ANDROID_PROC_aarch64_ARCH_ABI "arm64-v8a")
  249. set(_ANDROID_PROC_armv7-a_ARCH_ABI "armeabi-v7a")
  250. set(_ANDROID_PROC_armv6_ARCH_ABI "armeabi-v6")
  251. set(_ANDROID_PROC_armv5te_ARCH_ABI "armeabi")
  252. set(_ANDROID_PROC_i686_ARCH_ABI "x86")
  253. set(_ANDROID_PROC_mips_ARCH_ABI "mips")
  254. set(_ANDROID_PROC_mips64_ARCH_ABI "mips64")
  255. set(_ANDROID_PROC_x86_64_ARCH_ABI "x86_64")
  256. set(_ANDROID_ARCH_arm64_ABI "arm64-v8a")
  257. set(_ANDROID_ARCH_arm_ABI "armeabi")
  258. set(_ANDROID_ARCH_mips_ABI "mips")
  259. set(_ANDROID_ARCH_mips64_ABI "mips64")
  260. set(_ANDROID_ARCH_x86_ABI "x86")
  261. set(_ANDROID_ARCH_x86_64_ABI "x86_64")
  262. # Validate inputs.
  263. if(CMAKE_ANDROID_ARCH_ABI AND NOT DEFINED "_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC")
  264. message(FATAL_ERROR "Android: Unknown ABI CMAKE_ANDROID_ARCH_ABI='${CMAKE_ANDROID_ARCH_ABI}'.")
  265. endif()
  266. if(CMAKE_SYSTEM_PROCESSOR AND NOT DEFINED "_ANDROID_PROC_${CMAKE_SYSTEM_PROCESSOR}_ARCH_ABI")
  267. message(FATAL_ERROR "Android: Unknown processor CMAKE_SYSTEM_PROCESSOR='${CMAKE_SYSTEM_PROCESSOR}'.")
  268. endif()
  269. if(_ANDROID_SYSROOT_ARCH AND NOT DEFINED "_ANDROID_ARCH_${_ANDROID_SYSROOT_ARCH}_ABI")
  270. message(FATAL_ERROR
  271. "Android: Unknown architecture '${_ANDROID_SYSROOT_ARCH}' specified in CMAKE_SYSROOT:\n"
  272. " ${CMAKE_SYSROOT}"
  273. )
  274. endif()
  275. # Select an ABI.
  276. if(NOT CMAKE_ANDROID_ARCH_ABI)
  277. if(CMAKE_SYSTEM_PROCESSOR)
  278. set(CMAKE_ANDROID_ARCH_ABI "${_ANDROID_PROC_${CMAKE_SYSTEM_PROCESSOR}_ARCH_ABI}")
  279. elseif(_ANDROID_SYSROOT_ARCH)
  280. set(CMAKE_ANDROID_ARCH_ABI "${_ANDROID_ARCH_${_ANDROID_SYSROOT_ARCH}_ABI}")
  281. else()
  282. # https://developer.android.com/ndk/guides/application_mk.html
  283. # Default is the oldest ARM ABI.
  284. # Lookup the available ABIs among all toolchains.
  285. set(_ANDROID_ABIS "")
  286. file(GLOB _ANDROID_CONFIG_MKS
  287. "${CMAKE_ANDROID_NDK}/build/core/toolchains/*/config.mk"
  288. "${CMAKE_ANDROID_NDK}/toolchains/*/config.mk"
  289. )
  290. foreach(config_mk IN LISTS _ANDROID_CONFIG_MKS)
  291. file(STRINGS "${config_mk}" _ANDROID_TOOL_ABIS REGEX "^TOOLCHAIN_ABIS :=")
  292. string(REPLACE "TOOLCHAIN_ABIS :=" "" _ANDROID_TOOL_ABIS "${_ANDROID_TOOL_ABIS}")
  293. separate_arguments(_ANDROID_TOOL_ABIS UNIX_COMMAND "${_ANDROID_TOOL_ABIS}")
  294. list(APPEND _ANDROID_ABIS ${_ANDROID_TOOL_ABIS})
  295. unset(_ANDROID_TOOL_ABIS)
  296. endforeach()
  297. unset(_ANDROID_CONFIG_MKS)
  298. # Choose the oldest among the available arm ABIs.
  299. if(_ANDROID_ABIS)
  300. list(REMOVE_DUPLICATES _ANDROID_ABIS)
  301. cmake_policy(PUSH)
  302. cmake_policy(SET CMP0057 NEW)
  303. foreach(abi armeabi armeabi-v7a arm64-v8a)
  304. if("${abi}" IN_LIST _ANDROID_ABIS)
  305. set(CMAKE_ANDROID_ARCH_ABI "${abi}")
  306. break()
  307. endif()
  308. endforeach()
  309. cmake_policy(POP)
  310. endif()
  311. unset(_ANDROID_ABIS)
  312. if(NOT CMAKE_ANDROID_ARCH_ABI)
  313. set(CMAKE_ANDROID_ARCH_ABI "armeabi")
  314. endif()
  315. endif()
  316. endif()
  317. set(CMAKE_ANDROID_ARCH "${_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_ARCH}")
  318. if(_ANDROID_SYSROOT_ARCH AND NOT "x${_ANDROID_SYSROOT_ARCH}" STREQUAL "x${CMAKE_ANDROID_ARCH}")
  319. message(FATAL_ERROR
  320. "Android: Architecture '${_ANDROID_SYSROOT_ARCH}' specified in CMAKE_SYSROOT:\n"
  321. " ${CMAKE_SYSROOT}\n"
  322. "does not match architecture '${CMAKE_ANDROID_ARCH}' for the ABI '${CMAKE_ANDROID_ARCH_ABI}'."
  323. )
  324. endif()
  325. set(CMAKE_ANDROID_ARCH_TRIPLE "${_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_TRIPLE}")
  326. # Select a processor.
  327. if(NOT CMAKE_SYSTEM_PROCESSOR)
  328. set(CMAKE_SYSTEM_PROCESSOR "${_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC}")
  329. endif()
  330. # If the user specified both an ABI and a processor then they might not match.
  331. if(NOT _ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC STREQUAL CMAKE_SYSTEM_PROCESSOR)
  332. 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.")
  333. endif()
  334. if(CMAKE_ANDROID_NDK AND NOT DEFINED CMAKE_ANDROID_NDK_DEPRECATED_HEADERS)
  335. if(IS_DIRECTORY "${CMAKE_ANDROID_NDK}/sysroot/usr/include/${CMAKE_ANDROID_ARCH_TRIPLE}")
  336. # Unified headers exist so we use them by default.
  337. set(CMAKE_ANDROID_NDK_DEPRECATED_HEADERS 0)
  338. else()
  339. # Unified headers do not exist so use the deprecated headers.
  340. set(CMAKE_ANDROID_NDK_DEPRECATED_HEADERS 1)
  341. endif()
  342. endif()
  343. # Save the Android-specific information in CMakeSystem.cmake.
  344. set(CMAKE_SYSTEM_CUSTOM_CODE "
  345. set(CMAKE_ANDROID_NDK \"${CMAKE_ANDROID_NDK}\")
  346. set(CMAKE_ANDROID_STANDALONE_TOOLCHAIN \"${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}\")
  347. set(CMAKE_ANDROID_ARCH \"${CMAKE_ANDROID_ARCH}\")
  348. set(CMAKE_ANDROID_ARCH_ABI \"${CMAKE_ANDROID_ARCH_ABI}\")
  349. ")
  350. if(CMAKE_ANDROID_NDK)
  351. string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
  352. "set(CMAKE_ANDROID_ARCH_TRIPLE \"${CMAKE_ANDROID_ARCH_TRIPLE}\")\n"
  353. "set(CMAKE_ANDROID_NDK_DEPRECATED_HEADERS \"${CMAKE_ANDROID_NDK_DEPRECATED_HEADERS}\")\n"
  354. "set(CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG \"${CMAKE_ANDROID_NDK_TOOLCHAIN_HOST_TAG}\")\n"
  355. "set(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED \"${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}\")\n"
  356. )
  357. endif()
  358. # Select an ARM variant.
  359. if(CMAKE_ANDROID_ARCH_ABI MATCHES "^armeabi")
  360. if(CMAKE_ANDROID_ARM_MODE)
  361. set(CMAKE_ANDROID_ARM_MODE 1)
  362. else()
  363. set(CMAKE_ANDROID_ARM_MODE 0)
  364. endif()
  365. string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
  366. "set(CMAKE_ANDROID_ARM_MODE \"${CMAKE_ANDROID_ARM_MODE}\")\n"
  367. )
  368. elseif(DEFINED CMAKE_ANDROID_ARM_MODE)
  369. message(FATAL_ERROR "Android: CMAKE_ANDROID_ARM_MODE is set but is valid only for 'armeabi' architectures.")
  370. endif()
  371. if(CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a")
  372. if(CMAKE_ANDROID_ARM_NEON)
  373. set(CMAKE_ANDROID_ARM_NEON 1)
  374. else()
  375. set(CMAKE_ANDROID_ARM_NEON 0)
  376. endif()
  377. string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
  378. "set(CMAKE_ANDROID_ARM_NEON \"${CMAKE_ANDROID_ARM_NEON}\")\n"
  379. )
  380. elseif(DEFINED CMAKE_ANDROID_ARM_NEON)
  381. message(FATAL_ERROR "Android: CMAKE_ANDROID_ARM_NEON is set but is valid only for 'armeabi-v7a' architecture.")
  382. endif()
  383. # Report the chosen architecture.
  384. message(STATUS "Android: Targeting API '${CMAKE_SYSTEM_VERSION}' with architecture '${CMAKE_ANDROID_ARCH}', ABI '${CMAKE_ANDROID_ARCH_ABI}', and processor '${CMAKE_SYSTEM_PROCESSOR}'")
  385. cmake_policy(POP)