/src/CMake/Source/cmTestGenerator.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #include "cmTestGenerator.h" |
4 | | |
5 | | #include <cstddef> // IWYU pragma: keep |
6 | | #include <memory> |
7 | | #include <ostream> |
8 | | #include <set> |
9 | | #include <string> |
10 | | #include <utility> |
11 | | #include <vector> |
12 | | |
13 | | #include "cmGeneratorExpression.h" |
14 | | #include "cmGeneratorTarget.h" |
15 | | #include "cmGlobalGenerator.h" |
16 | | #include "cmList.h" |
17 | | #include "cmListFileCache.h" |
18 | | #include "cmLocalGenerator.h" |
19 | | #include "cmMakefile.h" |
20 | | #include "cmMessageType.h" |
21 | | #include "cmPolicies.h" |
22 | | #include "cmPropertyMap.h" |
23 | | #include "cmRange.h" |
24 | | #include "cmScriptGenerator.h" |
25 | | #include "cmStringAlgorithms.h" |
26 | | #include "cmSystemTools.h" |
27 | | #include "cmTargetTypes.h" |
28 | | #include "cmTest.h" |
29 | | #include "cmValue.h" |
30 | | |
31 | | namespace /* anonymous */ |
32 | | { |
33 | | |
34 | | bool needToQuoteTestName(cmMakefile const& mf, std::string const& name) |
35 | 0 | { |
36 | | // Determine if policy CMP0110 is set to NEW. |
37 | 0 | switch (mf.GetPolicyStatus(cmPolicies::CMP0110)) { |
38 | 0 | case cmPolicies::WARN: |
39 | | // Only warn if a forbidden character is used in the name. |
40 | 0 | if (name.find_first_of("$[] #;\t\n\"\\") != std::string::npos) { |
41 | 0 | mf.IssuePolicyWarning( |
42 | 0 | cmPolicies::CMP0110, {}, |
43 | 0 | cmStrCat("The following name given to add_test() is invalid if " |
44 | 0 | "CMP0110 is not set or set to OLD:\n `", |
45 | 0 | name, "ยด\n")); |
46 | 0 | } |
47 | 0 | CM_FALLTHROUGH; |
48 | 0 | case cmPolicies::OLD: |
49 | | // OLD behavior is to not quote the test's name. |
50 | 0 | return false; |
51 | 0 | case cmPolicies::NEW: |
52 | 0 | default: |
53 | | // NEW behavior is to quote the test's name. |
54 | 0 | return true; |
55 | 0 | } |
56 | 0 | } |
57 | | |
58 | | std::string TestName(cmTest* test) |
59 | 0 | { |
60 | 0 | std::string name = test->GetName(); |
61 | 0 | if (needToQuoteTestName(*test->GetMakefile(), name)) { |
62 | 0 | name = cmScriptGenerator::Quote(name); |
63 | 0 | } |
64 | 0 | return name; |
65 | 0 | } |
66 | | |
67 | | // Whether a path is produced by the build (a custom-command output or |
68 | | // byproduct) rather than a pre-existing file. The output-to-source map |
69 | | // records every generated path regardless of which target, if any, builds it. |
70 | | bool fileIsGenerated(cmGlobalGenerator* gg, std::string const& file) |
71 | 0 | { |
72 | 0 | std::string const collapsed = cmSystemTools::CollapseFullPath(file); |
73 | 0 | for (auto const& lg : gg->GetLocalGenerators()) { |
74 | 0 | cmSourcesWithOutput so = lg->GetSourcesWithOutput(collapsed); |
75 | 0 | if (so.Source || so.Target) { |
76 | 0 | return true; |
77 | 0 | } |
78 | 0 | if (file != collapsed) { |
79 | 0 | so = lg->GetSourcesWithOutput(file); |
80 | 0 | if (so.Source || so.Target) { |
81 | 0 | return true; |
82 | 0 | } |
83 | 0 | } |
84 | 0 | } |
85 | 0 | return false; |
86 | 0 | } |
87 | | |
88 | | } // End: anonymous namespace |
89 | | |
90 | | cmTestGenerator::cmTestGenerator( |
91 | | cmTest* test, std::vector<std::string> const& configurations) |
92 | 0 | : cmScriptGenerator("CTEST_CONFIGURATION_TYPE", configurations) |
93 | 0 | , Test(test) |
94 | 0 | { |
95 | 0 | this->ActionsPerConfig = test == nullptr || !test->GetOldStyle(); |
96 | 0 | this->TestGenerated = false; |
97 | 0 | this->LG = nullptr; |
98 | 0 | } |
99 | | |
100 | 0 | cmTestGenerator::~cmTestGenerator() = default; |
101 | | |
102 | | void cmTestGenerator::Compute(cmLocalGenerator* lg) |
103 | 0 | { |
104 | 0 | this->LG = lg; |
105 | 0 | } |
106 | | |
107 | | bool cmTestGenerator::TestsForConfig(std::string const& config) |
108 | 0 | { |
109 | 0 | return this->Test != nullptr && this->GeneratesForConfig(config); |
110 | 0 | } |
111 | | |
112 | | cmTest* cmTestGenerator::GetTest() const |
113 | 0 | { |
114 | 0 | return this->Test; |
115 | 0 | } |
116 | | |
117 | | bool cmTestGenerator::GetBuildDependencies(cmLocalGenerator* lg, |
118 | | BuildDependencies& info) |
119 | 0 | { |
120 | 0 | if (this->Test == nullptr || |
121 | 0 | !cmGeneratorExpression::IsValidTargetName(this->Test->GetName()) || |
122 | 0 | cmGlobalGenerator::IsReservedTarget(this->Test->GetName())) { |
123 | 0 | return false; |
124 | 0 | } |
125 | | |
126 | 0 | std::set<cmGeneratorTarget*> dependencies; |
127 | | |
128 | | // Get dependencies from generator expressions |
129 | 0 | cmGeneratorExpression ge(*this->Test->GetMakefile()->GetCMakeInstance(), |
130 | 0 | this->Test->GetBacktrace()); |
131 | 0 | std::string const config; |
132 | 0 | for (std::string const& arg : this->Test->GetCommand()) { |
133 | 0 | auto parsed = ge.Parse(arg); |
134 | 0 | parsed->Evaluate(lg, config); |
135 | 0 | for (cmGeneratorTarget* dep : parsed->GetTargets()) { |
136 | 0 | if (dep && !dep->IsImported()) { |
137 | 0 | dependencies.insert(dep); |
138 | 0 | } |
139 | 0 | } |
140 | 0 | } |
141 | | |
142 | | // Add target executed by test |
143 | 0 | if (!this->Test->GetCommand().empty()) { |
144 | 0 | std::string exe = this->Test->GetCommand().front(); |
145 | 0 | cmGeneratorTarget* target = lg->FindGeneratorTargetToUse(exe); |
146 | 0 | if (target && target->GetType() == cm::TargetType::EXECUTABLE && |
147 | 0 | !target->IsImported()) { |
148 | 0 | dependencies.insert(target); |
149 | 0 | } |
150 | 0 | } |
151 | | |
152 | | // Add dependencies from BUILD_DEPENDS keyword |
153 | 0 | for (auto const& depName : this->Test->GetDependencies()) { |
154 | 0 | if (depName.empty()) { |
155 | 0 | continue; |
156 | 0 | } |
157 | 0 | cmGeneratorTarget* depTarget = lg->FindGeneratorTargetToUse(depName); |
158 | 0 | if (!depTarget) { |
159 | 0 | cmGlobalGenerator* gg = lg->GetGlobalGenerator(); |
160 | 0 | BuildDependencies::FileDependency file; |
161 | 0 | file.Path = depName; |
162 | 0 | file.Owner = gg->FindOutputOwningTarget(depName); |
163 | 0 | file.Generated = fileIsGenerated(gg, depName); |
164 | 0 | info.Files.push_back(std::move(file)); |
165 | 0 | continue; |
166 | 0 | } |
167 | 0 | if (depTarget->IsImported()) { |
168 | 0 | lg->GetMakefile()->IssueMessage( |
169 | 0 | MessageType::FATAL_ERROR, |
170 | 0 | cmStrCat("Test \"", this->Test->GetName(), "\" DEPENDS target \"", |
171 | 0 | depName, "\" which is imported and cannot be built."), |
172 | 0 | this->Test->GetBacktrace()); |
173 | 0 | return false; |
174 | 0 | } |
175 | 0 | dependencies.insert(depTarget); |
176 | 0 | } |
177 | | |
178 | 0 | for (cmGeneratorTarget* gt : dependencies) { |
179 | 0 | if (gt->IsInBuildSystem()) { |
180 | 0 | info.Targets.push_back(gt); |
181 | 0 | } |
182 | 0 | } |
183 | 0 | return true; |
184 | 0 | } |
185 | | |
186 | | void cmTestGenerator::GenerateScriptActions(std::ostream& os, Indent indent) |
187 | 0 | { |
188 | 0 | if (this->ActionsPerConfig) { |
189 | | // This is the per-config generation in a single-configuration |
190 | | // build generator case. The superclass will call our per-config |
191 | | // method. |
192 | 0 | this->cmScriptGenerator::GenerateScriptActions(os, indent); |
193 | 0 | } else { |
194 | | // This is an old-style test, so there is only one config. |
195 | | // assert(this->Test->GetOldStyle()); |
196 | 0 | this->GenerateOldStyle(os, indent); |
197 | 0 | } |
198 | 0 | } |
199 | | |
200 | | void cmTestGenerator::GenerateCommand(std::ostream& os, |
201 | | std::vector<std::string> const& command, |
202 | | std::string const& config, bool expand, |
203 | | cmGeneratorExpression& ge, |
204 | | cmPolicies::PolicyStatus cmp0158, |
205 | | cmPolicies::PolicyStatus cmp0178) |
206 | 0 | { |
207 | | // Evaluate command line arguments |
208 | 0 | cmList argv{ |
209 | 0 | this->EvaluateCommandLineArguments(command, ge, config), |
210 | | // Expand arguments if COMMAND_EXPAND_LISTS is set |
211 | 0 | expand ? cmList::ExpandElements::Yes : cmList::ExpandElements::No, |
212 | 0 | cmList::EmptyElements::Yes, |
213 | 0 | }; |
214 | | // Expanding lists on an empty command may have left it empty |
215 | 0 | if (argv.empty()) { |
216 | 0 | argv.emplace_back(); |
217 | 0 | } |
218 | | |
219 | | // Check whether the command executable is a target whose name is to |
220 | | // be translated. |
221 | 0 | std::string exe = argv[0]; |
222 | 0 | cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(exe); |
223 | 0 | if (target && target->GetType() == cm::TargetType::EXECUTABLE) { |
224 | | // Use the target file on disk. |
225 | 0 | exe = target->GetFullPath(config); |
226 | |
|
227 | 0 | auto addLauncher = [&](std::string const& propertyName) { |
228 | 0 | cmValue launcher = target->GetProperty(propertyName); |
229 | 0 | if (!cmNonempty(launcher)) { |
230 | 0 | return; |
231 | 0 | } |
232 | 0 | auto const propVal = ge.Parse(*launcher)->Evaluate(this->LG, config); |
233 | 0 | cmList launcherWithArgs(propVal, cmList::ExpandElements::Yes, |
234 | 0 | cmp0178 == cmPolicies::NEW |
235 | 0 | ? cmList::EmptyElements::Yes |
236 | 0 | : cmList::EmptyElements::No); |
237 | 0 | if (!launcherWithArgs.empty() && !launcherWithArgs[0].empty()) { |
238 | 0 | if (cmp0178 == cmPolicies::WARN) { |
239 | 0 | cmList argsWithEmptyValuesPreserved( |
240 | 0 | propVal, cmList::ExpandElements::Yes, cmList::EmptyElements::Yes); |
241 | 0 | if (launcherWithArgs != argsWithEmptyValuesPreserved) { |
242 | 0 | this->LG->GetMakefile()->IssuePolicyWarning( |
243 | 0 | cmPolicies::CMP0178, |
244 | 0 | cmStrCat("The ", propertyName, " property of target '", |
245 | 0 | target->GetName(), |
246 | 0 | "' contains empty list items. Those empty items are " |
247 | 0 | "being silently discarded to preserve backward " |
248 | 0 | "compatibility.")); |
249 | 0 | } |
250 | 0 | } |
251 | 0 | std::string launcherExe(launcherWithArgs[0]); |
252 | 0 | cmSystemTools::ConvertToUnixSlashes(launcherExe); |
253 | 0 | os << cmScriptGenerator::Quote(launcherExe) << " "; |
254 | 0 | for (std::string const& arg : |
255 | 0 | cmMakeRange(launcherWithArgs).advance(1)) { |
256 | 0 | os << cmScriptGenerator::Quote(arg) << " "; |
257 | 0 | } |
258 | 0 | } |
259 | 0 | }; |
260 | | |
261 | | // Prepend with the test launcher if specified. |
262 | 0 | addLauncher("TEST_LAUNCHER"); |
263 | | |
264 | | // Prepend with the emulator when cross compiling if required. |
265 | 0 | if (cmp0158 != cmPolicies::NEW || |
266 | 0 | this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING")) { |
267 | 0 | addLauncher("CROSSCOMPILING_EMULATOR"); |
268 | 0 | } |
269 | 0 | } else { |
270 | | // Use the command name given. |
271 | 0 | cmSystemTools::ConvertToUnixSlashes(exe); |
272 | 0 | } |
273 | | |
274 | | // Generate the command line with full escapes. |
275 | 0 | os << cmScriptGenerator::Quote(exe); |
276 | |
|
277 | 0 | for (auto const& arg : cmMakeRange(argv).advance(1)) { |
278 | 0 | os << " " << cmScriptGenerator::Quote(arg); |
279 | 0 | } |
280 | 0 | } |
281 | | |
282 | | void cmTestGenerator::GenerateScriptForConfig(std::ostream& os, |
283 | | std::string const& config, |
284 | | Indent indent) |
285 | 0 | { |
286 | 0 | this->TestGenerated = true; |
287 | | |
288 | | // Set up generator expression evaluation context. |
289 | 0 | cmGeneratorExpression ge(*this->Test->GetMakefile()->GetCMakeInstance(), |
290 | 0 | this->Test->GetBacktrace()); |
291 | |
|
292 | 0 | auto const test_name = TestName(this->Test); |
293 | 0 | os << indent << "add_test(" << test_name << ' '; |
294 | 0 | this->GenerateCommand( |
295 | 0 | os, this->Test->GetCommand(), config, this->Test->GetCommandExpandLists(), |
296 | 0 | ge, this->GetTest()->GetCMP0158(), this->Test->GetCMP0178()); |
297 | 0 | os << ")\n"; |
298 | | |
299 | | // Output properties for the test. |
300 | 0 | os << indent << "set_tests_properties(" << test_name << " PROPERTIES "; |
301 | 0 | for (auto const& i : this->Test->GetProperties().GetList()) { |
302 | 0 | os << " " << i.first << " " |
303 | 0 | << cmScriptGenerator::Quote( |
304 | 0 | ge.Parse(i.second)->Evaluate(this->LG, config)); |
305 | 0 | } |
306 | 0 | BuildDependencies deps; |
307 | 0 | if (this->GetBuildDependencies(this->LG, deps)) { |
308 | 0 | cmList depList; |
309 | 0 | for (std::string const& dep : |
310 | 0 | this->LG->GetGlobalGenerator()->GetTestBuildDependencyPaths(config, |
311 | 0 | deps)) { |
312 | 0 | depList.append(dep); |
313 | 0 | } |
314 | 0 | os << " _CMAKE_TEST_BUILD_DEPENDS " |
315 | 0 | << cmScriptGenerator::Quote(depList.to_string()); |
316 | 0 | } |
317 | 0 | os << ' '; |
318 | 0 | this->GenerateBacktrace(os, this->Test->GetBacktrace()); |
319 | 0 | os << ")\n"; |
320 | 0 | } |
321 | | |
322 | | void cmTestGenerator::GenerateScriptNoConfig(std::ostream& os, Indent indent) |
323 | 0 | { |
324 | 0 | os << indent << "add_test(" << TestName(this->Test) << " NOT_AVAILABLE)\n"; |
325 | 0 | } |
326 | | |
327 | | bool cmTestGenerator::NeedsScriptNoConfig() const |
328 | 0 | { |
329 | 0 | return (this->TestGenerated && // test generated for at least one config |
330 | 0 | this->ActionsPerConfig && // test is config-aware |
331 | 0 | this->Configurations.empty() && // test runs in all configs |
332 | 0 | !this->ConfigurationTypes->empty()); // config-dependent command |
333 | 0 | } |
334 | | |
335 | | void cmTestGenerator::GenerateOldStyle(std::ostream& fout, Indent indent) |
336 | 0 | { |
337 | 0 | this->TestGenerated = true; |
338 | |
|
339 | 0 | auto const test_name = TestName(this->Test); |
340 | | |
341 | | // Get the test command line to be executed. |
342 | 0 | std::vector<std::string> const& command = this->Test->GetCommand(); |
343 | |
|
344 | 0 | std::string exe = command[0]; |
345 | 0 | cmSystemTools::ConvertToUnixSlashes(exe); |
346 | 0 | fout << indent << "add_test(" << test_name << " \"" << exe << "\""; |
347 | |
|
348 | 0 | for (std::string const& arg : cmMakeRange(command).advance(1)) { |
349 | | // Just double-quote all arguments so they are re-parsed |
350 | | // correctly by the test system. |
351 | 0 | fout << " \""; |
352 | 0 | for (char c : arg) { |
353 | | // Escape quotes within arguments. We should escape |
354 | | // backslashes too but we cannot because it makes the result |
355 | | // inconsistent with previous behavior of this command. |
356 | 0 | if (c == '"') { |
357 | 0 | fout << '\\'; |
358 | 0 | } |
359 | 0 | fout << c; |
360 | 0 | } |
361 | 0 | fout << '"'; |
362 | 0 | } |
363 | 0 | fout << ")\n"; |
364 | | |
365 | | // Output properties for the test. |
366 | 0 | fout << indent << "set_tests_properties(" << test_name << " PROPERTIES "; |
367 | 0 | for (auto const& i : this->Test->GetProperties().GetList()) { |
368 | 0 | fout << " " << i.first << " " << cmScriptGenerator::Quote(i.second); |
369 | 0 | } |
370 | 0 | fout << ' '; |
371 | 0 | this->GenerateBacktrace(fout, this->Test->GetBacktrace()); |
372 | 0 | fout << ")\n"; |
373 | 0 | } |
374 | | |
375 | | void cmTestGenerator::GenerateBacktrace(std::ostream& os, |
376 | | cmListFileBacktrace bt) |
377 | 0 | { |
378 | 0 | if (bt.Empty()) { |
379 | 0 | return; |
380 | 0 | } |
381 | | |
382 | 0 | os << "_BACKTRACE_TRIPLES \""; |
383 | |
|
384 | 0 | bool prependTripleSeparator = false; |
385 | 0 | while (!bt.Empty()) { |
386 | 0 | auto const& entry = bt.Top(); |
387 | 0 | if (prependTripleSeparator) { |
388 | 0 | os << ";"; |
389 | 0 | } |
390 | 0 | os << entry.FilePath << ";" << entry.Line << ";" << entry.Name; |
391 | 0 | bt = bt.Pop(); |
392 | 0 | prependTripleSeparator = true; |
393 | 0 | } |
394 | |
|
395 | 0 | os << '"'; |
396 | 0 | } |
397 | | |
398 | | std::vector<std::string> cmTestGenerator::EvaluateCommandLineArguments( |
399 | | std::vector<std::string> const& argv, cmGeneratorExpression& ge, |
400 | | std::string const& config) const |
401 | 0 | { |
402 | | // Evaluate executable name and arguments |
403 | 0 | auto evaluatedRange = |
404 | 0 | cmMakeRange(argv).transform([&](std::string const& arg) { |
405 | 0 | return ge.Parse(arg)->Evaluate(this->LG, config); |
406 | 0 | }); |
407 | |
|
408 | 0 | return { evaluatedRange.begin(), evaluatedRange.end() }; |
409 | 0 | } |