1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmInstallProgramsCommand.h"
7 #include "cmExecutionStatus.h"
8 #include "cmGeneratorExpression.h"
9 #include "cmGlobalGenerator.h"
10 #include "cmInstallFilesGenerator.h"
11 #include "cmInstallGenerator.h"
12 #include "cmLocalGenerator.h"
13 #include "cmMakefile.h"
14 #include "cmStringAlgorithms.h"
15 #include "cmSystemTools.h"
17 class cmListFileBacktrace;
19 static void FinalAction(cmMakefile& makefile, std::string const& dest,
20 std::vector<std::string> const& args);
21 static std::string FindInstallSource(cmMakefile& makefile, const char* name);
23 bool cmInstallProgramsCommand(std::vector<std::string> const& args,
24 cmExecutionStatus& status)
26 if (args.size() < 2) {
27 status.SetError("called with incorrect number of arguments");
31 cmMakefile& mf = status.GetMakefile();
33 // Enable the install target.
34 mf.GetGlobalGenerator()->EnableInstallTarget();
36 mf.GetGlobalGenerator()->AddInstallComponent(
37 mf.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME"));
39 std::string const& dest = args[0];
40 std::vector<std::string> const finalArgs(args.begin() + 1, args.end());
41 mf.AddGeneratorAction(
42 [dest, finalArgs](cmLocalGenerator& lg, const cmListFileBacktrace&) {
43 FinalAction(*lg.GetMakefile(), dest, finalArgs);
48 static void FinalAction(cmMakefile& makefile, std::string const& dest,
49 std::vector<std::string> const& args)
51 bool files_mode = false;
52 if (!args.empty() && args[0] == "FILES") {
56 std::vector<std::string> files;
58 // two different options
59 if (args.size() > 1 || files_mode) {
60 // for each argument, get the programs
61 auto s = args.begin();
63 // Skip the FILES argument in files mode.
66 for (; s != args.end(); ++s) {
68 files.push_back(FindInstallSource(makefile, s->c_str()));
70 } else // reg exp list
72 std::vector<std::string> programs;
73 cmSystemTools::Glob(makefile.GetCurrentSourceDirectory(), args[0],
76 auto s = programs.begin();
77 // for each argument, get the programs
78 for (; s != programs.end(); ++s) {
79 files.push_back(FindInstallSource(makefile, s->c_str()));
83 // Construct the destination. This command always installs under
84 // the prefix. We skip the leading slash given by the user.
85 std::string destination = dest.substr(1);
86 cmSystemTools::ConvertToUnixSlashes(destination);
87 if (destination.empty()) {
91 // Use a file install generator.
92 const std::string no_permissions;
93 const std::string no_rename;
94 bool no_exclude_from_all = false;
95 std::string no_component =
96 makefile.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME");
97 std::vector<std::string> no_configurations;
98 cmInstallGenerator::MessageLevel message =
99 cmInstallGenerator::SelectMessageLevel(&makefile);
100 makefile.AddInstallGenerator(cm::make_unique<cmInstallFilesGenerator>(
101 files, destination, true, no_permissions, no_configurations, no_component,
102 message, no_exclude_from_all, no_rename, false, makefile.GetBacktrace()));
106 * Find a file in the build or source tree for installation given a
107 * relative path from the CMakeLists.txt file. This will favor files
108 * present in the build tree. If a full path is given, it is just
111 static std::string FindInstallSource(cmMakefile& makefile, const char* name)
113 if (cmSystemTools::FileIsFullPath(name) ||
114 cmGeneratorExpression::Find(name) == 0) {
115 // This is a full path.
119 // This is a relative path.
120 std::string tb = cmStrCat(makefile.GetCurrentBinaryDirectory(), '/', name);
121 std::string ts = cmStrCat(makefile.GetCurrentSourceDirectory(), '/', name);
123 if (cmSystemTools::FileExists(tb)) {
124 // The file exists in the binary tree. Use it.
127 if (cmSystemTools::FileExists(ts)) {
128 // The file exists in the source tree. Use it.
131 // The file doesn't exist. Assume it will be present in the
132 // binary tree when the install occurs.