CMakeDetermineCompilerABI.cmake 7.9 KB

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