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