1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
11 if(NOT DEFINED ${arg})
12 message(FATAL_ERROR "${arg} not given!")
16 function(run_cmake test)
17 if(DEFINED ENV{RunCMake_TEST_FILTER})
18 set(test_and_variant "${test}${RunCMake_TEST_VARIANT_DESCRIPTION}")
19 if(NOT test_and_variant MATCHES "$ENV{RunCMake_TEST_FILTER}")
22 unset(test_and_variant)
25 set(top_src "${RunCMake_SOURCE_DIR}")
26 set(top_bin "${RunCMake_BINARY_DIR}")
27 if(EXISTS ${top_src}/${test}-result.txt)
28 file(READ ${top_src}/${test}-result.txt expect_result)
29 string(REGEX REPLACE "\n+$" "" expect_result "${expect_result}")
30 elseif(DEFINED RunCMake_TEST_EXPECT_RESULT)
31 set(expect_result "${RunCMake_TEST_EXPECT_RESULT}")
36 string(TOLOWER ${CMAKE_HOST_SYSTEM_NAME} platform_name)
37 #remove all additional bits from cygwin/msys name
38 if(platform_name MATCHES cygwin)
39 set(platform_name cygwin)
41 if(platform_name MATCHES msys)
42 set(platform_name msys)
45 foreach(o IN ITEMS stdout stderr config)
46 if(RunCMake-${o}-file AND EXISTS ${top_src}/${RunCMake-${o}-file})
47 file(READ ${top_src}/${RunCMake-${o}-file} expect_${o})
48 string(REGEX REPLACE "\n+$" "" expect_${o} "${expect_${o}}")
49 elseif(EXISTS ${top_src}/${test}-${o}-${platform_name}.txt)
50 file(READ ${top_src}/${test}-${o}-${platform_name}.txt expect_${o})
51 string(REGEX REPLACE "\n+$" "" expect_${o} "${expect_${o}}")
52 elseif(EXISTS ${top_src}/${test}-${o}.txt)
53 file(READ ${top_src}/${test}-${o}.txt expect_${o})
54 string(REGEX REPLACE "\n+$" "" expect_${o} "${expect_${o}}")
55 elseif(DEFINED RunCMake_TEST_EXPECT_${o})
56 string(REGEX REPLACE "\n+$" "" expect_${o} "${RunCMake_TEST_EXPECT_${o}}")
61 foreach(o IN ITEMS stdout stderr config)
62 if(DEFINED RunCMake_TEST_NOT_EXPECT_${o})
63 string(REGEX REPLACE "\n+$" "" not_expect_${o} "${RunCMake_TEST_NOT_EXPECT_${o}}")
66 if (NOT expect_stderr)
67 if (NOT RunCMake_DEFAULT_stderr)
68 set(RunCMake_DEFAULT_stderr "^$")
70 set(expect_stderr ${RunCMake_DEFAULT_stderr})
73 if (NOT RunCMake_TEST_SOURCE_DIR)
74 set(RunCMake_TEST_SOURCE_DIR "${top_src}")
76 if(NOT RunCMake_TEST_BINARY_DIR)
77 set(RunCMake_TEST_BINARY_DIR "${top_bin}/${test}-build")
79 if(NOT RunCMake_TEST_NO_CLEAN)
80 file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
82 file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
83 if(RunCMake-prep-file AND EXISTS ${top_src}/${RunCMake-prep-file})
84 include(${top_src}/${RunCMake-prep-file})
86 include(${top_src}/${test}-prep.cmake OPTIONAL)
88 if(RunCMake_TEST_OUTPUT_MERGE)
89 set(actual_stderr_var actual_stdout)
92 set(actual_stderr_var actual_stderr)
94 if(DEFINED RunCMake_TEST_TIMEOUT)
95 set(maybe_timeout TIMEOUT ${RunCMake_TEST_TIMEOUT})
99 if(RunCMake-stdin-file AND EXISTS ${top_src}/${RunCMake-stdin-file})
100 set(maybe_input_file INPUT_FILE ${top_src}/${RunCMake-stdin-file})
101 elseif(EXISTS ${top_src}/${test}-stdin.txt)
102 set(maybe_input_file INPUT_FILE ${top_src}/${test}-stdin.txt)
104 set(maybe_input_file "")
106 if(NOT RunCMake_TEST_COMMAND)
107 if(NOT DEFINED RunCMake_TEST_OPTIONS)
108 set(RunCMake_TEST_OPTIONS "")
111 list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0025=NEW)
113 if(RunCMake_TEST_LCC AND NOT RunCMake_TEST_NO_CMP0129)
114 list(APPEND RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0129=NEW)
116 if(RunCMake_MAKE_PROGRAM)
117 list(APPEND RunCMake_TEST_OPTIONS "-DCMAKE_MAKE_PROGRAM=${RunCMake_MAKE_PROGRAM}")
119 set(RunCMake_TEST_COMMAND ${CMAKE_COMMAND})
120 if(NOT RunCMake_TEST_NO_SOURCE_DIR)
121 list(APPEND RunCMake_TEST_COMMAND "${RunCMake_TEST_SOURCE_DIR}")
123 list(APPEND RunCMake_TEST_COMMAND -G "${RunCMake_GENERATOR}")
124 if(RunCMake_GENERATOR_PLATFORM)
125 list(APPEND RunCMake_TEST_COMMAND -A "${RunCMake_GENERATOR_PLATFORM}")
127 if(RunCMake_GENERATOR_TOOLSET)
128 list(APPEND RunCMake_TEST_COMMAND -T "${RunCMake_GENERATOR_TOOLSET}")
130 if(RunCMake_GENERATOR_INSTANCE)
131 list(APPEND RunCMake_TEST_COMMAND "-DCMAKE_GENERATOR_INSTANCE=${RunCMake_GENERATOR_INSTANCE}")
133 list(APPEND RunCMake_TEST_COMMAND
134 -DRunCMake_TEST=${test}
138 set(RunCMake_TEST_OPTIONS "")
140 if(NOT DEFINED RunCMake_TEST_RAW_ARGS)
141 set(RunCMake_TEST_RAW_ARGS "")
143 if(NOT RunCMake_TEST_COMMAND_WORKING_DIRECTORY)
144 set(RunCMake_TEST_COMMAND_WORKING_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
146 string(CONCAT _code [[execute_process(
147 COMMAND ${RunCMake_TEST_COMMAND}
148 ${RunCMake_TEST_OPTIONS}
149 ]] "${RunCMake_TEST_RAW_ARGS}\n" [[
150 WORKING_DIRECTORY "${RunCMake_TEST_COMMAND_WORKING_DIRECTORY}"
151 OUTPUT_VARIABLE actual_stdout
152 ERROR_VARIABLE ${actual_stderr_var}
153 RESULT_VARIABLE actual_result
159 set(old_pwd "$ENV{PWD}")
163 # Emulate a shell using this directory.
164 set(ENV{PWD} "${RunCMake_TEST_COMMAND_WORKING_DIRECTORY}")
165 cmake_language(EVAL CODE "${_code}")
167 set(ENV{PWD} "${old_pwd}")
172 if(NOT "${actual_result}" MATCHES "${expect_result}")
173 string(APPEND msg "Result is [${actual_result}], not [${expect_result}].\n")
175 set(config_file "${RunCMake_TEST_COMMAND_WORKING_DIRECTORY}/CMakeFiles/CMakeConfigureLog.yaml")
176 if(EXISTS "${config_file}")
177 file(READ "${config_file}" actual_config)
179 set(actual_config "")
182 # Special case: remove ninja no-op line from stderr, but not stdout.
183 # Test cases that look for it should use RunCMake_TEST_OUTPUT_MERGE.
184 string(REGEX REPLACE "(^|\r?\n)ninja: no work to do\\.\r?\n" "\\1" actual_stderr "${actual_stderr}")
186 # Remove incidental content from both stdout and stderr.
187 string(CONCAT ignore_line_regex
190 "|[a-z]+\\([0-9]+\\) malloc:"
191 "|clang[^:]*: warning: the object size sanitizer has no effect at -O0, but is explicitly enabled:"
192 "|flang-new: warning: argument unused during compilation: .-flang-experimental-exec."
193 "|icp?x: remark: Note that use of .-g. without any optimization-level option will turn off most compiler optimizations"
194 "|ifx: remark #10440: Note that use of a debug option without any optimization-level option will turnoff most compiler optimizations"
195 "|lld-link: warning: procedure symbol record for .* refers to PDB item index [0-9A-Fa-fx]+ which is not a valid function ID record"
196 "|Error kstat returned"
197 "|Hit xcodebuild bug"
198 "|Recompacting log\\.\\.\\."
201 "|Your license to use PGI[^\n]*expired"
202 "|Please obtain a new version at"
203 "|contact PGI Sales at"
204 "|ic(p?c|l): remark #10441: The Intel\\(R\\) C\\+\\+ Compiler Classic \\(ICC\\) is deprecated"
206 "|[^\n]*install_name_tool: warning: changes being made to the file will invalidate the code signature in:"
207 "|[^\n]*(createItemModels|_NSMainThread|Please file a bug at)"
208 "|[^\n]*xcodebuild[^\n]*DVTAssertions: Warning"
209 "|[^\n]*xcodebuild[^\n]*DVTCoreDeviceEnabledState: DVTCoreDeviceEnabledState_Disabled set via user default"
210 "|[^\n]*xcodebuild[^\n]*DVTPlugInManager"
211 "|[^\n]*xcodebuild[^\n]*DVTSDK: Warning: SDK path collision for path"
212 "|[^\n]*xcodebuild[^\n]*Requested but did not find extension point with identifier"
213 "|[^\n]*xcodebuild[^\n]*nil host used in call to allows.*HTTPSCertificateForHost"
214 "|[^\n]*xcodebuild[^\n]*warning: file type[^\n]*is based on missing file type"
215 "|[^\n]*objc[^\n]*: Class [^\n]* One of the two will be used. Which one is undefined."
216 "|[^\n]*is a member of multiple groups"
217 "|[^\n]*offset in archive not a multiple of 8"
218 "|[^\n]*from Time Machine by path"
219 "|[^\n]*Bullseye Testing Technology"
220 ${RunCMake_TEST_EXTRA_IGNORE_LINE_REGEX}
223 if(RunCMake_IGNORE_POLICY_VERSION_DEPRECATION)
224 string(REGEX REPLACE [[
225 ^CMake Deprecation Warning at [^
226 ]*CMakeLists.txt:1 \(cmake_minimum_required\):
227 Compatibility with CMake < 3\.10 will be removed from a future version of
230 Update the VERSION argument <min> value or use a \.\.\.<max> suffix to tell
231 CMake that the project does not need compatibility with older versions\.
233 ]] "" actual_stderr "${actual_stderr}")
235 foreach(o IN ITEMS stdout stderr config)
236 string(REGEX REPLACE "\r\n" "\n" actual_${o} "${actual_${o}}")
237 string(REGEX REPLACE "${ignore_line_regex}" "\\1" actual_${o} "${actual_${o}}")
238 string(REGEX REPLACE "\n+$" "" actual_${o} "${actual_${o}}")
239 if(DEFINED expect_${o})
240 if(NOT "${actual_${o}}" MATCHES "${expect_${o}}")
241 string(APPEND msg "${o} does not match that expected.\n")
244 if(DEFINED not_expect_${o})
245 if("${actual_${o}}" MATCHES "${not_expect_${o}}")
246 string(APPEND msg "${o} matches that not expected.\n")
250 unset(RunCMake_TEST_FAILED)
251 unset(RunCMake_TEST_FAILURE_MESSAGE)
252 if(RunCMake-check-file AND EXISTS ${top_src}/${RunCMake-check-file})
253 include(${top_src}/${RunCMake-check-file})
255 include(${top_src}/${test}-check.cmake OPTIONAL)
257 if(RunCMake_TEST_FAILED)
258 set(msg "${RunCMake_TEST_FAILED}\n${msg}")
261 string(REPLACE ";" "\" \"" command "\"${RunCMake_TEST_COMMAND}\"")
262 if(RunCMake_TEST_OPTIONS)
263 string(REPLACE ";" "\" \"" options "\"${RunCMake_TEST_OPTIONS}\"")
264 string(APPEND command " ${options}")
266 if(RunCMake_TEST_RAW_ARGS)
267 string(APPEND command " ${RunCMake_TEST_RAW_ARGS}")
269 string(APPEND msg "Command was:\n command> ${command}\n")
272 foreach(o IN ITEMS stdout stderr config)
273 if(DEFINED expect_${o})
274 string(REGEX REPLACE "\n" "\n expect-${o}> " expect_${o} " expect-${o}> ${expect_${o}}")
275 string(APPEND msg "Expected ${o} to match:\n${expect_${o}}\n")
277 if(NOT o STREQUAL "config" OR DEFINED expect_${o})
278 string(REGEX REPLACE "\n" "\n actual-${o}> " actual_${o} " actual-${o}> ${actual_${o}}")
279 string(APPEND msg "Actual ${o}:\n${actual_${o}}\n")
282 if(RunCMake_TEST_FAILURE_MESSAGE)
283 string(APPEND msg "${RunCMake_TEST_FAILURE_MESSAGE}")
285 message(SEND_ERROR "${test}${RunCMake_TEST_VARIANT_DESCRIPTION} - FAILED:\n${msg}")
287 message(STATUS "${test}${RunCMake_TEST_VARIANT_DESCRIPTION} - PASSED")
291 function(run_cmake_command test)
292 set(RunCMake_TEST_COMMAND "${ARGN}")
296 function(run_cmake_script test)
297 set(RunCMake_TEST_COMMAND ${CMAKE_COMMAND} ${ARGN} -P ${RunCMake_SOURCE_DIR}/${test}.cmake)
301 function(run_cmake_with_options test)
302 set(RunCMake_TEST_OPTIONS "${ARGN}")
306 function(run_cmake_with_raw_args test args)
307 set(RunCMake_TEST_RAW_ARGS "${args}")
311 function(ensure_files_match expected_file actual_file)
312 if(NOT EXISTS "${expected_file}")
313 message(FATAL_ERROR "Expected file does not exist:\n ${expected_file}")
315 if(NOT EXISTS "${actual_file}")
316 message(FATAL_ERROR "Actual file does not exist:\n ${actual_file}")
318 file(READ "${expected_file}" expected_file_content)
319 file(READ "${actual_file}" actual_file_content)
320 if(NOT "${expected_file_content}" STREQUAL "${actual_file_content}")
321 message(FATAL_ERROR "Actual file content does not match expected:\n
323 expected file: ${expected_file}\n
325 ${expected_file_content}\n
327 actual file: ${actual_file}\n
329 ${actual_file_content}\n
334 # Get the user id on unix if possible.
335 function(get_unix_uid var)
336 set("${var}" "" PARENT_SCOPE)
339 if(CMAKE_SYSTEM_NAME STREQUAL "SunOS" AND EXISTS "/usr/xpg4/bin/id")
340 set (ID "/usr/xpg4/bin/id")
342 execute_process(COMMAND ${ID} -u $ENV{USER} OUTPUT_VARIABLE uid ERROR_QUIET
343 RESULT_VARIABLE status OUTPUT_STRIP_TRAILING_WHITESPACE)
345 set("${var}" "${uid}" PARENT_SCOPE)
350 # Protect RunCMake tests from calling environment.
351 unset(ENV{MAKEFLAGS})