Android-Initialize.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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", CMakeSystemSpecificInitialize loads this
  4. # module.
  5. # Include the NDK hook.
  6. # It can be used by NDK to inject necessary fixes for an earlier cmake.
  7. if(CMAKE_ANDROID_NDK)
  8. include(${CMAKE_ANDROID_NDK}/build/cmake/hooks/pre/Android-Initialize.cmake OPTIONAL)
  9. endif()
  10. include(Platform/Linux-Initialize)
  11. unset(LINUX)
  12. set(ANDROID 1)
  13. # Support for NVIDIA Nsight Tegra Visual Studio Edition was previously
  14. # implemented in the CMake VS IDE generators. Avoid interfering with
  15. # that functionality for now.
  16. if(CMAKE_VS_PLATFORM_NAME STREQUAL "Tegra-Android")
  17. return()
  18. endif()
  19. # Commonly used Android toolchain files that pre-date CMake upstream support
  20. # set CMAKE_SYSTEM_VERSION to 1. Avoid interfering with them.
  21. if(CMAKE_SYSTEM_VERSION EQUAL 1)
  22. return()
  23. endif()
  24. set(CMAKE_BUILD_TYPE_INIT "RelWithDebInfo")
  25. if(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED)
  26. # Tell CMake not to search host sysroots for headers/libraries.
  27. # All paths added to CMAKE_SYSTEM_*_PATH below will be rerooted under
  28. # CMAKE_FIND_ROOT_PATH. This is set because:
  29. # 1. Users may structure their libraries in a way similar to NDK. When they do that,
  30. # they can simply append another path to CMAKE_FIND_ROOT_PATH.
  31. # 2. CMAKE_FIND_ROOT_PATH must be non-empty for CMAKE_FIND_ROOT_PATH_MODE_* == ONLY
  32. # to be meaningful. https://github.com/android-ndk/ndk/issues/890
  33. list(APPEND CMAKE_FIND_ROOT_PATH "${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/sysroot")
  34. # Allow users to override these values in case they want more strict behaviors.
  35. # For example, they may want to prevent the NDK's libz from being picked up so
  36. # they can use their own.
  37. # https://github.com/android-ndk/ndk/issues/517
  38. if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PROGRAM)
  39. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  40. endif()
  41. if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
  42. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  43. endif()
  44. if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)
  45. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  46. endif()
  47. if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PACKAGE)
  48. set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
  49. endif()
  50. # Allows CMake to find headers in the architecture-specific include directories.
  51. set(CMAKE_LIBRARY_ARCHITECTURE "${CMAKE_ANDROID_ARCH_TRIPLE}")
  52. # Instructs CMake to search the correct API level for libraries.
  53. # Besides the paths like <root>/<prefix>/lib/<arch>, cmake also searches <root>/<prefix>.
  54. # So we can add the API level specific directory directly.
  55. # https://github.com/android/ndk/issues/929
  56. list(PREPEND CMAKE_SYSTEM_PREFIX_PATH
  57. "/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/${CMAKE_SYSTEM_VERSION}"
  58. )
  59. list(APPEND CMAKE_SYSTEM_PROGRAM_PATH "${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/bin")
  60. endif()
  61. # Skip sysroot selection if the NDK has a unified toolchain.
  62. if(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED)
  63. return()
  64. endif()
  65. # Natively compiling on an Android host doesn't use the NDK cross-compilation
  66. # tools.
  67. if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Android")
  68. return()
  69. endif()
  70. if(NOT CMAKE_SYSROOT)
  71. if(CMAKE_ANDROID_NDK)
  72. set(CMAKE_SYSROOT "${CMAKE_ANDROID_NDK}/platforms/android-${CMAKE_SYSTEM_VERSION}/arch-${CMAKE_ANDROID_ARCH}")
  73. if(NOT CMAKE_ANDROID_NDK_DEPRECATED_HEADERS)
  74. set(CMAKE_SYSROOT_COMPILE "${CMAKE_ANDROID_NDK}/sysroot")
  75. endif()
  76. elseif(CMAKE_ANDROID_STANDALONE_TOOLCHAIN)
  77. set(CMAKE_SYSROOT "${CMAKE_ANDROID_STANDALONE_TOOLCHAIN}/sysroot")
  78. endif()
  79. endif()
  80. if(CMAKE_SYSROOT)
  81. if(NOT IS_DIRECTORY "${CMAKE_SYSROOT}")
  82. message(FATAL_ERROR
  83. "Android: The system root directory needed for the selected Android version and architecture does not exist:\n"
  84. " ${CMAKE_SYSROOT}\n"
  85. )
  86. endif()
  87. else()
  88. message(FATAL_ERROR
  89. "Android: No CMAKE_SYSROOT was selected."
  90. )
  91. endif()
  92. # Include the NDK hook.
  93. # It can be used by NDK to inject necessary fixes for an earlier cmake.
  94. if(CMAKE_ANDROID_NDK)
  95. include(${CMAKE_ANDROID_NDK}/build/cmake/hooks/post/Android-Initialize.cmake OPTIONAL)
  96. endif()