/src/CMake/Source/cmExportCommand.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 "cmExportCommand.h" |
4 | | |
5 | | #include <map> |
6 | | #include <sstream> |
7 | | #include <utility> |
8 | | |
9 | | #include <cm/memory> |
10 | | #include <cm/optional> |
11 | | #include <cmext/algorithm> |
12 | | #include <cmext/string_view> |
13 | | |
14 | | #include "cmsys/RegularExpression.hxx" |
15 | | |
16 | | #include "cmArgumentParser.h" |
17 | | #include "cmArgumentParserTypes.h" |
18 | | #include "cmBuildSbomGenerator.h" |
19 | | #include "cmCryptoHash.h" |
20 | | #include "cmDiagnosticContext.h" |
21 | | #include "cmDiagnostics.h" |
22 | | #include "cmExecutionStatus.h" |
23 | | #include "cmExperimental.h" |
24 | | #include "cmExportBuildAndroidMKGenerator.h" |
25 | | #include "cmExportBuildCMakeConfigGenerator.h" |
26 | | #include "cmExportBuildFileGenerator.h" |
27 | | #include "cmExportBuildPackageInfoGenerator.h" |
28 | | #include "cmExportSet.h" |
29 | | #include "cmGeneratedFileStream.h" |
30 | | #include "cmGlobalGenerator.h" |
31 | | #include "cmMakefile.h" |
32 | | #include "cmMessageType.h" |
33 | | #include "cmPackageInfoArguments.h" |
34 | | #include "cmPolicies.h" |
35 | | #include "cmRange.h" |
36 | | #include "cmSbomArguments.h" |
37 | | #include "cmStringAlgorithms.h" |
38 | | #include "cmSubcommandTable.h" |
39 | | #include "cmSystemTools.h" |
40 | | #include "cmTarget.h" |
41 | | #include "cmTargetTypes.h" |
42 | | #include "cmValue.h" |
43 | | |
44 | | #if defined(__HAIKU__) |
45 | | # include <FindDirectory.h> |
46 | | # include <StorageDefs.h> |
47 | | #endif |
48 | | |
49 | | #if defined(_WIN32) && !defined(__CYGWIN__) |
50 | | # include <windows.h> |
51 | | #endif |
52 | | |
53 | | static void StorePackageRegistry(cmMakefile& mf, std::string const& package, |
54 | | char const* content, char const* hash); |
55 | | |
56 | | static cm::optional<cmExportSet*> GetExportSet(std::string const& name, |
57 | | cmGlobalGenerator* generator, |
58 | | cmExecutionStatus& status) |
59 | 0 | { |
60 | 0 | cmExportSetMap& setMap = generator->GetExportSets(); |
61 | 0 | auto const it = setMap.find(name); |
62 | 0 | if (it == setMap.end()) { |
63 | 0 | status.SetError(cmStrCat("Export set \""_s, name, "\" not found."_s)); |
64 | 0 | return cm::nullopt; |
65 | 0 | } |
66 | 0 | return &it->second; |
67 | 0 | } |
68 | | |
69 | | static void AddExportGenerator( |
70 | | cmMakefile& makefile, cmGlobalGenerator* globalGenerator, |
71 | | std::unique_ptr<cmExportBuildFileGenerator> exportGenerator, |
72 | | std::string const& fileName, cmExportSet* exportSet, |
73 | | std::string const& cxxModulesDirectory) |
74 | 0 | { |
75 | 0 | exportGenerator->SetExportFile(fileName.c_str()); |
76 | 0 | exportGenerator->SetCxxModuleDirectory(cxxModulesDirectory); |
77 | 0 | if (exportSet) { |
78 | 0 | exportGenerator->SetExportSet(exportSet); |
79 | 0 | } |
80 | 0 | std::vector<std::string> configurationTypes = |
81 | 0 | makefile.GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig); |
82 | |
|
83 | 0 | for (std::string const& ct : configurationTypes) { |
84 | 0 | exportGenerator->AddConfiguration(ct); |
85 | 0 | } |
86 | 0 | if (exportSet) { |
87 | 0 | globalGenerator->AddBuildExportExportSet(exportGenerator.get()); |
88 | 0 | } |
89 | 0 | makefile.AddExportBuildFileGenerator(std::move(exportGenerator)); |
90 | 0 | } |
91 | | |
92 | | static bool ValidateExportableTarget(std::string const& name, cmMakefile& mf, |
93 | | cmGlobalGenerator* gg, |
94 | | cmExecutionStatus& status) |
95 | 0 | { |
96 | 0 | if (mf.IsAlias(name)) { |
97 | 0 | status.SetError(cmStrCat("given ALIAS target \"", name, |
98 | 0 | "\" which may not be exported.")); |
99 | 0 | return false; |
100 | 0 | } |
101 | 0 | cmTarget const* target = gg->FindTarget(name); |
102 | 0 | if (!target) { |
103 | 0 | status.SetError(cmStrCat("given target \"", name, |
104 | 0 | "\" which is not built by this project.")); |
105 | 0 | return false; |
106 | 0 | } |
107 | 0 | if (target->GetType() == cm::TargetType::UTILITY) { |
108 | 0 | status.SetError(cmStrCat("given custom target \"", name, |
109 | 0 | "\" which may not be exported.")); |
110 | 0 | return false; |
111 | 0 | } |
112 | 0 | return true; |
113 | 0 | } |
114 | | |
115 | | static bool HandleTargetsMode(std::vector<std::string> const& args, |
116 | | cmExecutionStatus& status) |
117 | 0 | { |
118 | 0 | struct Arguments |
119 | 0 | { |
120 | 0 | cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>> Targets; |
121 | 0 | ArgumentParser::NonEmpty<std::string> ExportSetName; |
122 | 0 | ArgumentParser::NonEmpty<std::string> Namespace; |
123 | 0 | ArgumentParser::NonEmpty<std::string> Filename; |
124 | 0 | ArgumentParser::NonEmpty<std::string> CxxModulesDirectory; |
125 | 0 | ArgumentParser::NonEmpty<std::string> AndroidMKFile; |
126 | |
|
127 | 0 | bool Append = false; |
128 | 0 | bool ExportOld = false; |
129 | 0 | }; |
130 | |
|
131 | 0 | auto parser = |
132 | 0 | cmArgumentParser<Arguments>{} |
133 | 0 | .Bind("NAMESPACE"_s, &Arguments::Namespace) |
134 | 0 | .Bind("FILE"_s, &Arguments::Filename) |
135 | 0 | .Bind("CXX_MODULES_DIRECTORY"_s, &Arguments::CxxModulesDirectory) |
136 | 0 | .Bind("TARGETS"_s, &Arguments::Targets) |
137 | 0 | .Bind("APPEND"_s, &Arguments::Append) |
138 | 0 | .Bind("ANDROID_MK"_s, &Arguments::AndroidMKFile) |
139 | 0 | .Bind("EXPORT_LINK_INTERFACE_LIBRARIES"_s, &Arguments::ExportOld); |
140 | |
|
141 | 0 | std::vector<std::string> unknownArgs; |
142 | 0 | Arguments arguments = parser.Parse(args, &unknownArgs); |
143 | |
|
144 | 0 | if (!unknownArgs.empty()) { |
145 | 0 | status.SetError( |
146 | 0 | cmStrCat("Unknown argument: \"", unknownArgs.front(), "\".")); |
147 | 0 | return false; |
148 | 0 | } |
149 | | |
150 | 0 | std::string fname; |
151 | 0 | bool android = false; |
152 | 0 | if (!arguments.AndroidMKFile.empty()) { |
153 | 0 | fname = arguments.AndroidMKFile; |
154 | 0 | android = true; |
155 | 0 | } else if (arguments.Filename.empty()) { |
156 | 0 | fname = arguments.ExportSetName + ".cmake"; |
157 | 0 | } else { |
158 | | // Make sure the file has a .cmake extension. |
159 | 0 | if (!cmHasSuffix(arguments.Filename, ".cmake"_s)) { |
160 | 0 | std::ostringstream e; |
161 | 0 | e << "FILE option given filename \"" << arguments.Filename |
162 | 0 | << "\" which does not have an extension of \".cmake\".\n"; |
163 | 0 | status.SetError(e.str()); |
164 | 0 | return false; |
165 | 0 | } |
166 | 0 | fname = arguments.Filename; |
167 | 0 | } |
168 | | |
169 | 0 | cmMakefile& mf = status.GetMakefile(); |
170 | | |
171 | | // Get the file to write. |
172 | 0 | if (cmSystemTools::FileIsFullPath(fname)) { |
173 | 0 | if (!mf.CanIWriteThisFile(fname)) { |
174 | 0 | std::ostringstream e; |
175 | 0 | e << "FILE option given filename \"" << fname |
176 | 0 | << "\" which is in the source tree.\n"; |
177 | 0 | status.SetError(e.str()); |
178 | 0 | return false; |
179 | 0 | } |
180 | 0 | } else { |
181 | | // Interpret relative paths with respect to the current build dir. |
182 | 0 | std::string const& dir = mf.GetCurrentBinaryDirectory(); |
183 | 0 | fname = cmStrCat(dir, '/', fname); |
184 | 0 | } |
185 | | |
186 | 0 | std::vector<cmExportBuildFileGenerator::TargetExport> targets; |
187 | 0 | cmGlobalGenerator* gg = mf.GetGlobalGenerator(); |
188 | |
|
189 | 0 | for (std::string const& currentTarget : *arguments.Targets) { |
190 | 0 | if (!ValidateExportableTarget(currentTarget, mf, gg, status)) { |
191 | 0 | return false; |
192 | 0 | } |
193 | 0 | targets.emplace_back(currentTarget, std::string{}); |
194 | 0 | } |
195 | 0 | if (arguments.Append) { |
196 | 0 | if (cmExportBuildFileGenerator* ebfg = gg->GetExportedTargetsFile(fname)) { |
197 | 0 | ebfg->AppendTargets(targets); |
198 | 0 | return true; |
199 | 0 | } |
200 | 0 | } |
201 | | |
202 | | // if cmExportBuildFileGenerator is already defined for the file |
203 | | // and APPEND is not specified, if CMP0103 is OLD ignore previous definition |
204 | | // else raise an error |
205 | 0 | if (gg->GetExportedTargetsFile(fname)) { |
206 | 0 | switch (mf.GetPolicyStatus(cmPolicies::CMP0103)) { |
207 | 0 | case cmPolicies::WARN: |
208 | 0 | mf.IssuePolicyWarning( |
209 | 0 | cmPolicies::CMP0103, {}, |
210 | 0 | cmStrCat("export() command already specified for the file\n ", |
211 | 0 | arguments.Filename, "\nDid you miss 'APPEND' keyword?")); |
212 | 0 | CM_FALLTHROUGH; |
213 | 0 | case cmPolicies::OLD: |
214 | 0 | break; |
215 | 0 | default: |
216 | 0 | status.SetError(cmStrCat("command already specified for the file\n ", |
217 | 0 | arguments.Filename, |
218 | 0 | "\nDid you miss 'APPEND' keyword?")); |
219 | 0 | return false; |
220 | 0 | } |
221 | 0 | } |
222 | | |
223 | 0 | cmDiagnosticContext context = cmExportBuildFileGenerator::CaptureContext(mf); |
224 | 0 | std::unique_ptr<cmExportBuildFileGenerator> ebfg = nullptr; |
225 | 0 | if (android) { |
226 | 0 | auto ebag = |
227 | 0 | cm::make_unique<cmExportBuildAndroidMKGenerator>(std::move(context)); |
228 | 0 | ebag->SetNamespace(arguments.Namespace); |
229 | 0 | ebag->SetAppendMode(arguments.Append); |
230 | 0 | ebfg = std::move(ebag); |
231 | 0 | } else { |
232 | 0 | auto ebcg = |
233 | 0 | cm::make_unique<cmExportBuildCMakeConfigGenerator>(std::move(context)); |
234 | 0 | ebcg->SetNamespace(arguments.Namespace); |
235 | 0 | ebcg->SetAppendMode(arguments.Append); |
236 | 0 | ebcg->SetExportOld(arguments.ExportOld); |
237 | 0 | ebfg = std::move(ebcg); |
238 | 0 | } |
239 | |
|
240 | 0 | ebfg->SetExportFile(fname.c_str()); |
241 | 0 | ebfg->SetCxxModuleDirectory(arguments.CxxModulesDirectory); |
242 | 0 | ebfg->SetTargets(targets); |
243 | 0 | std::vector<std::string> configurationTypes = |
244 | 0 | mf.GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig); |
245 | |
|
246 | 0 | for (std::string const& ct : configurationTypes) { |
247 | 0 | ebfg->AddConfiguration(ct); |
248 | 0 | } |
249 | 0 | gg->AddBuildExportSet(ebfg.get()); |
250 | 0 | mf.AddExportBuildFileGenerator(std::move(ebfg)); |
251 | |
|
252 | 0 | return true; |
253 | 0 | } |
254 | | |
255 | | static bool HandleExportMode(std::vector<std::string> const& args, |
256 | | cmExecutionStatus& status) |
257 | 0 | { |
258 | 0 | struct ExportArguments : public ArgumentParser::ParseResult |
259 | 0 | { |
260 | 0 | ArgumentParser::NonEmpty<std::string> ExportSetName; |
261 | 0 | ArgumentParser::MaybeEmpty<std::string> Namespace; |
262 | 0 | ArgumentParser::NonEmpty<std::string> Filename; |
263 | 0 | ArgumentParser::NonEmpty<std::string> CxxModulesDirectory; |
264 | 0 | bool ExportPackageDependencies = false; |
265 | 0 | }; |
266 | |
|
267 | 0 | auto parser = |
268 | 0 | cmArgumentParser<ExportArguments>{} |
269 | 0 | .Bind("EXPORT"_s, &ExportArguments::ExportSetName) |
270 | 0 | .Bind("NAMESPACE"_s, &ExportArguments::Namespace) |
271 | 0 | .Bind("FILE"_s, &ExportArguments::Filename) |
272 | 0 | .Bind("CXX_MODULES_DIRECTORY"_s, &ExportArguments::CxxModulesDirectory); |
273 | |
|
274 | 0 | if (cmExperimental::HasSupportEnabled( |
275 | 0 | status.GetMakefile(), |
276 | 0 | cmExperimental::Feature::ExportPackageDependencies)) { |
277 | 0 | parser.Bind("EXPORT_PACKAGE_DEPENDENCIES"_s, |
278 | 0 | &ExportArguments::ExportPackageDependencies); |
279 | 0 | } |
280 | |
|
281 | 0 | std::vector<std::string> unknownArgs; |
282 | 0 | ExportArguments arguments = parser.Parse(args, &unknownArgs); |
283 | |
|
284 | 0 | cmMakefile& mf = status.GetMakefile(); |
285 | 0 | cmGlobalGenerator* gg = mf.GetGlobalGenerator(); |
286 | |
|
287 | 0 | if (!arguments.Check(args[0], &unknownArgs, status)) { |
288 | 0 | cmPolicies::PolicyStatus const p = |
289 | 0 | status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0208); |
290 | 0 | if (!unknownArgs.empty() || p == cmPolicies::NEW) { |
291 | 0 | return false; |
292 | 0 | } |
293 | 0 | if (p == cmPolicies::WARN) { |
294 | 0 | status.GetMakefile().IssueDiagnostic( |
295 | 0 | cmDiagnostics::CMD_AUTHOR, cmStrCat("export "_s, status.GetError())); |
296 | 0 | status.GetMakefile().IssuePolicyWarning(cmPolicies::CMP0208); |
297 | 0 | } |
298 | 0 | } |
299 | | |
300 | 0 | std::string fname; |
301 | 0 | if (arguments.Filename.empty()) { |
302 | 0 | fname = arguments.ExportSetName + ".cmake"; |
303 | 0 | } else { |
304 | 0 | if (!cmHasSuffix(arguments.Filename, ".cmake"_s)) { |
305 | 0 | std::ostringstream e; |
306 | 0 | e << "FILE option given filename \"" << arguments.Filename |
307 | 0 | << "\" which does not have an extension of \".cmake\".\n"; |
308 | 0 | status.SetError(e.str()); |
309 | 0 | return false; |
310 | 0 | } |
311 | 0 | fname = arguments.Filename; |
312 | 0 | } |
313 | | |
314 | 0 | if (cmSystemTools::FileIsFullPath(fname)) { |
315 | 0 | if (!mf.CanIWriteThisFile(fname)) { |
316 | 0 | std::ostringstream e; |
317 | 0 | e << "FILE option given filename \"" << fname |
318 | 0 | << "\" which is in the source tree.\n"; |
319 | 0 | status.SetError(e.str()); |
320 | 0 | return false; |
321 | 0 | } |
322 | 0 | } else { |
323 | | // Interpret relative paths with respect to the current build dir. |
324 | 0 | std::string const& dir = mf.GetCurrentBinaryDirectory(); |
325 | 0 | fname = cmStrCat(dir, '/', fname); |
326 | 0 | } |
327 | | |
328 | 0 | cm::optional<cmExportSet*> const exportSet = |
329 | 0 | GetExportSet(arguments.ExportSetName, gg, status); |
330 | 0 | if (!exportSet) { |
331 | 0 | return false; |
332 | 0 | } |
333 | | |
334 | | // Set up export file generation. |
335 | 0 | auto ebcg = cm::make_unique<cmExportBuildCMakeConfigGenerator>( |
336 | 0 | cmExportBuildFileGenerator::CaptureContext(mf)); |
337 | 0 | ebcg->SetNamespace(arguments.Namespace); |
338 | 0 | ebcg->SetExportPackageDependencies(arguments.ExportPackageDependencies); |
339 | |
|
340 | 0 | AddExportGenerator(mf, gg, std::move(ebcg), fname, *exportSet, |
341 | 0 | arguments.CxxModulesDirectory); |
342 | 0 | return true; |
343 | 0 | } |
344 | | |
345 | | template <typename ArgumentsType, typename GeneratorType> |
346 | | static bool HandleSpecialExportMode(std::vector<std::string> const& args, |
347 | | cmExecutionStatus& status) |
348 | 0 | { |
349 | 0 | struct ExportArguments |
350 | 0 | : public ArgumentsType |
351 | 0 | , public ArgumentParser::ParseResult |
352 | 0 | { |
353 | 0 | ArgumentParser::NonEmpty<std::string> ExportSetName; |
354 | 0 | ArgumentParser::NonEmpty<std::string> CxxModulesDirectory; |
355 | |
|
356 | 0 | using ArgumentsType::Check; |
357 | 0 | using ArgumentParser::ParseResult::Check; |
358 | 0 | }; |
359 | |
|
360 | 0 | auto parser = |
361 | 0 | cmArgumentParser<ExportArguments>{} |
362 | 0 | .Bind("EXPORT"_s, &ExportArguments::ExportSetName) |
363 | 0 | .Bind("CXX_MODULES_DIRECTORY"_s, &ExportArguments::CxxModulesDirectory); |
364 | 0 | ArgumentsType::Bind(parser); |
365 | |
|
366 | 0 | std::vector<std::string> unknownArgs; |
367 | 0 | ExportArguments arguments = parser.Parse(args, &unknownArgs); |
368 | |
|
369 | 0 | if (!arguments.Check(args[0], &unknownArgs, status)) { |
370 | 0 | return false; |
371 | 0 | } |
372 | | |
373 | 0 | if (arguments.ExportSetName.empty()) { |
374 | 0 | status.SetError(cmStrCat(args[0], " missing EXPORT.")); |
375 | 0 | return false; |
376 | 0 | } |
377 | | |
378 | 0 | if (!arguments.Check(status) || !arguments.SetMetadataFromProject(status)) { |
379 | 0 | return false; |
380 | 0 | } |
381 | | |
382 | 0 | cmMakefile& mf = status.GetMakefile(); |
383 | 0 | cmGlobalGenerator* gg = mf.GetGlobalGenerator(); |
384 | |
|
385 | 0 | std::string const& dir = |
386 | 0 | arguments.GetDefaultDestination(mf.GetCurrentBinaryDirectory()); |
387 | 0 | std::string const fname = cmStrCat(dir, '/', arguments.GetPackageFileName()); |
388 | |
|
389 | 0 | if (gg->GetExportedTargetsFile(fname)) { |
390 | 0 | status.SetError(cmStrCat("command already specified for the file "_s, |
391 | 0 | cmSystemTools::GetFilenameNameView(fname), '.')); |
392 | 0 | return false; |
393 | 0 | } |
394 | | |
395 | | // Look up the export set |
396 | 0 | cm::optional<cmExportSet*> const exportSet = |
397 | 0 | GetExportSet(arguments.ExportSetName, gg, status); |
398 | 0 | if (!exportSet) { |
399 | 0 | return false; |
400 | 0 | } |
401 | | |
402 | | // Create the export build generator |
403 | 0 | auto ebpg = cm::make_unique<GeneratorType>( |
404 | 0 | arguments, cmExportBuildFileGenerator::CaptureContext(mf)); |
405 | 0 | AddExportGenerator(mf, gg, std::move(ebpg), fname, *exportSet, |
406 | 0 | arguments.CxxModulesDirectory); |
407 | 0 | return true; |
408 | 0 | } |
409 | | |
410 | | static bool HandlePackageInfoMode(std::vector<std::string> const& args, |
411 | | cmExecutionStatus& status) |
412 | 0 | { |
413 | 0 | using arg_t = cmPackageInfoArguments; |
414 | 0 | using gen_t = cmExportBuildPackageInfoGenerator; |
415 | 0 | return HandleSpecialExportMode<arg_t, gen_t>(args, status); |
416 | 0 | } |
417 | | |
418 | | static bool HandleSbomMode(std::vector<std::string> const& args, |
419 | | cmExecutionStatus& status) |
420 | 0 | { |
421 | 0 | if (!cmExperimental::HasSupportEnabled( |
422 | 0 | status.GetMakefile(), cmExperimental::Feature::GenerateSbom)) { |
423 | 0 | status.SetError("does not recognize sub-command SBOM"); |
424 | 0 | return false; |
425 | 0 | } |
426 | | |
427 | 0 | struct SbomExportArguments |
428 | 0 | : public cmSbomArguments |
429 | 0 | , public ArgumentParser::ParseResult |
430 | 0 | { |
431 | 0 | ArgumentParser::NonEmpty<std::vector<std::string>> ExportSetNames; |
432 | |
|
433 | 0 | using cmSbomArguments::Check; |
434 | 0 | using ArgumentParser::ParseResult::Check; |
435 | 0 | }; |
436 | |
|
437 | 0 | auto parser = cmArgumentParser<SbomExportArguments>{}; |
438 | 0 | cmSbomArguments::Bind(parser); |
439 | 0 | parser.Bind("EXPORTS"_s, &SbomExportArguments::ExportSetNames); |
440 | |
|
441 | 0 | std::vector<std::string> unknownArgs; |
442 | 0 | SbomExportArguments arguments = parser.Parse(args, &unknownArgs); |
443 | |
|
444 | 0 | if (!arguments.Check(args[0], &unknownArgs, status)) { |
445 | 0 | return false; |
446 | 0 | } |
447 | | |
448 | 0 | if (arguments.ExportSetNames.empty()) { |
449 | 0 | status.SetError(cmStrCat(args[0], " missing EXPORTS.")); |
450 | 0 | return false; |
451 | 0 | } |
452 | | |
453 | 0 | if (!arguments.Check(status) || !arguments.SetMetadataFromProject(status)) { |
454 | 0 | return false; |
455 | 0 | } |
456 | | |
457 | 0 | cmMakefile& mf = status.GetMakefile(); |
458 | 0 | cmGlobalGenerator* gg = mf.GetGlobalGenerator(); |
459 | |
|
460 | 0 | std::string const dir = |
461 | 0 | arguments.GetDefaultDestination(mf.GetCurrentBinaryDirectory()); |
462 | 0 | std::string const fpath = cmStrCat(dir, '/', arguments.GetPackageName()); |
463 | |
|
464 | 0 | if (gg->IsBuildSbomFile(fpath)) { |
465 | 0 | status.SetError(cmStrCat("SBOM command already specified for the file "_s, |
466 | 0 | cmSystemTools::GetFilenameNameView(fpath), '.')); |
467 | 0 | return false; |
468 | 0 | } |
469 | | |
470 | 0 | std::vector<cmExportSet*> sets; |
471 | 0 | sets.reserve(arguments.ExportSetNames.size()); |
472 | 0 | for (std::string const& name : arguments.ExportSetNames) { |
473 | 0 | cm::optional<cmExportSet*> const exportSet = |
474 | 0 | GetExportSet(name, gg, status); |
475 | 0 | if (!exportSet) { |
476 | 0 | return false; |
477 | 0 | } |
478 | 0 | sets.push_back(*exportSet); |
479 | 0 | } |
480 | | |
481 | 0 | auto builder = cm::make_unique<cmBuildSbomGenerator>(arguments, sets, fpath); |
482 | 0 | cmBuildSbomGenerator* rawPtr = builder.get(); |
483 | 0 | mf.AddBuildSbomGenerator(std::move(builder)); |
484 | 0 | gg->AddBuildSbomGenerator(rawPtr); |
485 | 0 | return true; |
486 | 0 | } |
487 | | |
488 | | static bool HandleSetupMode(std::vector<std::string> const& args, |
489 | | cmExecutionStatus& status) |
490 | 0 | { |
491 | 0 | struct SetupArguments |
492 | 0 | { |
493 | 0 | ArgumentParser::NonEmpty<std::string> ExportSetName; |
494 | 0 | ArgumentParser::NonEmpty<std::string> CxxModulesDirectory; |
495 | 0 | std::vector<std::vector<std::string>> PackageDependencyArgs; |
496 | 0 | std::vector<std::vector<std::string>> TargetArgs; |
497 | 0 | }; |
498 | |
|
499 | 0 | auto parser = cmArgumentParser<SetupArguments>{}; |
500 | 0 | parser.Bind("SETUP"_s, &SetupArguments::ExportSetName); |
501 | 0 | if (cmExperimental::HasSupportEnabled( |
502 | 0 | status.GetMakefile(), |
503 | 0 | cmExperimental::Feature::ExportPackageDependencies)) { |
504 | 0 | parser.Bind("PACKAGE_DEPENDENCY"_s, |
505 | 0 | &SetupArguments::PackageDependencyArgs); |
506 | 0 | } |
507 | 0 | parser.Bind("TARGET"_s, &SetupArguments::TargetArgs); |
508 | |
|
509 | 0 | std::vector<std::string> unknownArgs; |
510 | 0 | SetupArguments arguments = parser.Parse(args, &unknownArgs); |
511 | |
|
512 | 0 | if (!unknownArgs.empty()) { |
513 | 0 | status.SetError("SETUP given unknown argument: \"" + unknownArgs.front() + |
514 | 0 | "\"."); |
515 | 0 | return false; |
516 | 0 | } |
517 | | |
518 | 0 | cmMakefile& mf = status.GetMakefile(); |
519 | 0 | cmGlobalGenerator* gg = mf.GetGlobalGenerator(); |
520 | |
|
521 | 0 | cmExportSetMap& setMap = gg->GetExportSets(); |
522 | 0 | auto& exportSet = setMap[arguments.ExportSetName]; |
523 | |
|
524 | 0 | struct PackageDependencyArguments |
525 | 0 | { |
526 | 0 | std::string Enabled; |
527 | 0 | ArgumentParser::MaybeEmpty<std::vector<std::string>> ExtraArgs; |
528 | 0 | }; |
529 | |
|
530 | 0 | auto packageDependencyParser = |
531 | 0 | cmArgumentParser<PackageDependencyArguments>{} |
532 | 0 | .Bind("ENABLED"_s, &PackageDependencyArguments::Enabled) |
533 | 0 | .Bind("EXTRA_ARGS"_s, &PackageDependencyArguments::ExtraArgs); |
534 | |
|
535 | 0 | for (auto const& packageDependencyArgs : arguments.PackageDependencyArgs) { |
536 | 0 | if (packageDependencyArgs.empty()) { |
537 | 0 | continue; |
538 | 0 | } |
539 | 0 | PackageDependencyArguments const packageDependencyArguments = |
540 | 0 | packageDependencyParser.Parse( |
541 | 0 | cmMakeRange(packageDependencyArgs).advance(1), &unknownArgs); |
542 | |
|
543 | 0 | if (!unknownArgs.empty()) { |
544 | 0 | status.SetError(cmStrCat("PACKAGE_DEPENDENCY given unknown argument: \"", |
545 | 0 | unknownArgs.front(), "\".")); |
546 | 0 | return false; |
547 | 0 | } |
548 | 0 | auto& packageDependency = |
549 | 0 | exportSet.GetPackageDependencyForSetup(packageDependencyArgs.front()); |
550 | 0 | if (!packageDependencyArguments.Enabled.empty()) { |
551 | 0 | if (packageDependencyArguments.Enabled == "AUTO") { |
552 | 0 | packageDependency.Enabled = |
553 | 0 | cmExportSet::PackageDependencyExportEnabled::Auto; |
554 | 0 | } else if (cmIsOff(packageDependencyArguments.Enabled)) { |
555 | 0 | packageDependency.Enabled = |
556 | 0 | cmExportSet::PackageDependencyExportEnabled::Off; |
557 | 0 | } else if (cmIsOn(packageDependencyArguments.Enabled)) { |
558 | 0 | packageDependency.Enabled = |
559 | 0 | cmExportSet::PackageDependencyExportEnabled::On; |
560 | 0 | } else { |
561 | 0 | status.SetError( |
562 | 0 | cmStrCat("Invalid enable setting for package dependency: \"", |
563 | 0 | packageDependencyArguments.Enabled, '"')); |
564 | 0 | return false; |
565 | 0 | } |
566 | 0 | } |
567 | 0 | cm::append(packageDependency.ExtraArguments, |
568 | 0 | packageDependencyArguments.ExtraArgs); |
569 | 0 | } |
570 | | |
571 | 0 | struct TargetArguments |
572 | 0 | { |
573 | 0 | std::string XcFrameworkLocation; |
574 | 0 | }; |
575 | |
|
576 | 0 | auto targetParser = cmArgumentParser<TargetArguments>{}.Bind( |
577 | 0 | "XCFRAMEWORK_LOCATION"_s, &TargetArguments::XcFrameworkLocation); |
578 | |
|
579 | 0 | for (auto const& targetArgs : arguments.TargetArgs) { |
580 | 0 | if (targetArgs.empty()) { |
581 | 0 | continue; |
582 | 0 | } |
583 | 0 | TargetArguments const targetArguments = |
584 | 0 | targetParser.Parse(cmMakeRange(targetArgs).advance(1), &unknownArgs); |
585 | |
|
586 | 0 | if (!unknownArgs.empty()) { |
587 | 0 | status.SetError(cmStrCat("TARGET given unknown argument: \"", |
588 | 0 | unknownArgs.front(), "\".")); |
589 | 0 | return false; |
590 | 0 | } |
591 | 0 | exportSet.SetXcFrameworkLocation(targetArgs.front(), |
592 | 0 | targetArguments.XcFrameworkLocation); |
593 | 0 | } |
594 | 0 | return true; |
595 | 0 | } |
596 | | |
597 | | static bool HandlePackageMode(std::vector<std::string> const& args, |
598 | | cmExecutionStatus& status) |
599 | 0 | { |
600 | | // Parse PACKAGE mode arguments. |
601 | 0 | enum Doing |
602 | 0 | { |
603 | 0 | DoingNone, |
604 | 0 | DoingPackage |
605 | 0 | }; |
606 | 0 | Doing doing = DoingPackage; |
607 | 0 | std::string package; |
608 | 0 | for (unsigned int i = 1; i < args.size(); ++i) { |
609 | 0 | if (doing == DoingPackage) { |
610 | 0 | package = args[i]; |
611 | 0 | doing = DoingNone; |
612 | 0 | } else { |
613 | 0 | std::ostringstream e; |
614 | 0 | e << "PACKAGE given unknown argument: " << args[i]; |
615 | 0 | status.SetError(e.str()); |
616 | 0 | return false; |
617 | 0 | } |
618 | 0 | } |
619 | | |
620 | | // Verify the package name. |
621 | 0 | if (package.empty()) { |
622 | 0 | status.SetError("PACKAGE must be given a package name."); |
623 | 0 | return false; |
624 | 0 | } |
625 | 0 | char const* packageExpr = "^[A-Za-z0-9_.-]+$"; |
626 | 0 | cmsys::RegularExpression packageRegex(packageExpr); |
627 | 0 | if (!packageRegex.find(package)) { |
628 | 0 | std::ostringstream e; |
629 | 0 | e << "PACKAGE given invalid package name \"" << package << "\". " |
630 | 0 | << "Package names must match \"" << packageExpr << "\"."; |
631 | 0 | status.SetError(e.str()); |
632 | 0 | return false; |
633 | 0 | } |
634 | | |
635 | 0 | cmMakefile& mf = status.GetMakefile(); |
636 | | |
637 | | // CMP0090 decides both the default and what variable changes it. |
638 | 0 | switch (mf.GetPolicyStatus(cmPolicies::CMP0090)) { |
639 | 0 | case cmPolicies::WARN: |
640 | 0 | CM_FALLTHROUGH; |
641 | 0 | case cmPolicies::OLD: |
642 | | // Default is to export, but can be disabled. |
643 | 0 | if (mf.IsOn("CMAKE_EXPORT_NO_PACKAGE_REGISTRY")) { |
644 | 0 | return true; |
645 | 0 | } |
646 | 0 | break; |
647 | 0 | case cmPolicies::NEW: |
648 | | // Default is to not export, but can be enabled. |
649 | 0 | if (!mf.IsOn("CMAKE_EXPORT_PACKAGE_REGISTRY")) { |
650 | 0 | return true; |
651 | 0 | } |
652 | 0 | break; |
653 | 0 | } |
654 | | |
655 | | // We store the current build directory in the registry as a value |
656 | | // named by a hash of its own content. This is deterministic and is |
657 | | // unique with high probability. |
658 | 0 | std::string const& outDir = mf.GetCurrentBinaryDirectory(); |
659 | 0 | cmCryptoHash hasher(cmCryptoHash::AlgoMD5); |
660 | 0 | std::string hash = hasher.HashString(outDir); |
661 | 0 | StorePackageRegistry(mf, package, outDir.c_str(), hash.c_str()); |
662 | |
|
663 | 0 | return true; |
664 | 0 | } |
665 | | |
666 | | #if defined(_WIN32) && !defined(__CYGWIN__) |
667 | | |
668 | | static void ReportRegistryError(cmMakefile& mf, std::string const& msg, |
669 | | std::string const& key, long err) |
670 | | { |
671 | | std::ostringstream e; |
672 | | e << msg << "\n" |
673 | | << " HKEY_CURRENT_USER\\" << key << "\n"; |
674 | | wchar_t winmsg[1024]; |
675 | | if (FormatMessageW( |
676 | | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, err, |
677 | | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), winmsg, 1024, 0) > 0) { |
678 | | e << "Windows reported:\n" |
679 | | << " " << cmsys::Encoding::ToNarrow(winmsg); |
680 | | } |
681 | | mf.IssueMessage(MessageType::WARNING, e.str()); |
682 | | } |
683 | | |
684 | | static void StorePackageRegistry(cmMakefile& mf, std::string const& package, |
685 | | char const* content, char const* hash) |
686 | | { |
687 | | std::string key = cmStrCat("Software\\Kitware\\CMake\\Packages\\", package); |
688 | | HKEY hKey; |
689 | | LONG err = |
690 | | RegCreateKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(key).c_str(), 0, |
691 | | 0, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, 0, &hKey, 0); |
692 | | if (err != ERROR_SUCCESS) { |
693 | | ReportRegistryError(mf, "Cannot create/open registry key", key, err); |
694 | | return; |
695 | | } |
696 | | |
697 | | std::wstring wcontent = cmsys::Encoding::ToWide(content); |
698 | | err = |
699 | | RegSetValueExW(hKey, cmsys::Encoding::ToWide(hash).c_str(), 0, REG_SZ, |
700 | | (BYTE const*)wcontent.c_str(), |
701 | | static_cast<DWORD>(wcontent.size() + 1) * sizeof(wchar_t)); |
702 | | RegCloseKey(hKey); |
703 | | if (err != ERROR_SUCCESS) { |
704 | | std::ostringstream msg; |
705 | | msg << "Cannot set registry value \"" << hash << "\" under key"; |
706 | | ReportRegistryError(mf, msg.str(), key, err); |
707 | | return; |
708 | | } |
709 | | } |
710 | | #else |
711 | | static void StorePackageRegistry(cmMakefile& mf, std::string const& package, |
712 | | char const* content, char const* hash) |
713 | 0 | { |
714 | | # if defined(__HAIKU__) |
715 | | char dir[B_PATH_NAME_LENGTH]; |
716 | | if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, false, dir, sizeof(dir)) != |
717 | | B_OK) { |
718 | | return; |
719 | | } |
720 | | std::string fname = cmStrCat(dir, "/cmake/packages/", package); |
721 | | # else |
722 | 0 | std::string fname; |
723 | 0 | if (!cmSystemTools::GetEnv("HOME", fname)) { |
724 | 0 | return; |
725 | 0 | } |
726 | 0 | cmSystemTools::ConvertToUnixSlashes(fname); |
727 | 0 | fname += "/.cmake/packages/"; |
728 | 0 | fname += package; |
729 | 0 | # endif |
730 | 0 | cmSystemTools::MakeDirectory(fname); |
731 | 0 | fname += "/"; |
732 | 0 | fname += hash; |
733 | 0 | if (!cmSystemTools::FileExists(fname)) { |
734 | 0 | cmGeneratedFileStream entry(fname, true); |
735 | 0 | if (entry) { |
736 | 0 | entry << content << "\n"; |
737 | 0 | } else { |
738 | 0 | mf.IssueMessage(MessageType::WARNING, |
739 | 0 | cmStrCat("Cannot create package registry file:\n" |
740 | 0 | " ", |
741 | 0 | fname, '\n', |
742 | 0 | cmSystemTools::GetLastSystemError(), '\n')); |
743 | 0 | } |
744 | 0 | } |
745 | 0 | } |
746 | | #endif |
747 | | |
748 | | bool cmExportCommand(std::vector<std::string> const& args, |
749 | | cmExecutionStatus& status) |
750 | 0 | { |
751 | 0 | if (args.empty()) { |
752 | 0 | return true; |
753 | 0 | } |
754 | | |
755 | 0 | static cmSubcommandTable const subcommand{ |
756 | 0 | { "TARGETS"_s, HandleTargetsMode }, |
757 | 0 | { "EXPORT"_s, HandleExportMode }, |
758 | 0 | { "SETUP"_s, HandleSetupMode }, |
759 | 0 | { "PACKAGE"_s, HandlePackageMode }, |
760 | 0 | { "PACKAGE_INFO"_s, HandlePackageInfoMode }, |
761 | 0 | { "SBOM"_s, HandleSbomMode }, |
762 | 0 | }; |
763 | |
|
764 | 0 | return subcommand(args[0], args, status); |
765 | 0 | } |