kwsysPlatformTests.cmake 7.3 KB
Newer Older
Kitware Robot's avatar
Kitware Robot committed
1 2 3
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing#kwsys for details.

Kitware Robot's avatar
Kitware Robot committed
4 5 6 7
SET(KWSYS_PLATFORM_TEST_FILE_C kwsysPlatformTestsC.c)
SET(KWSYS_PLATFORM_TEST_FILE_CXX kwsysPlatformTestsCXX.cxx)

MACRO(KWSYS_PLATFORM_TEST lang var description invert)
Kitware Robot's avatar
Kitware Robot committed
8
  IF(NOT DEFINED ${var}_COMPILED)
Kitware Robot's avatar
Kitware Robot committed
9
    MESSAGE(STATUS "${description}")
Kitware Robot's avatar
Kitware Robot committed
10 11 12 13
    set(maybe_cxx_standard "")
    if(CMAKE_VERSION VERSION_LESS 3.8 AND CMAKE_CXX_STANDARD)
      set(maybe_cxx_standard "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}")
    endif()
Kitware Robot's avatar
Kitware Robot committed
14 15 16 17
    TRY_COMPILE(${var}_COMPILED
      ${CMAKE_CURRENT_BINARY_DIR}
      ${CMAKE_CURRENT_SOURCE_DIR}/${KWSYS_PLATFORM_TEST_FILE_${lang}}
      COMPILE_DEFINITIONS -DTEST_${var} ${KWSYS_PLATFORM_TEST_DEFINES} ${KWSYS_PLATFORM_TEST_EXTRA_FLAGS}
Kitware Robot's avatar
Kitware Robot committed
18
      CMAKE_FLAGS "-DLINK_LIBRARIES:STRING=${KWSYS_PLATFORM_TEST_LINK_LIBRARIES}"
Kitware Robot's avatar
Kitware Robot committed
19
                  ${maybe_cxx_standard}
Kitware Robot's avatar
Kitware Robot committed
20 21 22 23 24
      OUTPUT_VARIABLE OUTPUT)
    IF(${var}_COMPILED)
      FILE(APPEND
        ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
        "${description} compiled with the following output:\n${OUTPUT}\n\n")
Kitware Robot's avatar
Kitware Robot committed
25
    ELSE()
Kitware Robot's avatar
Kitware Robot committed
26 27 28
      FILE(APPEND
        ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
        "${description} failed to compile with the following output:\n${OUTPUT}\n\n")
Kitware Robot's avatar
Kitware Robot committed
29
    ENDIF()
Kitware Robot's avatar
Kitware Robot committed
30 31 32
    IF(${invert} MATCHES INVERT)
      IF(${var}_COMPILED)
        MESSAGE(STATUS "${description} - no")
Kitware Robot's avatar
Kitware Robot committed
33
      ELSE()
Kitware Robot's avatar
Kitware Robot committed
34
        MESSAGE(STATUS "${description} - yes")
Kitware Robot's avatar
Kitware Robot committed
35 36
      ENDIF()
    ELSE()
Kitware Robot's avatar
Kitware Robot committed
37 38
      IF(${var}_COMPILED)
        MESSAGE(STATUS "${description} - yes")
Kitware Robot's avatar
Kitware Robot committed
39
      ELSE()
Kitware Robot's avatar
Kitware Robot committed
40
        MESSAGE(STATUS "${description} - no")
Kitware Robot's avatar
Kitware Robot committed
41 42
      ENDIF()
    ENDIF()
Kitware Robot's avatar
Kitware Robot committed
43
  ENDIF()
Kitware Robot's avatar
Kitware Robot committed
44 45 46
  IF(${invert} MATCHES INVERT)
    IF(${var}_COMPILED)
      SET(${var} 0)
Kitware Robot's avatar
Kitware Robot committed
47
    ELSE()
Kitware Robot's avatar
Kitware Robot committed
48
      SET(${var} 1)
Kitware Robot's avatar
Kitware Robot committed
49 50
    ENDIF()
  ELSE()
Kitware Robot's avatar
Kitware Robot committed
51 52
    IF(${var}_COMPILED)
      SET(${var} 1)
Kitware Robot's avatar
Kitware Robot committed
53
    ELSE()
Kitware Robot's avatar
Kitware Robot committed
54
      SET(${var} 0)
Kitware Robot's avatar
Kitware Robot committed
55 56 57
    ENDIF()
  ENDIF()
ENDMACRO()
Kitware Robot's avatar
Kitware Robot committed
58 59

MACRO(KWSYS_PLATFORM_TEST_RUN lang var description invert)
Kitware Robot's avatar
Kitware Robot committed
60
  IF(NOT DEFINED ${var})
Kitware Robot's avatar
Kitware Robot committed
61 62 63 64 65 66 67 68 69 70 71 72 73
    MESSAGE(STATUS "${description}")
    TRY_RUN(${var} ${var}_COMPILED
      ${CMAKE_CURRENT_BINARY_DIR}
      ${CMAKE_CURRENT_SOURCE_DIR}/${KWSYS_PLATFORM_TEST_FILE_${lang}}
      COMPILE_DEFINITIONS -DTEST_${var} ${KWSYS_PLATFORM_TEST_DEFINES} ${KWSYS_PLATFORM_TEST_EXTRA_FLAGS}
      OUTPUT_VARIABLE OUTPUT)

    # Note that ${var} will be a 0 return value on success.
    IF(${var}_COMPILED)
      IF(${var})
        FILE(APPEND
          ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
          "${description} compiled but failed to run with the following output:\n${OUTPUT}\n\n")
Kitware Robot's avatar
Kitware Robot committed
74
      ELSE()
Kitware Robot's avatar
Kitware Robot committed
75 76 77
        FILE(APPEND
          ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
          "${description} compiled and ran with the following output:\n${OUTPUT}\n\n")
Kitware Robot's avatar
Kitware Robot committed
78 79
      ENDIF()
    ELSE()
Kitware Robot's avatar
Kitware Robot committed
80 81 82 83
      FILE(APPEND
        ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
        "${description} failed to compile with the following output:\n${OUTPUT}\n\n")
      SET(${var} -1 CACHE INTERNAL "${description} failed to compile.")
Kitware Robot's avatar
Kitware Robot committed
84
    ENDIF()
Kitware Robot's avatar
Kitware Robot committed
85 86 87 88 89

    IF(${invert} MATCHES INVERT)
      IF(${var}_COMPILED)
        IF(${var})
          MESSAGE(STATUS "${description} - yes")
Kitware Robot's avatar
Kitware Robot committed
90
        ELSE()
Kitware Robot's avatar
Kitware Robot committed
91
          MESSAGE(STATUS "${description} - no")
Kitware Robot's avatar
Kitware Robot committed
92 93
        ENDIF()
      ELSE()
Kitware Robot's avatar
Kitware Robot committed
94
        MESSAGE(STATUS "${description} - failed to compile")
Kitware Robot's avatar
Kitware Robot committed
95 96
      ENDIF()
    ELSE()
Kitware Robot's avatar
Kitware Robot committed
97 98 99
      IF(${var}_COMPILED)
        IF(${var})
          MESSAGE(STATUS "${description} - no")
Kitware Robot's avatar
Kitware Robot committed
100
        ELSE()
Kitware Robot's avatar
Kitware Robot committed
101
          MESSAGE(STATUS "${description} - yes")
Kitware Robot's avatar
Kitware Robot committed
102 103
        ENDIF()
      ELSE()
Kitware Robot's avatar
Kitware Robot committed
104
        MESSAGE(STATUS "${description} - failed to compile")
Kitware Robot's avatar
Kitware Robot committed
105 106
      ENDIF()
    ENDIF()
Kitware Robot's avatar
Kitware Robot committed
107
  ENDIF()
Kitware Robot's avatar
Kitware Robot committed
108 109 110 111 112

  IF(${invert} MATCHES INVERT)
    IF(${var}_COMPILED)
      IF(${var})
        SET(${var} 1)
Kitware Robot's avatar
Kitware Robot committed
113
      ELSE()
Kitware Robot's avatar
Kitware Robot committed
114
        SET(${var} 0)
Kitware Robot's avatar
Kitware Robot committed
115 116
      ENDIF()
    ELSE()
Kitware Robot's avatar
Kitware Robot committed
117
      SET(${var} 1)
Kitware Robot's avatar
Kitware Robot committed
118 119
    ENDIF()
  ELSE()
Kitware Robot's avatar
Kitware Robot committed
120 121 122
    IF(${var}_COMPILED)
      IF(${var})
        SET(${var} 0)
Kitware Robot's avatar
Kitware Robot committed
123
      ELSE()
Kitware Robot's avatar
Kitware Robot committed
124
        SET(${var} 1)
Kitware Robot's avatar
Kitware Robot committed
125 126
      ENDIF()
    ELSE()
Kitware Robot's avatar
Kitware Robot committed
127
      SET(${var} 0)
Kitware Robot's avatar
Kitware Robot committed
128 129 130
    ENDIF()
  ENDIF()
ENDMACRO()
Kitware Robot's avatar
Kitware Robot committed
131 132 133 134 135 136 137

MACRO(KWSYS_PLATFORM_C_TEST var description invert)
  SET(KWSYS_PLATFORM_TEST_DEFINES ${KWSYS_PLATFORM_C_TEST_DEFINES})
  SET(KWSYS_PLATFORM_TEST_EXTRA_FLAGS ${KWSYS_PLATFORM_C_TEST_EXTRA_FLAGS})
  KWSYS_PLATFORM_TEST(C "${var}" "${description}" "${invert}")
  SET(KWSYS_PLATFORM_TEST_DEFINES)
  SET(KWSYS_PLATFORM_TEST_EXTRA_FLAGS)
Kitware Robot's avatar
Kitware Robot committed
138
ENDMACRO()
Kitware Robot's avatar
Kitware Robot committed
139 140 141 142 143 144 145

MACRO(KWSYS_PLATFORM_C_TEST_RUN var description invert)
  SET(KWSYS_PLATFORM_TEST_DEFINES ${KWSYS_PLATFORM_C_TEST_DEFINES})
  SET(KWSYS_PLATFORM_TEST_EXTRA_FLAGS ${KWSYS_PLATFORM_C_TEST_EXTRA_FLAGS})
  KWSYS_PLATFORM_TEST_RUN(C "${var}" "${description}" "${invert}")
  SET(KWSYS_PLATFORM_TEST_DEFINES)
  SET(KWSYS_PLATFORM_TEST_EXTRA_FLAGS)
Kitware Robot's avatar
Kitware Robot committed
146
ENDMACRO()
Kitware Robot's avatar
Kitware Robot committed
147 148 149 150

MACRO(KWSYS_PLATFORM_CXX_TEST var description invert)
  SET(KWSYS_PLATFORM_TEST_DEFINES ${KWSYS_PLATFORM_CXX_TEST_DEFINES})
  SET(KWSYS_PLATFORM_TEST_EXTRA_FLAGS ${KWSYS_PLATFORM_CXX_TEST_EXTRA_FLAGS})
Kitware Robot's avatar
Kitware Robot committed
151
  SET(KWSYS_PLATFORM_TEST_LINK_LIBRARIES ${KWSYS_PLATFORM_CXX_TEST_LINK_LIBRARIES})
Kitware Robot's avatar
Kitware Robot committed
152 153 154
  KWSYS_PLATFORM_TEST(CXX "${var}" "${description}" "${invert}")
  SET(KWSYS_PLATFORM_TEST_DEFINES)
  SET(KWSYS_PLATFORM_TEST_EXTRA_FLAGS)
Kitware Robot's avatar
Kitware Robot committed
155
  SET(KWSYS_PLATFORM_TEST_LINK_LIBRARIES)
Kitware Robot's avatar
Kitware Robot committed
156
ENDMACRO()
Kitware Robot's avatar
Kitware Robot committed
157 158 159 160 161 162 163

MACRO(KWSYS_PLATFORM_CXX_TEST_RUN var description invert)
  SET(KWSYS_PLATFORM_TEST_DEFINES ${KWSYS_PLATFORM_CXX_TEST_DEFINES})
  SET(KWSYS_PLATFORM_TEST_EXTRA_FLAGS ${KWSYS_PLATFORM_CXX_TEST_EXTRA_FLAGS})
  KWSYS_PLATFORM_TEST_RUN(CXX "${var}" "${description}" "${invert}")
  SET(KWSYS_PLATFORM_TEST_DEFINES)
  SET(KWSYS_PLATFORM_TEST_EXTRA_FLAGS)
Kitware Robot's avatar
Kitware Robot committed
164
ENDMACRO()
Kitware Robot's avatar
Kitware Robot committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

#-----------------------------------------------------------------------------
# KWSYS_PLATFORM_INFO_TEST(lang var description)
#
# Compile test named by ${var} and store INFO strings extracted from binary.
MACRO(KWSYS_PLATFORM_INFO_TEST lang var description)
  # We can implement this macro on CMake 2.6 and above.
  IF("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" LESS 2.6)
    SET(${var} "")
  ELSE()
    # Choose a location for the result binary.
    SET(KWSYS_PLATFORM_INFO_FILE
      ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/${var}.bin)

    # Compile the test binary.
    IF(NOT EXISTS ${KWSYS_PLATFORM_INFO_FILE})
      MESSAGE(STATUS "${description}")
      TRY_COMPILE(${var}_COMPILED
        ${CMAKE_CURRENT_BINARY_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/${KWSYS_PLATFORM_TEST_FILE_${lang}}
        COMPILE_DEFINITIONS -DTEST_${var}
          ${KWSYS_PLATFORM_${lang}_TEST_DEFINES}
          ${KWSYS_PLATFORM_${lang}_TEST_EXTRA_FLAGS}
        OUTPUT_VARIABLE OUTPUT
        COPY_FILE ${KWSYS_PLATFORM_INFO_FILE}
        )
      IF(${var}_COMPILED)
        FILE(APPEND
          ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
          "${description} compiled with the following output:\n${OUTPUT}\n\n")
      ELSE()
        FILE(APPEND
          ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
          "${description} failed to compile with the following output:\n${OUTPUT}\n\n")
      ENDIF()
      IF(${var}_COMPILED)
        MESSAGE(STATUS "${description} - compiled")
      ELSE()
        MESSAGE(STATUS "${description} - failed")
      ENDIF()
    ENDIF()

    # Parse info strings out of the compiled binary.
    IF(${var}_COMPILED)
      FILE(STRINGS ${KWSYS_PLATFORM_INFO_FILE} ${var} REGEX "INFO:[A-Za-z0-9]+\\[[^]]*\\]")
    ELSE()
      SET(${var} "")
    ENDIF()

    SET(KWSYS_PLATFORM_INFO_FILE)
  ENDIF()
ENDMACRO()