Coverage Report

Created: 2026-06-15 07:03

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