CMakeDetermineCompilerABI.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # Function to compile a source file to identify the compiler ABI.
  4. # This is used internally by CMake and should not be included by user
  5. # code.
  6. include(${CMAKE_ROOT}/Modules/Internal/CMakeDetermineLinkerId.cmake)
  7. include(${CMAKE_ROOT}/Modules/CMakeParseImplicitIncludeInfo.cmake)
  8. include(${CMAKE_ROOT}/Modules/CMakeParseImplicitLinkInfo.cmake)
  9. include(${CMAKE_ROOT}/Modules/CMakeParseLibraryArchitecture.cmake)
  10. include(CMakeTestCompilerCommon)
  11. function(CMAKE_DETERMINE_COMPILER_ABI lang src)
  12. if(NOT DEFINED CMAKE_${lang}_ABI_COMPILED)
  13. message(CHECK_START "Detecting ${lang} compiler ABI info")
  14. # Compile the ABI identification source.
  15. set(BIN "${CMAKE_PLATFORM_INFO_DIR}/CMakeDetermineCompilerABI_${lang}.bin")
  16. set(CMAKE_FLAGS )
  17. set(COMPILE_DEFINITIONS )
  18. set(LINK_OPTIONS )
  19. if(DEFINED CMAKE_${lang}_VERBOSE_FLAG)
  20. set(LINK_OPTIONS "${CMAKE_${lang}_VERBOSE_FLAG}")
  21. set(COMPILE_DEFINITIONS "${CMAKE_${lang}_VERBOSE_FLAG}")
  22. endif()
  23. if(DEFINED CMAKE_${lang}_VERBOSE_COMPILE_FLAG)
  24. set(COMPILE_DEFINITIONS "${CMAKE_${lang}_VERBOSE_COMPILE_FLAG}")
  25. endif()
  26. if(DEFINED CMAKE_${lang}_VERBOSE_LINK_FLAG)
  27. list(APPEND LINK_OPTIONS "${CMAKE_${lang}_VERBOSE_LINK_FLAG}")
  28. endif()
  29. if(lang MATCHES "^(CUDA|HIP)$")
  30. if(CMAKE_CUDA_ARCHITECTURES STREQUAL "native")
  31. # We are about to detect the native architectures, so we do
  32. # not yet know them. Use all architectures during detection.
  33. set(CMAKE_${lang}_ARCHITECTURES "all")
  34. endif()
  35. set(CMAKE_${lang}_RUNTIME_LIBRARY "Static")
  36. endif()
  37. if(NOT "x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xMSVC")
  38. # Avoid adding our own platform standard libraries for compilers
  39. # from which we might detect implicit link libraries.
  40. list(APPEND CMAKE_FLAGS "-DCMAKE_${lang}_STANDARD_LIBRARIES=")
  41. endif()
  42. list(JOIN LINK_OPTIONS " " LINK_OPTIONS)
  43. list(APPEND CMAKE_FLAGS "-DEXE_LINKER_FLAGS=${LINK_OPTIONS}")
  44. __TestCompiler_setTryCompileTargetType()
  45. # Avoid failing ABI detection on warnings.
  46. if(CMAKE_TRY_COMPILE_CONFIGURATION)
  47. string(TOUPPER "${CMAKE_TRY_COMPILE_CONFIGURATION}" _tc_config)
  48. else()
  49. set(_tc_config "DEBUG")
  50. endif()
  51. foreach(v CMAKE_${lang}_FLAGS CMAKE_${lang}_FLAGS_${_tc_config})
  52. string(REGEX REPLACE "(^| )-Werror([= ][^-][^ ]*)?( |$)" " " ${v} "${${v}}")
  53. endforeach()
  54. # Save the current LC_ALL, LC_MESSAGES, and LANG environment variables
  55. # and set them to "C" that way GCC's "search starts here" text is in
  56. # English and we can grok it.
  57. set(_orig_lc_all $ENV{LC_ALL})
  58. set(_orig_lc_messages $ENV{LC_MESSAGES})
  59. set(_orig_lang $ENV{LANG})
  60. set(ENV{LC_ALL} C)
  61. set(ENV{LC_MESSAGES} C)
  62. set(ENV{LANG} C)
  63. try_compile(CMAKE_${lang}_ABI_COMPILED
  64. SOURCES ${src}
  65. CMAKE_FLAGS ${CMAKE_FLAGS}
  66. # Ignore unused flags when we are just determining the ABI.
  67. "--no-warn-unused-cli"
  68. COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS}
  69. OUTPUT_VARIABLE OUTPUT
  70. COPY_FILE "${BIN}"
  71. COPY_FILE_ERROR _copy_error
  72. __CMAKE_INTERNAL ABI
  73. )
  74. # Restore original LC_ALL, LC_MESSAGES, and LANG
  75. set(ENV{LC_ALL} ${_orig_lc_all})
  76. set(ENV{LC_MESSAGES} ${_orig_lc_messages})
  77. set(ENV{LANG} ${_orig_lang})
  78. # Move result from cache to normal variable.
  79. set(CMAKE_${lang}_ABI_COMPILED ${CMAKE_${lang}_ABI_COMPILED})
  80. unset(CMAKE_${lang}_ABI_COMPILED CACHE)
  81. if(CMAKE_${lang}_ABI_COMPILED AND _copy_error)
  82. set(CMAKE_${lang}_ABI_COMPILED 0)
  83. endif()
  84. set(CMAKE_${lang}_ABI_COMPILED ${CMAKE_${lang}_ABI_COMPILED} PARENT_SCOPE)
  85. # Load the resulting information strings.
  86. if(CMAKE_${lang}_ABI_COMPILED)
  87. message(CHECK_PASS "done")
  88. cmake_policy(PUSH)
  89. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  90. file(STRINGS "${BIN}" ABI_STRINGS LIMIT_COUNT 32 REGEX "INFO:[A-Za-z0-9_]+\\[[^]]*\\]")
  91. cmake_policy(POP)
  92. set(ABI_SIZEOF_DPTR "NOTFOUND")
  93. set(ABI_BYTE_ORDER "NOTFOUND")
  94. set(ABI_NAME "NOTFOUND")
  95. foreach(info ${ABI_STRINGS})
  96. if("${info}" MATCHES "INFO:sizeof_dptr\\[0*([^]]*)\\]" AND NOT ABI_SIZEOF_DPTR)
  97. set(ABI_SIZEOF_DPTR "${CMAKE_MATCH_1}")
  98. endif()
  99. if("${info}" MATCHES "INFO:byte_order\\[(BIG_ENDIAN|LITTLE_ENDIAN)\\]")
  100. set(byte_order "${CMAKE_MATCH_1}")
  101. if(ABI_BYTE_ORDER STREQUAL "NOTFOUND")
  102. # Tentatively use the value because this is the first occurrence.
  103. set(ABI_BYTE_ORDER "${byte_order}")
  104. elseif(NOT ABI_BYTE_ORDER STREQUAL "${byte_order}")
  105. # Drop value because multiple occurrences do not match.
  106. set(ABI_BYTE_ORDER "")
  107. endif()
  108. endif()
  109. if("${info}" MATCHES "INFO:abi\\[([^]]*)\\]" AND NOT ABI_NAME)
  110. set(ABI_NAME "${CMAKE_MATCH_1}")
  111. endif()
  112. endforeach()
  113. if(ABI_SIZEOF_DPTR)
  114. set(CMAKE_${lang}_SIZEOF_DATA_PTR "${ABI_SIZEOF_DPTR}" PARENT_SCOPE)
  115. elseif(CMAKE_${lang}_SIZEOF_DATA_PTR_DEFAULT)
  116. set(CMAKE_${lang}_SIZEOF_DATA_PTR "${CMAKE_${lang}_SIZEOF_DATA_PTR_DEFAULT}" PARENT_SCOPE)
  117. endif()
  118. if(ABI_BYTE_ORDER)
  119. set(CMAKE_${lang}_BYTE_ORDER "${ABI_BYTE_ORDER}" PARENT_SCOPE)
  120. endif()
  121. if(ABI_NAME)
  122. set(CMAKE_${lang}_COMPILER_ABI "${ABI_NAME}" PARENT_SCOPE)
  123. endif()
  124. # Parse implicit include directory for this language, if available.
  125. if(CMAKE_${lang}_VERBOSE_FLAG)
  126. set (implicit_incdirs "")
  127. cmake_parse_implicit_include_info("${OUTPUT}" "${lang}"
  128. implicit_incdirs log rv)
  129. message(CONFIGURE_LOG
  130. "Parsed ${lang} implicit include dir info: rv=${rv}\n${log}\n\n")
  131. if("${rv}" STREQUAL "done")
  132. # Entries that we have been told to explicitly pass as standard include
  133. # directories will not be implicitly added by the compiler.
  134. if(CMAKE_${lang}_STANDARD_INCLUDE_DIRECTORIES)
  135. list(REMOVE_ITEM implicit_incdirs ${CMAKE_${lang}_STANDARD_INCLUDE_DIRECTORIES})
  136. endif()
  137. # We parsed implicit include directories, so override the default initializer.
  138. set(_CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES_INIT "${implicit_incdirs}")
  139. endif()
  140. endif()
  141. set(CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES "${_CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES_INIT}" PARENT_SCOPE)
  142. if(_CMAKE_${lang}_IMPLICIT_LINK_INFORMATION_DETERMINED_EARLY)
  143. # Use implicit linker information detected during compiler id step.
  144. set(implicit_dirs "${CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES}")
  145. set(implicit_objs "")
  146. set(implicit_libs "${CMAKE_${lang}_IMPLICIT_LINK_LIBRARIES}")
  147. set(implicit_fwks "${CMAKE_${lang}_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES}")
  148. else()
  149. # Parse implicit linker information for this language, if available.
  150. set(implicit_dirs "")
  151. set(implicit_objs "")
  152. set(implicit_libs "")
  153. set(implicit_fwks "")
  154. set(compute_artifacts COMPUTE_LINKER linker_tool)
  155. if(CMAKE_${lang}_VERBOSE_FLAG)
  156. list(APPEND compute_artifacts COMPUTE_IMPLICIT_LIBS implicit_libs
  157. COMPUTE_IMPLICIT_DIRS implicit_dirs
  158. COMPUTE_IMPLICIT_FWKS implicit_fwks
  159. COMPUTE_IMPLICIT_OBJECTS implicit_objs)
  160. endif()
  161. cmake_parse_implicit_link_info2("${OUTPUT}" log "${CMAKE_${lang}_IMPLICIT_OBJECT_REGEX}"
  162. ${compute_artifacts} LANGUAGE ${lang})
  163. message(CONFIGURE_LOG
  164. "Parsed ${lang} implicit link information:\n${log}\n\n")
  165. # for VS IDE Intel Fortran we have to figure out the
  166. # implicit link path for the fortran run time using
  167. # a try-compile
  168. if("${lang}" MATCHES "Fortran"
  169. AND "${CMAKE_GENERATOR}" MATCHES "Visual Studio")
  170. message(CHECK_START "Determine Intel Fortran Compiler Implicit Link Path")
  171. # Build a sample project which reports symbols.
  172. try_compile(IFORT_LIB_PATH_COMPILED
  173. PROJECT IntelFortranImplicit
  174. SOURCE_DIR ${CMAKE_ROOT}/Modules/IntelVSImplicitPath
  175. BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath
  176. CMAKE_FLAGS
  177. "-DCMAKE_Fortran_FLAGS:STRING=${CMAKE_Fortran_FLAGS}"
  178. OUTPUT_VARIABLE _output)
  179. file(WRITE
  180. "${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath/output.txt"
  181. "${_output}")
  182. include(${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath/output.cmake OPTIONAL)
  183. message(CHECK_PASS "done")
  184. endif()
  185. endif()
  186. # Implicit link libraries cannot be used explicitly for multiple
  187. # OS X architectures, so we skip it.
  188. if(DEFINED CMAKE_OSX_ARCHITECTURES)
  189. if("${CMAKE_OSX_ARCHITECTURES}" MATCHES ";")
  190. set(implicit_libs "")
  191. endif()
  192. endif()
  193. if(DEFINED ENV{CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES_EXCLUDE})
  194. list(REMOVE_ITEM implicit_dirs $ENV{CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES_EXCLUDE})
  195. endif()
  196. set(CMAKE_${lang}_COMPILER_LINKER "${linker_tool}" PARENT_SCOPE)
  197. cmake_determine_linker_id(${lang} "${linker_tool}")
  198. set(CMAKE_${lang}_COMPILER_LINKER_ID "${CMAKE_${lang}_COMPILER_LINKER_ID}" PARENT_SCOPE)
  199. set(CMAKE_${lang}_COMPILER_LINKER_VERSION ${CMAKE_${lang}_COMPILER_LINKER_VERSION} PARENT_SCOPE)
  200. set(CMAKE_${lang}_COMPILER_LINKER_FRONTEND_VARIANT ${CMAKE_${lang}_COMPILER_LINKER_FRONTEND_VARIANT} PARENT_SCOPE)
  201. set(CMAKE_${lang}_IMPLICIT_LINK_LIBRARIES "${implicit_libs}" PARENT_SCOPE)
  202. set(CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES "${implicit_dirs}" PARENT_SCOPE)
  203. set(CMAKE_${lang}_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "${implicit_fwks}" PARENT_SCOPE)
  204. cmake_parse_library_architecture(${lang} "${implicit_dirs}" "${implicit_objs}" architecture_flag)
  205. if(architecture_flag)
  206. set(CMAKE_${lang}_LIBRARY_ARCHITECTURE "${architecture_flag}" PARENT_SCOPE)
  207. endif()
  208. else()
  209. message(CHECK_FAIL "failed")
  210. endif()
  211. endif()
  212. endfunction()