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"
4
5 #include <cm/memory>
6
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"
16
17 class cmListFileBacktrace;
18
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);
22
cmInstallProgramsCommand(std::vector<std::string> const & args,cmExecutionStatus & status)23 bool cmInstallProgramsCommand(std::vector<std::string> const& args,
24 cmExecutionStatus& status)
25 {
26 if (args.size() < 2) {
27 status.SetError("called with incorrect number of arguments");
28 return false;
29 }
30
31 cmMakefile& mf = status.GetMakefile();
32
33 // Enable the install target.
34 mf.GetGlobalGenerator()->EnableInstallTarget();
35
36 mf.GetGlobalGenerator()->AddInstallComponent(
37 mf.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME"));
38
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);
44 });
45 return true;
46 }
47
FinalAction(cmMakefile & makefile,std::string const & dest,std::vector<std::string> const & args)48 static void FinalAction(cmMakefile& makefile, std::string const& dest,
49 std::vector<std::string> const& args)
50 {
51 bool files_mode = false;
52 if (!args.empty() && args[0] == "FILES") {
53 files_mode = true;
54 }
55
56 std::vector<std::string> files;
57
58 // two different options
59 if (args.size() > 1 || files_mode) {
60 // for each argument, get the programs
61 auto s = args.begin();
62 if (files_mode) {
63 // Skip the FILES argument in files mode.
64 ++s;
65 }
66 for (; s != args.end(); ++s) {
67 // add to the result
68 files.push_back(FindInstallSource(makefile, s->c_str()));
69 }
70 } else // reg exp list
71 {
72 std::vector<std::string> programs;
73 cmSystemTools::Glob(makefile.GetCurrentSourceDirectory(), args[0],
74 programs);
75
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()));
80 }
81 }
82
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()) {
88 destination = ".";
89 }
90
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()));
103 }
104
105 /**
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
109 * returned.
110 */
FindInstallSource(cmMakefile & makefile,const char * name)111 static std::string FindInstallSource(cmMakefile& makefile, const char* name)
112 {
113 if (cmSystemTools::FileIsFullPath(name) ||
114 cmGeneratorExpression::Find(name) == 0) {
115 // This is a full path.
116 return name;
117 }
118
119 // This is a relative path.
120 std::string tb = cmStrCat(makefile.GetCurrentBinaryDirectory(), '/', name);
121 std::string ts = cmStrCat(makefile.GetCurrentSourceDirectory(), '/', name);
122
123 if (cmSystemTools::FileExists(tb)) {
124 // The file exists in the binary tree. Use it.
125 return tb;
126 }
127 if (cmSystemTools::FileExists(ts)) {
128 // The file exists in the source tree. Use it.
129 return ts;
130 }
131 // The file doesn't exist. Assume it will be present in the
132 // binary tree when the install occurs.
133 return tb;
134 }
135