3 MACRO(TEST_ASSERT value msg)
5 MESSAGE (SEND_ERROR "Assertion failure:" ${msg} )
9 MACRO(TEST_FAIL value msg)
11 MESSAGE (SEND_ERROR "Failing test succeeded:" ${msg} )
15 MACRO(TEST_EXPECT_EXACT command expected)
16 IF(NOT "x${result}" STREQUAL "x${expected}")
17 MESSAGE(SEND_ERROR "${CMAKE_CURRENT_LIST_LINE}: TEST \"${command}\" failed: \"${result}\" expected: \"${expected}\"")
18 ENDIF(NOT "x${result}" STREQUAL "x${expected}")
19 ENDMACRO(TEST_EXPECT_EXACT command expected)
21 MACRO(TEST_EXPECT_CONTAINS command expected)
22 IF(NOT "${result}" MATCHES "${expected}")
23 MESSAGE(SEND_ERROR "${CMAKE_CURRENT_LIST_LINE}: TEST \"${command}\" failed: \"${result}\" expected: \"${expected}\"")
24 ENDIF(NOT "${result}" MATCHES "${expected}")
25 ENDMACRO(TEST_EXPECT_CONTAINS command expected)
27 # try to compile a file that should compile
28 # also check that COPY_FILE works
29 TRY_COMPILE(SHOULD_PASS
30 ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
31 ${TryCompile_SOURCE_DIR}/pass.c
32 OUTPUT_VARIABLE TRY_OUT
33 COPY_FILE ${TryCompile_BINARY_DIR}/CopyOfPass
37 MESSAGE(SEND_ERROR "should pass failed ${TRY_OUT}")
38 ENDIF(NOT SHOULD_PASS)
39 IF(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfPass")
40 MESSAGE(SEND_ERROR "COPY_FILE to \"${TryCompile_BINARY_DIR}/CopyOfPass\" failed")
41 ELSE(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfPass")
42 FILE(REMOVE "${TryCompile_BINARY_DIR}/CopyOfPass")
43 ENDIF(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfPass")
45 # try to compile a file that should not compile
46 TRY_COMPILE(SHOULD_FAIL
47 ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
48 ${TryCompile_SOURCE_DIR}/fail.c
49 OUTPUT_VARIABLE TRY_OUT)
51 MESSAGE(SEND_ERROR "Should fail passed ${TRY_OUT}")
54 # try to compile a file that should compile
55 TRY_COMPILE(SHOULD_PASS
56 ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
57 ${TryCompile_SOURCE_DIR}/pass.c
58 OUTPUT_VARIABLE TRY_OUT)
60 MESSAGE(SEND_ERROR "should pass failed ${TRY_OUT}")
61 ENDIF(NOT SHOULD_PASS)
63 # try to compile a file that should not compile
64 TRY_COMPILE(SHOULD_FAIL
65 ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
66 ${TryCompile_SOURCE_DIR}/fail.c
67 OUTPUT_VARIABLE TRY_OUT)
69 MESSAGE(SEND_ERROR "Should fail passed ${TRY_OUT}")
74 MESSAGE("All Tests passed, ignore all previous output.")
76 MESSAGE("Test failed")
79 MESSAGE("Test failed")
80 ENDIF(NOT SHOULD_FAIL)
81 TRY_COMPILE(CMAKE_ANSI_FOR_SCOPE
82 ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
83 ${CMAKE_ROOT}/Modules/TestForAnsiForScope.cxx OUTPUT_VARIABLE OUT)
84 IF (CMAKE_ANSI_FOR_SCOPE)
85 MESSAGE("Compiler supports ansi for")
86 ELSE(CMAKE_ANSI_FOR_SCOPE)
87 MESSAGE("Compiler does not support ansi for scope")
88 ENDIF(CMAKE_ANSI_FOR_SCOPE)
90 TRY_COMPILE(CMAKE_ANSI_FOR_SCOPE
91 ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
92 ${CMAKE_ROOT}/Modules/TestForAnsiForScope.cxx OUTPUT_VARIABLE OUT)
93 IF (CMAKE_ANSI_FOR_SCOPE)
94 MESSAGE("Compiler supports ansi for")
95 ELSE(CMAKE_ANSI_FOR_SCOPE)
96 MESSAGE("Compiler does not support ansi for scope")
97 ENDIF(CMAKE_ANSI_FOR_SCOPE)
99 MESSAGE("use the module now")
100 INCLUDE(${CMAKE_ROOT}/Modules/TestForANSIForScope.cmake)
101 IF (CMAKE_ANSI_FOR_SCOPE)
102 MESSAGE("Compiler supports ansi for")
103 ELSE(CMAKE_ANSI_FOR_SCOPE)
104 MESSAGE("Compiler does not support ansi for scope")
105 ENDIF(CMAKE_ANSI_FOR_SCOPE)
107 ADD_EXECUTABLE(TryCompile pass.c)
109 ######################################
111 # now two tests for TRY_RUN
113 # try to run a file that should compile and run without error
114 # also check that OUTPUT_VARIABLE contains both the compile output
116 TRY_RUN(SHOULD_RUN SHOULD_COMPILE
117 ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
118 ${TryCompile_SOURCE_DIR}/exit_success.c
119 OUTPUT_VARIABLE TRY_OUT)
120 IF(NOT SHOULD_COMPILE)
121 MESSAGE(SEND_ERROR "exit_success failed compiling: ${TRY_OUT}")
122 ENDIF(NOT SHOULD_COMPILE)
123 IF(NOT "${SHOULD_RUN}" STREQUAL "0")
124 MESSAGE(SEND_ERROR "exit_success failed running with exit code ${SHOULD_RUN}")
125 ENDIF(NOT "${SHOULD_RUN}" STREQUAL "0")
126 # check the compile output for the filename
127 IF(NOT "${TRY_OUT}" MATCHES "exit_success")
128 MESSAGE(SEND_ERROR " TRY_OUT didn't contain \"exit_success\": \"${TRY_OUT}\"")
129 ENDIF(NOT "${TRY_OUT}" MATCHES "exit_success")
130 # check the run output
131 IF(NOT "${TRY_OUT}" MATCHES "hello world")
132 MESSAGE(SEND_ERROR " TRY_OUT didn't contain \"hello world\": \"${TRY_OUT}\"")
133 ENDIF(NOT "${TRY_OUT}" MATCHES "hello world")
136 # try to run a file that should compile and run, but return an error
137 TRY_RUN(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
138 ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
139 ${TryCompile_SOURCE_DIR}/exit_with_error.c
140 COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
141 RUN_OUTPUT_VARIABLE RUN_OUTPUT)
143 IF(NOT SHOULD_COMPILE)
144 MESSAGE(STATUS " exit_with_error failed compiling: ${COMPILE_OUTPUT}")
145 ENDIF(NOT SHOULD_COMPILE)
146 IF("${SHOULD_EXIT_WITH_ERROR}" STREQUAL "0")
147 MESSAGE(SEND_ERROR " exit_with_error passed with exit code ${SHOULD_EXIT_WITH_ERROR}")
148 ENDIF("${SHOULD_EXIT_WITH_ERROR}" STREQUAL "0")
150 # check the compile output, it should contain the filename
151 IF(NOT "${COMPILE_OUTPUT}" MATCHES "exit_with_error")
152 MESSAGE(SEND_ERROR " COMPILE_OUT didn't contain \"exit_with_error\": \"${COMPILE_OUTPUT}\"")
153 ENDIF(NOT "${COMPILE_OUTPUT}" MATCHES "exit_with_error")
154 #... but not the run time output
155 IF("${COMPILE_OUTPUT}" MATCHES "hello world")
156 MESSAGE(SEND_ERROR " COMPILE_OUT contains the run output: \"${COMPILE_OUTPUT}\"")
157 ENDIF("${COMPILE_OUTPUT}" MATCHES "hello world")
158 # check the run output, it should stdout
159 IF(NOT "${RUN_OUTPUT}" MATCHES "hello world")
160 MESSAGE(SEND_ERROR " RUN_OUTPUT didn't contain \"hello world\": \"${RUN_OUTPUT}\"")
161 ENDIF(NOT "${RUN_OUTPUT}" MATCHES "hello world")
163 #######################################################################
165 # also test that the CHECK_C_SOURCE_COMPILES, CHECK_CXX_SOURCE_COMPILES
166 # CHECK_C_SOURCE_RUNS and CHECK_CXX_SOURCE_RUNS macros work
168 INCLUDE(CheckCSourceCompiles)
169 INCLUDE(CheckCXXSourceCompiles)
170 INCLUDE(CheckCSourceRuns)
171 INCLUDE(CheckCXXSourceRuns)
173 CHECK_C_SOURCE_COMPILES("I dont build" C_BUILD_SHOULD_FAIL)
174 CHECK_C_SOURCE_COMPILES("int main() {return 0;}" C_BUILD_SHOULD_WORK)
175 CHECK_C_SOURCE_RUNS("int main() {return 1;}" C_RUN_SHOULD_FAIL)
176 CHECK_C_SOURCE_RUNS("int main() {return 0;}" C_RUN_SHOULD_WORK)
178 TEST_FAIL(C_BUILD_SHOULD_FAIL "CHECK_C_SOURCE_COMPILES() succeeded, but should have failed")
179 TEST_ASSERT(C_BUILD_SHOULD_WORK "CHECK_C_SOURCE_COMPILES() failed")
180 TEST_FAIL(C_RUN_SHOULD_FAIL "CHECK_C_SOURCE_RUNS() succeeded, but should have failed")
181 TEST_ASSERT(C_RUN_SHOULD_WORK "CHECK_C_SOURCE_RUNS() failed")
183 CHECK_CXX_SOURCE_COMPILES("I dont build" CXX_BUILD_SHOULD_FAIL)
184 CHECK_CXX_SOURCE_COMPILES("int main() {return 0;}" CXX_BUILD_SHOULD_WORK)
185 CHECK_CXX_SOURCE_RUNS("int main() {return 2;}" CXX_RUN_SHOULD_FAIL)
186 CHECK_CXX_SOURCE_RUNS("int main() {return 0;}" CXX_RUN_SHOULD_WORK)
188 TEST_FAIL(CXX_BUILD_SHOULD_FAIL "CHECK_CXX_SOURCE_COMPILES() succeeded, but should have failed")
189 TEST_ASSERT(CXX_BUILD_SHOULD_WORK "CHECK_CXX_SOURCE_COMPILES() failed")
190 TEST_FAIL(CXX_RUN_SHOULD_FAIL "CHECK_CXX_SOURCE_RUNS() succeeded, but should have failed")
191 TEST_ASSERT(CXX_RUN_SHOULD_WORK "CHECK_CXX_SOURCE_RUNS() failed")