CMakeDetermineCompilerABI.cmake 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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(STATUS "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(STATUS "Detecting ${lang} compiler ABI info - 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. set(_desc "Determine Intel Fortran Compiler Implicit Link Path")
  113. message(STATUS "${_desc}")
  114. # Build a sample project which reports symbols.
  115. try_compile(IFORT_LIB_PATH_COMPILED
  116. ${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath
  117. ${CMAKE_ROOT}/Modules/IntelVSImplicitPath
  118. IntelFortranImplicit
  119. CMAKE_FLAGS
  120. "-DCMAKE_Fortran_FLAGS:STRING=${CMAKE_Fortran_FLAGS}"
  121. OUTPUT_VARIABLE _output)
  122. file(WRITE
  123. "${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath/output.txt"
  124. "${_output}")
  125. include(${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath/output.cmake OPTIONAL)
  126. set(_desc "Determine Intel Fortran Compiler Implicit Link Path -- done")
  127. message(STATUS "${_desc}")
  128. endif()
  129. # Implicit link libraries cannot be used explicitly for multiple
  130. # OS X architectures, so we skip it.
  131. if(DEFINED CMAKE_OSX_ARCHITECTURES)
  132. if("${CMAKE_OSX_ARCHITECTURES}" MATCHES ";")
  133. set(implicit_libs "")
  134. endif()
  135. endif()
  136. set(CMAKE_${lang}_IMPLICIT_LINK_LIBRARIES "${implicit_libs}" PARENT_SCOPE)
  137. set(CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES "${implicit_dirs}" PARENT_SCOPE)
  138. set(CMAKE_${lang}_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "${implicit_fwks}" PARENT_SCOPE)
  139. # Detect library architecture directory name.
  140. if(CMAKE_LIBRARY_ARCHITECTURE_REGEX)
  141. foreach(dir ${implicit_dirs})
  142. if("${dir}" MATCHES "/lib/${CMAKE_LIBRARY_ARCHITECTURE_REGEX}$")
  143. get_filename_component(arch "${dir}" NAME)
  144. set(CMAKE_${lang}_LIBRARY_ARCHITECTURE "${arch}" PARENT_SCOPE)
  145. break()
  146. endif()
  147. endforeach()
  148. endif()
  149. else()
  150. message(STATUS "Detecting ${lang} compiler ABI info - failed")
  151. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  152. "Detecting ${lang} compiler ABI info failed to compile with the following output:\n${OUTPUT}\n${_copy_error}\n\n")
  153. endif()
  154. endif()
  155. endfunction()