Coverage Report

Created: 2026-02-09 06:05

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