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