CMakeDetermineCompilerABI.cmake 8.6 KB

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