CMakeDetermineCompilerABI.cmake 5.9 KB

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