5e66a521ec0d20958c5ddd29bb78fd4773f201d4
[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]*warning: file type[^\n]*is based on missing file type"
160     "|[^\n]*objc[^\n]*: Class [^\n]* One of the two will be used. Which one is undefined."
161     "|[^\n]*is a member of multiple groups"
162     "|[^\n]*offset in archive not a multiple of 8"
163     "|[^\n]*from Time Machine by path"
164     "|[^\n]*Bullseye Testing Technology"
165     ")[^\n]*\n)+"
166     )
167   foreach(o out err)
168     string(REGEX REPLACE "\r\n" "\n" actual_std${o} "${actual_std${o}}")
169     string(REGEX REPLACE "${ignore_line_regex}" "\\1" actual_std${o} "${actual_std${o}}")
170     string(REGEX REPLACE "\n+$" "" actual_std${o} "${actual_std${o}}")
171     set(expect_${o} "")
172     if(DEFINED expect_std${o})
173       if(NOT "${actual_std${o}}" MATCHES "${expect_std${o}}")
174         string(REGEX REPLACE "\n" "\n expect-${o}> " expect_${o}
175           " expect-${o}> ${expect_std${o}}")
176         set(expect_${o} "Expected std${o} to match:\n${expect_${o}}\n")
177         string(APPEND msg "std${o} does not match that expected.\n")
178       endif()
179     endif()
180   endforeach()
181   unset(RunCMake_TEST_FAILED)
182   if(RunCMake-check-file AND EXISTS ${top_src}/${RunCMake-check-file})
183     include(${top_src}/${RunCMake-check-file})
184   else()
185     include(${top_src}/${test}-check.cmake OPTIONAL)
186   endif()
187   if(RunCMake_TEST_FAILED)
188     set(msg "${RunCMake_TEST_FAILED}\n${msg}")
189   endif()
190   if(msg)
191     string(REPLACE ";" "\" \"" command "\"${RunCMake_TEST_COMMAND}\"")
192     if(RunCMake_TEST_OPTIONS)
193       string(REPLACE ";" "\" \"" options "\"${RunCMake_TEST_OPTIONS}\"")
194       string(APPEND command " ${options}")
195     endif()
196     string(APPEND msg "Command was:\n command> ${command}\n")
197   endif()
198   if(msg)
199     string(REGEX REPLACE "\n" "\n actual-out> " actual_out " actual-out> ${actual_stdout}")
200     string(REGEX REPLACE "\n" "\n actual-err> " actual_err " actual-err> ${actual_stderr}")
201     message(SEND_ERROR "${test}${RunCMake_TEST_VARIANT_DESCRIPTION} - FAILED:\n"
202       "${msg}"
203       "${expect_out}"
204       "Actual stdout:\n${actual_out}\n"
205       "${expect_err}"
206       "Actual stderr:\n${actual_err}\n"
207       )
208   else()
209     message(STATUS "${test}${RunCMake_TEST_VARIANT_DESCRIPTION} - PASSED")
210   endif()
211 endfunction()
212
213 function(run_cmake_command test)
214   set(RunCMake_TEST_COMMAND "${ARGN}")
215   run_cmake(${test})
216 endfunction()
217
218 function(run_cmake_script test)
219   set(RunCMake_TEST_COMMAND ${CMAKE_COMMAND} ${ARGN} -P ${RunCMake_SOURCE_DIR}/${test}.cmake)
220   run_cmake(${test})
221 endfunction()
222
223 function(run_cmake_with_options test)
224   set(RunCMake_TEST_OPTIONS "${ARGN}")
225   run_cmake(${test})
226 endfunction()
227
228 function(ensure_files_match expected_file actual_file)
229   if(NOT EXISTS "${expected_file}")
230     message(FATAL_ERROR "Expected file does not exist:\n  ${expected_file}")
231   endif()
232   if(NOT EXISTS "${actual_file}")
233     message(FATAL_ERROR "Actual file does not exist:\n  ${actual_file}")
234   endif()
235   file(READ "${expected_file}" expected_file_content)
236   file(READ "${actual_file}" actual_file_content)
237   if(NOT "${expected_file_content}" STREQUAL "${actual_file_content}")
238     message(FATAL_ERROR "Actual file content does not match expected:\n
239     \n
240       expected file: ${expected_file}\n
241       expected content:\n
242       ${expected_file_content}\n
243     \n
244       actual file: ${actual_file}\n
245       actual content:\n
246       ${actual_file_content}\n
247     ")
248   endif()
249 endfunction()
250
251 # Protect RunCMake tests from calling environment.
252 unset(ENV{MAKEFLAGS})