CMakeDetermineCompilerABI.cmake 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_${lang}_VERBOSE_FLAG}")
  25. endif()
  26. if(CMAKE_${lang}_COMPILER_TARGET)
  27. set(CMAKE_FLAGS "${CMAKE_FLAGS} -DCMAKE_${lang}_COMPILER_TARGET=${CMAKE_${lang}_COMPILER_TARGET}")
  28. endif()
  29. try_compile(CMAKE_${lang}_ABI_COMPILED
  30. ${CMAKE_BINARY_DIR} ${src}
  31. CMAKE_FLAGS "${CMAKE_FLAGS}"
  32. "-DCMAKE_${lang}_STANDARD_LIBRARIES="
  33. # We need ignore these warnings because some platforms need
  34. # CMAKE_${lang}_STANDARD_LIBRARIES to link properly and we
  35. # don't care when we are just determining the ABI.
  36. "--no-warn-unused-cli"
  37. OUTPUT_VARIABLE OUTPUT
  38. COPY_FILE "${BIN}"
  39. COPY_FILE_ERROR _copy_error
  40. )
  41. # Move result from cache to normal variable.
  42. set(CMAKE_${lang}_ABI_COMPILED ${CMAKE_${lang}_ABI_COMPILED})
  43. unset(CMAKE_${lang}_ABI_COMPILED CACHE)
  44. set(CMAKE_${lang}_ABI_COMPILED ${CMAKE_${lang}_ABI_COMPILED} PARENT_SCOPE)
  45. # Load the resulting information strings.
  46. if(CMAKE_${lang}_ABI_COMPILED AND NOT _copy_error)
  47. message(STATUS "Detecting ${lang} compiler ABI info - done")
  48. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  49. "Detecting ${lang} compiler ABI info compiled with the following output:\n${OUTPUT}\n\n")
  50. file(STRINGS "${BIN}" ABI_STRINGS LIMIT_COUNT 2 REGEX "INFO:[^[]*\\[")
  51. foreach(info ${ABI_STRINGS})
  52. if("${info}" MATCHES ".*INFO:sizeof_dptr\\[0*([^]]*)\\].*")
  53. string(REGEX REPLACE ".*INFO:sizeof_dptr\\[0*([^]]*)\\].*" "\\1" ABI_SIZEOF_DPTR "${info}")
  54. endif()
  55. if("${info}" MATCHES ".*INFO:abi\\[([^]]*)\\].*")
  56. string(REGEX REPLACE ".*INFO:abi\\[([^]]*)\\].*" "\\1" ABI_NAME "${info}")
  57. endif()
  58. endforeach()
  59. if(ABI_SIZEOF_DPTR)
  60. set(CMAKE_${lang}_SIZEOF_DATA_PTR "${ABI_SIZEOF_DPTR}" PARENT_SCOPE)
  61. elseif(CMAKE_${lang}_SIZEOF_DATA_PTR_DEFAULT)
  62. set(CMAKE_${lang}_SIZEOF_DATA_PTR "${CMAKE_${lang}_SIZEOF_DATA_PTR_DEFAULT}" PARENT_SCOPE)
  63. endif()
  64. if(ABI_NAME)
  65. set(CMAKE_${lang}_COMPILER_ABI "${ABI_NAME}" PARENT_SCOPE)
  66. endif()
  67. # Parse implicit linker information for this language, if available.
  68. set(implicit_dirs "")
  69. set(implicit_libs "")
  70. set(implicit_fwks "")
  71. if(CMAKE_${lang}_VERBOSE_FLAG)
  72. CMAKE_PARSE_IMPLICIT_LINK_INFO("${OUTPUT}" implicit_libs implicit_dirs implicit_fwks log
  73. "${CMAKE_${lang}_IMPLICIT_OBJECT_REGEX}")
  74. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  75. "Parsed ${lang} implicit link information from above output:\n${log}\n\n")
  76. endif()
  77. # for VS IDE Intel Fortran we have to figure out the
  78. # implicit link path for the fortran run time using
  79. # a try-compile
  80. if("${lang}" MATCHES "Fortran"
  81. AND "${CMAKE_GENERATOR}" MATCHES "Visual Studio")
  82. set(_desc "Determine Intel Fortran Compiler Implicit Link Path")
  83. message(STATUS "${_desc}")
  84. # Build a sample project which reports symbols.
  85. try_compile(IFORT_LIB_PATH_COMPILED
  86. ${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath
  87. ${CMAKE_ROOT}/Modules/IntelVSImplicitPath
  88. IntelFortranImplicit
  89. CMAKE_FLAGS
  90. "-DCMAKE_Fortran_FLAGS:STRING=${CMAKE_Fortran_FLAGS}"
  91. OUTPUT_VARIABLE _output)
  92. file(WRITE
  93. "${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath/output.txt"
  94. "${_output}")
  95. include(${CMAKE_BINARY_DIR}/CMakeFiles/IntelVSImplicitPath/output.cmake OPTIONAL)
  96. set(_desc "Determine Intel Fortran Compiler Implicit Link Path -- done")
  97. message(STATUS "${_desc}")
  98. endif()
  99. # Implicit link libraries cannot be used explicitly for multiple
  100. # OS X architectures, so we skip it.
  101. if(DEFINED CMAKE_OSX_ARCHITECTURES)
  102. if("${CMAKE_OSX_ARCHITECTURES}" MATCHES ";")
  103. set(implicit_libs "")
  104. endif()
  105. endif()
  106. set(CMAKE_${lang}_IMPLICIT_LINK_LIBRARIES "${implicit_libs}" PARENT_SCOPE)
  107. set(CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES "${implicit_dirs}" PARENT_SCOPE)
  108. set(CMAKE_${lang}_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "${implicit_fwks}" PARENT_SCOPE)
  109. # Detect library architecture directory name.
  110. if(CMAKE_LIBRARY_ARCHITECTURE_REGEX)
  111. foreach(dir ${implicit_dirs})
  112. if("${dir}" MATCHES "/lib/${CMAKE_LIBRARY_ARCHITECTURE_REGEX}$")
  113. get_filename_component(arch "${dir}" NAME)
  114. set(CMAKE_${lang}_LIBRARY_ARCHITECTURE "${arch}" PARENT_SCOPE)
  115. break()
  116. endif()
  117. endforeach()
  118. endif()
  119. else()
  120. message(STATUS "Detecting ${lang} compiler ABI info - failed")
  121. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  122. "Detecting ${lang} compiler ABI info failed to compile with the following output:\n${OUTPUT}\n${_copy_error}\n\n")
  123. endif()
  124. endif()
  125. endfunction()