1 # try to compile a file that should compile
2 try_compile(SHOULD_PASS
3 ${try_compile_bindir_or_SOURCES}
4 ${TryCompile_SOURCE_DIR}/pass.c
5 OUTPUT_VARIABLE TRY_OUT)
6 EXPECT_PASS(SHOULD_PASS "${TRY_OUT}")
8 # try to compile a file that should compile
9 # also check that COPY_FILE works
10 try_compile(SHOULD_PASS
11 ${try_compile_bindir_or_SOURCES}
12 ${TryCompile_SOURCE_DIR}/pass.c
13 OUTPUT_VARIABLE TRY_OUT
14 COPY_FILE ${TryCompile_BINARY_DIR}/CopyOfPass
16 EXPECT_PASS(SHOULD_PASS "${TRY_OUT}")
18 if(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfPass")
19 message(SEND_ERROR "COPY_FILE to \"${TryCompile_BINARY_DIR}/CopyOfPass\" failed")
21 file(REMOVE "${TryCompile_BINARY_DIR}/CopyOfPass")
24 # try to compile a file that should compile
25 # also check that COPY_FILE_ERROR works
26 file(WRITE ${TryCompile_BINARY_DIR}/invalid "")
27 try_compile(SHOULD_PASS
28 ${try_compile_bindir_or_SOURCES}
29 ${TryCompile_SOURCE_DIR}/pass.c
30 OUTPUT_VARIABLE TRY_OUT
31 COPY_FILE ${TryCompile_BINARY_DIR}/invalid/path
32 COPY_FILE_ERROR _captured
34 EXPECT_PASS(SHOULD_PASS "${TRY_OUT}")
36 if(NOT _captured MATCHES "Cannot copy output executable.*/invalid/path")
37 message(SEND_ERROR "COPY_FILE_ERROR did not capture expected message")
40 # try to compile a file that should not compile
41 try_compile(SHOULD_FAIL
42 ${try_compile_bindir_or_SOURCES}
43 ${TryCompile_SOURCE_DIR}/fail.c
44 OUTPUT_VARIABLE TRY_OUT)
45 EXPECT_FAIL(SHOULD_FAIL "${TRY_OUT}")
47 # try to compile two files that should compile
48 try_compile(SHOULD_PASS
49 ${try_compile_bindir_or_SOURCES}
50 ${try_compile_redundant_SOURCES}
51 ${TryCompile_SOURCE_DIR}/pass2a.c
52 ${TryCompile_SOURCE_DIR}/pass2b.cxx
53 OUTPUT_VARIABLE TRY_OUT)
54 EXPECT_PASS(SHOULD_PASS "${TRY_OUT}")
56 # try to compile two files that should not compile
57 try_compile(SHOULD_FAIL
58 ${try_compile_bindir_or_SOURCES}
59 ${try_compile_redundant_SOURCES}
60 ${TryCompile_SOURCE_DIR}/fail2a.c
61 ${TryCompile_SOURCE_DIR}/fail2b.c
62 OUTPUT_VARIABLE TRY_OUT)
63 EXPECT_FAIL(SHOULD_FAIL "${TRY_OUT}")
65 # try to compile a file that should compile
66 set(_c_flags "${CMAKE_C_FLAGS}")
68 string(APPEND CMAKE_C_FLAGS " -dTESTDEF")
70 string(APPEND CMAKE_C_FLAGS " \"-DTESTDEF\"")
72 try_compile(SHOULD_PASS
73 ${try_compile_bindir_or_SOURCES}
74 ${TryCompile_SOURCE_DIR}/testdef.c
75 OUTPUT_VARIABLE TRY_OUT)
76 EXPECT_PASS(SHOULD_PASS "${TRY_OUT}")
77 set(CMAKE_C_FLAGS "${_c_flags}")
79 try_compile(CMAKE_ANSI_FOR_SCOPE
80 ${try_compile_bindir_or_SOURCES}
81 ${CMAKE_ROOT}/Modules/TestForAnsiForScope.cxx OUTPUT_VARIABLE OUT)
82 if(CMAKE_ANSI_FOR_SCOPE)
83 message("Compiler supports ansi for")
85 message("Compiler does not support ansi for scope")
88 message("use the module now")
89 include(${CMAKE_ROOT}/Modules/TestForANSIForScope.cmake)
90 if(CMAKE_ANSI_FOR_SCOPE)
91 message("Compiler supports ansi for")
93 message("Compiler does not support ansi for scope")
96 # test that COMPILE_DEFINITIONS are correctly expanded
97 try_compile(COMPILE_DEFINITIONS_LIST_EXPANDED
98 ${try_compile_bindir_or_SOURCES}
99 ${TryCompile_SOURCE_DIR}/check_a_b.c
100 OUTPUT_VARIABLE output
101 COMPILE_DEFINITIONS "-DDEF_A;-DDEF_B"
103 if(COMPILE_DEFINITIONS_LIST_EXPANDED)
104 message(STATUS "COMPILE_DEFINITIONS list expanded correctly")
106 string(REPLACE "\n" "\n " output " ${output}")
107 message(SEND_ERROR "COMPILE_DEFINITIONS list did not expand correctly\n${output}")
110 # try to compile a file that doesn't exist
111 try_compile(SHOULD_FAIL_DUE_TO_BAD_SOURCE
112 ${try_compile_bindir_or_SOURCES}
113 ${TryCompile_SOURCE_DIR}/pass.c
114 OUTPUT_VARIABLE output
115 COMPILE_DEFINITIONS "bad#source.c"
117 if(SHOULD_FAIL_DUE_TO_BAD_SOURCE AND NOT CMAKE_GENERATOR MATCHES "Watcom WMake|NMake Makefiles")
118 string(REPLACE "\n" "\n " output " ${output}")
119 message(SEND_ERROR "try_compile with bad#source.c did not fail:\n${output}")
120 elseif(NOT output MATCHES [[(bad#source\.c|bad\.c|bad[':])]])
121 string(REPLACE "\n" "\n " output " ${output}")
122 message(SEND_ERROR "try_compile with bad#source.c failed without mentioning bad source:\n${output}")
124 message(STATUS "try_compile with bad#source.c correctly failed")
128 # try to compile a file that should compile
129 try_compile(SHOULD_PASS
130 ${try_compile_bindir_or_SOURCES}
131 ${TryCompile_SOURCE_DIR}/pass.m
132 OUTPUT_VARIABLE TRY_OUT)
133 EXPECT_PASS(SHOULD_PASS "${TRY_OUT}")
135 # try to compile a file that should not compile
136 try_compile(SHOULD_FAIL
137 ${try_compile_bindir_or_SOURCES}
138 ${TryCompile_SOURCE_DIR}/fail.m
139 OUTPUT_VARIABLE TRY_OUT)
140 EXPECT_FAIL(SHOULD_FAIL "${TRY_OUT}")
143 # check that try_compile honors NO_CACHE
144 function(try_compile_scope_test)
147 ${try_compile_bindir_or_SOURCES}
148 ${TryCompile_SOURCE_DIR}/pass.c)
150 SHOULD_NOT_ESCAPE_SCOPE_RESULT
151 ${try_compile_bindir_or_SOURCES}
152 ${TryCompile_SOURCE_DIR}/pass.c
156 try_compile_scope_test()
158 if(NOT DEFINED CACHE{CACHED_RESULT})
159 message(SEND_ERROR " Result from try_compile was not cached")
161 if(DEFINED SHOULD_NOT_ESCAPE_SCOPE_RESULT)
162 message(SEND_ERROR " Result from try_compile(NO_CACHE) leaked")
165 ######################################
169 # try to run a file that should compile and run without error
170 # also check that OUTPUT_VARIABLE contains both the compile output
172 try_run(SHOULD_RUN SHOULD_COMPILE
173 ${try_compile_bindir_or_SOURCES}
174 ${TryCompile_SOURCE_DIR}/exit_success.c
175 ${try_compile_output_vars})
176 EXPECT_COMPILED("exit_success" SHOULD_COMPILE "${${try_compile_compile_output_var}}")
177 EXPECT_RUN_RESULT("exit_success" SHOULD_RUN 0)
179 # check the compile output for the filename
180 if(NOT "${${try_compile_compile_output_var}}" MATCHES "exit_success")
182 " ${try_compile_compile_output_var} didn't contain \"exit_success\":"
183 " \"${${try_compile_compile_output_var}}\"")
185 # check the run output
186 if(NOT "${${try_compile_run_output_var}}" MATCHES "hello world")
188 " ${try_compile_run_output_var} didn't contain \"hello world\":"
189 " \"${${try_compile_run_output_var}}\"")
192 try_run(ARG_TEST_RUN ARG_TEST_COMPILE
193 ${try_compile_bindir_or_SOURCES}
194 ${TryCompile_SOURCE_DIR}/expect_arg.c
195 COMPILE_OUTPUT_VARIABLE TRY_OUT
197 EXPECT_COMPILED("expect_arg" ARG_TEST_COMPILE "${TRY_OUT}")
198 EXPECT_RUN_RESULT("expect_arg" ARG_TEST_RUN 0)
200 # try to run a file that should compile and run, but return an error
201 try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
202 ${try_compile_bindir_or_SOURCES}
203 ${TryCompile_SOURCE_DIR}/exit_with_error.c
204 COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
205 RUN_OUTPUT_VARIABLE RUN_OUTPUT)
206 EXPECT_COMPILED("exit_with_error" SHOULD_COMPILE "${COMPILE_OUTPUT}")
207 EXPECT_RUN_RESULT("exit_with_error" SHOULD_EXIT_WITH_ERROR 1)
209 # check the compile output, it should contain the filename
210 if(NOT "${COMPILE_OUTPUT}" MATCHES "exit_with_error")
211 message(SEND_ERROR " COMPILE_OUT didn't contain \"exit_with_error\": \"${COMPILE_OUTPUT}\"")
213 #... but not the run time output
214 if("${COMPILE_OUTPUT}" MATCHES "hello world")
215 message(SEND_ERROR " COMPILE_OUT contains the run output: \"${COMPILE_OUTPUT}\"")
217 # check the run output, it should contain stdout
218 if(NOT "${RUN_OUTPUT}" MATCHES "hello world")
219 message(SEND_ERROR " RUN_OUTPUT didn't contain \"hello world\": \"${RUN_OUTPUT}\"")
222 # try to run a file and parse stdout and stderr separately
223 # also check that COPY_FILE works
224 try_run(SHOULD_EXIT_WITH_ERROR SHOULD_COMPILE
225 ${try_compile_bindir_or_SOURCES}
226 ${TryCompile_SOURCE_DIR}/stdout_and_stderr.c
227 COPY_FILE ${TryCompile_BINARY_DIR}/CopyOfRun
228 COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT
229 RUN_OUTPUT_STDOUT_VARIABLE RUN_OUTPUT_STDOUT
230 RUN_OUTPUT_STDERR_VARIABLE RUN_OUTPUT_STDERR)
231 EXPECT_PASS(SHOULD_COMPILE "${COMPILE_OUTPUT}")
233 if(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfRun")
234 message(SEND_ERROR "COPY_FILE to \"${TryCompile_BINARY_DIR}/CopyOfRun\" failed")
236 file(REMOVE "${TryCompile_BINARY_DIR}/CopyOfRun")
239 # check the run stdout output
240 if(NOT "${RUN_OUTPUT_STDOUT}" MATCHES "hello world")
241 message(SEND_ERROR " RUN_OUTPUT_STDOUT didn't contain \"hello world\": \"${RUN_OUTPUT_STDOUT}\"")
243 # check the run stderr output
244 if(NOT "${RUN_OUTPUT_STDERR}" MATCHES "error")
245 message(SEND_ERROR " RUN_OUTPUT_STDERR didn't contain \"error\": \"${RUN_OUTPUT_STDERR}\"")
248 # check that try_run honors NO_CACHE
249 function(try_run_scope_test)
252 CACHED_COMPILE_RESULT
253 ${try_compile_bindir_or_SOURCES}
254 ${TryCompile_SOURCE_DIR}/exit_success.c)
256 SHOULD_NOT_ESCAPE_SCOPE_RUN_RESULT
257 SHOULD_NOT_ESCAPE_SCOPE_COMPILE_RESULT
258 ${try_compile_bindir_or_SOURCES}
259 ${TryCompile_SOURCE_DIR}/exit_success.c
265 if(NOT DEFINED CACHE{CACHED_COMPILE_RESULT})
266 message(SEND_ERROR " Compile result from try_run was not cached")
268 if(NOT DEFINED CACHE{CACHED_RUN_RESULT})
269 message(SEND_ERROR " Run result from try_run was not cached")
271 if(DEFINED SHOULD_NOT_ESCAPE_SCOPE_COMPILE_RESULT)
272 message(SEND_ERROR " Compile result from try_run(NO_CACHE) leaked")
274 if(DEFINED SHOULD_NOT_ESCAPE_SCOPE_RUN_RESULT)
275 message(SEND_ERROR " Run result from try_run(NO_CACHE) leaked")