CMakeDetermineCompilerABI.cmake 8.1 KB

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