CMakeDetermineCompilerABI.cmake 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. )
  48. # Restore original LC_ALL, LC_MESSAGES, and LANG
  49. set(ENV{LC_ALL} ${_orig_lc_all})
  50. set(ENV{LC_MESSAGES} ${_orig_lc_messages})
  51. set(ENV{LANG} ${_orig_lang})
  52. # Move result from cache to normal variable.
  53. set(CMAKE_${lang}_ABI_COMPILED ${CMAKE_${lang}_ABI_COMPILED})
  54. unset(CMAKE_${lang}_ABI_COMPILED CACHE)
  55. set(CMAKE_${lang}_ABI_COMPILED ${CMAKE_${lang}_ABI_COMPILED} PARENT_SCOPE)
  56. # Load the resulting information strings.
  57. if(CMAKE_${lang}_ABI_COMPILED AND NOT _copy_error)
  58. message(STATUS "Detecting ${lang} compiler ABI info - done")
  59. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  60. "Detecting ${lang} compiler ABI info compiled with the following output:\n${OUTPUT}\n\n")
  61. file(STRINGS "${BIN}" ABI_STRINGS LIMIT_COUNT 2 REGEX "INFO:[A-Za-z0-9_]+\\[[^]]*\\]")
  62. foreach(info ${ABI_STRINGS})
  63. if("${info}" MATCHES "INFO:sizeof_dptr\\[0*([^]]*)\\]")
  64. set(ABI_SIZEOF_DPTR "${CMAKE_MATCH_1}")
  65. endif()
  66. if("${info}" MATCHES "INFO:abi\\[([^]]*)\\]")
  67. set(ABI_NAME "${CMAKE_MATCH_1}")
  68. endif()
  69. endforeach()
  70. if(ABI_SIZEOF_DPTR)
  71. set(CMAKE_${lang}_SIZEOF_DATA_PTR "${ABI_SIZEOF_DPTR}" PARENT_SCOPE)
  72. elseif(CMAKE_${lang}_SIZEOF_DATA_PTR_DEFAULT)
  73. set(CMAKE_${lang}_SIZEOF_DATA_PTR "${CMAKE_${lang}_SIZEOF_DATA_PTR_DEFAULT}" PARENT_SCOPE)
  74. endif()
  75. if(ABI_NAME)
  76. set(CMAKE_${lang}_COMPILER_ABI "${ABI_NAME}" PARENT_SCOPE)
  77. endif()
  78. # Parse implicit include directory for this language, if available.
  79. if(CMAKE_${lang}_VERBOSE_FLAG)
  80. set (implicit_incdirs "")
  81. cmake_parse_implicit_include_info("${OUTPUT}" "${lang}"
  82. implicit_incdirs log rv)
  83. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  84. "Parsed ${lang} implicit include dir info from above output: rv=${rv}\n${log}\n\n")
  85. if("${rv}" STREQUAL "done")
  86. # Entries that we have been told to explicitly pass as standard include
  87. # directories will not be implicitly added by the compiler.
  88. if(CMAKE_${lang}_STANDARD_INCLUDE_DIRECTORIES)
  89. list(REMOVE_ITEM implicit_incdirs ${CMAKE_${lang}_STANDARD_INCLUDE_DIRECTORIES})
  90. endif()
  91. # We parsed implicit include directories, so override the default initializer.
  92. set(_CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES_INIT "${implicit_incdirs}")
  93. endif()
  94. endif()
  95. set(CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES "${_CMAKE_${lang}_IMPLICIT_INCLUDE_DIRECTORIES_INIT}" PARENT_SCOPE)
  96. # Parse implicit linker information for this language, if available.
  97. set(implicit_dirs "")
  98. set(implicit_libs "")
  99. set(implicit_fwks "")
  100. if(CMAKE_${lang}_VERBOSE_FLAG)
  101. CMAKE_PARSE_IMPLICIT_LINK_INFO("${OUTPUT}" implicit_libs implicit_dirs implicit_fwks log
  102. "${CMAKE_${lang}_IMPLICIT_OBJECT_REGEX}")
  103. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  104. "Parsed ${lang} implicit link information from above output:\n${log}\n\n")
  105. endif()
  106. # for VS IDE Intel Fortran we have to figure out the
  107. # implicit link path for the fortran run time using
  108. # a try-compile
  109. if("${lang}" MATCHES "Fortran"
  110. AND "${CMAKE_GENERATOR}" MATCHES "Visual Studio")
  111. set(_desc "Determine Intel Fortran Compiler Implicit Link Path")
  112. message(STATUS "${_desc}")
  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. set(_desc "Determine Intel Fortran Compiler Implicit Link Path -- done")
  126. message(STATUS "${_desc}")
  127. endif()
  128. # Implicit link libraries cannot be used explicitly for multiple
  129. # OS X architectures, so we skip it.
  130. if(DEFINED CMAKE_OSX_ARCHITECTURES)
  131. if("${CMAKE_OSX_ARCHITECTURES}" MATCHES ";")
  132. set(implicit_libs "")
  133. endif()
  134. endif()
  135. set(CMAKE_${lang}_IMPLICIT_LINK_LIBRARIES "${implicit_libs}" PARENT_SCOPE)
  136. set(CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES "${implicit_dirs}" PARENT_SCOPE)
  137. set(CMAKE_${lang}_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "${implicit_fwks}" PARENT_SCOPE)
  138. # Detect library architecture directory name.
  139. if(CMAKE_LIBRARY_ARCHITECTURE_REGEX)
  140. foreach(dir ${implicit_dirs})
  141. if("${dir}" MATCHES "/lib/${CMAKE_LIBRARY_ARCHITECTURE_REGEX}$")
  142. get_filename_component(arch "${dir}" NAME)
  143. set(CMAKE_${lang}_LIBRARY_ARCHITECTURE "${arch}" PARENT_SCOPE)
  144. break()
  145. endif()
  146. endforeach()
  147. endif()
  148. else()
  149. message(STATUS "Detecting ${lang} compiler ABI info - failed")
  150. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  151. "Detecting ${lang} compiler ABI info failed to compile with the following output:\n${OUTPUT}\n${_copy_error}\n\n")
  152. endif()
  153. endif()
  154. endfunction()