Android-Common.cmake 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # This module is shared by multiple languages; use include blocker.
  4. if(__ANDROID_COMPILER_COMMON)
  5. return()
  6. endif()
  7. set(__ANDROID_COMPILER_COMMON 1)
  8. if(CMAKE_ANDROID_NDK)
  9. # <ndk>/build/core/definitions.mk
  10. set(_ANDROID_STL_TYPES
  11. none
  12. system
  13. c++_static
  14. c++_shared
  15. gabi++_static
  16. gabi++_shared
  17. gnustl_static
  18. gnustl_shared
  19. stlport_static
  20. stlport_shared
  21. )
  22. if(CMAKE_ANDROID_STL_TYPE)
  23. list(FIND _ANDROID_STL_TYPES "${CMAKE_ANDROID_STL_TYPE}" _ANDROID_STL_TYPE_FOUND)
  24. if(_ANDROID_STL_TYPE_FOUND EQUAL -1)
  25. string(REPLACE ";" "\n " _msg ";${_ANDROID_STL_TYPES}")
  26. message(FATAL_ERROR
  27. "The CMAKE_ANDROID_STL_TYPE '${CMAKE_ANDROID_STL_TYPE}' is not one of the allowed values:${_msg}\n"
  28. )
  29. endif()
  30. unset(_ANDROID_STL_TYPE_FOUND)
  31. else()
  32. set(CMAKE_ANDROID_STL_TYPE "gnustl_static")
  33. endif()
  34. unset(_ANDROID_STL_TYPES)
  35. # Forward Android-specific platform variables to try_compile projects.
  36. list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
  37. CMAKE_ANDROID_STL_TYPE
  38. )
  39. endif()
  40. if(CMAKE_ANDROID_STL_TYPE)
  41. if(CMAKE_ANDROID_NDK)
  42. macro(__android_stl_inc lang dir req)
  43. if(EXISTS "${dir}")
  44. list(APPEND CMAKE_${lang}_STANDARD_INCLUDE_DIRECTORIES "${dir}")
  45. elseif(${req})
  46. message(FATAL_ERROR
  47. "Android: STL '${CMAKE_ANDROID_STL_TYPE}' include directory not found:\n"
  48. " ${dir}"
  49. )
  50. endif()
  51. endmacro()
  52. macro(__android_stl_lib lang lib req)
  53. if(CMAKE_ANDROID_ARCH_ABI MATCHES "^armeabi" AND NOT CMAKE_ANDROID_ARM_MODE)
  54. get_filename_component(_ANDROID_STL_LIBDIR "${lib}" DIRECTORY)
  55. get_filename_component(_ANDROID_STL_LIBNAME "${lib}" NAME)
  56. set(_ANDROID_STL_LIBTHUMB "${_ANDROID_STL_LIBDIR}/thumb/${_ANDROID_STL_LIBNAME}")
  57. unset(_ANDROID_STL_LIBDIR)
  58. unset(_ANDROID_STL_LIBNAME)
  59. else()
  60. set(_ANDROID_STL_LIBTHUMB "")
  61. endif()
  62. if(_ANDROID_STL_LIBTHUMB AND EXISTS "${_ANDROID_STL_LIBTHUMB}")
  63. string(APPEND CMAKE_${lang}_STANDARD_LIBRARIES " \"${_ANDROID_STL_LIBTHUMB}\"")
  64. elseif(EXISTS "${lib}")
  65. string(APPEND CMAKE_${lang}_STANDARD_LIBRARIES " \"${lib}\"")
  66. elseif(${req})
  67. message(FATAL_ERROR
  68. "Android: STL '${CMAKE_ANDROID_STL_TYPE}' library file not found:\n"
  69. " ${lib}"
  70. )
  71. endif()
  72. unset(_ANDROID_STL_LIBTHUMB)
  73. endmacro()
  74. include(Platform/Android/ndk-stl-${CMAKE_ANDROID_STL_TYPE})
  75. else()
  76. macro(__android_stl lang)
  77. endmacro()
  78. endif()
  79. else()
  80. macro(__android_stl lang)
  81. endmacro()
  82. endif()
  83. # The NDK toolchain configuration files at:
  84. #
  85. # <ndk>/[build/core/]toolchains/*/setup.mk
  86. #
  87. # contain logic to set TARGET_CFLAGS and TARGET_LDFLAGS (and debug/release
  88. # variants) to tell their build system what flags to pass for each ABI.
  89. # We need to produce the same flags here to produce compatible binaries.
  90. # We initialize these variables here and set them in the compiler-specific
  91. # modules that include this one. Then we use them in the macro below when
  92. # it is called.
  93. set(_ANDROID_ABI_INIT_CFLAGS "")
  94. set(_ANDROID_ABI_INIT_CFLAGS_DEBUG "")
  95. set(_ANDROID_ABI_INIT_CFLAGS_RELEASE "")
  96. set(_ANDROID_ABI_INIT_LDFLAGS "")
  97. set(_ANDROID_ABI_INIT_EXE_LDFLAGS "")
  98. macro(__android_compiler_common lang)
  99. if(_ANDROID_ABI_INIT_CFLAGS)
  100. string(APPEND CMAKE_${lang}_FLAGS_INIT " ${_ANDROID_ABI_INIT_CFLAGS}")
  101. endif()
  102. if(_ANDROID_ABI_INIT_CFLAGS_DEBUG)
  103. string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT " ${_ANDROID_ABI_INIT_CFLAGS_DEBUG}")
  104. endif()
  105. if(_ANDROID_ABI_INIT_CFLAGS_RELEASE)
  106. string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT " ${_ANDROID_ABI_INIT_CFLAGS_RELEASE}")
  107. string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT " ${_ANDROID_ABI_INIT_CFLAGS_RELEASE}")
  108. string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT " ${_ANDROID_ABI_INIT_CFLAGS_RELEASE}")
  109. endif()
  110. if(_ANDROID_ABI_INIT_LDFLAGS)
  111. foreach(t EXE SHARED MODULE)
  112. string(APPEND CMAKE_${t}_LINKER_FLAGS_INIT " ${_ANDROID_ABI_INIT_LDFLAGS}")
  113. endforeach()
  114. endif()
  115. if(_ANDROID_ABI_INIT_EXE_LDFLAGS)
  116. string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT " ${_ANDROID_ABI_INIT_EXE_LDFLAGS}")
  117. endif()
  118. if(DEFINED _ANDROID_STL_EXCEPTIONS)
  119. if(_ANDROID_STL_EXCEPTIONS)
  120. string(APPEND CMAKE_${lang}_FLAGS_INIT " -fexceptions")
  121. else()
  122. string(APPEND CMAKE_${lang}_FLAGS_INIT " -fno-exceptions")
  123. endif()
  124. endif()
  125. if("x${lang}" STREQUAL "xCXX" AND DEFINED _ANDROID_STL_RTTI)
  126. if(_ANDROID_STL_RTTI)
  127. string(APPEND CMAKE_${lang}_FLAGS_INIT " -frtti")
  128. else()
  129. string(APPEND CMAKE_${lang}_FLAGS_INIT " -fno-rtti")
  130. endif()
  131. endif()
  132. if("x${lang}" STREQUAL "xCXX")
  133. __android_stl(CXX)
  134. endif()
  135. # <ndk>/build/core/definitions.mk appends the sysroot's include directory
  136. # explicitly at the end of the command-line include path so that it
  137. # precedes the toolchain's builtin include directories. This is
  138. # necessary so that Android API-version-specific headers are preferred
  139. # over those in the toolchain's `include-fixed` directory (which cannot
  140. # possibly match all versions).
  141. #
  142. # Do not do this for a standalone toolchain because it is already
  143. # tied to a specific API version.
  144. if(CMAKE_ANDROID_NDK)
  145. if(CMAKE_SYSROOT_COMPILE)
  146. set(_cmake_sysroot_compile "${CMAKE_SYSROOT_COMPILE}")
  147. else()
  148. set(_cmake_sysroot_compile "${CMAKE_SYSROOT}")
  149. endif()
  150. if(NOT CMAKE_ANDROID_NDK_DEPRECATED_HEADERS)
  151. list(APPEND CMAKE_${lang}_STANDARD_INCLUDE_DIRECTORIES
  152. "${_cmake_sysroot_compile}/usr/include"
  153. "${_cmake_sysroot_compile}/usr/include/${CMAKE_ANDROID_ARCH_HEADER_TRIPLE}"
  154. )
  155. else()
  156. list(APPEND CMAKE_${lang}_STANDARD_INCLUDE_DIRECTORIES "${_cmake_sysroot_compile}/usr/include")
  157. endif()
  158. unset(_cmake_sysroot_compile)
  159. endif()
  160. endmacro()