Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Tests / TryCompile / old_and_new_signature_tests.cmake
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}")
7
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
15   )
16 EXPECT_PASS(SHOULD_PASS "${TRY_OUT}")
17
18 if(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfPass")
19   message(SEND_ERROR "COPY_FILE to \"${TryCompile_BINARY_DIR}/CopyOfPass\" failed")
20 else()
21   file(REMOVE "${TryCompile_BINARY_DIR}/CopyOfPass")
22 endif()
23
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
33   )
34 EXPECT_PASS(SHOULD_PASS "${TRY_OUT}")
35
36 if(NOT _captured MATCHES "Cannot copy output executable.*/invalid/path")
37   message(SEND_ERROR "COPY_FILE_ERROR did not capture expected message")
38 endif()
39
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}")
46
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}")
55
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}")
64
65 # try to compile a file that should compile
66 set(_c_flags "${CMAKE_C_FLAGS}")
67 if(WATCOM)
68   string(APPEND CMAKE_C_FLAGS " -dTESTDEF")
69 else()
70   string(APPEND CMAKE_C_FLAGS " \"-DTESTDEF\"")
71 endif()
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}")
78
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")
84 else()
85   message("Compiler does not support ansi for scope")
86 endif()
87
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")
92 else()
93   message("Compiler does not support ansi for scope")
94 endif()
95
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"
102     )
103 if(COMPILE_DEFINITIONS_LIST_EXPANDED)
104   message(STATUS "COMPILE_DEFINITIONS list expanded correctly")
105 else()
106   string(REPLACE "\n" "\n  " output "  ${output}")
107   message(SEND_ERROR "COMPILE_DEFINITIONS list did not expand correctly\n${output}")
108 endif()
109
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"
116     )
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}")
123 else()
124   message(STATUS "try_compile with bad#source.c correctly failed")
125 endif()
126
127 if(APPLE)
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}")
134
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}")
141 endif()
142
143 # check that try_compile honors NO_CACHE
144 function(try_compile_scope_test)
145   try_compile(
146     CACHED_RESULT
147     ${try_compile_bindir_or_SOURCES}
148     ${TryCompile_SOURCE_DIR}/pass.c)
149   try_compile(
150     SHOULD_NOT_ESCAPE_SCOPE_RESULT
151     ${try_compile_bindir_or_SOURCES}
152     ${TryCompile_SOURCE_DIR}/pass.c
153     NO_CACHE)
154 endfunction()
155
156 try_compile_scope_test()
157
158 if(NOT DEFINED CACHE{CACHED_RESULT})
159   message(SEND_ERROR " Result from try_compile was not cached")
160 endif()
161 if(DEFINED SHOULD_NOT_ESCAPE_SCOPE_RESULT)
162   message(SEND_ERROR " Result from try_compile(NO_CACHE) leaked")
163 endif()
164
165 ######################################
166
167 # now test try_run()
168
169 # try to run a file that should compile and run without error
170 # also check that OUTPUT_VARIABLE contains both the compile output
171 # and the run 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)
178
179 # check the compile output for the filename
180 if(NOT "${${try_compile_compile_output_var}}" MATCHES "exit_success")
181   message(SEND_ERROR
182     " ${try_compile_compile_output_var} didn't contain \"exit_success\":"
183     " \"${${try_compile_compile_output_var}}\"")
184 endif()
185 # check the run output
186 if(NOT "${${try_compile_run_output_var}}" MATCHES "hello world")
187   message(SEND_ERROR
188     " ${try_compile_run_output_var} didn't contain \"hello world\":"
189     " \"${${try_compile_run_output_var}}\"")
190 endif()
191
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
196     ARGS arg1 arg2)
197 EXPECT_COMPILED("expect_arg" ARG_TEST_COMPILE "${TRY_OUT}")
198 EXPECT_RUN_RESULT("expect_arg" ARG_TEST_RUN 0)
199
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)
208
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}\"")
212 endif()
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}\"")
216 endif()
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}\"")
220 endif()
221
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}")
232
233 if(NOT EXISTS "${TryCompile_BINARY_DIR}/CopyOfRun")
234   message(SEND_ERROR "COPY_FILE to \"${TryCompile_BINARY_DIR}/CopyOfRun\" failed")
235 else()
236   file(REMOVE "${TryCompile_BINARY_DIR}/CopyOfRun")
237 endif()
238
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}\"")
242 endif()
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}\"")
246 endif()
247
248 # check that try_run honors NO_CACHE
249 function(try_run_scope_test)
250   try_run(
251     CACHED_RUN_RESULT
252     CACHED_COMPILE_RESULT
253     ${try_compile_bindir_or_SOURCES}
254     ${TryCompile_SOURCE_DIR}/exit_success.c)
255   try_run(
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
260     NO_CACHE)
261 endfunction()
262
263 try_run_scope_test()
264
265 if(NOT DEFINED CACHE{CACHED_COMPILE_RESULT})
266   message(SEND_ERROR " Compile result from try_run was not cached")
267 endif()
268 if(NOT DEFINED CACHE{CACHED_RUN_RESULT})
269   message(SEND_ERROR " Run result from try_run was not cached")
270 endif()
271 if(DEFINED SHOULD_NOT_ESCAPE_SCOPE_COMPILE_RESULT)
272   message(SEND_ERROR " Compile result from try_run(NO_CACHE) leaked")
273 endif()
274 if(DEFINED SHOULD_NOT_ESCAPE_SCOPE_RUN_RESULT)
275   message(SEND_ERROR " Run result from try_run(NO_CACHE) leaked")
276 endif()