Coverage Report

Created: 2026-07-30 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmInstallCommand.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 "cmInstallCommand.h"
4
5
#include <algorithm>
6
#include <cassert>
7
#include <cstddef>
8
#include <iterator>
9
#include <map>
10
#include <set>
11
#include <utility>
12
13
#include <cm/memory>
14
#include <cm/optional>
15
#include <cm/string_view>
16
#include <cmext/string_view>
17
18
#include "cmsys/Glob.hxx"
19
20
#include "cmArgumentParser.h"
21
#include "cmArgumentParserTypes.h"
22
#include "cmCMakePath.h"
23
#include "cmDiagnosticContext.h"
24
#include "cmDiagnostics.h"
25
#include "cmExecutionStatus.h"
26
#include "cmExperimental.h"
27
#include "cmExportSet.h"
28
#include "cmGeneratorExpression.h"
29
#include "cmGlobalGenerator.h"
30
#include "cmInstallAndroidMKExportGenerator.h"
31
#include "cmInstallCMakeConfigExportGenerator.h"
32
#include "cmInstallCommandArguments.h"
33
#include "cmInstallCxxModuleBmiGenerator.h"
34
#include "cmInstallDirectoryGenerator.h"
35
#include "cmInstallDirs.h"
36
#include "cmInstallFileSetGenerator.h"
37
#include "cmInstallFilesGenerator.h"
38
#include "cmInstallGenerator.h"
39
#include "cmInstallGetRuntimeDependenciesGenerator.h"
40
#include "cmInstallImportedRuntimeArtifactsGenerator.h"
41
#include "cmInstallPackageInfoExportGenerator.h"
42
#include "cmInstallRuntimeDependencySet.h"
43
#include "cmInstallRuntimeDependencySetGenerator.h"
44
#include "cmInstallSbomGenerator.h"
45
#include "cmInstallScriptGenerator.h"
46
#include "cmInstallTargetGenerator.h"
47
#include "cmList.h"
48
#include "cmMakefile.h"
49
#include "cmMessageType.h"
50
#include "cmPackageInfoArguments.h"
51
#include "cmPolicies.h"
52
#include "cmRange.h"
53
#include "cmRuntimeDependencyArchive.h"
54
#include "cmSbomArguments.h"
55
#include "cmStringAlgorithms.h"
56
#include "cmSubcommandTable.h"
57
#include "cmSystemTools.h"
58
#include "cmTarget.h"
59
#include "cmTargetExport.h"
60
#include "cmTargetTypes.h"
61
#include "cmValue.h"
62
63
namespace {
64
65
struct RuntimeDependenciesArgs
66
{
67
  ArgumentParser::MaybeEmpty<std::vector<std::string>> Directories;
68
  ArgumentParser::MaybeEmpty<std::vector<std::string>> PreIncludeRegexes;
69
  ArgumentParser::MaybeEmpty<std::vector<std::string>> PreExcludeRegexes;
70
  ArgumentParser::MaybeEmpty<std::vector<std::string>> PostIncludeRegexes;
71
  ArgumentParser::MaybeEmpty<std::vector<std::string>> PostExcludeRegexes;
72
  ArgumentParser::MaybeEmpty<std::vector<std::string>> PostIncludeFiles;
73
  ArgumentParser::MaybeEmpty<std::vector<std::string>> PostExcludeFiles;
74
};
75
76
auto const RuntimeDependenciesArgHelper =
77
  cmArgumentParser<RuntimeDependenciesArgs>{}
78
    .Bind("DIRECTORIES"_s, &RuntimeDependenciesArgs::Directories)
79
    .Bind("PRE_INCLUDE_REGEXES"_s, &RuntimeDependenciesArgs::PreIncludeRegexes)
80
    .Bind("PRE_EXCLUDE_REGEXES"_s, &RuntimeDependenciesArgs::PreExcludeRegexes)
81
    .Bind("POST_INCLUDE_REGEXES"_s,
82
          &RuntimeDependenciesArgs::PostIncludeRegexes)
83
    .Bind("POST_EXCLUDE_REGEXES"_s,
84
          &RuntimeDependenciesArgs::PostExcludeRegexes)
85
    .Bind("POST_INCLUDE_FILES"_s, &RuntimeDependenciesArgs::PostIncludeFiles)
86
    .Bind("POST_EXCLUDE_FILES"_s, &RuntimeDependenciesArgs::PostExcludeFiles);
87
88
class Helper
89
{
90
public:
91
  Helper(cmExecutionStatus& status)
92
0
    : Status(status)
93
0
    , Makefile(&status.GetMakefile())
94
0
  {
95
0
    this->DefaultComponentName = this->Makefile->GetSafeDefinition(
96
0
      "CMAKE_INSTALL_DEFAULT_COMPONENT_NAME");
97
0
    if (this->DefaultComponentName == "<PROJECT_NAME>") {
98
0
      cmValue projectName = this->Makefile->GetDefinition("PROJECT_NAME");
99
0
      if (!projectName->empty()) {
100
0
        this->DefaultComponentName = projectName;
101
0
      } else {
102
0
        this->DefaultComponentName = "Unspecified";
103
0
      }
104
0
    }
105
0
    if (this->DefaultComponentName.empty()) {
106
0
      this->DefaultComponentName = "Unspecified";
107
0
    }
108
0
  }
109
110
  cmDiagnosticContext CaptureContext() const
111
0
  {
112
0
    return cmInstallGenerator::CaptureContext(this->Makefile);
113
0
  }
114
115
0
  void SetError(std::string const& err) { this->Status.SetError(err); }
116
117
  bool MakeFilesFullPath(char const* modeName,
118
                         std::vector<std::string> const& relFiles,
119
                         std::vector<std::string>& absFiles);
120
  bool MakeFilesFullPath(char const* modeName, std::string const& basePath,
121
                         std::vector<std::string> const& relFiles,
122
                         std::vector<std::string>& absFiles);
123
124
  std::string GetRuntimeDestination(
125
    cmInstallCommandArguments const* args) const;
126
  std::string GetArchiveDestination(
127
    cmInstallCommandArguments const* args) const;
128
  std::string GetLibraryDestination(
129
    cmInstallCommandArguments const* args) const;
130
  std::string GetCxxModulesBmiDestination(
131
    cmInstallCommandArguments const* args) const;
132
  std::string GetIncludeDestination(
133
    cmInstallCommandArguments const* args) const;
134
  std::string GetDestinationForType(cmInstallCommandArguments const* args,
135
                                    std::string const& type) const;
136
137
  cmExecutionStatus& Status;
138
  cmMakefile* Makefile;
139
  std::string DefaultComponentName;
140
};
141
142
std::unique_ptr<cmInstallTargetGenerator> CreateInstallTargetGenerator(
143
  cmTarget& target, cmInstallCommandArguments const& args, bool impLib,
144
  cmDiagnosticContext context, std::string const& destination,
145
  bool forceOpt = false, bool namelink = false)
146
0
{
147
0
  cmInstallGenerator::MessageLevel message =
148
0
    cmInstallGenerator::SelectMessageLevel(target.GetMakefile());
149
0
  target.SetHaveInstallRule(true);
150
0
  std::string const& component =
151
0
    namelink ? args.GetNamelinkComponent() : args.GetComponent();
152
0
  auto g = cm::make_unique<cmInstallTargetGenerator>(
153
0
    target.GetName(), destination, impLib, args.GetPermissions(),
154
0
    args.GetConfigurations(), component, message, args.GetExcludeFromAll(),
155
0
    args.GetOptional() || forceOpt, std::move(context));
156
0
  target.AddInstallGenerator(g.get());
157
0
  return g;
158
0
}
159
160
std::unique_ptr<cmInstallTargetGenerator> CreateInstallTargetGenerator(
161
  cmTarget& target, cmInstallCommandArguments const& args, bool impLib,
162
  cmDiagnosticContext context, bool forceOpt = false, bool namelink = false)
163
0
{
164
0
  return CreateInstallTargetGenerator(target, args, impLib, std::move(context),
165
0
                                      args.GetDestination(), forceOpt,
166
0
                                      namelink);
167
0
}
168
169
std::unique_ptr<cmInstallFilesGenerator> CreateInstallFilesGenerator(
170
  cmMakefile* mf, std::vector<std::string> const& absFiles,
171
  cmInstallCommandArguments const& args, bool programs,
172
  std::string const& destination)
173
0
{
174
0
  cmInstallGenerator::MessageLevel message =
175
0
    cmInstallGenerator::SelectMessageLevel(mf);
176
0
  return cm::make_unique<cmInstallFilesGenerator>(
177
0
    absFiles, destination, programs, args.GetPermissions(),
178
0
    args.GetConfigurations(), args.GetComponent(), message,
179
0
    args.GetExcludeFromAll(), args.GetRename(), args.GetOptional(),
180
0
    cmInstallGenerator::CaptureContext(mf));
181
0
}
182
183
std::unique_ptr<cmInstallFilesGenerator> CreateInstallFilesGenerator(
184
  cmMakefile* mf, std::vector<std::string> const& absFiles,
185
  cmInstallCommandArguments const& args, bool programs)
186
0
{
187
0
  return CreateInstallFilesGenerator(mf, absFiles, args, programs,
188
0
                                     args.GetDestination());
189
0
}
190
191
std::unique_ptr<cmInstallFileSetGenerator> CreateInstallFileSetGenerator(
192
  Helper& helper, cmTarget& target,
193
  cmInstallCommandFileSetArguments const& args)
194
0
{
195
0
  cmInstallGenerator::MessageLevel message =
196
0
    cmInstallGenerator::SelectMessageLevel(helper.Makefile);
197
0
  return cm::make_unique<cmInstallFileSetGenerator>(
198
0
    target.GetName(), args.GetFileSet(), args.GetDestination(),
199
0
    args.GetPermissions(), args.GetConfigurations(), args.GetComponent(),
200
0
    message, args.GetExcludeFromAll(), args.GetOptional(),
201
0
    helper.CaptureContext());
202
0
}
203
204
void AddInstallRuntimeDependenciesGenerator(
205
  Helper& helper, cmInstallRuntimeDependencySet* runtimeDependencySet,
206
  cmInstallCommandArguments const& runtimeArgs,
207
  cmInstallCommandArguments const& libraryArgs,
208
  cmInstallCommandArguments const& frameworkArgs,
209
  RuntimeDependenciesArgs runtimeDependenciesArgs, bool& installsRuntime,
210
  bool& installsLibrary, bool& installsFramework)
211
0
{
212
0
  bool dllPlatform =
213
0
    !helper.Makefile->GetSafeDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX").empty();
214
0
  bool apple =
215
0
    helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME") == "Darwin";
216
0
  auto const& runtimeDependenciesArgsRef =
217
0
    dllPlatform ? runtimeArgs : libraryArgs;
218
0
  std::vector<std::string> configurations =
219
0
    runtimeDependenciesArgsRef.GetConfigurations();
220
0
  if (apple) {
221
0
    std::copy(frameworkArgs.GetConfigurations().begin(),
222
0
              frameworkArgs.GetConfigurations().end(),
223
0
              std::back_inserter(configurations));
224
0
  }
225
226
  // Create file(GET_RUNTIME_DEPENDENCIES) generator.
227
0
  auto getRuntimeDependenciesGenerator =
228
0
    cm::make_unique<cmInstallGetRuntimeDependenciesGenerator>(
229
0
      runtimeDependencySet, std::move(runtimeDependenciesArgs.Directories),
230
0
      std::move(runtimeDependenciesArgs.PreIncludeRegexes),
231
0
      std::move(runtimeDependenciesArgs.PreExcludeRegexes),
232
0
      std::move(runtimeDependenciesArgs.PostIncludeRegexes),
233
0
      std::move(runtimeDependenciesArgs.PostExcludeRegexes),
234
0
      std::move(runtimeDependenciesArgs.PostIncludeFiles),
235
0
      std::move(runtimeDependenciesArgs.PostExcludeFiles),
236
0
      runtimeDependenciesArgsRef.GetComponent(),
237
0
      apple ? frameworkArgs.GetComponent() : "", true, "_CMAKE_DEPS",
238
0
      "_CMAKE_RPATH", configurations,
239
0
      cmInstallGenerator::SelectMessageLevel(helper.Makefile),
240
0
      runtimeDependenciesArgsRef.GetExcludeFromAll() &&
241
0
        (apple ? frameworkArgs.GetExcludeFromAll() : true),
242
0
      helper.Makefile->GetBacktrace(),
243
0
      helper.Makefile->GetPolicyStatus(cmPolicies::CMP0207));
244
0
  helper.Makefile->AddInstallGenerator(
245
0
    std::move(getRuntimeDependenciesGenerator));
246
247
  // Create the library dependencies generator.
248
0
  auto libraryRuntimeDependenciesGenerator =
249
0
    cm::make_unique<cmInstallRuntimeDependencySetGenerator>(
250
0
      cmInstallRuntimeDependencySetGenerator::DependencyType::Library,
251
0
      runtimeDependencySet, std::vector<std::string>{}, true, std::string{},
252
0
      true, "_CMAKE_DEPS", "_CMAKE_RPATH", "_CMAKE_TMP",
253
0
      dllPlatform ? helper.GetRuntimeDestination(&runtimeArgs)
254
0
                  : helper.GetLibraryDestination(&libraryArgs),
255
0
      runtimeDependenciesArgsRef.GetConfigurations(),
256
0
      runtimeDependenciesArgsRef.GetComponent(),
257
0
      runtimeDependenciesArgsRef.GetPermissions(),
258
0
      cmInstallGenerator::SelectMessageLevel(helper.Makefile),
259
0
      runtimeDependenciesArgsRef.GetExcludeFromAll(), helper.CaptureContext());
260
0
  helper.Makefile->AddInstallGenerator(
261
0
    std::move(libraryRuntimeDependenciesGenerator));
262
0
  if (dllPlatform) {
263
0
    installsRuntime = true;
264
0
  } else {
265
0
    installsLibrary = true;
266
0
  }
267
268
0
  if (apple) {
269
    // Create the framework dependencies generator.
270
0
    auto frameworkRuntimeDependenciesGenerator =
271
0
      cm::make_unique<cmInstallRuntimeDependencySetGenerator>(
272
0
        cmInstallRuntimeDependencySetGenerator::DependencyType::Framework,
273
0
        runtimeDependencySet, std::vector<std::string>{}, true, std::string{},
274
0
        true, "_CMAKE_DEPS", "_CMAKE_RPATH", "_CMAKE_TMP",
275
0
        frameworkArgs.GetDestination(), frameworkArgs.GetConfigurations(),
276
0
        frameworkArgs.GetComponent(), frameworkArgs.GetPermissions(),
277
0
        cmInstallGenerator::SelectMessageLevel(helper.Makefile),
278
0
        frameworkArgs.GetExcludeFromAll(), helper.CaptureContext());
279
0
    helper.Makefile->AddInstallGenerator(
280
0
      std::move(frameworkRuntimeDependenciesGenerator));
281
0
    installsFramework = true;
282
0
  }
283
0
}
284
285
std::set<std::string> const allowedTypes{
286
  "BIN",         "SBIN",       "LIB",      "INCLUDE", "SYSCONF",
287
  "SHAREDSTATE", "LOCALSTATE", "RUNSTATE", "DATA",    "INFO",
288
  "LOCALE",      "MAN",        "DOC",      "LIBEXEC",
289
};
290
291
template <typename T>
292
bool AddBundleExecutable(Helper& helper,
293
                         cmInstallRuntimeDependencySet* runtimeDependencySet,
294
                         T&& bundleExecutable)
295
0
{
296
0
  if (!runtimeDependencySet->AddBundleExecutable(bundleExecutable)) {
297
0
    helper.SetError(
298
0
      "A runtime dependency set may only have one bundle executable.");
299
0
    return false;
300
0
  }
301
0
  return true;
302
0
}
Unexecuted instantiation: cmInstallCommand.cxx:bool (anonymous namespace)::AddBundleExecutable<cmInstallTargetGenerator*>((anonymous namespace)::Helper&, cmInstallRuntimeDependencySet*, cmInstallTargetGenerator*&&)
Unexecuted instantiation: cmInstallCommand.cxx:bool (anonymous namespace)::AddBundleExecutable<cmInstallImportedRuntimeArtifactsGenerator*>((anonymous namespace)::Helper&, cmInstallRuntimeDependencySet*, cmInstallImportedRuntimeArtifactsGenerator*&&)
303
304
void CheckAbsoluteDestination(Helper& helper, std::string const& destination)
305
0
{
306
  // Check for an absolute destination.
307
0
  if (cmGeneratorExpression::Find(destination) == std::string::npos &&
308
0
      cmSystemTools::FileIsFullPath(destination)) {
309
0
    helper.Makefile->IssueDiagnostic(
310
0
      cmDiagnostics::CMD_INSTALL_ABSOLUTE_DESTINATION,
311
0
      cmStrCat("INSTALL command given absolute DESTINATION path:\n  ",
312
0
               destination));
313
0
  }
314
0
}
315
316
bool HandleScriptMode(std::vector<std::string> const& args,
317
                      cmExecutionStatus& status)
318
0
{
319
0
  Helper helper(status);
320
321
0
  std::string component = helper.DefaultComponentName;
322
0
  int componentCount = 0;
323
0
  bool doingScript = false;
324
0
  bool doingCode = false;
325
0
  bool excludeFromAll = false;
326
0
  bool allComponents = false;
327
328
  // Scan the args once for COMPONENT. Only allow one.
329
  //
330
0
  for (size_t i = 0; i < args.size(); ++i) {
331
0
    if (args[i] == "COMPONENT" && i + 1 < args.size()) {
332
0
      ++componentCount;
333
0
      ++i;
334
0
      component = args[i];
335
0
    }
336
0
    if (args[i] == "EXCLUDE_FROM_ALL") {
337
0
      excludeFromAll = true;
338
0
    } else if (args[i] == "ALL_COMPONENTS") {
339
0
      allComponents = true;
340
0
    }
341
0
  }
342
343
0
  if (componentCount > 1) {
344
0
    status.SetError("given more than one COMPONENT for the SCRIPT or CODE "
345
0
                    "signature of the INSTALL command. "
346
0
                    "Use multiple INSTALL commands with one COMPONENT each.");
347
0
    return false;
348
0
  }
349
350
0
  if (allComponents && componentCount == 1) {
351
0
    status.SetError("ALL_COMPONENTS and COMPONENT are mutually exclusive");
352
0
    return false;
353
0
  }
354
355
  // Scan the args again, this time adding install generators each time we
356
  // encounter a SCRIPT or CODE arg:
357
  //
358
0
  for (std::string const& arg : args) {
359
0
    if (arg == "SCRIPT") {
360
0
      doingScript = true;
361
0
      doingCode = false;
362
0
    } else if (arg == "CODE") {
363
0
      doingScript = false;
364
0
      doingCode = true;
365
0
    } else if (arg == "COMPONENT") {
366
0
      doingScript = false;
367
0
      doingCode = false;
368
0
    } else if (doingScript) {
369
0
      doingScript = false;
370
0
      std::string script = arg;
371
0
      if (!cmHasLiteralPrefix(script, "$<INSTALL_PREFIX>")) {
372
0
        if (!cmSystemTools::FileIsFullPath(script)) {
373
0
          script =
374
0
            cmStrCat(helper.Makefile->GetCurrentSourceDirectory(), '/', arg);
375
0
        }
376
0
        if (cmSystemTools::FileIsDirectory(script)) {
377
0
          status.SetError("given a directory as value of SCRIPT argument.");
378
0
          return false;
379
0
        }
380
0
      }
381
0
      helper.Makefile->AddInstallGenerator(
382
0
        cm::make_unique<cmInstallScriptGenerator>(
383
0
          script, false, component, excludeFromAll, allComponents,
384
0
          helper.Makefile->GetBacktrace()));
385
0
    } else if (doingCode) {
386
0
      doingCode = false;
387
0
      std::string const& code = arg;
388
0
      helper.Makefile->AddInstallGenerator(
389
0
        cm::make_unique<cmInstallScriptGenerator>(
390
0
          code, true, component, excludeFromAll, allComponents,
391
0
          helper.Makefile->GetBacktrace()));
392
0
    }
393
0
  }
394
395
0
  if (doingScript) {
396
0
    status.SetError("given no value for SCRIPT argument.");
397
0
    return false;
398
0
  }
399
0
  if (doingCode) {
400
0
    status.SetError("given no value for CODE argument.");
401
0
    return false;
402
0
  }
403
404
  // Tell the global generator about any installation component names
405
  // specified.
406
0
  helper.Makefile->GetGlobalGenerator()->AddInstallComponent(component);
407
408
0
  return true;
409
0
}
410
411
bool HandleTargetsMode(std::vector<std::string> const& args,
412
                       cmExecutionStatus& status)
413
0
{
414
0
  Helper helper(status);
415
416
  // This is the TARGETS mode.
417
0
  std::vector<cmTarget*> targets;
418
419
0
  struct ArgVectors
420
0
  {
421
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Archive;
422
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Library;
423
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Runtime;
424
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Object;
425
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Framework;
426
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Bundle;
427
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Includes;
428
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> PrivateHeader;
429
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> PublicHeader;
430
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Resource;
431
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> CxxModulesBmi;
432
0
    std::vector<std::vector<std::string>> FileSets;
433
0
  };
434
435
0
  static auto const argHelper =
436
0
    cmArgumentParser<ArgVectors>{}
437
0
      .Bind("ARCHIVE"_s, &ArgVectors::Archive)
438
0
      .Bind("LIBRARY"_s, &ArgVectors::Library)
439
0
      .Bind("RUNTIME"_s, &ArgVectors::Runtime)
440
0
      .Bind("OBJECTS"_s, &ArgVectors::Object)
441
0
      .Bind("FRAMEWORK"_s, &ArgVectors::Framework)
442
0
      .Bind("BUNDLE"_s, &ArgVectors::Bundle)
443
0
      .Bind("INCLUDES"_s, &ArgVectors::Includes)
444
0
      .Bind("PRIVATE_HEADER"_s, &ArgVectors::PrivateHeader)
445
0
      .Bind("PUBLIC_HEADER"_s, &ArgVectors::PublicHeader)
446
0
      .Bind("RESOURCE"_s, &ArgVectors::Resource)
447
0
      .Bind("FILE_SET"_s, &ArgVectors::FileSets)
448
0
      .Bind("CXX_MODULES_BMI"_s, &ArgVectors::CxxModulesBmi);
449
450
0
  std::vector<std::string> genericArgVector;
451
0
  ArgVectors const argVectors = argHelper.Parse(args, &genericArgVector);
452
453
  // now parse the generic args (i.e. the ones not specialized on LIBRARY/
454
  // ARCHIVE, RUNTIME etc. (see above)
455
  // These generic args also contain the targets and the export stuff
456
0
  ArgumentParser::MaybeEmpty<std::vector<std::string>> targetList;
457
0
  std::string exports;
458
0
  cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>>
459
0
    runtimeDependenciesArgVector;
460
0
  std::string runtimeDependencySetArg;
461
0
  std::vector<std::string> unknownArgs;
462
0
  cmInstallCommandArguments genericArgs(helper.DefaultComponentName,
463
0
                                        *helper.Makefile);
464
0
  genericArgs.Bind("TARGETS"_s, targetList);
465
0
  genericArgs.Bind("EXPORT"_s, exports);
466
0
  genericArgs.Bind("RUNTIME_DEPENDENCIES"_s, runtimeDependenciesArgVector);
467
0
  genericArgs.Bind("RUNTIME_DEPENDENCY_SET"_s, runtimeDependencySetArg);
468
0
  genericArgs.Parse(genericArgVector, &unknownArgs);
469
0
  bool success = genericArgs.Finalize();
470
471
0
  RuntimeDependenciesArgs runtimeDependenciesArgs =
472
0
    runtimeDependenciesArgVector
473
0
    ? RuntimeDependenciesArgHelper.Parse(*runtimeDependenciesArgVector,
474
0
                                         &unknownArgs)
475
0
    : RuntimeDependenciesArgs();
476
477
0
  cmInstallCommandArguments archiveArgs(helper.DefaultComponentName,
478
0
                                        *helper.Makefile);
479
0
  cmInstallCommandArguments libraryArgs(helper.DefaultComponentName,
480
0
                                        *helper.Makefile);
481
0
  cmInstallCommandArguments runtimeArgs(helper.DefaultComponentName,
482
0
                                        *helper.Makefile);
483
0
  cmInstallCommandArguments objectArgs(helper.DefaultComponentName,
484
0
                                       *helper.Makefile);
485
0
  cmInstallCommandArguments frameworkArgs(helper.DefaultComponentName,
486
0
                                          *helper.Makefile);
487
0
  cmInstallCommandArguments bundleArgs(helper.DefaultComponentName,
488
0
                                       *helper.Makefile);
489
0
  cmInstallCommandArguments privateHeaderArgs(helper.DefaultComponentName,
490
0
                                              *helper.Makefile);
491
0
  cmInstallCommandArguments publicHeaderArgs(helper.DefaultComponentName,
492
0
                                             *helper.Makefile);
493
0
  cmInstallCommandArguments resourceArgs(helper.DefaultComponentName,
494
0
                                         *helper.Makefile);
495
0
  cmInstallCommandIncludesArgument includesArgs;
496
0
  std::vector<cmInstallCommandFileSetArguments> fileSetArgs(
497
0
    argVectors.FileSets.size(),
498
0
    { cmInstallCommandFileSetArguments(helper.DefaultComponentName,
499
0
                                       *helper.Makefile) });
500
0
  cmInstallCommandArguments cxxModuleBmiArgs(helper.DefaultComponentName,
501
0
                                             *helper.Makefile);
502
503
  // now parse the args for specific parts of the target (e.g. LIBRARY,
504
  // RUNTIME, ARCHIVE etc.
505
0
  archiveArgs.Parse(argVectors.Archive, &unknownArgs);
506
0
  libraryArgs.Parse(argVectors.Library, &unknownArgs);
507
0
  runtimeArgs.Parse(argVectors.Runtime, &unknownArgs);
508
0
  objectArgs.Parse(argVectors.Object, &unknownArgs);
509
0
  frameworkArgs.Parse(argVectors.Framework, &unknownArgs);
510
0
  bundleArgs.Parse(argVectors.Bundle, &unknownArgs);
511
0
  privateHeaderArgs.Parse(argVectors.PrivateHeader, &unknownArgs);
512
0
  publicHeaderArgs.Parse(argVectors.PublicHeader, &unknownArgs);
513
0
  resourceArgs.Parse(argVectors.Resource, &unknownArgs);
514
0
  includesArgs.Parse(&argVectors.Includes, &unknownArgs);
515
0
  cxxModuleBmiArgs.Parse(argVectors.CxxModulesBmi, &unknownArgs);
516
0
  for (std::size_t i = 0; i < argVectors.FileSets.size(); i++) {
517
    // We have to create a separate object for the parsing because
518
    // cmArgumentParser<void>::Bind() binds to a specific address, but the
519
    // objects in the vector can move around. So we parse in an object with a
520
    // fixed address and then copy the data into the vector.
521
0
    cmInstallCommandFileSetArguments fileSetArg(helper.DefaultComponentName,
522
0
                                                *helper.Makefile);
523
0
    fileSetArg.Parse(argVectors.FileSets[i], &unknownArgs);
524
0
    fileSetArgs[i] = std::move(fileSetArg);
525
0
  }
526
527
0
  if (!unknownArgs.empty()) {
528
    // Unknown argument.
529
0
    status.SetError(
530
0
      cmStrCat("TARGETS given unknown argument \"", unknownArgs[0], "\"."));
531
0
    return false;
532
0
  }
533
534
  // apply generic args
535
0
  archiveArgs.SetGenericArguments(&genericArgs);
536
0
  libraryArgs.SetGenericArguments(&genericArgs);
537
0
  runtimeArgs.SetGenericArguments(&genericArgs);
538
0
  objectArgs.SetGenericArguments(&genericArgs);
539
0
  frameworkArgs.SetGenericArguments(&genericArgs);
540
0
  bundleArgs.SetGenericArguments(&genericArgs);
541
0
  privateHeaderArgs.SetGenericArguments(&genericArgs);
542
0
  publicHeaderArgs.SetGenericArguments(&genericArgs);
543
0
  resourceArgs.SetGenericArguments(&genericArgs);
544
0
  for (auto& fileSetArg : fileSetArgs) {
545
0
    fileSetArg.SetGenericArguments(&genericArgs);
546
0
  }
547
0
  cxxModuleBmiArgs.SetGenericArguments(&genericArgs);
548
549
0
  success = success && archiveArgs.Finalize();
550
0
  success = success && libraryArgs.Finalize();
551
0
  success = success && runtimeArgs.Finalize();
552
0
  success = success && objectArgs.Finalize();
553
0
  success = success && frameworkArgs.Finalize();
554
0
  success = success && bundleArgs.Finalize();
555
0
  success = success && privateHeaderArgs.Finalize();
556
0
  success = success && publicHeaderArgs.Finalize();
557
0
  success = success && resourceArgs.Finalize();
558
0
  success = success && cxxModuleBmiArgs.Finalize();
559
0
  for (auto& fileSetArg : fileSetArgs) {
560
0
    success = success && fileSetArg.Finalize();
561
0
  }
562
563
0
  if (!success) {
564
0
    return false;
565
0
  }
566
567
  // Enforce argument rules too complex to specify for the
568
  // general-purpose parser.
569
0
  if (runtimeArgs.GetNamelinkOnly() || objectArgs.GetNamelinkOnly() ||
570
0
      frameworkArgs.GetNamelinkOnly() || bundleArgs.GetNamelinkOnly() ||
571
0
      privateHeaderArgs.GetNamelinkOnly() ||
572
0
      publicHeaderArgs.GetNamelinkOnly() || resourceArgs.GetNamelinkOnly() ||
573
0
      std::any_of(fileSetArgs.begin(), fileSetArgs.end(),
574
0
                  [](cmInstallCommandFileSetArguments const& fileSetArg)
575
0
                    -> bool { return fileSetArg.GetNamelinkOnly(); }) ||
576
0
      cxxModuleBmiArgs.GetNamelinkOnly()) {
577
0
    status.SetError(
578
0
      "TARGETS given NAMELINK_ONLY option not in LIBRARY or ARCHIVE group.  "
579
0
      "The NAMELINK_ONLY option may be specified only following LIBRARY or "
580
0
      "ARCHIVE.");
581
0
    return false;
582
0
  }
583
0
  if (runtimeArgs.GetNamelinkSkip() || objectArgs.GetNamelinkSkip() ||
584
0
      frameworkArgs.GetNamelinkSkip() || bundleArgs.GetNamelinkSkip() ||
585
0
      privateHeaderArgs.GetNamelinkSkip() ||
586
0
      publicHeaderArgs.GetNamelinkSkip() || resourceArgs.GetNamelinkSkip() ||
587
0
      std::any_of(fileSetArgs.begin(), fileSetArgs.end(),
588
0
                  [](cmInstallCommandFileSetArguments const& fileSetArg)
589
0
                    -> bool { return fileSetArg.GetNamelinkSkip(); }) ||
590
0
      cxxModuleBmiArgs.GetNamelinkSkip()) {
591
0
    status.SetError(
592
0
      "TARGETS given NAMELINK_SKIP option not in LIBRARY or ARCHIVE group.  "
593
0
      "The NAMELINK_SKIP option may be specified only following LIBRARY or "
594
0
      "ARCHIVE.");
595
0
    return false;
596
0
  }
597
0
  if (runtimeArgs.HasNamelinkComponent() ||
598
0
      objectArgs.HasNamelinkComponent() ||
599
0
      frameworkArgs.HasNamelinkComponent() ||
600
0
      bundleArgs.HasNamelinkComponent() ||
601
0
      privateHeaderArgs.HasNamelinkComponent() ||
602
0
      publicHeaderArgs.HasNamelinkComponent() ||
603
0
      resourceArgs.HasNamelinkComponent() ||
604
0
      std::any_of(fileSetArgs.begin(), fileSetArgs.end(),
605
0
                  [](cmInstallCommandFileSetArguments const& fileSetArg)
606
0
                    -> bool { return fileSetArg.HasNamelinkComponent(); }) ||
607
0
      cxxModuleBmiArgs.HasNamelinkComponent()) {
608
0
    status.SetError(
609
0
      "TARGETS given NAMELINK_COMPONENT option not in LIBRARY or ARCHIVE "
610
0
      "group.  The NAMELINK_COMPONENT option may be specified only following "
611
0
      "LIBRARY or ARCHIVE.");
612
0
    return false;
613
0
  }
614
0
  if (libraryArgs.GetNamelinkOnly() && libraryArgs.GetNamelinkSkip()) {
615
0
    status.SetError("TARGETS given NAMELINK_ONLY and NAMELINK_SKIP.  "
616
0
                    "At most one of these two options may be specified.");
617
0
    return false;
618
0
  }
619
0
  if (!genericArgs.GetType().empty() || !archiveArgs.GetType().empty() ||
620
0
      !libraryArgs.GetType().empty() || !runtimeArgs.GetType().empty() ||
621
0
      !objectArgs.GetType().empty() || !frameworkArgs.GetType().empty() ||
622
0
      !bundleArgs.GetType().empty() || !privateHeaderArgs.GetType().empty() ||
623
0
      !publicHeaderArgs.GetType().empty() || !resourceArgs.GetType().empty() ||
624
0
      std::any_of(fileSetArgs.begin(), fileSetArgs.end(),
625
0
                  [](cmInstallCommandFileSetArguments const& fileSetArg)
626
0
                    -> bool { return !fileSetArg.GetType().empty(); }) ||
627
0
      !cxxModuleBmiArgs.GetType().empty()) {
628
0
    status.SetError(
629
0
      "TARGETS given TYPE option. The TYPE option may only be specified in "
630
0
      " install(FILES) and install(DIRECTORIES).");
631
0
    return false;
632
0
  }
633
0
  if (std::any_of(fileSetArgs.begin(), fileSetArgs.end(),
634
0
                  [](cmInstallCommandFileSetArguments const& fileSetArg)
635
0
                    -> bool { return fileSetArg.GetFileSet().empty(); })) {
636
0
    status.SetError("TARGETS given FILE_SET option without file set name.");
637
0
    return false;
638
0
  }
639
640
0
  cmInstallRuntimeDependencySet* runtimeDependencySet = nullptr;
641
0
  if (runtimeDependenciesArgVector) {
642
0
    if (!runtimeDependencySetArg.empty()) {
643
0
      status.SetError("TARGETS cannot have both RUNTIME_DEPENDENCIES and "
644
0
                      "RUNTIME_DEPENDENCY_SET.");
645
0
      return false;
646
0
    }
647
0
    auto system = helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
648
0
    if (!cmRuntimeDependencyArchive::PlatformSupportsRuntimeDependencies(
649
0
          system)) {
650
0
      status.SetError(
651
0
        cmStrCat("TARGETS RUNTIME_DEPENDENCIES is not supported on system \"",
652
0
                 system, '"'));
653
0
      return false;
654
0
    }
655
0
    if (helper.Makefile->IsOn("CMAKE_CROSSCOMPILING")) {
656
0
      status.SetError("TARGETS RUNTIME_DEPENDENCIES is not supported "
657
0
                      "when cross-compiling.");
658
0
      return false;
659
0
    }
660
0
    if (helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME") ==
661
0
          "Darwin" &&
662
0
        frameworkArgs.GetDestination().empty()) {
663
0
      status.SetError(
664
0
        "TARGETS RUNTIME_DEPENDENCIES given no FRAMEWORK DESTINATION");
665
0
      return false;
666
0
    }
667
0
    runtimeDependencySet = helper.Makefile->GetGlobalGenerator()
668
0
                             ->CreateAnonymousRuntimeDependencySet();
669
0
  } else if (!runtimeDependencySetArg.empty()) {
670
0
    auto system = helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
671
0
    if (!cmRuntimeDependencyArchive::PlatformSupportsRuntimeDependencies(
672
0
          system)) {
673
0
      status.SetError(cmStrCat(
674
0
        "TARGETS RUNTIME_DEPENDENCY_SET is not supported on system \"", system,
675
0
        '"'));
676
0
      return false;
677
0
    }
678
0
    runtimeDependencySet =
679
0
      helper.Makefile->GetGlobalGenerator()->GetNamedRuntimeDependencySet(
680
0
        runtimeDependencySetArg);
681
0
  }
682
683
  // Select the mode for installing symlinks to versioned shared libraries.
684
0
  cmInstallTargetGenerator::NamelinkModeType namelinkMode =
685
0
    cmInstallTargetGenerator::NamelinkModeNone;
686
0
  if (libraryArgs.GetNamelinkOnly()) {
687
0
    namelinkMode = cmInstallTargetGenerator::NamelinkModeOnly;
688
0
  } else if (libraryArgs.GetNamelinkSkip()) {
689
0
    namelinkMode = cmInstallTargetGenerator::NamelinkModeSkip;
690
0
  }
691
  // Select the mode for installing symlinks to versioned imported libraries.
692
0
  cmInstallTargetGenerator::NamelinkModeType importlinkMode =
693
0
    cmInstallTargetGenerator::NamelinkModeNone;
694
0
  if (archiveArgs.GetNamelinkOnly()) {
695
0
    importlinkMode = cmInstallTargetGenerator::NamelinkModeOnly;
696
0
  } else if (archiveArgs.GetNamelinkSkip()) {
697
0
    importlinkMode = cmInstallTargetGenerator::NamelinkModeSkip;
698
0
  }
699
700
  // Check if there is something to do.
701
0
  if (targetList.empty()) {
702
0
    return true;
703
0
  }
704
705
0
  for (std::string const& tgt : targetList) {
706
707
    // Lookup this target in the current directory.
708
0
    cmTarget* target = helper.Makefile->FindLocalNonAliasTarget(tgt);
709
0
    if (!target) {
710
      // If no local target has been found, find it in the global scope.
711
0
      cmTarget* const globalTarget =
712
0
        helper.Makefile->GetGlobalGenerator()->FindTarget(tgt);
713
0
      if (globalTarget && !globalTarget->IsImported()) {
714
0
        target = globalTarget;
715
0
      }
716
0
    }
717
0
    if (target) {
718
      // Found the target.  Check its type.
719
0
      if (target->GetType() != cm::TargetType::EXECUTABLE &&
720
0
          target->GetType() != cm::TargetType::STATIC_LIBRARY &&
721
0
          target->GetType() != cm::TargetType::SHARED_LIBRARY &&
722
0
          target->GetType() != cm::TargetType::MODULE_LIBRARY &&
723
0
          target->GetType() != cm::TargetType::OBJECT_LIBRARY &&
724
0
          target->GetType() != cm::TargetType::INTERFACE_LIBRARY) {
725
0
        status.SetError(
726
0
          cmStrCat("TARGETS given target \"", tgt,
727
0
                   "\" which is not an executable, library, or module."));
728
0
        return false;
729
0
      }
730
      // Store the target in the list to be installed.
731
0
      targets.push_back(target);
732
0
    } else {
733
      // Did not find the target.
734
0
      status.SetError(
735
0
        cmStrCat("TARGETS given target \"", tgt, "\" which does not exist."));
736
0
      return false;
737
0
    }
738
0
  }
739
740
  // Keep track of whether we will be performing an installation of
741
  // any files of the given type.
742
0
  bool installsArchive = false;
743
0
  bool installsLibrary = false;
744
0
  bool installsNamelink = false;
745
0
  bool installsImportlink = false;
746
0
  bool installsRuntime = false;
747
0
  bool installsObject = false;
748
0
  bool installsFramework = false;
749
0
  bool installsBundle = false;
750
0
  bool installsPrivateHeader = false;
751
0
  bool installsPublicHeader = false;
752
0
  bool installsResource = false;
753
0
  std::vector<bool> installsFileSet(fileSetArgs.size(), false);
754
0
  bool installsCxxModuleBmi = false;
755
756
  // Generate install script code to install the given targets.
757
0
  for (cmTarget* ti : targets) {
758
    // Handle each target type.
759
0
    cmTarget& target = *ti;
760
0
    std::unique_ptr<cmInstallTargetGenerator> archiveGenerator;
761
0
    std::unique_ptr<cmInstallTargetGenerator> libraryGenerator;
762
0
    std::unique_ptr<cmInstallTargetGenerator> namelinkGenerator;
763
0
    std::unique_ptr<cmInstallTargetGenerator> importlinkGenerator;
764
0
    std::unique_ptr<cmInstallTargetGenerator> runtimeGenerator;
765
0
    std::unique_ptr<cmInstallTargetGenerator> objectGenerator;
766
0
    std::unique_ptr<cmInstallTargetGenerator> frameworkGenerator;
767
0
    std::unique_ptr<cmInstallTargetGenerator> bundleGenerator;
768
0
    std::unique_ptr<cmInstallFilesGenerator> privateHeaderGenerator;
769
0
    std::unique_ptr<cmInstallFilesGenerator> publicHeaderGenerator;
770
0
    std::unique_ptr<cmInstallFilesGenerator> resourceGenerator;
771
0
    std::vector<std::unique_ptr<cmInstallFileSetGenerator>> fileSetGenerators;
772
0
    std::unique_ptr<cmInstallCxxModuleBmiGenerator> cxxModuleBmiGenerator;
773
774
    // Avoid selecting default destinations for PUBLIC_HEADER and
775
    // PRIVATE_HEADER if any artifacts are specified.
776
0
    bool artifactsSpecified = false;
777
778
    // Track whether this is a namelink-only rule.
779
0
    bool namelinkOnly = false;
780
781
0
    auto addTargetExport = [&]() -> bool {
782
      // Add this install rule to an export if one was specified.
783
0
      if (!exports.empty()) {
784
0
        auto interfaceFileSets = target.GetAllInterfaceFileSets();
785
0
        if (std::any_of(
786
0
              interfaceFileSets.begin(), interfaceFileSets.end(),
787
0
              [=](std::string const& name) -> bool {
788
0
                return !std::any_of(
789
0
                  fileSetArgs.begin(), fileSetArgs.end(),
790
0
                  [=](cmInstallCommandFileSetArguments const& fileSetArg)
791
0
                    -> bool { return fileSetArg.GetFileSet() == name; });
792
0
              })) {
793
0
          status.SetError(cmStrCat("TARGETS target ", target.GetName(),
794
0
                                   " is exported but not all of its interface "
795
0
                                   "file sets are installed"));
796
0
          return false;
797
0
        }
798
799
0
        auto te = cm::make_unique<cmTargetExport>();
800
0
        te->TargetName = target.GetName();
801
0
        te->ArchiveGenerator = archiveGenerator.get();
802
0
        te->BundleGenerator = bundleGenerator.get();
803
0
        te->FrameworkGenerator = frameworkGenerator.get();
804
0
        te->HeaderGenerator = publicHeaderGenerator.get();
805
0
        te->LibraryGenerator = libraryGenerator.get();
806
0
        te->RuntimeGenerator = runtimeGenerator.get();
807
0
        te->ObjectsGenerator = objectGenerator.get();
808
0
        for (auto const& gen : fileSetGenerators) {
809
0
          te->FileSetGenerators[gen->GetFileSetName()] = gen.get();
810
0
        }
811
0
        te->CxxModuleBmiGenerator = cxxModuleBmiGenerator.get();
812
0
        target.AddInstallIncludeDirectories(
813
0
          *te, cmMakeRange(includesArgs.GetIncludeDirs()));
814
0
        te->NamelinkOnly = namelinkOnly;
815
0
        helper.Makefile->GetGlobalGenerator()
816
0
          ->GetExportSets()[exports]
817
0
          .AddTargetExport(std::move(te));
818
0
      }
819
0
      return true;
820
0
    };
821
822
0
    switch (target.GetType()) {
823
0
      case cm::TargetType::SHARED_LIBRARY: {
824
        // Shared libraries are handled differently on DLL and non-DLL
825
        // platforms.  All windows platforms are DLL platforms including
826
        // cygwin.  Currently no other platform is a DLL platform.
827
0
        if (target.IsDLLPlatform()) {
828
          // When in namelink only mode skip all libraries on Windows.
829
0
          if (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly) {
830
0
            namelinkOnly = true;
831
0
            if (!addTargetExport()) {
832
0
              return false;
833
0
            }
834
0
            continue;
835
0
          }
836
837
          // This is a DLL platform.
838
0
          if (!archiveArgs.GetDestination().empty()) {
839
            // The import library uses the ARCHIVE properties.
840
0
            archiveGenerator = CreateInstallTargetGenerator(
841
0
              target, archiveArgs, true, helper.CaptureContext());
842
0
            artifactsSpecified = true;
843
0
          }
844
0
          if (!runtimeArgs.GetDestination().empty()) {
845
            // The DLL uses the RUNTIME properties.
846
0
            runtimeGenerator = CreateInstallTargetGenerator(
847
0
              target, runtimeArgs, false, helper.CaptureContext());
848
0
            artifactsSpecified = true;
849
0
          }
850
0
          if (!archiveGenerator && !runtimeGenerator) {
851
0
            archiveGenerator = CreateInstallTargetGenerator(
852
0
              target, archiveArgs, true, helper.CaptureContext(),
853
0
              helper.GetArchiveDestination(nullptr));
854
0
            runtimeGenerator = CreateInstallTargetGenerator(
855
0
              target, runtimeArgs, false, helper.CaptureContext(),
856
0
              helper.GetRuntimeDestination(nullptr));
857
0
          }
858
0
          if (runtimeDependencySet && runtimeGenerator) {
859
0
            runtimeDependencySet->AddLibrary(runtimeGenerator.get());
860
0
          }
861
0
        } else {
862
          // This is a non-DLL platform.
863
          // If it is marked with FRAMEWORK property use the FRAMEWORK set of
864
          // INSTALL properties. Otherwise, use the LIBRARY properties.
865
0
          if (target.IsFrameworkOnApple()) {
866
            // When in namelink only mode skip frameworks.
867
0
            if (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly) {
868
0
              namelinkOnly = true;
869
0
              if (!addTargetExport()) {
870
0
                return false;
871
0
              }
872
0
              continue;
873
0
            }
874
875
            // Use the FRAMEWORK properties.
876
0
            if (!frameworkArgs.GetDestination().empty()) {
877
0
              frameworkGenerator = CreateInstallTargetGenerator(
878
0
                target, frameworkArgs, false, helper.CaptureContext());
879
0
            } else {
880
0
              status.SetError(
881
0
                cmStrCat("TARGETS given no FRAMEWORK DESTINATION for shared "
882
0
                         "library FRAMEWORK target \"",
883
0
                         target.GetName(), "\"."));
884
0
              return false;
885
0
            }
886
0
          } else {
887
            // The shared library uses the LIBRARY properties.
888
0
            if (!libraryArgs.GetDestination().empty()) {
889
0
              artifactsSpecified = true;
890
0
            }
891
0
            if (namelinkMode != cmInstallTargetGenerator::NamelinkModeOnly) {
892
0
              libraryGenerator = CreateInstallTargetGenerator(
893
0
                target, libraryArgs, false, helper.CaptureContext(),
894
0
                helper.GetLibraryDestination(&libraryArgs));
895
0
              libraryGenerator->SetNamelinkMode(
896
0
                cmInstallTargetGenerator::NamelinkModeSkip);
897
0
            }
898
0
            if (namelinkMode != cmInstallTargetGenerator::NamelinkModeSkip) {
899
0
              namelinkGenerator = CreateInstallTargetGenerator(
900
0
                target, libraryArgs, false, helper.CaptureContext(),
901
0
                helper.GetLibraryDestination(&libraryArgs), false, true);
902
0
              namelinkGenerator->SetNamelinkMode(
903
0
                cmInstallTargetGenerator::NamelinkModeOnly);
904
0
            }
905
0
            namelinkOnly =
906
0
              (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly);
907
908
0
            if (target.GetMakefile()->PlatformSupportsAppleTextStubs() &&
909
0
                target.IsSharedLibraryWithExports()) {
910
              // Apple .tbd files use the ARCHIVE properties
911
0
              if (!archiveArgs.GetDestination().empty()) {
912
0
                artifactsSpecified = true;
913
0
              }
914
0
              if (importlinkMode !=
915
0
                  cmInstallTargetGenerator::NamelinkModeOnly) {
916
0
                archiveGenerator = CreateInstallTargetGenerator(
917
0
                  target, archiveArgs, true, helper.CaptureContext(),
918
0
                  helper.GetLibraryDestination(&archiveArgs));
919
0
                archiveGenerator->SetImportlinkMode(
920
0
                  cmInstallTargetGenerator::NamelinkModeSkip);
921
0
              }
922
0
              if (importlinkMode !=
923
0
                  cmInstallTargetGenerator::NamelinkModeSkip) {
924
0
                importlinkGenerator = CreateInstallTargetGenerator(
925
0
                  target, archiveArgs, true, helper.CaptureContext(),
926
0
                  helper.GetLibraryDestination(&archiveArgs), false, true);
927
0
                importlinkGenerator->SetImportlinkMode(
928
0
                  cmInstallTargetGenerator::NamelinkModeOnly);
929
0
              }
930
0
              namelinkOnly =
931
0
                (importlinkMode == cmInstallTargetGenerator::NamelinkModeOnly);
932
0
            }
933
0
          }
934
0
          if (runtimeDependencySet && libraryGenerator) {
935
0
            runtimeDependencySet->AddLibrary(libraryGenerator.get());
936
0
          }
937
0
        }
938
0
      } break;
939
0
      case cm::TargetType::STATIC_LIBRARY: {
940
        // If it is marked with FRAMEWORK property use the FRAMEWORK set of
941
        // INSTALL properties. Otherwise, use the LIBRARY properties.
942
0
        if (target.IsFrameworkOnApple()) {
943
          // When in namelink only mode skip frameworks.
944
0
          if (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly) {
945
0
            namelinkOnly = true;
946
0
            if (!addTargetExport()) {
947
0
              return false;
948
0
            }
949
0
            continue;
950
0
          }
951
952
          // Use the FRAMEWORK properties.
953
0
          if (!frameworkArgs.GetDestination().empty()) {
954
0
            frameworkGenerator = CreateInstallTargetGenerator(
955
0
              target, frameworkArgs, false, helper.CaptureContext());
956
0
          } else {
957
0
            status.SetError(
958
0
              cmStrCat("TARGETS given no FRAMEWORK DESTINATION for static "
959
0
                       "library FRAMEWORK target \"",
960
0
                       target.GetName(), "\"."));
961
0
            return false;
962
0
          }
963
0
        } else {
964
          // Static libraries use ARCHIVE properties.
965
0
          if (!archiveArgs.GetDestination().empty()) {
966
0
            artifactsSpecified = true;
967
0
          }
968
0
          archiveGenerator = CreateInstallTargetGenerator(
969
0
            target, archiveArgs, false, helper.CaptureContext(),
970
0
            helper.GetArchiveDestination(&archiveArgs));
971
0
        }
972
0
      } break;
973
0
      case cm::TargetType::MODULE_LIBRARY: {
974
        // Modules use LIBRARY properties.
975
0
        if (!libraryArgs.GetDestination().empty()) {
976
0
          libraryGenerator = CreateInstallTargetGenerator(
977
0
            target, libraryArgs, false, helper.CaptureContext());
978
0
          libraryGenerator->SetNamelinkMode(namelinkMode);
979
0
          namelinkOnly =
980
0
            (namelinkMode == cmInstallTargetGenerator::NamelinkModeOnly);
981
0
          if (runtimeDependencySet) {
982
0
            runtimeDependencySet->AddModule(libraryGenerator.get());
983
0
          }
984
0
        } else {
985
0
          status.SetError(
986
0
            cmStrCat("TARGETS given no LIBRARY DESTINATION for module "
987
0
                     "target \"",
988
0
                     target.GetName(), "\"."));
989
0
          return false;
990
0
        }
991
0
      } break;
992
0
      case cm::TargetType::OBJECT_LIBRARY: {
993
        // Objects use OBJECT properties.
994
0
        if (!objectArgs.GetDestination().empty()) {
995
          // Verify that we know where the objects are to install them.
996
0
          std::string reason;
997
0
          if (!target.HasKnownObjectFileLocation(&reason)) {
998
0
            status.SetError(
999
0
              cmStrCat("TARGETS given OBJECT library \"", target.GetName(),
1000
0
                       "\" whose objects may not be installed", reason, '.'));
1001
0
            return false;
1002
0
          }
1003
1004
0
          objectGenerator = CreateInstallTargetGenerator(
1005
0
            target, objectArgs, false, helper.CaptureContext());
1006
0
        } else {
1007
          // Installing an OBJECT library without a destination transforms
1008
          // it to an INTERFACE library.  It installs no files but can be
1009
          // exported.
1010
0
        }
1011
0
      } break;
1012
0
      case cm::TargetType::EXECUTABLE: {
1013
0
        if (target.IsAppBundleOnApple()) {
1014
          // Application bundles use the BUNDLE properties.
1015
0
          if (!bundleArgs.GetDestination().empty()) {
1016
0
            bundleGenerator = CreateInstallTargetGenerator(
1017
0
              target, bundleArgs, false, helper.CaptureContext());
1018
0
          }
1019
0
          if (!bundleGenerator) {
1020
0
            status.SetError(cmStrCat("TARGETS given no BUNDLE DESTINATION for "
1021
0
                                     "MACOSX_BUNDLE executable target \"",
1022
0
                                     target.GetName(), "\"."));
1023
0
            return false;
1024
0
          }
1025
0
          if (runtimeDependencySet) {
1026
0
            if (!AddBundleExecutable(helper, runtimeDependencySet,
1027
0
                                     bundleGenerator.get())) {
1028
0
              return false;
1029
0
            }
1030
0
          }
1031
0
        } else {
1032
          // Executables use the RUNTIME properties.
1033
0
          if (!runtimeArgs.GetDestination().empty()) {
1034
0
            artifactsSpecified = true;
1035
0
          }
1036
0
          runtimeGenerator = CreateInstallTargetGenerator(
1037
0
            target, runtimeArgs, false, helper.CaptureContext(),
1038
0
            helper.GetRuntimeDestination(&runtimeArgs));
1039
0
          if (runtimeDependencySet) {
1040
0
            runtimeDependencySet->AddExecutable(runtimeGenerator.get());
1041
0
          }
1042
0
        }
1043
1044
        // On DLL platforms an executable may also have an import
1045
        // library.  Install it to the archive destination if it
1046
        // exists.
1047
0
        if ((target.IsDLLPlatform() || target.IsAIX()) &&
1048
0
            !archiveArgs.GetDestination().empty() &&
1049
0
            target.IsExecutableWithExports()) {
1050
          // The import library uses the ARCHIVE properties.
1051
0
          artifactsSpecified = true;
1052
0
          archiveGenerator = CreateInstallTargetGenerator(
1053
0
            target, archiveArgs, true, helper.CaptureContext(), true);
1054
0
        }
1055
0
      } break;
1056
0
      case cm::TargetType::INTERFACE_LIBRARY:
1057
        // Nothing to do. An INTERFACE_LIBRARY can be installed, but the
1058
        // only effect of that is to make it exportable. It installs no
1059
        // other files itself.
1060
0
      default:
1061
        // This should never happen due to the above type check.
1062
        // Ignore the case.
1063
0
        break;
1064
0
    }
1065
1066
    // These well-known sets of files are installed *automatically* for
1067
    // FRAMEWORK SHARED library targets on the Mac as part of installing the
1068
    // FRAMEWORK.  For other target types or on other platforms, they are not
1069
    // installed automatically and so we need to create install files
1070
    // generators for them.
1071
0
    bool createInstallGeneratorsForTargetFileSets = true;
1072
1073
0
    if (target.IsFrameworkOnApple()) {
1074
0
      createInstallGeneratorsForTargetFileSets = false;
1075
0
    }
1076
1077
0
    if (createInstallGeneratorsForTargetFileSets && !namelinkOnly) {
1078
0
      cmValue files = target.GetProperty("PRIVATE_HEADER");
1079
0
      if (cmNonempty(files)) {
1080
0
        cmList relFiles{ *files };
1081
0
        std::vector<std::string> absFiles;
1082
0
        if (!helper.MakeFilesFullPath("PRIVATE_HEADER", relFiles, absFiles)) {
1083
0
          return false;
1084
0
        }
1085
1086
        // Create the files install generator.
1087
0
        if (!artifactsSpecified ||
1088
0
            !privateHeaderArgs.GetDestination().empty()) {
1089
0
          privateHeaderGenerator = CreateInstallFilesGenerator(
1090
0
            helper.Makefile, absFiles, privateHeaderArgs, false,
1091
0
            helper.GetIncludeDestination(&privateHeaderArgs));
1092
0
        } else {
1093
0
          helper.Makefile->IssueDiagnostic(
1094
0
            cmDiagnostics::CMD_AUTHOR,
1095
0
            cmStrCat("Target ", target.GetName(),
1096
0
                     " has PRIVATE_HEADER files"
1097
0
                     " but no PRIVATE_HEADER DESTINATION."));
1098
0
        }
1099
0
      }
1100
1101
0
      files = target.GetProperty("PUBLIC_HEADER");
1102
0
      if (cmNonempty(files)) {
1103
0
        cmList relFiles{ *files };
1104
0
        std::vector<std::string> absFiles;
1105
0
        if (!helper.MakeFilesFullPath("PUBLIC_HEADER", relFiles, absFiles)) {
1106
0
          return false;
1107
0
        }
1108
1109
        // Create the files install generator.
1110
0
        if (!artifactsSpecified ||
1111
0
            !publicHeaderArgs.GetDestination().empty()) {
1112
0
          publicHeaderGenerator = CreateInstallFilesGenerator(
1113
0
            helper.Makefile, absFiles, publicHeaderArgs, false,
1114
0
            helper.GetIncludeDestination(&publicHeaderArgs));
1115
0
        } else {
1116
0
          helper.Makefile->IssueDiagnostic(
1117
0
            cmDiagnostics::CMD_AUTHOR,
1118
0
            cmStrCat("Target ", target.GetName(),
1119
0
                     " has PUBLIC_HEADER files"
1120
0
                     " but no PUBLIC_HEADER DESTINATION."));
1121
0
        }
1122
0
      }
1123
1124
0
      files = target.GetProperty("RESOURCE");
1125
0
      if (cmNonempty(files)) {
1126
0
        cmList relFiles{ *files };
1127
0
        std::vector<std::string> absFiles;
1128
0
        if (!helper.MakeFilesFullPath("RESOURCE", relFiles, absFiles)) {
1129
0
          return false;
1130
0
        }
1131
1132
        // Create the files install generator.
1133
0
        if (!resourceArgs.GetDestination().empty()) {
1134
0
          resourceGenerator = CreateInstallFilesGenerator(
1135
0
            helper.Makefile, absFiles, resourceArgs, false);
1136
0
        } else if (!target.IsAppBundleOnApple()) {
1137
0
          helper.Makefile->IssueDiagnostic(
1138
0
            cmDiagnostics::CMD_AUTHOR,
1139
0
            cmStrCat("Target ", target.GetName(),
1140
0
                     " has RESOURCE files but no RESOURCE DESTINATION."));
1141
0
        }
1142
0
      }
1143
0
    }
1144
1145
0
    if (!namelinkOnly) {
1146
0
      for (std::size_t i = 0; i < fileSetArgs.size(); i++) {
1147
0
        fileSetGenerators.push_back(
1148
0
          CreateInstallFileSetGenerator(helper, target, fileSetArgs[i]));
1149
0
        installsFileSet[i] = true;
1150
0
      }
1151
0
    }
1152
1153
0
    if (!cxxModuleBmiArgs.GetDestination().empty()) {
1154
0
      cxxModuleBmiGenerator = cm::make_unique<cmInstallCxxModuleBmiGenerator>(
1155
0
        target.GetName(),
1156
0
        helper.GetCxxModulesBmiDestination(&cxxModuleBmiArgs),
1157
0
        cxxModuleBmiArgs.GetPermissions(),
1158
0
        cxxModuleBmiArgs.GetConfigurations(), cxxModuleBmiArgs.GetComponent(),
1159
0
        cmInstallGenerator::SelectMessageLevel(target.GetMakefile()),
1160
0
        cxxModuleBmiArgs.GetExcludeFromAll(), cxxModuleBmiArgs.GetOptional(),
1161
0
        helper.CaptureContext());
1162
0
      target.SetHaveInstallRule(true);
1163
0
    }
1164
1165
    // Add this install rule to an export if one was specified.
1166
0
    if (!addTargetExport()) {
1167
0
      return false;
1168
0
    }
1169
1170
    // Keep track of whether we're installing anything in each category
1171
0
    installsArchive = installsArchive || archiveGenerator;
1172
0
    installsLibrary = installsLibrary || libraryGenerator;
1173
0
    installsNamelink = installsNamelink || namelinkGenerator;
1174
0
    installsImportlink = installsImportlink || importlinkGenerator;
1175
0
    installsRuntime = installsRuntime || runtimeGenerator;
1176
0
    installsObject = installsObject || objectGenerator;
1177
0
    installsFramework = installsFramework || frameworkGenerator;
1178
0
    installsBundle = installsBundle || bundleGenerator;
1179
0
    installsPrivateHeader = installsPrivateHeader || privateHeaderGenerator;
1180
0
    installsPublicHeader = installsPublicHeader || publicHeaderGenerator;
1181
0
    installsResource = installsResource || resourceGenerator;
1182
0
    installsCxxModuleBmi = installsCxxModuleBmi || cxxModuleBmiGenerator;
1183
1184
0
    helper.Makefile->AddInstallGenerator(std::move(archiveGenerator));
1185
0
    helper.Makefile->AddInstallGenerator(std::move(libraryGenerator));
1186
0
    helper.Makefile->AddInstallGenerator(std::move(namelinkGenerator));
1187
0
    helper.Makefile->AddInstallGenerator(std::move(importlinkGenerator));
1188
0
    helper.Makefile->AddInstallGenerator(std::move(runtimeGenerator));
1189
0
    helper.Makefile->AddInstallGenerator(std::move(objectGenerator));
1190
0
    helper.Makefile->AddInstallGenerator(std::move(frameworkGenerator));
1191
0
    helper.Makefile->AddInstallGenerator(std::move(bundleGenerator));
1192
0
    helper.Makefile->AddInstallGenerator(std::move(privateHeaderGenerator));
1193
0
    helper.Makefile->AddInstallGenerator(std::move(publicHeaderGenerator));
1194
0
    helper.Makefile->AddInstallGenerator(std::move(resourceGenerator));
1195
0
    for (auto& gen : fileSetGenerators) {
1196
0
      helper.Makefile->AddInstallGenerator(std::move(gen));
1197
0
    }
1198
0
    helper.Makefile->AddInstallGenerator(std::move(cxxModuleBmiGenerator));
1199
0
  }
1200
1201
0
  if (runtimeDependenciesArgVector && !runtimeDependencySet->Empty()) {
1202
0
    AddInstallRuntimeDependenciesGenerator(
1203
0
      helper, runtimeDependencySet, runtimeArgs, libraryArgs, frameworkArgs,
1204
0
      std::move(runtimeDependenciesArgs), installsRuntime, installsLibrary,
1205
0
      installsFramework);
1206
0
  }
1207
1208
  // Tell the global generator about any installation component names
1209
  // specified
1210
0
  if (installsArchive) {
1211
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1212
0
      archiveArgs.GetComponent());
1213
0
  }
1214
0
  if (installsLibrary) {
1215
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1216
0
      libraryArgs.GetComponent());
1217
0
  }
1218
0
  if (installsNamelink) {
1219
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1220
0
      libraryArgs.GetNamelinkComponent());
1221
0
  }
1222
0
  if (installsImportlink) {
1223
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1224
0
      archiveArgs.GetNamelinkComponent());
1225
0
  }
1226
0
  if (installsRuntime) {
1227
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1228
0
      runtimeArgs.GetComponent());
1229
0
  }
1230
0
  if (installsObject) {
1231
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1232
0
      objectArgs.GetComponent());
1233
0
  }
1234
0
  if (installsFramework) {
1235
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1236
0
      frameworkArgs.GetComponent());
1237
0
  }
1238
0
  if (installsBundle) {
1239
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1240
0
      bundleArgs.GetComponent());
1241
0
  }
1242
0
  if (installsPrivateHeader) {
1243
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1244
0
      privateHeaderArgs.GetComponent());
1245
0
  }
1246
0
  if (installsPublicHeader) {
1247
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1248
0
      publicHeaderArgs.GetComponent());
1249
0
  }
1250
0
  if (installsResource) {
1251
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1252
0
      resourceArgs.GetComponent());
1253
0
  }
1254
0
  for (std::size_t i = 0; i < fileSetArgs.size(); i++) {
1255
0
    if (installsFileSet[i]) {
1256
0
      helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1257
0
        fileSetArgs[i].GetComponent());
1258
0
    }
1259
0
  }
1260
0
  if (installsCxxModuleBmi) {
1261
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1262
0
      cxxModuleBmiArgs.GetComponent());
1263
0
  }
1264
1265
0
  return true;
1266
0
}
1267
1268
bool HandleImportedRuntimeArtifactsMode(std::vector<std::string> const& args,
1269
                                        cmExecutionStatus& status)
1270
0
{
1271
0
  Helper helper(status);
1272
1273
  // This is the IMPORTED_RUNTIME_ARTIFACTS mode.
1274
0
  std::vector<cmTarget*> targets;
1275
1276
0
  struct ArgVectors
1277
0
  {
1278
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Library;
1279
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Runtime;
1280
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Framework;
1281
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Bundle;
1282
0
  };
1283
1284
0
  static auto const argHelper = cmArgumentParser<ArgVectors>{}
1285
0
                                  .Bind("LIBRARY"_s, &ArgVectors::Library)
1286
0
                                  .Bind("RUNTIME"_s, &ArgVectors::Runtime)
1287
0
                                  .Bind("FRAMEWORK"_s, &ArgVectors::Framework)
1288
0
                                  .Bind("BUNDLE"_s, &ArgVectors::Bundle);
1289
1290
0
  std::vector<std::string> genericArgVector;
1291
0
  ArgVectors const argVectors = argHelper.Parse(args, &genericArgVector);
1292
1293
  // now parse the generic args (i.e. the ones not specialized on LIBRARY,
1294
  // RUNTIME etc. (see above)
1295
0
  ArgumentParser::MaybeEmpty<std::vector<std::string>> targetList;
1296
0
  std::string runtimeDependencySetArg;
1297
0
  std::vector<std::string> unknownArgs;
1298
0
  cmInstallCommandArguments genericArgs(helper.DefaultComponentName,
1299
0
                                        *helper.Makefile);
1300
0
  genericArgs.Bind("IMPORTED_RUNTIME_ARTIFACTS"_s, targetList)
1301
0
    .Bind("RUNTIME_DEPENDENCY_SET"_s, runtimeDependencySetArg);
1302
0
  genericArgs.Parse(genericArgVector, &unknownArgs);
1303
0
  bool success = genericArgs.Finalize();
1304
1305
0
  cmInstallCommandArguments libraryArgs(helper.DefaultComponentName,
1306
0
                                        *helper.Makefile);
1307
0
  cmInstallCommandArguments runtimeArgs(helper.DefaultComponentName,
1308
0
                                        *helper.Makefile);
1309
0
  cmInstallCommandArguments frameworkArgs(helper.DefaultComponentName,
1310
0
                                          *helper.Makefile);
1311
0
  cmInstallCommandArguments bundleArgs(helper.DefaultComponentName,
1312
0
                                       *helper.Makefile);
1313
1314
  // now parse the args for specific parts of the target (e.g. LIBRARY,
1315
  // RUNTIME etc.
1316
0
  libraryArgs.Parse(argVectors.Library, &unknownArgs);
1317
0
  runtimeArgs.Parse(argVectors.Runtime, &unknownArgs);
1318
0
  frameworkArgs.Parse(argVectors.Framework, &unknownArgs);
1319
0
  bundleArgs.Parse(argVectors.Bundle, &unknownArgs);
1320
1321
0
  if (!unknownArgs.empty()) {
1322
    // Unknown argument.
1323
0
    status.SetError(
1324
0
      cmStrCat("IMPORTED_RUNTIME_ARTIFACTS given unknown argument \"",
1325
0
               unknownArgs[0], "\"."));
1326
0
    return false;
1327
0
  }
1328
1329
  // apply generic args
1330
0
  libraryArgs.SetGenericArguments(&genericArgs);
1331
0
  runtimeArgs.SetGenericArguments(&genericArgs);
1332
0
  frameworkArgs.SetGenericArguments(&genericArgs);
1333
0
  bundleArgs.SetGenericArguments(&genericArgs);
1334
1335
0
  success = success && libraryArgs.Finalize();
1336
0
  success = success && runtimeArgs.Finalize();
1337
0
  success = success && frameworkArgs.Finalize();
1338
0
  success = success && bundleArgs.Finalize();
1339
1340
0
  if (!success) {
1341
0
    return false;
1342
0
  }
1343
1344
0
  cmInstallRuntimeDependencySet* runtimeDependencySet = nullptr;
1345
0
  if (!runtimeDependencySetArg.empty()) {
1346
0
    auto system = helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
1347
0
    if (!cmRuntimeDependencyArchive::PlatformSupportsRuntimeDependencies(
1348
0
          system)) {
1349
0
      status.SetError(
1350
0
        cmStrCat("IMPORTED_RUNTIME_ARTIFACTS RUNTIME_DEPENDENCY_SET is not "
1351
0
                 "supported on system \"",
1352
0
                 system, '"'));
1353
0
      return false;
1354
0
    }
1355
0
    runtimeDependencySet =
1356
0
      helper.Makefile->GetGlobalGenerator()->GetNamedRuntimeDependencySet(
1357
0
        runtimeDependencySetArg);
1358
0
  }
1359
1360
  // Check if there is something to do.
1361
0
  if (targetList.empty()) {
1362
0
    return true;
1363
0
  }
1364
1365
0
  for (std::string const& tgt : targetList) {
1366
    // Lookup this target in the current directory.
1367
0
    cmTarget* target = helper.Makefile->FindTargetToUse(tgt);
1368
0
    if (!target || !target->IsImported()) {
1369
      // If no local target has been found, find it in the global scope.
1370
0
      cmTarget* const globalTarget =
1371
0
        helper.Makefile->GetGlobalGenerator()->FindTarget(
1372
0
          tgt, { cm::TargetDomain::NATIVE });
1373
0
      if (globalTarget && globalTarget->IsImported()) {
1374
0
        target = globalTarget;
1375
0
      }
1376
0
    }
1377
0
    if (target) {
1378
      // Found the target.  Check its type.
1379
0
      if (target->GetType() != cm::TargetType::EXECUTABLE &&
1380
0
          target->GetType() != cm::TargetType::SHARED_LIBRARY &&
1381
0
          target->GetType() != cm::TargetType::MODULE_LIBRARY) {
1382
0
        status.SetError(
1383
0
          cmStrCat("IMPORTED_RUNTIME_ARTIFACTS given target \"", tgt,
1384
0
                   "\" which is not an executable, library, or module."));
1385
0
        return false;
1386
0
      }
1387
      // Store the target in the list to be installed.
1388
0
      targets.push_back(target);
1389
0
    } else {
1390
      // Did not find the target.
1391
0
      status.SetError(cmStrCat("IMPORTED_RUNTIME_ARTIFACTS given target \"",
1392
0
                               tgt, "\" which does not exist."));
1393
0
      return false;
1394
0
    }
1395
0
  }
1396
1397
  // Keep track of whether we will be performing an installation of
1398
  // any files of the given type.
1399
0
  bool installsLibrary = false;
1400
0
  bool installsRuntime = false;
1401
0
  bool installsFramework = false;
1402
0
  bool installsBundle = false;
1403
1404
0
  auto const createInstallGenerator =
1405
0
    [helper](cmTarget& target, cmInstallCommandArguments const& typeArgs,
1406
0
             std::string const& destination)
1407
0
    -> std::unique_ptr<cmInstallImportedRuntimeArtifactsGenerator> {
1408
0
    return cm::make_unique<cmInstallImportedRuntimeArtifactsGenerator>(
1409
0
      target.GetName(), destination, typeArgs.GetPermissions(),
1410
0
      typeArgs.GetConfigurations(), typeArgs.GetComponent(),
1411
0
      cmInstallGenerator::SelectMessageLevel(helper.Makefile),
1412
0
      typeArgs.GetExcludeFromAll(), typeArgs.GetOptional(),
1413
0
      helper.CaptureContext());
1414
0
  };
1415
1416
  // Generate install script code to install the given targets.
1417
0
  for (cmTarget* ti : targets) {
1418
    // Handle each target type.
1419
0
    cmTarget& target = *ti;
1420
0
    std::unique_ptr<cmInstallImportedRuntimeArtifactsGenerator>
1421
0
      libraryGenerator;
1422
0
    std::unique_ptr<cmInstallImportedRuntimeArtifactsGenerator>
1423
0
      runtimeGenerator;
1424
0
    std::unique_ptr<cmInstallImportedRuntimeArtifactsGenerator>
1425
0
      frameworkGenerator;
1426
0
    std::unique_ptr<cmInstallImportedRuntimeArtifactsGenerator>
1427
0
      bundleGenerator;
1428
1429
0
    switch (target.GetType()) {
1430
0
      case cm::TargetType::SHARED_LIBRARY:
1431
0
        if (target.IsDLLPlatform()) {
1432
0
          runtimeGenerator = createInstallGenerator(
1433
0
            target, runtimeArgs, helper.GetRuntimeDestination(&runtimeArgs));
1434
0
          if (runtimeDependencySet) {
1435
0
            runtimeDependencySet->AddLibrary(runtimeGenerator.get());
1436
0
          }
1437
0
        } else if (target.IsFrameworkOnApple()) {
1438
0
          if (frameworkArgs.GetDestination().empty()) {
1439
0
            status.SetError(cmStrCat("IMPORTED_RUNTIME_ARTIFACTS given no "
1440
0
                                     "FRAMEWORK DESTINATION for shared "
1441
0
                                     "library FRAMEWORK target \"",
1442
0
                                     target.GetName(), "\"."));
1443
0
            return false;
1444
0
          }
1445
0
          frameworkGenerator = createInstallGenerator(
1446
0
            target, frameworkArgs, frameworkArgs.GetDestination());
1447
0
          if (runtimeDependencySet) {
1448
0
            runtimeDependencySet->AddLibrary(frameworkGenerator.get());
1449
0
          }
1450
0
        } else {
1451
0
          libraryGenerator = createInstallGenerator(
1452
0
            target, libraryArgs, helper.GetLibraryDestination(&libraryArgs));
1453
0
          if (runtimeDependencySet) {
1454
0
            runtimeDependencySet->AddLibrary(libraryGenerator.get());
1455
0
          }
1456
0
        }
1457
0
        break;
1458
0
      case cm::TargetType::MODULE_LIBRARY:
1459
0
        libraryGenerator = createInstallGenerator(
1460
0
          target, libraryArgs, helper.GetLibraryDestination(&libraryArgs));
1461
0
        if (runtimeDependencySet) {
1462
0
          runtimeDependencySet->AddModule(libraryGenerator.get());
1463
0
        }
1464
0
        break;
1465
0
      case cm::TargetType::EXECUTABLE:
1466
0
        if (target.IsAppBundleOnApple()) {
1467
0
          if (bundleArgs.GetDestination().empty()) {
1468
0
            status.SetError(
1469
0
              cmStrCat("IMPORTED_RUNTIME_ARTIFACTS given no BUNDLE "
1470
0
                       "DESTINATION for MACOSX_BUNDLE executable target \"",
1471
0
                       target.GetName(), "\"."));
1472
0
            return false;
1473
0
          }
1474
0
          bundleGenerator = createInstallGenerator(
1475
0
            target, bundleArgs, bundleArgs.GetDestination());
1476
0
          if (runtimeDependencySet) {
1477
0
            if (!AddBundleExecutable(helper, runtimeDependencySet,
1478
0
                                     bundleGenerator.get())) {
1479
0
              return false;
1480
0
            }
1481
0
          }
1482
0
        } else {
1483
0
          runtimeGenerator = createInstallGenerator(
1484
0
            target, runtimeArgs, helper.GetRuntimeDestination(&runtimeArgs));
1485
0
          if (runtimeDependencySet) {
1486
0
            runtimeDependencySet->AddExecutable(runtimeGenerator.get());
1487
0
          }
1488
0
        }
1489
0
        break;
1490
0
      default:
1491
0
        assert(false && "This should never happen");
1492
0
        break;
1493
0
    }
1494
1495
    // Keep track of whether we're installing anything in each category
1496
0
    installsLibrary = installsLibrary || libraryGenerator;
1497
0
    installsRuntime = installsRuntime || runtimeGenerator;
1498
0
    installsFramework = installsFramework || frameworkGenerator;
1499
0
    installsBundle = installsBundle || bundleGenerator;
1500
1501
0
    helper.Makefile->AddInstallGenerator(std::move(libraryGenerator));
1502
0
    helper.Makefile->AddInstallGenerator(std::move(runtimeGenerator));
1503
0
    helper.Makefile->AddInstallGenerator(std::move(frameworkGenerator));
1504
0
    helper.Makefile->AddInstallGenerator(std::move(bundleGenerator));
1505
0
  }
1506
1507
  // Tell the global generator about any installation component names
1508
  // specified
1509
0
  if (installsLibrary) {
1510
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1511
0
      libraryArgs.GetComponent());
1512
0
  }
1513
0
  if (installsRuntime) {
1514
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1515
0
      runtimeArgs.GetComponent());
1516
0
  }
1517
0
  if (installsFramework) {
1518
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1519
0
      frameworkArgs.GetComponent());
1520
0
  }
1521
0
  if (installsBundle) {
1522
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1523
0
      bundleArgs.GetComponent());
1524
0
  }
1525
1526
0
  return true;
1527
0
}
1528
1529
bool HandleFilesMode(std::vector<std::string> const& args,
1530
                     cmExecutionStatus& status)
1531
0
{
1532
0
  Helper helper(status);
1533
1534
  // This is the FILES mode.
1535
0
  bool programs = (args[0] == "PROGRAMS");
1536
0
  cmInstallCommandArguments ica(helper.DefaultComponentName, *helper.Makefile);
1537
0
  ArgumentParser::MaybeEmpty<std::vector<std::string>> files;
1538
0
  ica.Bind(programs ? "PROGRAMS"_s : "FILES"_s, files);
1539
0
  std::vector<std::string> unknownArgs;
1540
0
  ica.Parse(args, &unknownArgs);
1541
1542
0
  if (!unknownArgs.empty()) {
1543
    // Unknown argument.
1544
0
    status.SetError(
1545
0
      cmStrCat(args[0], " given unknown argument \"", unknownArgs[0], "\"."));
1546
0
    return false;
1547
0
  }
1548
1549
0
  std::string type = ica.GetType();
1550
0
  if (!type.empty() && allowedTypes.count(type) == 0) {
1551
0
    status.SetError(
1552
0
      cmStrCat(args[0], " given non-type \"", type, "\" with TYPE argument."));
1553
0
    return false;
1554
0
  }
1555
1556
0
  std::vector<std::string> const& filesVector = files;
1557
1558
  // Check if there is something to do.
1559
0
  if (filesVector.empty()) {
1560
0
    return true;
1561
0
  }
1562
1563
0
  if (!ica.GetRename().empty() && filesVector.size() > 1) {
1564
    // The rename option works only with one file.
1565
0
    status.SetError(
1566
0
      cmStrCat(args[0], " given RENAME option with more than one file."));
1567
0
    return false;
1568
0
  }
1569
1570
0
  std::vector<std::string> absFiles;
1571
0
  if (!helper.MakeFilesFullPath(args[0].c_str(), filesVector, absFiles)) {
1572
0
    return false;
1573
0
  }
1574
1575
0
  cmGlobalGenerator* gg = helper.Makefile->GetGlobalGenerator();
1576
0
  for (std::string const& file : filesVector) {
1577
0
    if (gg->IsExportedTargetsFile(file)) {
1578
0
      helper.Makefile->IssueMessage(
1579
0
        MessageType::FATAL_ERROR,
1580
0
        cmStrCat("The file\n  ", file,
1581
0
                 "\n"
1582
0
                 "was generated by the export() command.  "
1583
0
                 "It may not be installed with the install() command.  "
1584
0
                 "Use the install(EXPORT) mechanism instead.  "
1585
0
                 "See the cmake-packages(7) manual for more."));
1586
0
      return false;
1587
0
    }
1588
0
  }
1589
1590
0
  if (!ica.Finalize()) {
1591
0
    return false;
1592
0
  }
1593
1594
0
  if (!type.empty() && !ica.GetDestination().empty()) {
1595
0
    status.SetError(cmStrCat(args[0],
1596
0
                             " given both TYPE and DESTINATION arguments. "
1597
0
                             "You may only specify one."));
1598
0
    return false;
1599
0
  }
1600
1601
0
  std::string destination = helper.GetDestinationForType(&ica, type);
1602
0
  if (destination.empty()) {
1603
    // A destination is required.
1604
0
    status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
1605
0
    return false;
1606
0
  }
1607
1608
  // Create the files install generator.
1609
0
  helper.Makefile->AddInstallGenerator(CreateInstallFilesGenerator(
1610
0
    helper.Makefile, absFiles, ica, programs, destination));
1611
1612
  // Tell the global generator about any installation component names
1613
  // specified.
1614
0
  helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
1615
0
    ica.GetComponent());
1616
1617
0
  return true;
1618
0
}
1619
1620
bool HandleDirectoryMode(std::vector<std::string> const& args,
1621
                         cmExecutionStatus& status)
1622
0
{
1623
0
  Helper helper(status);
1624
1625
0
  enum Doing
1626
0
  {
1627
0
    DoingNone,
1628
0
    DoingDirs,
1629
0
    DoingDestination,
1630
0
    DoingPattern,
1631
0
    DoingRegex,
1632
0
    DoingPermsFile,
1633
0
    DoingPermsDir,
1634
0
    DoingPermsMatch,
1635
0
    DoingConfigurations,
1636
0
    DoingComponent,
1637
0
    DoingType
1638
0
  };
1639
0
  Doing doing = DoingDirs;
1640
0
  bool inMatchMode = false;
1641
0
  bool optional = false;
1642
0
  bool excludeFromAll = false;
1643
0
  bool messageNever = false;
1644
0
  std::vector<std::string> dirs;
1645
0
  cm::optional<std::string> destination;
1646
0
  std::string permissionsFile;
1647
0
  std::string permissionsDir;
1648
0
  std::vector<std::string> configurations;
1649
0
  std::string component = helper.DefaultComponentName;
1650
0
  std::string literalArgs;
1651
0
  std::string type;
1652
0
  for (unsigned int i = 1; i < args.size(); ++i) {
1653
0
    if (args[i] == "DESTINATION") {
1654
0
      if (inMatchMode) {
1655
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1656
0
                                 "\" after PATTERN or REGEX."));
1657
0
        return false;
1658
0
      }
1659
1660
      // Switch to setting the destination property.
1661
0
      doing = DoingDestination;
1662
0
    } else if (args[i] == "TYPE") {
1663
0
      if (inMatchMode) {
1664
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1665
0
                                 "\" after PATTERN or REGEX."));
1666
0
        return false;
1667
0
      }
1668
1669
      // Switch to setting the type.
1670
0
      doing = DoingType;
1671
0
    } else if (args[i] == "OPTIONAL") {
1672
0
      if (inMatchMode) {
1673
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1674
0
                                 "\" after PATTERN or REGEX."));
1675
0
        return false;
1676
0
      }
1677
1678
      // Mark the rule as optional.
1679
0
      optional = true;
1680
0
      doing = DoingNone;
1681
0
    } else if (args[i] == "MESSAGE_NEVER") {
1682
0
      if (inMatchMode) {
1683
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1684
0
                                 "\" after PATTERN or REGEX."));
1685
0
        return false;
1686
0
      }
1687
1688
      // Mark the rule as quiet.
1689
0
      messageNever = true;
1690
0
      doing = DoingNone;
1691
0
    } else if (args[i] == "PATTERN") {
1692
      // Switch to a new pattern match rule.
1693
0
      doing = DoingPattern;
1694
0
      inMatchMode = true;
1695
0
    } else if (args[i] == "REGEX") {
1696
      // Switch to a new regex match rule.
1697
0
      doing = DoingRegex;
1698
0
      inMatchMode = true;
1699
0
    } else if (args[i] == "EXCLUDE") {
1700
      // Add this property to the current match rule.
1701
0
      if (!inMatchMode || doing == DoingPattern || doing == DoingRegex) {
1702
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1703
0
                                 "\" before a PATTERN or REGEX is given."));
1704
0
        return false;
1705
0
      }
1706
0
      literalArgs += " EXCLUDE";
1707
0
      doing = DoingNone;
1708
0
    } else if (args[i] == "PERMISSIONS") {
1709
0
      if (!inMatchMode) {
1710
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1711
0
                                 "\" before a PATTERN or REGEX is given."));
1712
0
        return false;
1713
0
      }
1714
1715
      // Switch to setting the current match permissions property.
1716
0
      literalArgs += " PERMISSIONS";
1717
0
      doing = DoingPermsMatch;
1718
0
    } else if (args[i] == "FILE_PERMISSIONS") {
1719
0
      if (inMatchMode) {
1720
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1721
0
                                 "\" after PATTERN or REGEX."));
1722
0
        return false;
1723
0
      }
1724
1725
      // Switch to setting the file permissions property.
1726
0
      doing = DoingPermsFile;
1727
0
    } else if (args[i] == "DIRECTORY_PERMISSIONS") {
1728
0
      if (inMatchMode) {
1729
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1730
0
                                 "\" after PATTERN or REGEX."));
1731
0
        return false;
1732
0
      }
1733
1734
      // Switch to setting the directory permissions property.
1735
0
      doing = DoingPermsDir;
1736
0
    } else if (args[i] == "USE_SOURCE_PERMISSIONS") {
1737
0
      if (inMatchMode) {
1738
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1739
0
                                 "\" after PATTERN or REGEX."));
1740
0
        return false;
1741
0
      }
1742
1743
      // Add this option literally.
1744
0
      literalArgs += " USE_SOURCE_PERMISSIONS";
1745
0
      doing = DoingNone;
1746
0
    } else if (args[i] == "FILES_MATCHING") {
1747
0
      if (inMatchMode) {
1748
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1749
0
                                 "\" after PATTERN or REGEX."));
1750
0
        return false;
1751
0
      }
1752
1753
      // Add this option literally.
1754
0
      literalArgs += " FILES_MATCHING";
1755
0
      doing = DoingNone;
1756
0
    } else if (args[i] == "CONFIGURATIONS") {
1757
0
      if (inMatchMode) {
1758
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1759
0
                                 "\" after PATTERN or REGEX."));
1760
0
        return false;
1761
0
      }
1762
1763
      // Switch to setting the configurations property.
1764
0
      doing = DoingConfigurations;
1765
0
    } else if (args[i] == "COMPONENT") {
1766
0
      if (inMatchMode) {
1767
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1768
0
                                 "\" after PATTERN or REGEX."));
1769
0
        return false;
1770
0
      }
1771
1772
      // Switch to setting the component property.
1773
0
      doing = DoingComponent;
1774
0
    } else if (args[i] == "EXCLUDE_FROM_ALL") {
1775
0
      if (inMatchMode) {
1776
0
        status.SetError(cmStrCat(args[0], " does not allow \"", args[i],
1777
0
                                 "\" after PATTERN or REGEX."));
1778
0
        return false;
1779
0
      }
1780
0
      excludeFromAll = true;
1781
0
      doing = DoingNone;
1782
0
    } else if (doing == DoingDirs) {
1783
      // If the given directory is not a full path, convert it to one by
1784
      // assuming it's relative to the current source directory.
1785
0
      std::string dir = args[i];
1786
0
      std::string::size_type gpos = cmGeneratorExpression::Find(dir);
1787
0
      if (!dir.empty() && gpos != 0 && !cmSystemTools::FileIsFullPath(dir)) {
1788
0
        dir =
1789
0
          cmStrCat(helper.Makefile->GetCurrentSourceDirectory(), '/', args[i]);
1790
0
      }
1791
1792
      // Make sure the name is a directory.
1793
0
      if (cmSystemTools::FileExists(dir, true)) {
1794
0
        status.SetError(cmStrCat(args[0], " given non-directory \"", args[i],
1795
0
                                 "\" to install."));
1796
0
        return false;
1797
0
      }
1798
1799
      // Store the directory for installation.
1800
0
      dirs.push_back(std::move(dir));
1801
0
    } else if (doing == DoingConfigurations) {
1802
0
      configurations.push_back(args[i]);
1803
0
    } else if (doing == DoingDestination) {
1804
      // A trailing slash is meaningful for this form, but normalization
1805
      // preserves it if present
1806
0
      switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0177)) {
1807
0
        case cmPolicies::NEW:
1808
0
          destination = cmCMakePath(args[i]).Normal().String();
1809
0
          break;
1810
0
        case cmPolicies::WARN:
1811
          // We can't be certain if a warning is appropriate if there are any
1812
          // generator expressions
1813
0
          if (cmGeneratorExpression::Find(args[i]) == cm::string_view::npos &&
1814
0
              args[i] != cmCMakePath(args[i]).Normal().String()) {
1815
0
            status.GetMakefile().IssuePolicyWarning(cmPolicies::CMP0177);
1816
0
          }
1817
0
          CM_FALLTHROUGH;
1818
0
        case cmPolicies::OLD:
1819
0
          destination = args[i];
1820
0
          break;
1821
0
      }
1822
0
      doing = DoingNone;
1823
0
    } else if (doing == DoingType) {
1824
0
      if (allowedTypes.count(args[i]) == 0) {
1825
0
        status.SetError(cmStrCat(args[0], " given non-type \"", args[i],
1826
0
                                 "\" with TYPE argument."));
1827
0
        return false;
1828
0
      }
1829
1830
0
      type = args[i];
1831
0
      doing = DoingNone;
1832
0
    } else if (doing == DoingPattern) {
1833
      // Convert the pattern to a regular expression.  Require a
1834
      // leading slash and trailing end-of-string in the matched
1835
      // string to make sure the pattern matches only whole file
1836
      // names.
1837
0
      literalArgs += " REGEX \"/";
1838
0
      std::string regex = cmsys::Glob::PatternToRegex(args[i], false);
1839
0
      cmSystemTools::ReplaceString(regex, "\\", "\\\\");
1840
0
      literalArgs += regex;
1841
0
      literalArgs += "$\"";
1842
0
      doing = DoingNone;
1843
0
    } else if (doing == DoingRegex) {
1844
0
      literalArgs += " REGEX \"";
1845
// Match rules are case-insensitive on some platforms.
1846
#if defined(_WIN32) || defined(__APPLE__)
1847
      std::string regex = cmSystemTools::LowerCase(args[i]);
1848
#else
1849
0
      std::string regex = args[i];
1850
0
#endif
1851
0
      cmSystemTools::ReplaceString(regex, "\\", "\\\\");
1852
0
      literalArgs += regex;
1853
0
      literalArgs += "\"";
1854
0
      doing = DoingNone;
1855
0
    } else if (doing == DoingComponent) {
1856
0
      component = args[i];
1857
0
      doing = DoingNone;
1858
0
    } else if (doing == DoingPermsFile) {
1859
      // Check the requested permission.
1860
0
      if (!cmInstallCommandArguments::CheckPermissions(args[i],
1861
0
                                                       permissionsFile)) {
1862
0
        status.SetError(cmStrCat(args[0], " given invalid file permission \"",
1863
0
                                 args[i], "\"."));
1864
0
        return false;
1865
0
      }
1866
0
    } else if (doing == DoingPermsDir) {
1867
      // Check the requested permission.
1868
0
      if (!cmInstallCommandArguments::CheckPermissions(args[i],
1869
0
                                                       permissionsDir)) {
1870
0
        status.SetError(cmStrCat(
1871
0
          args[0], " given invalid directory permission \"", args[i], "\"."));
1872
0
        return false;
1873
0
      }
1874
0
    } else if (doing == DoingPermsMatch) {
1875
      // Check the requested permission.
1876
0
      if (!cmInstallCommandArguments::CheckPermissions(args[i], literalArgs)) {
1877
0
        status.SetError(
1878
0
          cmStrCat(args[0], " given invalid permission \"", args[i], "\"."));
1879
0
        return false;
1880
0
      }
1881
0
    } else {
1882
      // Unknown argument.
1883
0
      status.SetError(
1884
0
        cmStrCat(args[0], " given unknown argument \"", args[i], "\"."));
1885
0
      return false;
1886
0
    }
1887
0
  }
1888
1889
  // Support installing an empty directory.
1890
0
  if (dirs.empty() && destination.has_value()) {
1891
0
    dirs.emplace_back();
1892
0
  }
1893
1894
  // Check if there is something to do.
1895
0
  if (dirs.empty()) {
1896
0
    return true;
1897
0
  }
1898
0
  if (!destination.has_value()) {
1899
0
    if (type.empty()) {
1900
      // A destination is required.
1901
0
      status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
1902
0
      return false;
1903
0
    }
1904
0
    destination = helper.GetDestinationForType(nullptr, type);
1905
0
  } else if (!type.empty()) {
1906
0
    status.SetError(cmStrCat(args[0],
1907
0
                             " given both TYPE and DESTINATION "
1908
0
                             "arguments. You may only specify one."));
1909
0
    return false;
1910
0
  }
1911
1912
0
  cmInstallGenerator::MessageLevel message =
1913
0
    cmInstallGenerator::SelectMessageLevel(helper.Makefile, messageNever);
1914
1915
  // Check for CMD_INSTALL_ABSOLUTE_DESTINATION diagnostics.
1916
0
  CheckAbsoluteDestination(helper, *destination);
1917
1918
  // Create the directory install generator.
1919
0
  helper.Makefile->AddInstallGenerator(
1920
0
    cm::make_unique<cmInstallDirectoryGenerator>(
1921
0
      dirs, *destination, permissionsFile, permissionsDir, configurations,
1922
0
      component, message, excludeFromAll, literalArgs, optional,
1923
0
      helper.CaptureContext()));
1924
1925
  // Tell the global generator about any installation component names
1926
  // specified.
1927
0
  helper.Makefile->GetGlobalGenerator()->AddInstallComponent(component);
1928
1929
0
  return true;
1930
0
}
1931
1932
bool HandleExportAndroidMKMode(std::vector<std::string> const& args,
1933
                               cmExecutionStatus& status)
1934
0
{
1935
0
#ifndef CMAKE_BOOTSTRAP
1936
0
  Helper helper(status);
1937
1938
  // This is the EXPORT mode.
1939
0
  cmInstallCommandArguments ica(helper.DefaultComponentName, *helper.Makefile);
1940
1941
0
  std::string exp;
1942
0
  std::string exportNamespace;
1943
0
  bool exportOld = false;
1944
0
  std::string filename;
1945
1946
0
  ica.Bind("EXPORT_ANDROID_MK"_s, exp);
1947
0
  ica.Bind("NAMESPACE"_s, exportNamespace);
1948
0
  ica.Bind("EXPORT_LINK_INTERFACE_LIBRARIES"_s, exportOld);
1949
0
  ica.Bind("FILE"_s, filename);
1950
1951
0
  std::vector<std::string> unknownArgs;
1952
0
  ica.Parse(args, &unknownArgs);
1953
1954
0
  if (!unknownArgs.empty()) {
1955
    // Unknown argument.
1956
0
    status.SetError(
1957
0
      cmStrCat(args[0], " given unknown argument \"", unknownArgs[0], "\"."));
1958
0
    return false;
1959
0
  }
1960
1961
0
  if (!ica.Finalize()) {
1962
0
    return false;
1963
0
  }
1964
1965
  // Make sure there is a destination.
1966
0
  if (ica.GetDestination().empty()) {
1967
    // A destination is required.
1968
0
    status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
1969
0
    return false;
1970
0
  }
1971
1972
  // Check for CMD_INSTALL_ABSOLUTE_DESTINATION diagnostics.
1973
0
  CheckAbsoluteDestination(helper, ica.GetDestination());
1974
1975
  // Check the file name.
1976
0
  std::string fname = filename;
1977
0
  if (fname.find_first_of(":/\\") != std::string::npos) {
1978
0
    status.SetError(cmStrCat(args[0], " given invalid export file name \"",
1979
0
                             fname,
1980
0
                             "\".  The FILE argument may not contain a path.  "
1981
0
                             "Specify the path in the DESTINATION argument."));
1982
0
    return false;
1983
0
  }
1984
1985
  // Check the file extension.
1986
0
  if (!fname.empty() && !cmHasSuffix(fname, ".mk"_s)) {
1987
0
    status.SetError(cmStrCat(
1988
0
      args[0], " given invalid export file name \"", fname,
1989
0
      R"(".  The FILE argument must specify a name ending in ".mk".)"));
1990
0
    return false;
1991
0
  }
1992
0
  if (fname.find_first_of(":/\\") != std::string::npos) {
1993
0
    status.SetError(
1994
0
      cmStrCat(args[0], " given export name \"", exp,
1995
0
               "\".  "
1996
0
               "This name cannot be safely converted to a file name.  "
1997
0
               "Specify a different export name or use the FILE option to set "
1998
0
               "a file name explicitly."));
1999
0
    return false;
2000
0
  }
2001
  // Use the default name
2002
0
  if (fname.empty()) {
2003
0
    fname = "Android.mk";
2004
0
  }
2005
2006
0
  cmExportSet& exportSet =
2007
0
    helper.Makefile->GetGlobalGenerator()->GetExportSets()[exp];
2008
2009
0
  cmInstallGenerator::MessageLevel message =
2010
0
    cmInstallGenerator::SelectMessageLevel(helper.Makefile);
2011
2012
  // Tell the global generator about any installation component names
2013
  // specified
2014
0
  helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
2015
0
    ica.GetComponent());
2016
2017
  // Create the export install generator.
2018
0
  helper.Makefile->AddInstallGenerator(
2019
0
    cm::make_unique<cmInstallAndroidMKExportGenerator>(
2020
0
      &exportSet, ica.GetDestination(), ica.GetPermissions(),
2021
0
      ica.GetConfigurations(), ica.GetComponent(), message,
2022
0
      ica.GetExcludeFromAll(), std::move(fname), std::move(exportNamespace),
2023
0
      helper.CaptureContext()));
2024
2025
0
  return true;
2026
#else
2027
  static_cast<void>(args);
2028
  status.SetError("EXPORT_ANDROID_MK not supported in bootstrap cmake");
2029
  return false;
2030
#endif
2031
0
}
2032
2033
#ifndef CMAKE_BOOTSTRAP
2034
cm::optional<cm::string_view> MatchExport(cm::string_view directive,
2035
                                          std::string const& exportName)
2036
0
{
2037
0
  std::string::size_type const l = exportName.size();
2038
0
  if (directive.substr(0, l) == exportName) {
2039
0
    if (directive.size() > l && directive[l] == ':') {
2040
0
      return directive.substr(l + 1);
2041
0
    }
2042
0
  }
2043
0
  return cm::nullopt;
2044
0
}
2045
2046
void AssignValue(std::string& dest, std::string const& value)
2047
0
{
2048
0
  dest = value;
2049
0
}
2050
2051
void AssignValue(std::vector<std::string>& dest, std::string const& value)
2052
0
{
2053
0
  dest = cmList{ value }.data();
2054
0
}
2055
2056
template <typename T>
2057
void GetExportArgumentFromVariable(cmMakefile const* makefile,
2058
                                   cmExportSet const& exportSet,
2059
                                   cm::string_view suffix, T& variable)
2060
0
{
2061
0
  std::string const& name =
2062
0
    cmStrCat(exportSet.GetName(), "_EXPORT_PACKAGE_INFO_"_s, suffix);
2063
0
  if (cmValue const& value = makefile->GetDefinition(name)) {
2064
0
    std::string realValue;
2065
0
    makefile->ConfigureString(value, realValue, true, false);
2066
0
    AssignValue(variable, realValue);
2067
0
  }
2068
0
}
Unexecuted instantiation: cmInstallCommand.cxx:void (anonymous namespace)::GetExportArgumentFromVariable<ArgumentParser::NonEmpty<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >(cmMakefile const*, cmExportSet const&, std::__1::basic_string_view<char, std::__1::char_traits<char> >, ArgumentParser::NonEmpty<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >&)
Unexecuted instantiation: cmInstallCommand.cxx:void (anonymous namespace)::GetExportArgumentFromVariable<ArgumentParser::NonEmpty<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> > > > > >(cmMakefile const*, cmExportSet const&, std::__1::basic_string_view<char, std::__1::char_traits<char> >, ArgumentParser::NonEmpty<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> > > > >&)
2069
2070
bool HandleMappedPackageInfo(
2071
  cmExportSet& exportSet, cm::string_view directive, Helper& helper,
2072
  cmInstallCommandArguments const& installCommandArgs,
2073
  cmExecutionStatus& status, cmInstallGenerator::MessageLevel message,
2074
  std::string const& cxxModulesDirectory)
2075
0
{
2076
0
  cmPackageInfoArguments arguments;
2077
2078
  // Extract information from the directive.
2079
0
  std::string::size_type const n = directive.find('/');
2080
0
  if (n != std::string::npos) {
2081
0
    arguments.PackageName = std::string{ directive.substr(0, n) };
2082
0
    directive = directive.substr(n + 1);
2083
2084
0
    if (!directive.empty() && directive[0] == 'l') {
2085
0
      arguments.LowerCase = true;
2086
0
      directive = directive.substr(1);
2087
0
    }
2088
2089
0
    if (!directive.empty() && directive[0] == 'a') {
2090
0
      std::string::size_type const d = directive.find('/');
2091
0
      if (d != std::string::npos) {
2092
0
        arguments.Appendix = std::string{ directive.substr(1, d - 1) };
2093
0
        directive = directive.substr(d);
2094
0
      } else {
2095
0
        arguments.Appendix = std::string{ directive.substr(1) };
2096
0
        directive = {};
2097
0
      }
2098
2099
0
      if (arguments.Appendix.empty()) {
2100
0
        status.SetError(cmStrCat(
2101
0
          "CMAKE_INSTALL_EXPORTS_AS_PACKAGE_INFO given APPENDIX "
2102
0
          R"(directive for export ")"_s,
2103
0
          exportSet.GetName(), R"(", but no appendix name was provided.)"_s));
2104
0
        return false;
2105
0
      }
2106
0
    }
2107
2108
0
    if (!directive.empty()) {
2109
0
      if (directive[0] != '/') {
2110
0
        status.SetError(
2111
0
          cmStrCat("CMAKE_INSTALL_EXPORTS_AS_PACKAGE_INFO given unrecognized "
2112
0
                   R"(directive  ")"_s,
2113
0
                   directive, R"(".)"_s));
2114
0
        return false;
2115
0
      }
2116
2117
0
      directive = directive.substr(1);
2118
0
    }
2119
0
  } else {
2120
0
    arguments.PackageName = std::string{ directive };
2121
0
    directive = {};
2122
0
  }
2123
2124
0
  if (arguments.PackageName.empty()) {
2125
0
    status.SetError(
2126
0
      cmStrCat("CMAKE_INSTALL_EXPORTS_AS_PACKAGE_INFO missing package name "
2127
0
               R"(for export ")"_s,
2128
0
               exportSet.GetName(), R"(".)"_s));
2129
0
    return false;
2130
0
  }
2131
2132
  // Build destination.
2133
0
  std::string dest = std::string{ directive };
2134
0
  if (dest.empty()) {
2135
0
    if (helper.Makefile->GetSafeDefinition("CMAKE_SYSTEM_NAME") == "Windows") {
2136
0
      dest = arguments.GetDefaultDestination();
2137
0
    } else {
2138
0
      dest =
2139
0
        arguments.GetDefaultDestination(helper.GetLibraryDestination(nullptr));
2140
0
    }
2141
0
  }
2142
2143
  // Check for CMD_INSTALL_ABSOLUTE_DESTINATION diagnostics.
2144
0
  CheckAbsoluteDestination(helper, dest);
2145
2146
0
  if (arguments.Appendix.empty()) {
2147
    // Get additional export information from variables.
2148
0
    GetExportArgumentFromVariable( // BR
2149
0
      helper.Makefile, exportSet, "VERSION"_s, arguments.Version);
2150
0
    GetExportArgumentFromVariable( // BR
2151
0
      helper.Makefile, exportSet, "COMPAT_VERSION"_s, arguments.VersionCompat);
2152
0
    GetExportArgumentFromVariable( // BR
2153
0
      helper.Makefile, exportSet, "VERSION_SCHEMA"_s, arguments.VersionSchema);
2154
0
    GetExportArgumentFromVariable( // BR
2155
0
      helper.Makefile, exportSet, "LICENSE"_s, arguments.License);
2156
0
    GetExportArgumentFromVariable( // BR
2157
0
      helper.Makefile, exportSet, "DEFAULT_LICENSE"_s,
2158
0
      arguments.DefaultLicense);
2159
0
    GetExportArgumentFromVariable( // BR
2160
0
      helper.Makefile, exportSet, "DEFAULT_CONFIGURATIONS"_s,
2161
0
      arguments.DefaultConfigs);
2162
0
  }
2163
2164
  // Sanity-check export information.
2165
0
  if (!arguments.Check(status)) {
2166
0
    return false;
2167
0
  }
2168
2169
  // Create the package info generator.
2170
0
  helper.Makefile->AddInstallGenerator(
2171
0
    cm::make_unique<cmInstallPackageInfoExportGenerator>(
2172
0
      &exportSet, dest, installCommandArgs.GetPermissions(),
2173
0
      installCommandArgs.GetConfigurations(),
2174
0
      installCommandArgs.GetComponent(), message,
2175
0
      installCommandArgs.GetExcludeFromAll(), std::move(arguments),
2176
0
      cxxModulesDirectory, helper.CaptureContext()));
2177
2178
0
  return true;
2179
0
}
2180
#endif
2181
2182
bool HandleExportMode(std::vector<std::string> const& args,
2183
                      cmExecutionStatus& status)
2184
0
{
2185
0
  Helper helper(status);
2186
2187
  // This is the EXPORT mode.
2188
0
  cmInstallCommandArguments ica(helper.DefaultComponentName, *helper.Makefile);
2189
2190
0
  std::string exp;
2191
0
  std::string exportNamespace;
2192
0
  bool exportOld = false;
2193
0
  std::string filename;
2194
0
  std::string cxxModulesDirectory;
2195
0
  bool exportPackageDependencies = false;
2196
2197
0
  ica.Bind("EXPORT"_s, exp);
2198
0
  ica.Bind("NAMESPACE"_s, exportNamespace);
2199
0
  ica.Bind("EXPORT_LINK_INTERFACE_LIBRARIES"_s, exportOld);
2200
0
  ica.Bind("FILE"_s, filename);
2201
0
  ica.Bind("CXX_MODULES_DIRECTORY"_s, cxxModulesDirectory);
2202
2203
0
  if (cmExperimental::HasSupportEnabled(
2204
0
        status.GetMakefile(),
2205
0
        cmExperimental::Feature::ExportPackageDependencies)) {
2206
0
    ica.Bind("EXPORT_PACKAGE_DEPENDENCIES"_s, exportPackageDependencies);
2207
0
  }
2208
2209
0
  std::vector<std::string> unknownArgs;
2210
0
  ica.Parse(args, &unknownArgs);
2211
2212
0
  if (!unknownArgs.empty()) {
2213
    // Unknown argument.
2214
0
    status.SetError(
2215
0
      cmStrCat(args[0], " given unknown argument \"", unknownArgs[0], "\"."));
2216
0
    return false;
2217
0
  }
2218
2219
0
  if (!ica.Finalize()) {
2220
0
    return false;
2221
0
  }
2222
2223
0
  if (exp.empty()) {
2224
0
    status.SetError(cmStrCat(args[0], " missing EXPORT."));
2225
0
    return false;
2226
0
  }
2227
2228
  // Make sure there is a destination.
2229
0
  if (ica.GetDestination().empty()) {
2230
    // A destination is required.
2231
0
    status.SetError(cmStrCat(args[0], " given no DESTINATION!"));
2232
0
    return false;
2233
0
  }
2234
2235
  // Check the file name.
2236
0
  std::string fname = filename;
2237
0
  if (fname.find_first_of(":/\\") != std::string::npos) {
2238
0
    status.SetError(cmStrCat(args[0], " given invalid export file name \"",
2239
0
                             fname,
2240
0
                             "\".  "
2241
0
                             "The FILE argument may not contain a path.  "
2242
0
                             "Specify the path in the DESTINATION argument."));
2243
0
    return false;
2244
0
  }
2245
2246
  // Check the file extension.
2247
0
  if (!fname.empty() && !cmHasSuffix(fname, ".cmake"_s)) {
2248
0
    status.SetError(
2249
0
      cmStrCat(args[0], " given invalid export file name \"", fname,
2250
0
               "\".  "
2251
0
               "The FILE argument must specify a name ending in \".cmake\"."));
2252
0
    return false;
2253
0
  }
2254
2255
  // Construct the file name.
2256
0
  if (fname.empty()) {
2257
0
    fname = cmStrCat(exp, ".cmake");
2258
2259
0
    if (fname.find_first_of(":/\\") != std::string::npos) {
2260
0
      status.SetError(cmStrCat(
2261
0
        args[0], " given export name \"", exp,
2262
0
        "\".  "
2263
0
        "This name cannot be safely converted to a file name.  "
2264
0
        "Specify a different export name or use the FILE option to set "
2265
0
        "a file name explicitly."));
2266
0
      return false;
2267
0
    }
2268
0
  }
2269
2270
0
  cmExportSet& exportSet =
2271
0
    helper.Makefile->GetGlobalGenerator()->GetExportSets()[exp];
2272
2273
0
  cmInstallGenerator::MessageLevel message =
2274
0
    cmInstallGenerator::SelectMessageLevel(helper.Makefile);
2275
2276
  // Tell the global generator about any installation component names
2277
  // specified
2278
0
  helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
2279
0
    ica.GetComponent());
2280
2281
0
#ifndef CMAKE_BOOTSTRAP
2282
  // Check if PACKAGE_INFO export has been requested for this export set.
2283
0
  if (cmExperimental::HasSupportEnabled(
2284
0
        status.GetMakefile(), cmExperimental::Feature::MappedPackageInfo)) {
2285
0
    if (cmValue const& piExports = helper.Makefile->GetDefinition(
2286
0
          "CMAKE_INSTALL_EXPORTS_AS_PACKAGE_INFO")) {
2287
0
      for (auto const& pie : cmList{ piExports }) {
2288
0
        cm::optional<cm::string_view> const directive = MatchExport(pie, exp);
2289
0
        if (directive) {
2290
0
          if (!HandleMappedPackageInfo(exportSet, *directive, helper, ica,
2291
0
                                       status, message, cxxModulesDirectory)) {
2292
0
            return false;
2293
0
          }
2294
0
        }
2295
0
      }
2296
0
    }
2297
0
  }
2298
0
#endif
2299
2300
  // Check for CMD_INSTALL_ABSOLUTE_DESTINATION diagnostics.
2301
0
  CheckAbsoluteDestination(helper, ica.GetDestination());
2302
2303
  // Create the export install generator.
2304
0
  helper.Makefile->AddInstallGenerator(
2305
0
    cm::make_unique<cmInstallCMakeConfigExportGenerator>(
2306
0
      &exportSet, ica.GetDestination(), ica.GetPermissions(),
2307
0
      ica.GetConfigurations(), ica.GetComponent(), message,
2308
0
      ica.GetExcludeFromAll(), std::move(fname), std::move(exportNamespace),
2309
0
      std::move(cxxModulesDirectory), exportOld, exportPackageDependencies,
2310
0
      helper.CaptureContext()));
2311
2312
0
  return true;
2313
0
}
2314
2315
bool HandlePackageInfoMode(std::vector<std::string> const& args,
2316
                           cmExecutionStatus& status)
2317
0
{
2318
0
#ifndef CMAKE_BOOTSTRAP
2319
0
  Helper helper(status);
2320
2321
  // This is the PACKAGE_INFO mode.
2322
0
  cmInstallCommandArguments ica(helper.DefaultComponentName, *helper.Makefile);
2323
2324
0
  cmPackageInfoArguments arguments;
2325
0
  ArgumentParser::NonEmpty<std::string> exportName;
2326
0
  ArgumentParser::NonEmpty<std::string> cxxModulesDirectory;
2327
2328
0
  arguments.Bind(ica);
2329
0
  ica.Bind("EXPORT"_s, exportName);
2330
0
  ica.Bind("CXX_MODULES_DIRECTORY"_s, cxxModulesDirectory);
2331
2332
0
  std::vector<std::string> unknownArgs;
2333
0
  ArgumentParser::ParseResult result = ica.Parse(args, &unknownArgs);
2334
2335
0
  if (!result.Check(args[0], &unknownArgs, status)) {
2336
0
    return false;
2337
0
  }
2338
2339
0
  if (!ica.Finalize()) {
2340
0
    return false;
2341
0
  }
2342
2343
0
  if (exportName.empty()) {
2344
0
    status.SetError(cmStrCat(args[0], " missing EXPORT."));
2345
0
    return false;
2346
0
  }
2347
2348
0
  if (!arguments.Check(status) || !arguments.SetMetadataFromProject(status)) {
2349
0
    return false;
2350
0
  }
2351
2352
  // Get or construct the destination path.
2353
0
  std::string dest = ica.GetDestination();
2354
0
  if (dest.empty()) {
2355
0
    if (helper.Makefile->GetSafeDefinition("CMAKE_SYSTEM_NAME") == "Windows") {
2356
0
      dest = arguments.GetDefaultDestination();
2357
0
    } else {
2358
0
      dest =
2359
0
        arguments.GetDefaultDestination(helper.GetLibraryDestination(nullptr));
2360
0
    }
2361
0
  }
2362
2363
  // Check for CMD_INSTALL_ABSOLUTE_DESTINATION diagnostics.
2364
0
  CheckAbsoluteDestination(helper, dest);
2365
2366
0
  cmExportSet& exportSet =
2367
0
    helper.Makefile->GetGlobalGenerator()->GetExportSets()[exportName];
2368
2369
0
  cmInstallGenerator::MessageLevel message =
2370
0
    cmInstallGenerator::SelectMessageLevel(helper.Makefile);
2371
2372
  // Tell the global generator about any installation component names
2373
  // specified
2374
0
  helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
2375
0
    ica.GetComponent());
2376
2377
  // Create the export install generator.
2378
0
  helper.Makefile->AddInstallGenerator(
2379
0
    cm::make_unique<cmInstallPackageInfoExportGenerator>(
2380
0
      &exportSet, dest, ica.GetPermissions(), ica.GetConfigurations(),
2381
0
      ica.GetComponent(), message, ica.GetExcludeFromAll(),
2382
0
      std::move(arguments), std::move(cxxModulesDirectory),
2383
0
      helper.CaptureContext()));
2384
2385
0
  return true;
2386
#else
2387
  static_cast<void>(args);
2388
  status.SetError("PACKAGE_INFO not supported in bootstrap cmake");
2389
  return false;
2390
#endif
2391
0
}
2392
2393
bool HandleRuntimeDependencySetMode(std::vector<std::string> const& args,
2394
                                    cmExecutionStatus& status)
2395
0
{
2396
0
  Helper helper(status);
2397
2398
0
  auto system = helper.Makefile->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
2399
0
  if (!cmRuntimeDependencyArchive::PlatformSupportsRuntimeDependencies(
2400
0
        system)) {
2401
0
    status.SetError(cmStrCat(
2402
0
      "RUNTIME_DEPENDENCY_SET is not supported on system \"", system, '"'));
2403
0
    return false;
2404
0
  }
2405
2406
  // This is the RUNTIME_DEPENDENCY_SET mode.
2407
0
  cmInstallRuntimeDependencySet* runtimeDependencySet;
2408
2409
0
  struct ArgVectors
2410
0
  {
2411
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Library;
2412
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Runtime;
2413
0
    ArgumentParser::MaybeEmpty<std::vector<std::string>> Framework;
2414
0
  };
2415
2416
0
  static auto const argHelper = cmArgumentParser<ArgVectors>{}
2417
0
                                  .Bind("LIBRARY"_s, &ArgVectors::Library)
2418
0
                                  .Bind("RUNTIME"_s, &ArgVectors::Runtime)
2419
0
                                  .Bind("FRAMEWORK"_s, &ArgVectors::Framework);
2420
2421
0
  std::vector<std::string> genericArgVector;
2422
0
  ArgVectors const argVectors = argHelper.Parse(args, &genericArgVector);
2423
2424
  // now parse the generic args (i.e. the ones not specialized on LIBRARY,
2425
  // RUNTIME, FRAMEWORK etc. (see above)
2426
  // These generic args also contain the runtime dependency set
2427
0
  std::string runtimeDependencySetArg;
2428
0
  std::vector<std::string> runtimeDependencyArgVector;
2429
0
  cmInstallCommandArguments genericArgs(helper.DefaultComponentName,
2430
0
                                        *helper.Makefile);
2431
0
  genericArgs.Bind("RUNTIME_DEPENDENCY_SET"_s, runtimeDependencySetArg);
2432
0
  genericArgs.Parse(genericArgVector, &runtimeDependencyArgVector);
2433
0
  bool success = genericArgs.Finalize();
2434
2435
0
  cmInstallCommandArguments libraryArgs(helper.DefaultComponentName,
2436
0
                                        *helper.Makefile);
2437
0
  cmInstallCommandArguments runtimeArgs(helper.DefaultComponentName,
2438
0
                                        *helper.Makefile);
2439
0
  cmInstallCommandArguments frameworkArgs(helper.DefaultComponentName,
2440
0
                                          *helper.Makefile);
2441
2442
  // Now also parse the file(GET_RUNTIME_DEPENDENCY) args
2443
0
  std::vector<std::string> unknownArgs;
2444
0
  auto runtimeDependencyArgs = RuntimeDependenciesArgHelper.Parse(
2445
0
    runtimeDependencyArgVector, &unknownArgs);
2446
2447
  // now parse the args for specific parts of the target (e.g. LIBRARY,
2448
  // RUNTIME, FRAMEWORK etc.
2449
0
  libraryArgs.Parse(argVectors.Library, &unknownArgs);
2450
0
  runtimeArgs.Parse(argVectors.Runtime, &unknownArgs);
2451
0
  frameworkArgs.Parse(argVectors.Framework, &unknownArgs);
2452
2453
0
  libraryArgs.SetGenericArguments(&genericArgs);
2454
0
  runtimeArgs.SetGenericArguments(&genericArgs);
2455
0
  frameworkArgs.SetGenericArguments(&genericArgs);
2456
2457
0
  success = success && libraryArgs.Finalize();
2458
0
  success = success && runtimeArgs.Finalize();
2459
0
  success = success && frameworkArgs.Finalize();
2460
2461
0
  if (!success) {
2462
0
    return false;
2463
0
  }
2464
2465
0
  if (!unknownArgs.empty()) {
2466
0
    helper.SetError(
2467
0
      cmStrCat("RUNTIME_DEPENDENCY_SET given unknown argument \"",
2468
0
               unknownArgs.front(), "\"."));
2469
0
    return false;
2470
0
  }
2471
2472
0
  if (runtimeDependencySetArg.empty()) {
2473
0
    helper.SetError(
2474
0
      "RUNTIME_DEPENDENCY_SET not given a runtime dependency set.");
2475
0
    return false;
2476
0
  }
2477
2478
0
  runtimeDependencySet =
2479
0
    helper.Makefile->GetGlobalGenerator()->GetNamedRuntimeDependencySet(
2480
0
      runtimeDependencySetArg);
2481
2482
0
  bool installsRuntime = false;
2483
0
  bool installsLibrary = false;
2484
0
  bool installsFramework = false;
2485
2486
0
  AddInstallRuntimeDependenciesGenerator(
2487
0
    helper, runtimeDependencySet, runtimeArgs, libraryArgs, frameworkArgs,
2488
0
    std::move(runtimeDependencyArgs), installsRuntime, installsLibrary,
2489
0
    installsFramework);
2490
2491
  // Tell the global generator about any installation component names
2492
  // specified
2493
0
  if (installsLibrary) {
2494
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
2495
0
      libraryArgs.GetComponent());
2496
0
  }
2497
0
  if (installsRuntime) {
2498
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
2499
0
      runtimeArgs.GetComponent());
2500
0
  }
2501
0
  if (installsFramework) {
2502
0
    helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
2503
0
      frameworkArgs.GetComponent());
2504
0
  }
2505
2506
0
  return true;
2507
0
}
2508
2509
bool HandleSbomMode(std::vector<std::string> const& args,
2510
                    cmExecutionStatus& status)
2511
0
{
2512
0
#ifndef CMAKE_BOOTSTRAP
2513
0
  if (!cmExperimental::HasSupportEnabled(
2514
0
        status.GetMakefile(), cmExperimental::Feature::GenerateSbom)) {
2515
0
    status.SetError("does not recognize sub-command SBOM");
2516
0
    return false;
2517
0
  }
2518
2519
0
  Helper helper(status);
2520
0
  cmInstallCommandArguments ica(helper.DefaultComponentName, *helper.Makefile);
2521
2522
0
  cmSbomArguments arguments;
2523
0
  ArgumentParser::NonEmpty<std::vector<std::string>> exportNames;
2524
2525
0
  arguments.Bind(ica);
2526
0
  ica.Bind("EXPORTS"_s, exportNames);
2527
  // ica.Bind("CXX_MODULES_DIRECTORY"_s, cxxModulesDirectory); TODO?
2528
2529
0
  std::vector<std::string> unknownArgs;
2530
0
  ArgumentParser::ParseResult result = ica.Parse(args, &unknownArgs);
2531
0
  if (!result.Check(args[0], &unknownArgs, status)) {
2532
0
    return false;
2533
0
  }
2534
2535
0
  if (!ica.Finalize()) {
2536
0
    return false;
2537
0
  }
2538
2539
0
  if (arguments.PackageName.empty()) {
2540
    // TODO: Fix our use of the parser to enforce this.
2541
0
    status.SetError(cmStrCat(args[0], " missing SBOM name."));
2542
0
    return false;
2543
0
  }
2544
2545
0
  if (exportNames.empty()) {
2546
0
    status.SetError(cmStrCat(args[0], " missing EXPORTS."));
2547
0
    return false;
2548
0
  }
2549
2550
0
  if (!arguments.Check(status) || !arguments.SetMetadataFromProject(status)) {
2551
0
    return false;
2552
0
  }
2553
2554
  // Get or construct the destination path.
2555
0
  std::string dest = ica.GetDestination();
2556
0
  if (dest.empty()) {
2557
0
    dest =
2558
0
      arguments.GetDefaultDestination(helper.GetLibraryDestination(nullptr));
2559
0
  }
2560
2561
  // Check for CMD_INSTALL_ABSOLUTE_DESTINATION diagnostics.
2562
0
  CheckAbsoluteDestination(helper, dest);
2563
2564
0
  cmGlobalGenerator* gg = helper.Makefile->GetGlobalGenerator();
2565
2566
0
  std::string const fpath = cmStrCat(dest, '/', arguments.GetPackageName());
2567
0
  if (gg->IsInstallSbomFile(fpath)) {
2568
0
    status.SetError(cmStrCat("SBOM command already specified for the file "_s,
2569
0
                             cmSystemTools::GetFilenameNameView(fpath), '.'));
2570
0
    return false;
2571
0
  }
2572
2573
0
  cmExportSetMap& allExportSets = gg->GetExportSets();
2574
0
  std::vector<cmExportSet*> sets;
2575
0
  sets.reserve(exportNames.size());
2576
0
  for (std::string const& name : exportNames) {
2577
0
    sets.push_back(&allExportSets[name]);
2578
0
  }
2579
2580
0
  cmInstallGenerator::MessageLevel message =
2581
0
    cmInstallGenerator::SelectMessageLevel(helper.Makefile);
2582
2583
  // Tell the global generator about any installation component names
2584
  // specified.
2585
0
  helper.Makefile->GetGlobalGenerator()->AddInstallComponent(
2586
0
    ica.GetComponent());
2587
2588
  // Create the SBOM install generator.
2589
0
  auto sbomGen = cm::make_unique<cmInstallSbomGenerator>(
2590
0
    std::move(sets), dest, ica.GetPermissions(), ica.GetConfigurations(),
2591
0
    ica.GetComponent(), message, ica.GetExcludeFromAll(), std::move(arguments),
2592
0
    helper.CaptureContext());
2593
0
  cmInstallSbomGenerator const* rawPtr = sbomGen.get();
2594
0
  helper.Makefile->AddInstallGenerator(std::move(sbomGen));
2595
0
  helper.Makefile->GetGlobalGenerator()->AddInstallSbomGenerator(rawPtr);
2596
2597
0
  return true;
2598
#else
2599
  static_cast<void>(args);
2600
  status.SetError("SBOM not supported in bootstrap cmake");
2601
  return false;
2602
#endif
2603
0
}
2604
2605
bool Helper::MakeFilesFullPath(char const* modeName,
2606
                               std::vector<std::string> const& relFiles,
2607
                               std::vector<std::string>& absFiles)
2608
0
{
2609
0
  return this->MakeFilesFullPath(
2610
0
    modeName, this->Makefile->GetCurrentSourceDirectory(), relFiles, absFiles);
2611
0
}
2612
2613
bool Helper::MakeFilesFullPath(char const* modeName,
2614
                               std::string const& basePath,
2615
                               std::vector<std::string> const& relFiles,
2616
                               std::vector<std::string>& absFiles)
2617
0
{
2618
0
  for (std::string const& relFile : relFiles) {
2619
0
    std::string file = relFile;
2620
0
    std::string::size_type gpos = cmGeneratorExpression::Find(file);
2621
0
    if (gpos != 0 && !cmSystemTools::FileIsFullPath(file)) {
2622
0
      file = cmStrCat(basePath, '/', relFile);
2623
0
    }
2624
2625
    // Make sure the file is not a directory.
2626
0
    if (gpos == std::string::npos && !cmSystemTools::FileIsSymlink(file) &&
2627
0
        cmSystemTools::FileIsDirectory(file)) {
2628
0
      this->SetError(
2629
0
        cmStrCat(modeName, " given directory \"", relFile, "\" to install."));
2630
0
      return false;
2631
0
    }
2632
    // Store the file for installation.
2633
0
    absFiles.push_back(std::move(file));
2634
0
  }
2635
0
  return true;
2636
0
}
2637
2638
std::string Helper::GetRuntimeDestination(
2639
  cmInstallCommandArguments const* args) const
2640
0
{
2641
0
  if (args && !args->GetDestination().empty()) {
2642
0
    return args->GetDestination();
2643
0
  }
2644
0
  return cm::InstallDirs::GetRuntimeDirectory(this->Makefile);
2645
0
}
2646
2647
std::string Helper::GetArchiveDestination(
2648
  cmInstallCommandArguments const* args) const
2649
0
{
2650
0
  if (args && !args->GetDestination().empty()) {
2651
0
    return args->GetDestination();
2652
0
  }
2653
0
  return cm::InstallDirs::GetArchiveDirectory(this->Makefile);
2654
0
}
2655
2656
std::string Helper::GetLibraryDestination(
2657
  cmInstallCommandArguments const* args) const
2658
0
{
2659
0
  if (args && !args->GetDestination().empty()) {
2660
0
    return args->GetDestination();
2661
0
  }
2662
0
  return cm::InstallDirs::GetLibraryDirectory(this->Makefile);
2663
0
}
2664
2665
std::string Helper::GetCxxModulesBmiDestination(
2666
  cmInstallCommandArguments const* args) const
2667
0
{
2668
0
  if (args) {
2669
0
    return args->GetDestination();
2670
0
  }
2671
0
  return {};
2672
0
}
2673
2674
std::string Helper::GetIncludeDestination(
2675
  cmInstallCommandArguments const* args) const
2676
0
{
2677
0
  if (args && !args->GetDestination().empty()) {
2678
0
    return args->GetDestination();
2679
0
  }
2680
0
  return cm::InstallDirs::GetIncludeDirectory(this->Makefile);
2681
0
}
2682
2683
std::string Helper::GetDestinationForType(
2684
  cmInstallCommandArguments const* args, std::string const& type) const
2685
0
{
2686
0
  if (args && !args->GetDestination().empty()) {
2687
0
    return args->GetDestination();
2688
0
  }
2689
0
  return cm::InstallDirs::GetDirectoryForType(this->Makefile, type);
2690
0
}
2691
2692
} // namespace
2693
2694
bool cmInstallCommand(std::vector<std::string> const& args,
2695
                      cmExecutionStatus& status)
2696
0
{
2697
  // Allow calling with no arguments so that arguments may be built up
2698
  // using a variable that may be left empty.
2699
0
  if (args.empty()) {
2700
0
    return true;
2701
0
  }
2702
2703
  // Enable the install target.
2704
0
  status.GetMakefile().GetGlobalGenerator()->EnableInstallTarget();
2705
2706
0
  static cmSubcommandTable const subcommand{
2707
0
    { "SCRIPT"_s, HandleScriptMode },
2708
0
    { "CODE"_s, HandleScriptMode },
2709
0
    { "TARGETS"_s, HandleTargetsMode },
2710
0
    { "IMPORTED_RUNTIME_ARTIFACTS"_s, HandleImportedRuntimeArtifactsMode },
2711
0
    { "FILES"_s, HandleFilesMode },
2712
0
    { "PROGRAMS"_s, HandleFilesMode },
2713
0
    { "DIRECTORY"_s, HandleDirectoryMode },
2714
0
    { "EXPORT"_s, HandleExportMode },
2715
0
    { "EXPORT_ANDROID_MK"_s, HandleExportAndroidMKMode },
2716
0
    { "PACKAGE_INFO"_s, HandlePackageInfoMode },
2717
0
    { "RUNTIME_DEPENDENCY_SET"_s, HandleRuntimeDependencySetMode },
2718
0
    { "SBOM"_s, HandleSbomMode }
2719
0
  };
2720
2721
0
  return subcommand(args[0], args, status);
2722
0
}