Android.cmake 3.7 KB

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