Android-Determine.cmake 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #=============================================================================
  2. # Copyright 2015-2016 Kitware, Inc.
  3. #
  4. # Distributed under the OSI-approved BSD License (the "License");
  5. # see accompanying file Copyright.txt for details.
  6. #
  7. # This software is distributed WITHOUT ANY WARRANTY; without even the
  8. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. # See the License for more information.
  10. #=============================================================================
  11. # (To distribute this file outside of CMake, substitute the full
  12. # License text for the above reference.)
  13. # When CMAKE_SYSTEM_NAME is "Android", CMakeDetermineSystem loads this module.
  14. # This module detects platform-wide information about the Android target
  15. # in order to store it in "CMakeSystem.cmake".
  16. # Support for NVIDIA Nsight Tegra Visual Studio Edition was previously
  17. # implemented in the CMake VS IDE generators. Avoid interfering with
  18. # that functionality for now. Later we may try to integrate this.
  19. if(CMAKE_VS_PLATFORM_NAME STREQUAL "Tegra-Android")
  20. return()
  21. endif()
  22. # If the user provided CMAKE_SYSROOT for us, extract information from it.
  23. set(_ANDROID_SYSROOT_NDK "")
  24. set(_ANDROID_SYSROOT_API "")
  25. set(_ANDROID_SYSROOT_ARCH "")
  26. if(CMAKE_SYSROOT)
  27. if(NOT IS_DIRECTORY "${CMAKE_SYSROOT}")
  28. message(FATAL_ERROR
  29. "Android: The specified CMAKE_SYSROOT:\n"
  30. " ${CMAKE_SYSROOT}\n"
  31. "is not an existing directory."
  32. )
  33. endif()
  34. if(CMAKE_SYSROOT MATCHES "^([^\\\n]*)/platforms/android-([0-9]+)/arch-([a-z0-9_]+)$")
  35. set(_ANDROID_SYSROOT_NDK "${CMAKE_MATCH_1}")
  36. set(_ANDROID_SYSROOT_API "${CMAKE_MATCH_2}")
  37. set(_ANDROID_SYSROOT_ARCH "${CMAKE_MATCH_3}")
  38. else()
  39. message(FATAL_ERROR
  40. "The value of CMAKE_SYSROOT:\n"
  41. " ${CMAKE_SYSROOT}\n"
  42. "does not match the form:\n"
  43. " <ndk>/platforms/android-<api>/arch-<arch>\n"
  44. "where:\n"
  45. " <ndk> = Android NDK directory (with forward slashes)\n"
  46. " <api> = Android API version number (decimal digits)\n"
  47. " <arch> = Android ARCH name (lower case)"
  48. )
  49. endif()
  50. endif()
  51. # Find the Android NDK.
  52. if(CMAKE_ANDROID_NDK)
  53. if(NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK}")
  54. message(FATAL_ERROR
  55. "Android: The NDK root directory specified by CMAKE_ANDROID_NDK:\n"
  56. " ${CMAKE_ANDROID_NDK}\n"
  57. "does not exist."
  58. )
  59. endif()
  60. else()
  61. if(IS_DIRECTORY "${_ANDROID_SYSROOT_NDK}")
  62. set(CMAKE_ANDROID_NDK "${_ANDROID_SYSROOT_NDK}")
  63. elseif(IS_DIRECTORY "$ENV{ANDROID_NDK_ROOT}")
  64. file(TO_CMAKE_PATH "$ENV{ANDROID_NDK_ROOT}" CMAKE_ANDROID_NDK)
  65. endif()
  66. # TODO: Search harder for the NDK.
  67. endif()
  68. if(NOT CMAKE_ANDROID_NDK)
  69. message(FATAL_ERROR "Android: The NDK root directory was not found.")
  70. endif()
  71. # Select an API.
  72. if(CMAKE_SYSTEM_VERSION)
  73. set(_ANDROID_API_VAR CMAKE_SYSTEM_VERSION)
  74. elseif(CMAKE_ANDROID_API)
  75. set(CMAKE_SYSTEM_VERSION "${CMAKE_ANDROID_API}")
  76. set(_ANDROID_API_VAR CMAKE_ANDROID_API)
  77. elseif(_ANDROID_SYSROOT_API)
  78. set(CMAKE_SYSTEM_VERSION "${_ANDROID_SYSROOT_API}")
  79. set(_ANDROID_API_VAR CMAKE_SYSROOT)
  80. endif()
  81. if(CMAKE_SYSTEM_VERSION)
  82. if(CMAKE_ANDROID_API AND NOT "x${CMAKE_ANDROID_API}" STREQUAL "x${CMAKE_SYSTEM_VERSION}")
  83. message(FATAL_ERROR
  84. "Android: The API specified by CMAKE_ANDROID_API='${CMAKE_ANDROID_API}' is not consistent with CMAKE_SYSTEM_VERSION='${CMAKE_SYSTEM_VERSION}'."
  85. )
  86. endif()
  87. if(_ANDROID_SYSROOT_API)
  88. foreach(v CMAKE_ANDROID_API CMAKE_SYSTEM_VERSION)
  89. if(${v} AND NOT "x${_ANDROID_SYSROOT_API}" STREQUAL "x${${v}}")
  90. message(FATAL_ERROR
  91. "Android: The API specified by ${v}='${${v}}' is not consistent with CMAKE_SYSROOT:\n"
  92. " ${CMAKE_SYSROOT}"
  93. )
  94. endif()
  95. endforeach()
  96. endif()
  97. if(CMAKE_ANDROID_NDK AND NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}")
  98. message(FATAL_ERROR
  99. "Android: The API specified by ${_ANDROID_API_VAR}='${${_ANDROID_API_VAR}}' does not exist in the NDK. "
  100. "The directory:\n"
  101. " ${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}\n"
  102. "does not exist."
  103. )
  104. endif()
  105. elseif(CMAKE_ANDROID_NDK)
  106. file(GLOB _ANDROID_APIS_1 RELATIVE "${CMAKE_ANDROID_NDK}/platforms" "${CMAKE_ANDROID_NDK}/platforms/android-[0-9]")
  107. file(GLOB _ANDROID_APIS_2 RELATIVE "${CMAKE_ANDROID_NDK}/platforms" "${CMAKE_ANDROID_NDK}/platforms/android-[0-9][0-9]")
  108. list(SORT _ANDROID_APIS_1)
  109. list(SORT _ANDROID_APIS_2)
  110. set(_ANDROID_APIS ${_ANDROID_APIS_1} ${_ANDROID_APIS_2})
  111. unset(_ANDROID_APIS_1)
  112. unset(_ANDROID_APIS_2)
  113. if(_ANDROID_APIS STREQUAL "")
  114. message(FATAL_ERROR
  115. "Android: No APIs found in the NDK. No\n"
  116. " ${CMAKE_ANDROID_NDK}/platforms/android-*\n"
  117. "directories exist."
  118. )
  119. endif()
  120. string(REPLACE "android-" "" _ANDROID_APIS "${_ANDROID_APIS}")
  121. list(REVERSE _ANDROID_APIS)
  122. list(GET _ANDROID_APIS 0 CMAKE_SYSTEM_VERSION)
  123. unset(_ANDROID_APIS)
  124. endif()
  125. if(NOT CMAKE_SYSTEM_VERSION MATCHES "^[0-9]+$")
  126. message(FATAL_ERROR "Android: The API specified by CMAKE_SYSTEM_VERSION='${CMAKE_SYSTEM_VERSION}' is not an integer.")
  127. endif()
  128. # https://developer.android.com/ndk/guides/abis.html
  129. set(_ANDROID_ABI_arm64-v8a_PROC "aarch64")
  130. set(_ANDROID_ABI_arm64-v8a_ARCH "arm64")
  131. set(_ANDROID_ABI_armeabi-v7a_PROC "armv7-a")
  132. set(_ANDROID_ABI_armeabi-v7a_ARCH "arm")
  133. set(_ANDROID_ABI_armeabi-v6_PROC "armv6")
  134. set(_ANDROID_ABI_armeabi-v6_ARCH "arm")
  135. set(_ANDROID_ABI_armeabi_PROC "armv5te")
  136. set(_ANDROID_ABI_armeabi_ARCH "arm")
  137. set(_ANDROID_ABI_mips_PROC "mips")
  138. set(_ANDROID_ABI_mips_ARCH "mips")
  139. set(_ANDROID_ABI_mips64_PROC "mips64")
  140. set(_ANDROID_ABI_mips64_ARCH "mips64")
  141. set(_ANDROID_ABI_x86_PROC "i686")
  142. set(_ANDROID_ABI_x86_ARCH "x86")
  143. set(_ANDROID_ABI_x86_64_PROC "x86_64")
  144. set(_ANDROID_ABI_x86_64_ARCH "x86_64")
  145. set(_ANDROID_PROC_aarch64_ARCH_ABI "arm64-v8a")
  146. set(_ANDROID_PROC_armv7-a_ARCH_ABI "armeabi-v7a")
  147. set(_ANDROID_PROC_armv6_ARCH_ABI "armeabi-v6")
  148. set(_ANDROID_PROC_armv5te_ARCH_ABI "armeabi")
  149. set(_ANDROID_PROC_i686_ARCH_ABI "x86")
  150. set(_ANDROID_PROC_mips_ARCH_ABI "mips")
  151. set(_ANDROID_PROC_mips64_ARCH_ABI "mips64")
  152. set(_ANDROID_PROC_x86_64_ARCH_ABI "x86_64")
  153. set(_ANDROID_ARCH_arm64_ABI "arm64-v8a")
  154. set(_ANDROID_ARCH_arm_ABI "armeabi")
  155. set(_ANDROID_ARCH_mips_ABI "mips")
  156. set(_ANDROID_ARCH_mips64_ABI "mips64")
  157. set(_ANDROID_ARCH_x86_ABI "x86")
  158. set(_ANDROID_ARCH_x86_64_ABI "x86_64")
  159. # Validate inputs.
  160. if(CMAKE_ANDROID_ARCH_ABI AND NOT DEFINED "_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC")
  161. message(FATAL_ERROR "Android: Unknown ABI CMAKE_ANDROID_ARCH_ABI='${CMAKE_ANDROID_ARCH_ABI}'.")
  162. endif()
  163. if(CMAKE_SYSTEM_PROCESSOR AND NOT DEFINED "_ANDROID_PROC_${CMAKE_SYSTEM_PROCESSOR}_ARCH_ABI")
  164. message(FATAL_ERROR "Android: Unknown processor CMAKE_SYSTEM_PROCESSOR='${CMAKE_SYSTEM_PROCESSOR}'.")
  165. endif()
  166. if(_ANDROID_SYSROOT_ARCH AND NOT DEFINED "_ANDROID_ARCH_${_ANDROID_SYSROOT_ARCH}_ABI")
  167. message(FATAL_ERROR
  168. "Android: Unknown architecture '${_ANDROID_SYSROOT_ARCH}' specified in CMAKE_SYSROOT:\n"
  169. " ${CMAKE_SYSROOT}"
  170. )
  171. endif()
  172. # Select an ABI.
  173. if(NOT CMAKE_ANDROID_ARCH_ABI)
  174. if(CMAKE_SYSTEM_PROCESSOR)
  175. set(CMAKE_ANDROID_ARCH_ABI "${_ANDROID_PROC_${CMAKE_SYSTEM_PROCESSOR}_ARCH_ABI}")
  176. elseif(_ANDROID_SYSROOT_ARCH)
  177. set(CMAKE_ANDROID_ARCH_ABI "${_ANDROID_ARCH_${_ANDROID_SYSROOT_ARCH}_ABI}")
  178. else()
  179. # https://developer.android.com/ndk/guides/application_mk.html
  180. # Default is the oldest ARM ABI.
  181. set(CMAKE_ANDROID_ARCH_ABI "armeabi")
  182. endif()
  183. endif()
  184. set(CMAKE_ANDROID_ARCH "${_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_ARCH}")
  185. if(_ANDROID_SYSROOT_ARCH AND NOT "x${_ANDROID_SYSROOT_ARCH}" STREQUAL "x${CMAKE_ANDROID_ARCH}")
  186. message(FATAL_ERROR
  187. "Android: Architecture '${_ANDROID_SYSROOT_ARCH}' specified in CMAKE_SYSROOT:\n"
  188. " ${CMAKE_SYSROOT}\n"
  189. "does not match architecture '${CMAKE_ANDROID_ARCH}' for the ABI '${CMAKE_ANDROID_ARCH_ABI}'."
  190. )
  191. endif()
  192. # Select a processor.
  193. if(NOT CMAKE_SYSTEM_PROCESSOR)
  194. set(CMAKE_SYSTEM_PROCESSOR "${_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC}")
  195. endif()
  196. # If the user specified both an ABI and a processor then they might not match.
  197. if(NOT _ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC STREQUAL CMAKE_SYSTEM_PROCESSOR)
  198. 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.")
  199. endif()
  200. # Save the Android-specific information in CMakeSystem.cmake.
  201. set(CMAKE_SYSTEM_CUSTOM_CODE "
  202. set(CMAKE_ANDROID_NDK \"${CMAKE_ANDROID_NDK}\")
  203. set(CMAKE_ANDROID_ARCH \"${CMAKE_ANDROID_ARCH}\")
  204. set(CMAKE_ANDROID_ARCH_ABI \"${CMAKE_ANDROID_ARCH_ABI}\")
  205. ")
  206. # Select an ARM variant.
  207. if(CMAKE_ANDROID_ARCH_ABI MATCHES "^armeabi")
  208. if(CMAKE_ANDROID_ARM_MODE)
  209. set(CMAKE_ANDROID_ARM_MODE 1)
  210. else()
  211. set(CMAKE_ANDROID_ARM_MODE 0)
  212. endif()
  213. string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
  214. "set(CMAKE_ANDROID_ARM_MODE \"${CMAKE_ANDROID_ARM_MODE}\")\n"
  215. )
  216. elseif(DEFINED CMAKE_ANDROID_ARM_MODE)
  217. message(FATAL_ERROR "Android: CMAKE_ANDROID_ARM_MODE is set but is valid only for 'armeabi' architectures.")
  218. endif()
  219. if(CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a")
  220. if(CMAKE_ANDROID_ARM_NEON)
  221. set(CMAKE_ANDROID_ARM_NEON 1)
  222. else()
  223. set(CMAKE_ANDROID_ARM_NEON 0)
  224. endif()
  225. string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
  226. "set(CMAKE_ANDROID_ARM_NEON \"${CMAKE_ANDROID_ARM_NEON}\")\n"
  227. )
  228. elseif(DEFINED CMAKE_ANDROID_ARM_NEON)
  229. message(FATAL_ERROR "Android: CMAKE_ANDROID_ARM_NEON is set but is valid only for 'armeabi-v7a' architecture.")
  230. endif()
  231. # Report the chosen architecture.
  232. message(STATUS "Android: Targeting API '${CMAKE_SYSTEM_VERSION}' with architecture '${CMAKE_ANDROID_ARCH}', ABI '${CMAKE_ANDROID_ARCH_ABI}', and processor '${CMAKE_SYSTEM_PROCESSOR}'")