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