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