Android-Common.cmake 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #=============================================================================
  2. # Copyright 2015-2016 Kitware, Inc.
  3. #
  4. # Distributed under the OSI-approved BSD License (the "License");
  5. # see accompanying file Copyright.txt for details.
  6. #
  7. # This software is distributed WITHOUT ANY WARRANTY; without even the
  8. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. # See the License for more information.
  10. #=============================================================================
  11. # (To distribute this file outside of CMake, substitute the full
  12. # License text for the above reference.)
  13. # This module is shared by multiple languages; use include blocker.
  14. if(__ANDROID_COMPILER_COMMON)
  15. return()
  16. endif()
  17. set(__ANDROID_COMPILER_COMMON 1)
  18. if(CMAKE_ANDROID_NDK)
  19. # <ndk>/build/core/definitions.mk
  20. set(_ANDROID_STL_TYPES
  21. none
  22. system
  23. c++_static
  24. c++_shared
  25. gabi++_static
  26. gabi++_shared
  27. gnustl_static
  28. gnustl_shared
  29. stlport_static
  30. stlport_shared
  31. )
  32. if(CMAKE_ANDROID_STL_TYPE)
  33. list(FIND _ANDROID_STL_TYPES "${CMAKE_ANDROID_STL_TYPE}" _ANDROID_STL_TYPE_FOUND)
  34. if(_ANDROID_STL_TYPE_FOUND EQUAL -1)
  35. string(REPLACE ";" "\n " _msg ";${_ANDROID_STL_TYPES}")
  36. message(FATAL_ERROR
  37. "The CMAKE_ANDROID_STL_TYPE '${CMAKE_ANDROID_STL_TYPE}' is not one of the allowed values:${_msg}\n"
  38. )
  39. endif()
  40. unset(_ANDROID_STL_TYPE_FOUND)
  41. else()
  42. set(CMAKE_ANDROID_STL_TYPE "gnustl_static")
  43. endif()
  44. unset(_ANDROID_STL_TYPES)
  45. # Forward Android-specific platform variables to try_compile projects.
  46. list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
  47. CMAKE_ANDROID_STL_TYPE
  48. )
  49. endif()
  50. if(CMAKE_ANDROID_STL_TYPE)
  51. if(CMAKE_ANDROID_NDK)
  52. macro(__android_stl_inc lang dir req)
  53. if(EXISTS "${dir}")
  54. list(APPEND CMAKE_${lang}_STANDARD_INCLUDE_DIRECTORIES "${dir}")
  55. elseif(${req})
  56. message(FATAL_ERROR
  57. "Android: STL '${CMAKE_ANDROID_STL_TYPE}' include directory not found:\n"
  58. " ${dir}"
  59. )
  60. endif()
  61. endmacro()
  62. macro(__android_stl_lib lang lib req)
  63. if(CMAKE_ANDROID_ARCH_ABI MATCHES "^armeabi" AND NOT CMAKE_ANDROID_ARM_MODE)
  64. get_filename_component(_ANDROID_STL_LIBDIR "${lib}" DIRECTORY)
  65. get_filename_component(_ANDROID_STL_LIBNAME "${lib}" NAME)
  66. set(_ANDROID_STL_LIBTHUMB "${_ANDROID_STL_LIBDIR}/thumb/${_ANDROID_STL_LIBNAME}")
  67. unset(_ANDROID_STL_LIBDIR)
  68. unset(_ANDROID_STL_LIBNAME)
  69. else()
  70. set(_ANDROID_STL_LIBTHUMB "")
  71. endif()
  72. if(_ANDROID_STL_LIBTHUMB AND EXISTS "${_ANDROID_STL_LIBTHUMB}")
  73. string(APPEND CMAKE_${lang}_STANDARD_LIBRARIES " \"${_ANDROID_STL_LIBTHUMB}\"")
  74. elseif(EXISTS "${lib}")
  75. string(APPEND CMAKE_${lang}_STANDARD_LIBRARIES " \"${lib}\"")
  76. elseif(${req})
  77. message(FATAL_ERROR
  78. "Android: STL '${CMAKE_ANDROID_STL_TYPE}' library file not found:\n"
  79. " ${lib}"
  80. )
  81. endif()
  82. unset(_ANDROID_STL_LIBTHUMB)
  83. endmacro()
  84. include(Platform/Android/ndk-stl-${CMAKE_ANDROID_STL_TYPE})
  85. else()
  86. macro(__android_stl lang)
  87. endmacro()
  88. endif()
  89. else()
  90. macro(__android_stl lang)
  91. endmacro()
  92. endif()
  93. # The NDK toolchain configuration files at:
  94. #
  95. # <ndk>/[build/core/]toolchains/*/setup.mk
  96. #
  97. # contain logic to set TARGET_CFLAGS and TARGET_LDFLAGS (and debug/release
  98. # variants) to tell their build system what flags to pass for each ABI.
  99. # We need to produce the same flags here to produce compatible binaries.
  100. # We initialize these variables here and set them in the compiler-specific
  101. # modules that include this one. Then we use them in the macro below when
  102. # it is called.
  103. set(_ANDROID_ABI_INIT_CFLAGS "")
  104. set(_ANDROID_ABI_INIT_CFLAGS_DEBUG "")
  105. set(_ANDROID_ABI_INIT_CFLAGS_RELEASE "")
  106. set(_ANDROID_ABI_INIT_LDFLAGS "")
  107. macro(__android_compiler_common lang)
  108. if(_ANDROID_ABI_INIT_CFLAGS)
  109. string(APPEND CMAKE_${lang}_FLAGS_INIT " ${_ANDROID_ABI_INIT_CFLAGS}")
  110. endif()
  111. if(_ANDROID_ABI_INIT_CFLAGS_DEBUG)
  112. string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT " ${_ANDROID_ABI_INIT_CFLAGS_DEBUG}")
  113. endif()
  114. if(_ANDROID_ABI_INIT_CFLAGS_RELEASE)
  115. string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT " ${_ANDROID_ABI_INIT_CFLAGS_RELEASE}")
  116. string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT " ${_ANDROID_ABI_INIT_CFLAGS_RELEASE}")
  117. string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT " ${_ANDROID_ABI_INIT_CFLAGS_RELEASE}")
  118. endif()
  119. if(_ANDROID_ABI_INIT_LDFLAGS)
  120. foreach(t EXE SHARED MODULE)
  121. string(APPEND CMAKE_${t}_LINKER_FLAGS_INIT " ${_ANDROID_ABI_INIT_LDFLAGS}")
  122. endforeach()
  123. endif()
  124. if(DEFINED _ANDROID_STL_EXCEPTIONS)
  125. if(_ANDROID_STL_EXCEPTIONS)
  126. string(APPEND CMAKE_${lang}_FLAGS_INIT " -fexceptions")
  127. else()
  128. string(APPEND CMAKE_${lang}_FLAGS_INIT " -fno-exceptions")
  129. endif()
  130. endif()
  131. if("x${lang}" STREQUAL "xCXX" AND DEFINED _ANDROID_STL_RTTI)
  132. if(_ANDROID_STL_RTTI)
  133. string(APPEND CMAKE_${lang}_FLAGS_INIT " -frtti")
  134. else()
  135. string(APPEND CMAKE_${lang}_FLAGS_INIT " -fno-rtti")
  136. endif()
  137. endif()
  138. if("x${lang}" STREQUAL "xCXX")
  139. __android_stl(CXX)
  140. endif()
  141. endmacro()