1
0

Android.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Include the NDK hook.
  2. # It can be used by NDK to inject necessary fixes for an earlier cmake.
  3. if(CMAKE_ANDROID_NDK)
  4. include(${CMAKE_ANDROID_NDK}/build/cmake/hooks/pre/Android.cmake OPTIONAL)
  5. endif()
  6. include(Platform/Linux)
  7. set(ANDROID 1)
  8. # Natively compiling on an Android host doesn't need these flags to be reset.
  9. if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Android")
  10. return()
  11. endif()
  12. # Conventionally Android does not use versioned soname
  13. # But in modern versions it is acceptable
  14. if(NOT DEFINED CMAKE_PLATFORM_NO_VERSIONED_SONAME)
  15. set(CMAKE_PLATFORM_NO_VERSIONED_SONAME 1)
  16. endif()
  17. # Android reportedly ignores RPATH, and we cannot predict the install
  18. # location anyway.
  19. set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "")
  20. # Nsight Tegra Visual Studio Edition takes care of
  21. # prefixing library names with '-l'.
  22. if(CMAKE_VS_PLATFORM_NAME STREQUAL "Tegra-Android")
  23. set(CMAKE_LINK_LIBRARY_FLAG "")
  24. endif()
  25. # Commonly used Android toolchain files that pre-date CMake upstream support
  26. # set CMAKE_SYSTEM_VERSION to 1. Avoid interfering with them.
  27. if(CMAKE_SYSTEM_VERSION EQUAL 1)
  28. return()
  29. endif()
  30. if(CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED)
  31. # Tell CMake not to search host sysroots for headers/libraries.
  32. # CMAKE_FIND_ROOT_PATH must be non-empty for CMAKE_FIND_ROOT_PATH_MODE_* == ONLY
  33. # to be meaningful. The actual path used here is fairly meaningless since CMake
  34. # doesn't handle the NDK sysroot layout (per-arch and per-verion subdirectories for
  35. # libraries), so find_library is handled separately by CMAKE_SYSTEM_LIBRARY_PATH.
  36. # https://github.com/android-ndk/ndk/issues/890
  37. if(NOT CMAKE_FIND_ROOT_PATH)
  38. list(APPEND CMAKE_FIND_ROOT_PATH "${CMAKE_ANDROID_NDK}")
  39. endif()
  40. # Allow users to override these values in case they want more strict behaviors.
  41. # For example, they may want to prevent the NDK's libz from being picked up so
  42. # they can use their own.
  43. # https://github.com/android-ndk/ndk/issues/517
  44. if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PROGRAM)
  45. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  46. endif()
  47. if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
  48. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
  49. endif()
  50. if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_INCLUDE)
  51. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  52. endif()
  53. if(NOT DEFINED CMAKE_FIND_ROOT_PATH_MODE_PACKAGE)
  54. set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
  55. endif()
  56. # find_library's default search paths below a prefix do not match the Android
  57. # sysroot layout, so we need to give the direct path to the libraries
  58. # via CMAKE_SYSTEM_*_PATH.
  59. #
  60. # Ideally we'd set CMAKE_SYSTEM_PREFIX_PATH. But that causes the
  61. # non-api-level-specific path to be searched first for find_library, which will
  62. # cause libdl.a to be found before libdl.so.
  63. # https://github.com/android/ndk/issues/929
  64. # Clears the paths set by UnixPaths.cmake.
  65. set(CMAKE_SYSTEM_PREFIX_PATH)
  66. set(CMAKE_SYSTEM_INCLUDE_PATH)
  67. set(CMAKE_SYSTEM_LIBRARY_PATH)
  68. # Don't search paths in PATH environment variable.
  69. if(NOT DEFINED CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH)
  70. set(CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH OFF)
  71. endif()
  72. # Allows CMake to find headers in the architecture-specific include directories.
  73. set(CMAKE_LIBRARY_ARCHITECTURE "${CMAKE_ANDROID_ARCH_TRIPLE}")
  74. set(_ANDROID_SYSROOT_PREFIX "${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/sysroot/usr")
  75. list(APPEND CMAKE_SYSTEM_INCLUDE_PATH
  76. "${_ANDROID_SYSROOT_PREFIX}/include/${CMAKE_LIBRARY_ARCHITECTURE}")
  77. list(APPEND CMAKE_SYSTEM_INCLUDE_PATH "${_ANDROID_SYSROOT_PREFIX}/include")
  78. # Instructs CMake to search the correct API level for libraries.
  79. list(APPEND CMAKE_SYSTEM_LIBRARY_PATH
  80. "${_ANDROID_SYSROOT_PREFIX}/lib/${CMAKE_LIBRARY_ARCHITECTURE}/${CMAKE_SYSTEM_VERSION}")
  81. list(APPEND CMAKE_SYSTEM_LIBRARY_PATH
  82. "${_ANDROID_SYSROOT_PREFIX}/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
  83. list(APPEND CMAKE_SYSTEM_PROGRAM_PATH "${CMAKE_ANDROID_NDK_TOOLCHAIN_UNIFIED}/bin")
  84. endif()
  85. # Include the NDK hook.
  86. # It can be used by NDK to inject necessary fixes for an earlier cmake.
  87. if(CMAKE_ANDROID_NDK)
  88. include(${CMAKE_ANDROID_NDK}/build/cmake/hooks/post/Android.cmake OPTIONAL)
  89. endif()