Android-Determine.cmake 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. # Save the Android-specific information in CMakeSystem.cmake.
  85. set(CMAKE_SYSTEM_CUSTOM_CODE "
  86. set(CMAKE_ANDROID_NDK \"${CMAKE_ANDROID_NDK}\")
  87. ")
  88. # Report the chosen architecture.
  89. message(STATUS "Android: Targeting API '${CMAKE_SYSTEM_VERSION}'")