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 "cmInstallFilesCommand.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"
15 #include "cmStringAlgorithms.h"
16 #include "cmSystemTools.h"
18 class cmListFileBacktrace;
20 static std::string FindInstallSource(cmMakefile& makefile, const char* name);
21 static void CreateInstallGenerator(cmMakefile& makefile,
22 std::string const& dest,
23 std::vector<std::string> const& files);
24 static void FinalAction(cmMakefile& makefile, std::string const& dest,
25 std::vector<std::string> const& args);
27 bool cmInstallFilesCommand(std::vector<std::string> const& args,
28 cmExecutionStatus& status)
30 if (args.size() < 2) {
31 status.SetError("called with incorrect number of arguments");
35 cmMakefile& mf = status.GetMakefile();
37 // Enable the install target.
38 mf.GetGlobalGenerator()->EnableInstallTarget();
40 std::string const& dest = args[0];
42 if ((args.size() > 1) && (args[1] == "FILES")) {
43 std::vector<std::string> files;
44 for (std::string const& arg : cmMakeRange(args).advance(2)) {
45 // Find the source location for each file listed.
46 files.push_back(FindInstallSource(mf, arg.c_str()));
48 CreateInstallGenerator(mf, dest, files);
50 std::vector<std::string> finalArgs(args.begin() + 1, args.end());
51 mf.AddGeneratorAction(
52 [dest, finalArgs](cmLocalGenerator& lg, const cmListFileBacktrace&) {
53 FinalAction(*lg.GetMakefile(), dest, finalArgs);
57 mf.GetGlobalGenerator()->AddInstallComponent(
58 mf.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME"));
63 static void FinalAction(cmMakefile& makefile, std::string const& dest,
64 std::vector<std::string> const& args)
67 std::string const& ext = args[0];
68 std::vector<std::string> installFiles;
70 // two different options
71 if (args.size() > 1) {
72 // now put the files into the list
73 auto s = args.begin();
75 // for each argument, get the files
76 for (; s != args.end(); ++s) {
77 // replace any variables
78 std::string const& temps = *s;
79 if (!cmSystemTools::GetFilenamePath(temps).empty()) {
80 testf = cmSystemTools::GetFilenamePath(temps) + "/" +
81 cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
83 testf = cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
87 installFiles.push_back(FindInstallSource(makefile, testf.c_str()));
89 } else // reg exp list
91 std::vector<std::string> files;
92 std::string const& regex = args[0];
93 cmSystemTools::Glob(makefile.GetCurrentSourceDirectory(), regex, files);
95 auto s = files.begin();
96 // for each argument, get the files
97 for (; s != files.end(); ++s) {
98 installFiles.push_back(FindInstallSource(makefile, s->c_str()));
102 CreateInstallGenerator(makefile, dest, installFiles);
105 static void CreateInstallGenerator(cmMakefile& makefile,
106 std::string const& dest,
107 std::vector<std::string> const& files)
109 // Construct the destination. This command always installs under
110 // the prefix. We skip the leading slash given by the user.
111 std::string destination = dest.substr(1);
112 cmSystemTools::ConvertToUnixSlashes(destination);
113 if (destination.empty()) {
117 // Use a file install generator.
118 const std::string no_permissions;
119 const std::string no_rename;
120 bool no_exclude_from_all = false;
121 std::string no_component =
122 makefile.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME");
123 std::vector<std::string> no_configurations;
124 cmInstallGenerator::MessageLevel message =
125 cmInstallGenerator::SelectMessageLevel(&makefile);
126 makefile.AddInstallGenerator(cm::make_unique<cmInstallFilesGenerator>(
127 files, destination, false, no_permissions, no_configurations, no_component,
128 message, no_exclude_from_all, no_rename, false, makefile.GetBacktrace()));
132 * Find a file in the build or source tree for installation given a
133 * relative path from the CMakeLists.txt file. This will favor files
134 * present in the build tree. If a full path is given, it is just
137 static std::string FindInstallSource(cmMakefile& makefile, const char* name)
139 if (cmSystemTools::FileIsFullPath(name) ||
140 cmGeneratorExpression::Find(name) == 0) {
141 // This is a full path.
145 // This is a relative path.
146 std::string tb = cmStrCat(makefile.GetCurrentBinaryDirectory(), '/', name);
147 std::string ts = cmStrCat(makefile.GetCurrentSourceDirectory(), '/', name);
149 if (cmSystemTools::FileExists(tb)) {
150 // The file exists in the binary tree. Use it.
153 if (cmSystemTools::FileExists(ts)) {
154 // The file exists in the source tree. Use it.
157 // The file doesn't exist. Assume it will be present in the
158 // binary tree when the install occurs.