Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Tests / RunCMake / RunCMake.cmake
1 foreach(
2   arg
3   IN ITEMS
4     RunCMake_GENERATOR
5     RunCMake_SOURCE_DIR
6     RunCMake_BINARY_DIR
7   )
8   if(NOT DEFINED ${arg})
9     message(FATAL_ERROR "${arg} not given!")
10   endif()
11 endforeach()
12
13 function(run_cmake test)
14   if(DEFINED ENV{RunCMake_TEST_FILTER} AND NOT test MATCHES "$ENV{RunCMake_TEST_FILTER}")
15     return()
16   endif()
17
18   set(top_src "${RunCMake_SOURCE_DIR}")
19   set(top_bin "${RunCMake_BINARY_DIR}")
20   if(EXISTS ${top_src}/${test}-result.txt)
21     file(READ ${top_src}/${test}-result.txt expect_result)
22     string(REGEX REPLACE "\n+$" "" expect_result "${expect_result}")
23   else()
24     set(expect_result 0)
25   endif()
26
27   string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} platform_name)
28   #remove all additional bits from cygwin/msys name
29   if(platform_name MATCHES cygwin)
30     set(platform_name cygwin)
31   endif()
32   if(platform_name MATCHES msys)
33     set(platform_name msys)
34   endif()
35
36   foreach(o IN ITEMS out err)
37     if(RunCMake-std${o}-file AND EXISTS ${top_src}/${RunCMake-std${o}-file})
38       file(READ ${top_src}/${RunCMake-std${o}-file} expect_std${o})
39       string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
40     elseif(EXISTS ${top_src}/${test}-std${o}-${platform_name}.txt)
41       file(READ ${top_src}/${test}-std${o}-${platform_name}.txt expect_std${o})
42       string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
43     elseif(EXISTS ${top_src}/${test}-std${o}.txt)
44       file(READ ${top_src}/${test}-std${o}.txt expect_std${o})
45       string(REGEX REPLACE "\n+$" "" expect_std${o} "${expect_std${o}}")
46     else()
47       unset(expect_std${o})
48     endif()
49   endforeach()
50   if (NOT expect_stderr)
51     if (NOT RunCMake_DEFAULT_stderr)
52       set(RunCMake_DEFAULT_stderr "^$")
53     endif()
54     set(expect_stderr ${RunCMake_DEFAULT_stderr})
55   endif()
56
57   if (NOT RunCMake_TEST_SOURCE_DIR)
58     set(RunCMake_TEST_SOURCE_DIR "${top_src}")
59   endif()
60   if(NOT RunCMake_TEST_BINARY_DIR)
61     set(RunCMake_TEST_BINARY_DIR "${top_bin}/${test}-build")
62   endif()
63   if(NOT RunCMake_TEST_NO_CLEAN)
64     file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
65   endif()
66   file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
67   if(RunCMake-prep-file AND EXISTS ${top_src}/${RunCMake-prep-file})
68     include(${top_src}/${RunCMake-prep-file})
69   else()
70     include(${top_src}/${test}-prep.cmake OPTIONAL)
71   endif()
72   if(RunCMake_TEST_OUTPUT_MERGE)
73     set(actual_stderr_var actual_stdout)
74     set(actual_stderr "")
75   else()
76     set(actual_stderr_var actual_stderr)
77   endif()
78   if(DEFINED RunCMake_TEST_TIMEOUT)
79     set(maybe_timeout TIMEOUT ${RunCMake_TEST_TIMEOUT})
80   else()
81     set(maybe_timeout "")
82   endif()
83   if(RunCMake-stdin-file AND EXISTS ${top_src}/${RunCMake-stdin-file})
84     set(maybe_input_file INPUT_FILE ${top_src}/${RunCMake-stdin-file})
85   elseif(EXISTS ${top_src}/${test}-stdin.txt)
86     set(maybe_input_file INPUT_FILE ${top_src}/${test}-stdin.txt)
87   else()
88     set(maybe_input_file "")
89   endif()
90   if(NOT RunCMake_TEST_COMMAND)
91     if(NOT DEFINED RunCMake_TEST_OPTIONS)
92       set(RunCMake_TEST_OPTIONS "")
93     endif()
94     if(APPLE)
95       list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0025=NEW)
96     endif()
97     if(NOT RunCMake_TEST_NO_CMP0129 AND CMAKE_C_COMPILER_ID STREQUAL "LCC")
98       list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0129=NEW)
99     endif()
100     if(RunCMake_MAKE_PROGRAM)
101       list(APPEND RunCMake_TEST_OPTIONS "-DCMAKE_MAKE_PROGRAM=${RunCMake_MAKE_PROGRAM}")
102     endif()
103     set(RunCMake_TEST_COMMAND ${CMAKE_COMMAND})
104     if(NOT RunCMake_TEST_NO_SOURCE_DIR)
105       list(APPEND RunCMake_TEST_COMMAND "${RunCMake_TEST_SOURCE_DIR}")
106     endif()
107     list(APPEND RunCMake_TEST_COMMAND -G "${RunCMake_GENERATOR}")
108     if(RunCMake_GENERATOR_PLATFORM)
109       list(APPEND RunCMake_TEST_COMMAND -A "${RunCMake_GENERATOR_PLATFORM}")
110     endif()
111     if(RunCMake_GENERATOR_TOOLSET)
112       list(APPEND RunCMake_TEST_COMMAND -T "${RunCMake_GENERATOR_TOOLSET}")
113     endif()
114     if(RunCMake_GENERATOR_INSTANCE)
115       list(APPEND RunCMake_TEST_COMMAND "-DCMAKE_GENERATOR_INSTANCE=${RunCMake_GENERATOR_INSTANCE}")
116     endif()
117     list(APPEND RunCMake_TEST_COMMAND
118       -DRunCMake_TEST=${test}
119       --no-warn-unused-cli
120       )
121   else()
122     set(RunCMake_TEST_OPTIONS "")
123   endif()
124   if(NOT DEFINED RunCMake_TEST_RAW_ARGS)
125     set(RunCMake_TEST_RAW_ARGS "")
126   endif()
127   if(NOT RunCMake_TEST_COMMAND_WORKING_DIRECTORY)
128     set(RunCMake_TEST_COMMAND_WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
129   endif()
130   string(CONCAT _code [[execute_process(
131     COMMAND ${RunCMake_TEST_COMMAND}
132             ${RunCMake_TEST_OPTIONS}
133             ]] "${RunCMake_TEST_RAW_ARGS}\n" [[
134     WORKING_DIRECTORY "${RunCMake_TEST_COMMAND_WORKING_DIRECTORY}"
135     OUTPUT_VARIABLE actual_stdout
136     ERROR_VARIABLE ${actual_stderr_var}
137     RESULT_VARIABLE actual_result
138     ENCODING UTF8
139     ${maybe_timeout}
140     ${maybe_input_file}
141     )]])
142   cmake_language(EVAL CODE "${_code}")
143   set(msg "")
144   if(NOT "${actual_result}" MATCHES "${expect_result}")
145     string(APPEND msg "Result is [${actual_result}], not [${expect_result}].\n")
146   endif()
147
148   # Special case: remove ninja no-op line from stderr, but not stdout.
149   # Test cases that look for it should use RunCMake_TEST_OUTPUT_MERGE.
150   string(REGEX REPLACE "(^|\r?\n)ninja: no work to do\\.\r?\n" "\\1" actual_stderr "${actual_stderr}")
151
152   # Remove incidental content from both stdout and stderr.
153   string(CONCAT ignore_line_regex
154     "(^|\n)((==[0-9]+=="
155     "|BullseyeCoverage"
156     "|[a-z]+\\([0-9]+\\) malloc:"
157     "|clang[^:]*: warning: the object size sanitizer has no effect at -O0, but is explicitly enabled:"
158     "|lld-link: warning: procedure symbol record for .* refers to PDB item index [0-9A-Fa-fx]+ which is not a valid function ID record"
159     "|Error kstat returned"
160     "|Hit xcodebuild bug"
161     "|Recompacting log\\.\\.\\."
162
163     "|LICENSE WARNING:"
164     "|Your license to use PGI[^\n]*expired"
165     "|Please obtain a new version at"
166     "|contact PGI Sales at"
167     "|icp?c: remark #10441: The Intel\\(R\\) C\\+\\+ Compiler Classic \\(ICC\\) is deprecated"
168
169     "|[^\n]*install_name_tool: warning: changes being made to the file will invalidate the code signature in:"
170     "|[^\n]*xcodebuild[^\n]*DVTPlugInManager"
171     "|[^\n]*xcodebuild[^\n]*DVTSDK: Warning: SDK path collision for path"
172     "|[^\n]*xcodebuild[^\n]*Requested but did not find extension point with identifier"
173     "|[^\n]*xcodebuild[^\n]*nil host used in call to allows.*HTTPSCertificateForHost"
174     "|[^\n]*xcodebuild[^\n]*warning: file type[^\n]*is based on missing file type"
175     "|[^\n]*objc[^\n]*: Class [^\n]* One of the two will be used. Which one is undefined."
176     "|[^\n]*is a member of multiple groups"
177     "|[^\n]*offset in archive not a multiple of 8"
178     "|[^\n]*from Time Machine by path"
179     "|[^\n]*Bullseye Testing Technology"
180     ")[^\n]*\n)+"
181     )
182   foreach(o IN ITEMS out err)
183     string(REGEX REPLACE "\r\n" "\n" actual_std${o} "${actual_std${o}}")
184     string(REGEX REPLACE "${ignore_line_regex}" "\\1" actual_std${o} "${actual_std${o}}")
185     string(REGEX REPLACE "\n+$" "" actual_std${o} "${actual_std${o}}")
186     set(expect_${o} "")
187     if(DEFINED expect_std${o})
188       if(NOT "${actual_std${o}}" MATCHES "${expect_std${o}}")
189         string(REGEX REPLACE "\n" "\n expect-${o}> " expect_${o}
190           " expect-${o}> ${expect_std${o}}")
191         set(expect_${o} "Expected std${o} to match:\n${expect_${o}}\n")
192         string(APPEND msg "std${o} does not match that expected.\n")
193       endif()
194     endif()
195   endforeach()
196   unset(RunCMake_TEST_FAILED)
197   if(RunCMake-check-file AND EXISTS ${top_src}/${RunCMake-check-file})
198     include(${top_src}/${RunCMake-check-file})
199   else()
200     include(${top_src}/${test}-check.cmake OPTIONAL)
201   endif()
202   if(RunCMake_TEST_FAILED)
203     set(msg "${RunCMake_TEST_FAILED}\n${msg}")
204   endif()
205   if(msg)
206     string(REPLACE ";" "\" \"" command "\"${RunCMake_TEST_COMMAND}\"")
207     if(RunCMake_TEST_OPTIONS)
208       string(REPLACE ";" "\" \"" options "\"${RunCMake_TEST_OPTIONS}\"")
209       string(APPEND command " ${options}")
210     endif()
211     if(RunCMake_TEST_RAW_ARGS)
212       string(APPEND command " ${RunCMake_TEST_RAW_ARGS}")
213     endif()
214     string(APPEND msg "Command was:\n command> ${command}\n")
215   endif()
216   if(msg)
217     string(REGEX REPLACE "\n" "\n actual-out> " actual_out " actual-out> ${actual_stdout}")
218     string(REGEX REPLACE "\n" "\n actual-err> " actual_err " actual-err> ${actual_stderr}")
219     message(SEND_ERROR "${test}${RunCMake_TEST_VARIANT_DESCRIPTION} - FAILED:\n"
220       "${msg}"
221       "${expect_out}"
222       "Actual stdout:\n${actual_out}\n"
223       "${expect_err}"
224       "Actual stderr:\n${actual_err}\n"
225       )
226   else()
227     message(STATUS "${test}${RunCMake_TEST_VARIANT_DESCRIPTION} - PASSED")
228   endif()
229 endfunction()
230
231 function(run_cmake_command test)
232   set(RunCMake_TEST_COMMAND "${ARGN}")
233   run_cmake(${test})
234 endfunction()
235
236 function(run_cmake_script test)
237   set(RunCMake_TEST_COMMAND ${CMAKE_COMMAND} ${ARGN} -P ${RunCMake_SOURCE_DIR}/${test}.cmake)
238   run_cmake(${test})
239 endfunction()
240
241 function(run_cmake_with_options test)
242   set(RunCMake_TEST_OPTIONS "${ARGN}")
243   run_cmake(${test})
244 endfunction()
245
246 function(run_cmake_with_raw_args test args)
247   set(RunCMake_TEST_RAW_ARGS "${args}")
248   run_cmake(${test})
249 endfunction()
250
251 function(ensure_files_match expected_file actual_file)
252   if(NOT EXISTS "${expected_file}")
253     message(FATAL_ERROR "Expected file does not exist:\n  ${expected_file}")
254   endif()
255   if(NOT EXISTS "${actual_file}")
256     message(FATAL_ERROR "Actual file does not exist:\n  ${actual_file}")
257   endif()
258   file(READ "${expected_file}" expected_file_content)
259   file(READ "${actual_file}" actual_file_content)
260   if(NOT "${expected_file_content}" STREQUAL "${actual_file_content}")
261     message(FATAL_ERROR "Actual file content does not match expected:\n
262     \n
263       expected file: ${expected_file}\n
264       expected content:\n
265       ${expected_file_content}\n
266     \n
267       actual file: ${actual_file}\n
268       actual content:\n
269       ${actual_file_content}\n
270     ")
271   endif()
272 endfunction()
273
274 # Protect RunCMake tests from calling environment.
275 unset(ENV{MAKEFLAGS})