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