CMakeDetermineCompilerABI.cmake 4.77 KB
Newer Older
1

2
3
4
5
6
7
8
9
10
11
#=============================================================================
# Copyright 2008-2009 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
12
# (To distribute this file outside of CMake, substitute the full
13
14
#  License text for the above reference.)

15
16
17
18
# Function to compile a source file to identify the compiler ABI.
# This is used internally by CMake and should not be included by user
# code.

19
20
INCLUDE(${CMAKE_ROOT}/Modules/CMakeParseImplicitLinkInfo.cmake)

21
22
FUNCTION(CMAKE_DETERMINE_COMPILER_ABI lang src)
  IF(NOT DEFINED CMAKE_DETERMINE_${lang}_ABI_COMPILED)
23
    MESSAGE(STATUS "Detecting ${lang} compiler ABI info")
24
25
26

    # Compile the ABI identification source.
    SET(BIN "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeDetermineCompilerABI_${lang}.bin")
27
28
29
30
    SET(CMAKE_FLAGS )
    IF(DEFINED CMAKE_${lang}_VERBOSE_FLAG)
      SET(CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_${lang}_VERBOSE_FLAG}")
    ENDIF()
31
32
    TRY_COMPILE(CMAKE_DETERMINE_${lang}_ABI_COMPILED
      ${CMAKE_BINARY_DIR} ${src}
33
      CMAKE_FLAGS "${CMAKE_FLAGS}"
34
                  "-DCMAKE_${lang}_STANDARD_LIBRARIES="
35
36
37
38
                  # We need ignore these warnings because some platforms need
                  # CMAKE_${lang}_STANDARD_LIBRARIES to link properly and we
                  # don't care when we are just determining the ABI.
                  "--no-warn-unused-cli"
39
40
41
42
43
44
      OUTPUT_VARIABLE OUTPUT
      COPY_FILE "${BIN}"
      )

    # Load the resulting information strings.
    IF(CMAKE_DETERMINE_${lang}_ABI_COMPILED)
45
      MESSAGE(STATUS "Detecting ${lang} compiler ABI info - done")
46
      FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
47
        "Detecting ${lang} compiler ABI info compiled with the following output:\n${OUTPUT}\n\n")
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
      FILE(STRINGS "${BIN}" ABI_STRINGS LIMIT_COUNT 2 REGEX "INFO:[^[]*\\[")
      FOREACH(info ${ABI_STRINGS})
        IF("${info}" MATCHES ".*INFO:sizeof_dptr\\[0*([^]]*)\\].*")
          STRING(REGEX REPLACE ".*INFO:sizeof_dptr\\[0*([^]]*)\\].*" "\\1" ABI_SIZEOF_DPTR "${info}")
        ENDIF("${info}" MATCHES ".*INFO:sizeof_dptr\\[0*([^]]*)\\].*")
        IF("${info}" MATCHES ".*INFO:abi\\[([^]]*)\\].*")
          STRING(REGEX REPLACE ".*INFO:abi\\[([^]]*)\\].*" "\\1" ABI_NAME "${info}")
        ENDIF("${info}" MATCHES ".*INFO:abi\\[([^]]*)\\].*")
      ENDFOREACH(info)

      IF(ABI_SIZEOF_DPTR)
        SET(CMAKE_${lang}_SIZEOF_DATA_PTR "${ABI_SIZEOF_DPTR}" PARENT_SCOPE)
      ENDIF(ABI_SIZEOF_DPTR)

      IF(ABI_NAME)
        SET(CMAKE_${lang}_COMPILER_ABI "${ABI_NAME}" PARENT_SCOPE)
      ENDIF(ABI_NAME)

66
67
68
      # Parse implicit linker information for this language, if available.
      SET(implicit_dirs "")
      SET(implicit_libs "")
69
70
71
72
73
74
      SET(MULTI_ARCH FALSE)
      IF(DEFINED CMAKE_OSX_ARCHITECTURES)
        IF( "${CMAKE_OSX_ARCHITECTURES}" MATCHES ";" )
          SET(MULTI_ARCH TRUE)
        ENDIF()
      ENDIF()
75
76
77
      IF(CMAKE_${lang}_VERBOSE_FLAG
          # Implicit link information cannot be used explicitly for
          # multiple OS X architectures, so we skip it.
78
          AND NOT MULTI_ARCH
79
80
          # Skip this with Xcode for now.
          AND NOT "${CMAKE_GENERATOR}" MATCHES Xcode)
81
82
        CMAKE_PARSE_IMPLICIT_LINK_INFO("${OUTPUT}" implicit_libs implicit_dirs log
          "${CMAKE_${lang}_IMPLICIT_OBJECT_REGEX}")
83
84
        FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
          "Parsed ${lang} implicit link information from above output:\n${log}\n\n")
85
86
87
88
      ENDIF()
      SET(CMAKE_${lang}_IMPLICIT_LINK_LIBRARIES "${implicit_libs}" PARENT_SCOPE)
      SET(CMAKE_${lang}_IMPLICIT_LINK_DIRECTORIES "${implicit_dirs}" PARENT_SCOPE)

89
90
91
92
93
94
95
96
97
98
99
      # Detect library architecture directory name.
      IF(CMAKE_LIBRARY_ARCHITECTURE_REGEX)
        FOREACH(dir ${implicit_dirs})
          IF("${dir}" MATCHES "/lib/${CMAKE_LIBRARY_ARCHITECTURE_REGEX}$")
            GET_FILENAME_COMPONENT(arch "${dir}" NAME)
            SET(CMAKE_${lang}_LIBRARY_ARCHITECTURE "${arch}" PARENT_SCOPE)
            BREAK()
          ENDIF()
        ENDFOREACH()
      ENDIF()

100
    ELSE(CMAKE_DETERMINE_${lang}_ABI_COMPILED)
101
      MESSAGE(STATUS "Detecting ${lang} compiler ABI info - failed")
102
      FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
103
        "Detecting ${lang} compiler ABI info failed to compile with the following output:\n${OUTPUT}\n\n")
104
105
106
    ENDIF(CMAKE_DETERMINE_${lang}_ABI_COMPILED)
  ENDIF(NOT DEFINED CMAKE_DETERMINE_${lang}_ABI_COMPILED)
ENDFUNCTION(CMAKE_DETERMINE_COMPILER_ABI)