Android-Determine.cmake 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. # Find the Android NDK.
  23. if(CMAKE_ANDROID_NDK)
  24. if(NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK}")
  25. message(FATAL_ERROR
  26. "Android: The NDK root directory specified by CMAKE_ANDROID_NDK:\n"
  27. " ${CMAKE_ANDROID_NDK}\n"
  28. "does not exist."
  29. )
  30. endif()
  31. else()
  32. if(IS_DIRECTORY "$ENV{ANDROID_NDK_ROOT}")
  33. file(TO_CMAKE_PATH "$ENV{ANDROID_NDK_ROOT}" CMAKE_ANDROID_NDK)
  34. endif()
  35. # TODO: Search harder for the NDK.
  36. endif()
  37. if(NOT CMAKE_ANDROID_NDK)
  38. message(FATAL_ERROR "Android: The NDK root directory was not found.")
  39. endif()
  40. # Select an API.
  41. if(CMAKE_SYSTEM_VERSION)
  42. set(_ANDROID_API_VAR CMAKE_SYSTEM_VERSION)
  43. elseif(CMAKE_ANDROID_API)
  44. set(CMAKE_SYSTEM_VERSION "${CMAKE_ANDROID_API}")
  45. set(_ANDROID_API_VAR CMAKE_ANDROID_API)
  46. endif()
  47. if(CMAKE_SYSTEM_VERSION)
  48. if(CMAKE_ANDROID_API AND NOT "x${CMAKE_ANDROID_API}" STREQUAL "x${CMAKE_SYSTEM_VERSION}")
  49. message(FATAL_ERROR
  50. "Android: The API specified by CMAKE_ANDROID_API='${CMAKE_ANDROID_API}' is not consistent with CMAKE_SYSTEM_VERSION='${CMAKE_SYSTEM_VERSION}'."
  51. )
  52. endif()
  53. if(CMAKE_ANDROID_NDK AND NOT IS_DIRECTORY "${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}")
  54. message(FATAL_ERROR
  55. "Android: The API specified by ${_ANDROID_API_VAR}='${${_ANDROID_API_VAR}}' does not exist in the NDK. "
  56. "The directory:\n"
  57. " ${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}\n"
  58. "does not exist."
  59. )
  60. endif()
  61. elseif(CMAKE_ANDROID_NDK)
  62. file(GLOB _ANDROID_APIS_1 RELATIVE "${CMAKE_ANDROID_NDK}/platforms" "${CMAKE_ANDROID_NDK}/platforms/android-[0-9]")
  63. file(GLOB _ANDROID_APIS_2 RELATIVE "${CMAKE_ANDROID_NDK}/platforms" "${CMAKE_ANDROID_NDK}/platforms/android-[0-9][0-9]")
  64. list(SORT _ANDROID_APIS_1)
  65. list(SORT _ANDROID_APIS_2)
  66. set(_ANDROID_APIS ${_ANDROID_APIS_1} ${_ANDROID_APIS_2})
  67. unset(_ANDROID_APIS_1)
  68. unset(_ANDROID_APIS_2)
  69. if(_ANDROID_APIS STREQUAL "")
  70. message(FATAL_ERROR
  71. "Android: No APIs found in the NDK. No\n"
  72. " ${CMAKE_ANDROID_NDK}/platforms/android-*\n"
  73. "directories exist."
  74. )
  75. endif()
  76. string(REPLACE "android-" "" _ANDROID_APIS "${_ANDROID_APIS}")
  77. list(REVERSE _ANDROID_APIS)
  78. list(GET _ANDROID_APIS 0 CMAKE_SYSTEM_VERSION)
  79. unset(_ANDROID_APIS)
  80. endif()
  81. if(NOT CMAKE_SYSTEM_VERSION MATCHES "^[0-9]+$")
  82. message(FATAL_ERROR "Android: The API specified by CMAKE_SYSTEM_VERSION='${CMAKE_SYSTEM_VERSION}' is not an integer.")
  83. endif()
  84. # https://developer.android.com/ndk/guides/abis.html
  85. set(_ANDROID_ABI_arm64-v8a_PROC "aarch64")
  86. set(_ANDROID_ABI_arm64-v8a_ARCH "arm64")
  87. set(_ANDROID_ABI_armeabi-v7a_PROC "armv7-a")
  88. set(_ANDROID_ABI_armeabi-v7a_ARCH "arm")
  89. set(_ANDROID_ABI_armeabi-v6_PROC "armv6")
  90. set(_ANDROID_ABI_armeabi-v6_ARCH "arm")
  91. set(_ANDROID_ABI_armeabi_PROC "armv5te")
  92. set(_ANDROID_ABI_armeabi_ARCH "arm")
  93. set(_ANDROID_ABI_mips_PROC "mips")
  94. set(_ANDROID_ABI_mips_ARCH "mips")
  95. set(_ANDROID_ABI_mips64_PROC "mips64")
  96. set(_ANDROID_ABI_mips64_ARCH "mips64")
  97. set(_ANDROID_ABI_x86_PROC "i686")
  98. set(_ANDROID_ABI_x86_ARCH "x86")
  99. set(_ANDROID_ABI_x86_64_PROC "x86_64")
  100. set(_ANDROID_ABI_x86_64_ARCH "x86_64")
  101. set(_ANDROID_PROC_aarch64_ARCH_ABI "arm64-v8a")
  102. set(_ANDROID_PROC_armv7-a_ARCH_ABI "armeabi-v7a")
  103. set(_ANDROID_PROC_armv6_ARCH_ABI "armeabi-v6")
  104. set(_ANDROID_PROC_armv5te_ARCH_ABI "armeabi")
  105. set(_ANDROID_PROC_i686_ARCH_ABI "x86")
  106. set(_ANDROID_PROC_mips_ARCH_ABI "mips")
  107. set(_ANDROID_PROC_mips64_ARCH_ABI "mips64")
  108. set(_ANDROID_PROC_x86_64_ARCH_ABI "x86_64")
  109. # Validate inputs.
  110. if(CMAKE_ANDROID_ARCH_ABI AND NOT DEFINED "_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC")
  111. message(FATAL_ERROR "Android: Unknown ABI CMAKE_ANDROID_ARCH_ABI='${CMAKE_ANDROID_ARCH_ABI}'.")
  112. endif()
  113. if(CMAKE_SYSTEM_PROCESSOR AND NOT DEFINED "_ANDROID_PROC_${CMAKE_SYSTEM_PROCESSOR}_ARCH_ABI")
  114. message(FATAL_ERROR "Android: Unknown processor CMAKE_SYSTEM_PROCESSOR='${CMAKE_SYSTEM_PROCESSOR}'.")
  115. endif()
  116. # Select an ABI.
  117. if(NOT CMAKE_ANDROID_ARCH_ABI)
  118. if(CMAKE_SYSTEM_PROCESSOR)
  119. set(CMAKE_ANDROID_ARCH_ABI "${_ANDROID_PROC_${CMAKE_SYSTEM_PROCESSOR}_ARCH_ABI}")
  120. else()
  121. # https://developer.android.com/ndk/guides/application_mk.html
  122. # Default is the oldest ARM ABI.
  123. set(CMAKE_ANDROID_ARCH_ABI "armeabi")
  124. endif()
  125. endif()
  126. set(CMAKE_ANDROID_ARCH "${_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_ARCH}")
  127. # Select a processor.
  128. if(NOT CMAKE_SYSTEM_PROCESSOR)
  129. set(CMAKE_SYSTEM_PROCESSOR "${_ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC}")
  130. endif()
  131. # If the user specified both an ABI and a processor then they might not match.
  132. if(NOT _ANDROID_ABI_${CMAKE_ANDROID_ARCH_ABI}_PROC STREQUAL CMAKE_SYSTEM_PROCESSOR)
  133. 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.")
  134. endif()
  135. # Save the Android-specific information in CMakeSystem.cmake.
  136. set(CMAKE_SYSTEM_CUSTOM_CODE "
  137. set(CMAKE_ANDROID_NDK \"${CMAKE_ANDROID_NDK}\")
  138. set(CMAKE_ANDROID_ARCH \"${CMAKE_ANDROID_ARCH}\")
  139. set(CMAKE_ANDROID_ARCH_ABI \"${CMAKE_ANDROID_ARCH_ABI}\")
  140. ")
  141. # Select an ARM variant.
  142. if(CMAKE_ANDROID_ARCH_ABI MATCHES "^armeabi")
  143. if(CMAKE_ANDROID_ARM_MODE)
  144. set(CMAKE_ANDROID_ARM_MODE 1)
  145. else()
  146. set(CMAKE_ANDROID_ARM_MODE 0)
  147. endif()
  148. string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
  149. "set(CMAKE_ANDROID_ARM_MODE \"${CMAKE_ANDROID_ARM_MODE}\")\n"
  150. )
  151. elseif(DEFINED CMAKE_ANDROID_ARM_MODE)
  152. message(FATAL_ERROR "Android: CMAKE_ANDROID_ARM_MODE is set but is valid only for 'armeabi' architectures.")
  153. endif()
  154. if(CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a")
  155. if(CMAKE_ANDROID_ARM_NEON)
  156. set(CMAKE_ANDROID_ARM_NEON 1)
  157. else()
  158. set(CMAKE_ANDROID_ARM_NEON 0)
  159. endif()
  160. string(APPEND CMAKE_SYSTEM_CUSTOM_CODE
  161. "set(CMAKE_ANDROID_ARM_NEON \"${CMAKE_ANDROID_ARM_NEON}\")\n"
  162. )
  163. elseif(DEFINED CMAKE_ANDROID_ARM_NEON)
  164. message(FATAL_ERROR "Android: CMAKE_ANDROID_ARM_NEON is set but is valid only for 'armeabi-v7a' architecture.")
  165. endif()
  166. # Report the chosen architecture.
  167. message(STATUS "Android: Targeting API '${CMAKE_SYSTEM_VERSION}' with architecture '${CMAKE_ANDROID_ARCH}', ABI '${CMAKE_ANDROID_ARCH_ABI}', and processor '${CMAKE_SYSTEM_PROCESSOR}'")