kwsysPlatformTests.cmake 7.09 KB
Newer Older
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.

4
SET(KWSYS_PLATFORM_TEST_FILE_C kwsysPlatformTestsC.c)
5 6 7
SET(KWSYS_PLATFORM_TEST_FILE_CXX kwsysPlatformTestsCXX.cxx)

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

54
MACRO(KWSYS_PLATFORM_TEST_RUN lang var description invert)
55
  IF(NOT DEFINED ${var})
56 57 58
    MESSAGE(STATUS "${description}")
    TRY_RUN(${var} ${var}_COMPILED
      ${CMAKE_CURRENT_BINARY_DIR}
59 60
      ${CMAKE_CURRENT_SOURCE_DIR}/${KWSYS_PLATFORM_TEST_FILE_${lang}}
      COMPILE_DEFINITIONS -DTEST_${var} ${KWSYS_PLATFORM_TEST_DEFINES} ${KWSYS_PLATFORM_TEST_EXTRA_FLAGS}
61 62 63 64 65 66 67 68
      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")
69
      ELSE()
70 71 72
        FILE(APPEND
          ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
          "${description} compiled and ran with the following output:\n${OUTPUT}\n\n")
73 74
      ENDIF()
    ELSE()
75 76 77
      FILE(APPEND
        ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
        "${description} failed to compile with the following output:\n${OUTPUT}\n\n")
78
      SET(${var} -1 CACHE INTERNAL "${description} failed to compile.")
79
    ENDIF()
80 81 82 83 84

    IF(${invert} MATCHES INVERT)
      IF(${var}_COMPILED)
        IF(${var})
          MESSAGE(STATUS "${description} - yes")
85
        ELSE()
86
          MESSAGE(STATUS "${description} - no")
87 88
        ENDIF()
      ELSE()
89
        MESSAGE(STATUS "${description} - failed to compile")
90 91
      ENDIF()
    ELSE()
92 93 94
      IF(${var}_COMPILED)
        IF(${var})
          MESSAGE(STATUS "${description} - no")
95
        ELSE()
96
          MESSAGE(STATUS "${description} - yes")
97 98
        ENDIF()
      ELSE()
99
        MESSAGE(STATUS "${description} - failed to compile")
100 101
      ENDIF()
    ENDIF()
102
  ENDIF()
103 104 105 106 107

  IF(${invert} MATCHES INVERT)
    IF(${var}_COMPILED)
      IF(${var})
        SET(${var} 1)
108
      ELSE()
109
        SET(${var} 0)
110 111
      ENDIF()
    ELSE()
112
      SET(${var} 1)
113 114
    ENDIF()
  ELSE()
115 116 117
    IF(${var}_COMPILED)
      IF(${var})
        SET(${var} 0)
118
      ELSE()
119
        SET(${var} 1)
120 121
      ENDIF()
    ELSE()
122
      SET(${var} 0)
123 124 125
    ENDIF()
  ENDIF()
ENDMACRO()
126 127 128 129 130 131 132

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)
133
ENDMACRO()
134 135 136 137 138 139 140

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)
141
ENDMACRO()
142 143 144 145

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})
146
  SET(KWSYS_PLATFORM_TEST_LINK_LIBRARIES ${KWSYS_PLATFORM_CXX_TEST_LINK_LIBRARIES})
147 148 149
  KWSYS_PLATFORM_TEST(CXX "${var}" "${description}" "${invert}")
  SET(KWSYS_PLATFORM_TEST_DEFINES)
  SET(KWSYS_PLATFORM_TEST_EXTRA_FLAGS)
150
  SET(KWSYS_PLATFORM_TEST_LINK_LIBRARIES)
151
ENDMACRO()
152 153 154 155 156 157 158

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)
159
ENDMACRO()
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

#-----------------------------------------------------------------------------
# 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.
175
    IF(NOT EXISTS ${KWSYS_PLATFORM_INFO_FILE})
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
      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()