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