Coverage Report

Created: 2026-07-30 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmInstallProgramsCommand.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 "cmInstallProgramsCommand.h"
4
5
#include <cm/memory>
6
7
#include "cmDiagnostics.h"
8
#include "cmExecutionStatus.h"
9
#include "cmGeneratorExpression.h"
10
#include "cmGlobalGenerator.h"
11
#include "cmInstallFilesGenerator.h"
12
#include "cmInstallGenerator.h"
13
#include "cmLocalGenerator.h"
14
#include "cmMakefile.h"
15
#include "cmStringAlgorithms.h"
16
#include "cmSystemTools.h"
17
18
class cmListFileBacktrace;
19
20
static void FinalAction(cmMakefile& makefile, std::string const& dest,
21
                        std::vector<std::string> const& args);
22
static std::string FindInstallSource(cmMakefile& makefile, char const* name);
23
24
bool cmInstallProgramsCommand(std::vector<std::string> const& args,
25
                              cmExecutionStatus& status)
26
0
{
27
0
  cmMakefile& mf = status.GetMakefile();
28
29
0
  mf.IssueDiagnostic(cmDiagnostics::CMD_DEPRECATED,
30
0
                     "The 'install_programs' command has been superseded. "
31
0
                     "Use 'install(PROGRAMS)' instead.");
32
33
0
  if (args.size() < 2) {
34
0
    status.SetError("called with incorrect number of arguments");
35
0
    return false;
36
0
  }
37
38
  // Enable the install target.
39
0
  mf.GetGlobalGenerator()->EnableInstallTarget();
40
41
0
  mf.GetGlobalGenerator()->AddInstallComponent(
42
0
    mf.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME"));
43
44
0
  std::string const& dest = args[0];
45
0
  std::vector<std::string> const finalArgs(args.begin() + 1, args.end());
46
0
  mf.AddGeneratorAction(
47
0
    [dest, finalArgs](cmLocalGenerator& lg, cmListFileBacktrace const&) {
48
0
      FinalAction(*lg.GetMakefile(), dest, finalArgs);
49
0
    });
50
0
  return true;
51
0
}
52
53
static void FinalAction(cmMakefile& makefile, std::string const& dest,
54
                        std::vector<std::string> const& args)
55
0
{
56
0
  bool filesMode = false;
57
0
  if (!args.empty() && args[0] == "FILES") {
58
0
    filesMode = true;
59
0
  }
60
61
0
  std::vector<std::string> files;
62
63
  // two different options
64
0
  if (args.size() > 1 || filesMode) {
65
    // for each argument, get the programs
66
0
    auto s = args.begin();
67
0
    if (filesMode) {
68
      // Skip the FILES argument in files mode.
69
0
      ++s;
70
0
    }
71
0
    for (; s != args.end(); ++s) {
72
      // add to the result
73
0
      files.push_back(FindInstallSource(makefile, s->c_str()));
74
0
    }
75
0
  } else // reg exp list
76
0
  {
77
0
    std::vector<std::string> programs;
78
0
    cmSystemTools::Glob(makefile.GetCurrentSourceDirectory(), args[0],
79
0
                        programs);
80
81
0
    auto s = programs.begin();
82
    // for each argument, get the programs
83
0
    for (; s != programs.end(); ++s) {
84
0
      files.push_back(FindInstallSource(makefile, s->c_str()));
85
0
    }
86
0
  }
87
88
  // Construct the destination.  This command always installs under
89
  // the prefix.  We skip the leading slash given by the user.
90
0
  std::string destination = dest.substr(1);
91
0
  cmSystemTools::ConvertToUnixSlashes(destination);
92
0
  if (destination.empty()) {
93
0
    destination = ".";
94
0
  }
95
96
  // Use a file install generator.
97
0
  std::string const no_permissions;
98
0
  std::string const no_rename;
99
0
  bool no_exclude_from_all = false;
100
0
  std::string no_component =
101
0
    makefile.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME");
102
0
  std::vector<std::string> no_configurations;
103
0
  cmInstallGenerator::MessageLevel message =
104
0
    cmInstallGenerator::SelectMessageLevel(&makefile);
105
0
  makefile.AddInstallGenerator(cm::make_unique<cmInstallFilesGenerator>(
106
0
    files, destination, true, no_permissions, no_configurations, no_component,
107
0
    message, no_exclude_from_all, no_rename, false,
108
0
    cmInstallGenerator::CaptureContext(makefile)));
109
0
}
110
111
/**
112
 * Find a file in the build or source tree for installation given a
113
 * relative path from the CMakeLists.txt file.  This will favor files
114
 * present in the build tree.  If a full path is given, it is just
115
 * returned.
116
 */
117
static std::string FindInstallSource(cmMakefile& makefile, char const* name)
118
0
{
119
0
  if (cmSystemTools::FileIsFullPath(name) ||
120
0
      cmGeneratorExpression::Find(name) == 0) {
121
    // This is a full path.
122
0
    return name;
123
0
  }
124
125
  // This is a relative path.
126
0
  std::string tb = cmStrCat(makefile.GetCurrentBinaryDirectory(), '/', name);
127
0
  std::string ts = cmStrCat(makefile.GetCurrentSourceDirectory(), '/', name);
128
129
0
  if (cmSystemTools::FileExists(tb)) {
130
    // The file exists in the binary tree.  Use it.
131
0
    return tb;
132
0
  }
133
0
  if (cmSystemTools::FileExists(ts)) {
134
    // The file exists in the source tree.  Use it.
135
0
    return ts;
136
0
  }
137
  // The file doesn't exist.  Assume it will be present in the
138
  // binary tree when the install occurs.
139
0
  return tb;
140
0
}