CMakeDetermineCompilerABI.cmake 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. SOURCES ${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(STRINGS "${BIN}" ABI_STRINGS LIMIT_COUNT 32 REGEX "INFO:[A-Za-z0-9_]+\\[[^]]*\\]")
  75. set(ABI_SIZEOF_DPTR "NOTFOUND")
  76. set(ABI_BYTE_ORDER "NOTFOUND")
  77. set(ABI_NAME "NOTFOUND")
  78. foreach(info ${ABI_STRINGS})
  79. if("${info}" MATCHES "INFO:sizeof_dptr\\[0*([^]]*)\\]" AND NOT ABI_SIZEOF_DPTR)
  80. set(ABI_SIZEOF_DPTR "${CMAKE_MATCH_1}")
  81. endif()
  82. if("${info}" MATCHES "INFO:byte_order\\[(BIG_ENDIAN|LITTLE_ENDIAN)\\]")
  83. set(byte_order "${CMAKE_MATCH_1}")
  84. if(ABI_BYTE_ORDER STREQUAL "NOTFOUND")
  85. # Tentatively use the value because this is the first occurrence.
  86. set(ABI_BYTE_ORDER "${byte_order}")
  87. elseif(NOT ABI_BYTE_ORDER STREQUAL "${byte_order}")
  88. # Drop value because multiple occurrences do not match.
  89. set(ABI_BYTE_ORDER "")
  90. endif()
  91. endif()
  92. if("${info}" MATCHES "INFO:abi\\[([^]]*)\\]" AND NOT ABI_NAME)
  93. set(ABI_NAME "${CMAKE_MATCH_1}")
  94. endif()
  95. endforeach()
  96. if(ABI_SIZEOF_DPTR)
  97. set(CMAKE_${lang}_SIZEOF_DATA_PTR "${ABI_SIZEOF_DPTR}" PARENT_SCOPE)
  98. elseif(CMAKE_${lang}_SIZEOF_DATA_PTR_DEFAULT)
  99. set(CMAKE_${lang}_SIZEOF_DATA_PTR "${CMAKE_${lang}_SIZEOF_DATA_PTR_DEFAULT}" PARENT_SCOPE)
  100. endif()
  101. if(ABI_BYTE_ORDER)
  102. set(CMAKE_${lang}_BYTE_ORDER "${ABI_BYTE_ORDER}" PARENT_SCOPE)
  103. endif()
  104. if(ABI_NAME)
  105. set(CMAKE_${lang}_COMPILER_ABI "${ABI_NAME}" PARENT_SCOPE)
  106. endif()
  107. # Parse implicit include directory for this language, if available.
  108. if(CMAKE_${lang}_VERBOSE_FLAG)
  109. set (implicit_incdirs "")
  110. cmake_parse_implicit_include_info("${OUTPUT}" "${lang}"
  111. implicit_incdirs log rv)
  112. message(CONFIGURE_LOG
  113. "Parsed ${lang} implicit include dir info: rv=${rv}\n${log}\n\n")
  114. if("${rv}" STREQUAL "done")
  115. # Entries that we have been told to explicitly pass as standard include
  116. # directories will not be implicitly added by the compiler.
  117. if(CMAKE_${lang}_STANDARD_INCLUDE_DIRECTORIES)
  118. list(REMOVE_ITEM implicit_incdirs ${CMAKE_${lang}_STANDARD_INCLUDE_DIRECTORIES})
  119. endif()
  120. # We parsed implicit include directories, so override the default initializer.
  121. set(_CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES_INIT "${implicit_incdirs}")
  122. endif()
  123. endif()
  124. set(CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES "${_CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES_INIT}" PARENT_SCOPE)
  125. # Parse implicit linker information for this language, if available.
  126. set(implicit_dirs "")
  127. set(implicit_objs "")
  128. set(implicit_libs "")
  129. set(implicit_fwks "")
  130. if(CMAKE_${lang}_VERBOSE_FLAG)
  131. CMAKE_PARSE_IMPLICIT_LINK_INFO("${OUTPUT}" implicit_libs implicit_dirs implicit_fwks log
  132. "${CMAKE_${lang}_IMPLICIT_OBJECT_REGEX}"
  133. COMPUTE_IMPLICIT_OBJECTS implicit_objs
  134. LANGUAGE ${lang})
  135. message(CONFIGURE_LOG
  136. "Parsed ${lang} implicit link information:\n${log}\n\n")
  137. endif()
  138. # for VS IDE Intel Fortran we have to figure out the
  139. # implicit link path for the fortran run time using
  140. # a try-compile
  141. if("${lang}" MATCHES "Fortran"
  142. AND "${CMAKE_GENERATOR}" MATCHES "Visual Studio")
  143. message(CHECK_START "Determine Intel Fortran Compiler Implicit Link Path")
  144. # Build a sample project which reports symbols.
  145. try_compile(IFORT_LIB_PATH_COMPILED
  146. PROJECT IntelFortranImplicit
  147. SOURCE_DIR ${CMAKE_ROOT}/Modules/IntelVSImplicitPath
  148. BINARY_DIR ${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath
  149. CMAKE_FLAGS
  150. "-DCMAKE_Fortran_FLAGS:STRING=${CMAKE_Fortran_FLAGS}"
  151. OUTPUT_VARIABLE _output)
  152. file(WRITE
  153. "${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath/output.txt"
  154. "${_output}")
  155. include(${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath/output.cmake OPTIONAL)
  156. message(CHECK_PASS "done")
  157. endif()
  158. # Implicit link libraries cannot be used explicitly for multiple
  159. # OS X architectures, so we skip it.
  160. if(DEFINED CMAKE_OSX_ARCHITECTURES)
  161. if("${CMAKE_OSX_ARCHITECTURES}" MATCHES ";")
  162. set(implicit_libs "")
  163. endif()
  164. endif()
  165. if(DEFINED ENV{CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES_EXCLUDE})
  166. list(REMOVE_ITEM implicit_dirs $ENV{CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES_EXCLUDE})
  167. endif()
  168. set(CMAKE_${lang}_IMPLICIT_LINK_LIBRARIES "${implicit_libs}" PARENT_SCOPE)
  169. set(CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES "${implicit_dirs}" PARENT_SCOPE)
  170. set(CMAKE_${lang}_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "${implicit_fwks}" PARENT_SCOPE)
  171. cmake_parse_library_architecture(${lang} "${implicit_dirs}" "${implicit_objs}" architecture_flag)
  172. if(architecture_flag)
  173. set(CMAKE_${lang}_LIBRARY_ARCHITECTURE "${architecture_flag}" PARENT_SCOPE)
  174. endif()
  175. else()
  176. message(CHECK_FAIL "failed")
  177. endif()
  178. endif()
  179. endfunction()