CMakeDetermineCompilerABI.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/CMakeParseImplicitLinkInfo.cmake)
  7. function(CMAKE_DETERMINE_COMPILER_ABI lang src)
  8. if(NOT DEFINED CMAKE_${lang}_ABI_COMPILED)
  9. message(STATUS "Detecting ${lang} compiler ABI info")
  10. # Compile the ABI identification source.
  11. set(BIN "${CMAKE_PLATFORM_INFO_DIR}/CMakeDetermineCompilerABI_${lang}.bin")
  12. set(CMAKE_FLAGS )
  13. if(DEFINED CMAKE_${lang}_VERBOSE_FLAG)
  14. set(CMAKE_FLAGS "-DEXE_LINKER_FLAGS=${CMAKE_${lang}_VERBOSE_FLAG}")
  15. endif()
  16. if(NOT "x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xMSVC")
  17. # Avoid adding our own platform standard libraries for compilers
  18. # from which we might detect implicit link libraries.
  19. list(APPEND CMAKE_FLAGS "-DCMAKE_${lang}_STANDARD_LIBRARIES=")
  20. endif()
  21. try_compile(CMAKE_${lang}_ABI_COMPILED
  22. ${CMAKE_BINARY_DIR} ${src}
  23. CMAKE_FLAGS ${CMAKE_FLAGS}
  24. # Ignore unused flags when we are just determining the ABI.
  25. "--no-warn-unused-cli"
  26. OUTPUT_VARIABLE OUTPUT
  27. COPY_FILE "${BIN}"
  28. COPY_FILE_ERROR _copy_error
  29. )
  30. # Move result from cache to normal variable.
  31. set(CMAKE_${lang}_ABI_COMPILED ${CMAKE_${lang}_ABI_COMPILED})
  32. unset(CMAKE_${lang}_ABI_COMPILED CACHE)
  33. set(CMAKE_${lang}_ABI_COMPILED ${CMAKE_${lang}_ABI_COMPILED} PARENT_SCOPE)
  34. # Load the resulting information strings.
  35. if(CMAKE_${lang}_ABI_COMPILED AND NOT _copy_error)
  36. message(STATUS "Detecting ${lang} compiler ABI info - done")
  37. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  38. "Detecting ${lang} compiler ABI info compiled with the following output:\n${OUTPUT}\n\n")
  39. file(STRINGS "${BIN}" ABI_STRINGS LIMIT_COUNT 2 REGEX "INFO:[A-Za-z0-9_]+\\[[^]]*\\]")
  40. foreach(info ${ABI_STRINGS})
  41. if("${info}" MATCHES "INFO:sizeof_dptr\\[0*([^]]*)\\]")
  42. set(ABI_SIZEOF_DPTR "${CMAKE_MATCH_1}")
  43. endif()
  44. if("${info}" MATCHES "INFO:abi\\[([^]]*)\\]")
  45. set(ABI_NAME "${CMAKE_MATCH_1}")
  46. endif()
  47. endforeach()
  48. if(ABI_SIZEOF_DPTR)
  49. set(CMAKE_${lang}_SIZEOF_DATA_PTR "${ABI_SIZEOF_DPTR}" PARENT_SCOPE)
  50. elseif(CMAKE_${lang}_SIZEOF_DATA_PTR_DEFAULT)
  51. set(CMAKE_${lang}_SIZEOF_DATA_PTR "${CMAKE_${lang}_SIZEOF_DATA_PTR_DEFAULT}" PARENT_SCOPE)
  52. endif()
  53. if(ABI_NAME)
  54. set(CMAKE_${lang}_COMPILER_ABI "${ABI_NAME}" PARENT_SCOPE)
  55. endif()
  56. # Parse implicit linker information for this language, if available.
  57. set(implicit_dirs "")
  58. set(implicit_libs "")
  59. set(implicit_fwks "")
  60. if(CMAKE_${lang}_VERBOSE_FLAG)
  61. CMAKE_PARSE_IMPLICIT_LINK_INFO("${OUTPUT}" implicit_libs implicit_dirs implicit_fwks log
  62. "${CMAKE_${lang}_IMPLICIT_OBJECT_REGEX}")
  63. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  64. "Parsed ${lang} implicit link information from above output:\n${log}\n\n")
  65. endif()
  66. # for VS IDE Intel Fortran we have to figure out the
  67. # implicit link path for the fortran run time using
  68. # a try-compile
  69. if("${lang}" MATCHES "Fortran"
  70. AND "${CMAKE_GENERATOR}" MATCHES "Visual Studio")
  71. set(_desc "Determine Intel Fortran Compiler Implicit Link Path")
  72. message(STATUS "${_desc}")
  73. # Build a sample project which reports symbols.
  74. try_compile(IFORT_LIB_PATH_COMPILED
  75. ${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath
  76. ${CMAKE_ROOT}/Modules/IntelVSImplicitPath
  77. IntelFortranImplicit
  78. CMAKE_FLAGS
  79. "-DCMAKE_Fortran_FLAGS:STRING=${CMAKE_Fortran_FLAGS}"
  80. OUTPUT_VARIABLE _output)
  81. file(WRITE
  82. "${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath/output.txt"
  83. "${_output}")
  84. include(${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath/output.cmake OPTIONAL)
  85. set(_desc "Determine Intel Fortran Compiler Implicit Link Path -- done")
  86. message(STATUS "${_desc}")
  87. endif()
  88. # Implicit link libraries cannot be used explicitly for multiple
  89. # OS X architectures, so we skip it.
  90. if(DEFINED CMAKE_OSX_ARCHITECTURES)
  91. if("${CMAKE_OSX_ARCHITECTURES}" MATCHES ";")
  92. set(implicit_libs "")
  93. endif()
  94. endif()
  95. set(CMAKE_${lang}_IMPLICIT_LINK_LIBRARIES "${implicit_libs}" PARENT_SCOPE)
  96. set(CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES "${implicit_dirs}" PARENT_SCOPE)
  97. set(CMAKE_${lang}_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "${implicit_fwks}" PARENT_SCOPE)
  98. # Detect library architecture directory name.
  99. if(CMAKE_LIBRARY_ARCHITECTURE_REGEX)
  100. foreach(dir ${implicit_dirs})
  101. if("${dir}" MATCHES "/lib/${CMAKE_LIBRARY_ARCHITECTURE_REGEX}$")
  102. get_filename_component(arch "${dir}" NAME)
  103. set(CMAKE_${lang}_LIBRARY_ARCHITECTURE "${arch}" PARENT_SCOPE)
  104. break()
  105. endif()
  106. endforeach()
  107. endif()
  108. else()
  109. message(STATUS "Detecting ${lang} compiler ABI info - failed")
  110. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  111. "Detecting ${lang} compiler ABI info failed to compile with the following output:\n${OUTPUT}\n${_copy_error}\n\n")
  112. endif()
  113. endif()
  114. endfunction()