kwsysPlatformTests.cmake 8.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#=============================================================================
# KWSys - Kitware System Library
# Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
#
# 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
SET(KWSYS_PLATFORM_TEST_FILE_C kwsysPlatformTestsC.c)
13
14
15
SET(KWSYS_PLATFORM_TEST_FILE_CXX kwsysPlatformTestsCXX.cxx)

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

62
MACRO(KWSYS_PLATFORM_TEST_RUN lang var description invert)
63
  IF(NOT DEFINED ${var})
64
65
66
    MESSAGE(STATUS "${description}")
    TRY_RUN(${var} ${var}_COMPILED
      ${CMAKE_CURRENT_BINARY_DIR}
67
68
      ${CMAKE_CURRENT_SOURCE_DIR}/${KWSYS_PLATFORM_TEST_FILE_${lang}}
      COMPILE_DEFINITIONS -DTEST_${var} ${KWSYS_PLATFORM_TEST_DEFINES} ${KWSYS_PLATFORM_TEST_EXTRA_FLAGS}
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
      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")
      ELSE(${var})
        FILE(APPEND
          ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
          "${description} compiled and ran with the following output:\n${OUTPUT}\n\n")
      ENDIF(${var})
    ELSE(${var}_COMPILED)
      FILE(APPEND
        ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
        "${description} failed to compile with the following output:\n${OUTPUT}\n\n")
86
      SET(${var} -1 CACHE INTERNAL "${description} failed to compile.")
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    ENDIF(${var}_COMPILED)

    IF(${invert} MATCHES INVERT)
      IF(${var}_COMPILED)
        IF(${var})
          MESSAGE(STATUS "${description} - yes")
        ELSE(${var})
          MESSAGE(STATUS "${description} - no")
        ENDIF(${var})
      ELSE(${var}_COMPILED)
        MESSAGE(STATUS "${description} - failed to compile")
      ENDIF(${var}_COMPILED)
    ELSE(${invert} MATCHES INVERT)
      IF(${var}_COMPILED)
        IF(${var})
          MESSAGE(STATUS "${description} - no")
        ELSE(${var})
          MESSAGE(STATUS "${description} - yes")
        ENDIF(${var})
      ELSE(${var}_COMPILED)
        MESSAGE(STATUS "${description} - failed to compile")
      ENDIF(${var}_COMPILED)
    ENDIF(${invert} MATCHES INVERT)
110
  ENDIF()
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

  IF(${invert} MATCHES INVERT)
    IF(${var}_COMPILED)
      IF(${var})
        SET(${var} 1)
      ELSE(${var})
        SET(${var} 0)
      ENDIF(${var})
    ELSE(${var}_COMPILED)
      SET(${var} 1)
    ENDIF(${var}_COMPILED)
  ELSE(${invert} MATCHES INVERT)
    IF(${var}_COMPILED)
      IF(${var})
        SET(${var} 0)
      ELSE(${var})
        SET(${var} 1)
      ENDIF(${var})
    ELSE(${var}_COMPILED)
      SET(${var} 0)
    ENDIF(${var}_COMPILED)
  ENDIF(${invert} MATCHES INVERT)
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
ENDMACRO(KWSYS_PLATFORM_TEST_RUN)

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)
ENDMACRO(KWSYS_PLATFORM_C_TEST)

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)
ENDMACRO(KWSYS_PLATFORM_C_TEST_RUN)

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})
154
  SET(KWSYS_PLATFORM_TEST_LINK_LIBRARIES ${KWSYS_PLATFORM_CXX_TEST_LINK_LIBRARIES})
155
156
157
  KWSYS_PLATFORM_TEST(CXX "${var}" "${description}" "${invert}")
  SET(KWSYS_PLATFORM_TEST_DEFINES)
  SET(KWSYS_PLATFORM_TEST_EXTRA_FLAGS)
158
  SET(KWSYS_PLATFORM_TEST_LINK_LIBRARIES)
159
160
161
162
163
164
165
166
ENDMACRO(KWSYS_PLATFORM_CXX_TEST)

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)
167
ENDMACRO(KWSYS_PLATFORM_CXX_TEST_RUN)
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182

#-----------------------------------------------------------------------------
# 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.
183
    IF(NOT EXISTS ${KWSYS_PLATFORM_INFO_FILE})
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
217
218
219
      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()