/src/CMake/Source/cmInstallProgramsCommand.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 "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, char const* name); |
22 | | |
23 | | bool cmInstallProgramsCommand(std::vector<std::string> const& args, |
24 | | cmExecutionStatus& status) |
25 | 0 | { |
26 | 0 | if (args.size() < 2) { |
27 | 0 | status.SetError("called with incorrect number of arguments"); |
28 | 0 | return false; |
29 | 0 | } |
30 | | |
31 | 0 | cmMakefile& mf = status.GetMakefile(); |
32 | | |
33 | | // Enable the install target. |
34 | 0 | mf.GetGlobalGenerator()->EnableInstallTarget(); |
35 | |
|
36 | 0 | mf.GetGlobalGenerator()->AddInstallComponent( |
37 | 0 | mf.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME")); |
38 | |
|
39 | 0 | std::string const& dest = args[0]; |
40 | 0 | std::vector<std::string> const finalArgs(args.begin() + 1, args.end()); |
41 | 0 | mf.AddGeneratorAction( |
42 | 0 | [dest, finalArgs](cmLocalGenerator& lg, cmListFileBacktrace const&) { |
43 | 0 | FinalAction(*lg.GetMakefile(), dest, finalArgs); |
44 | 0 | }); |
45 | 0 | return true; |
46 | 0 | } |
47 | | |
48 | | static void FinalAction(cmMakefile& makefile, std::string const& dest, |
49 | | std::vector<std::string> const& args) |
50 | 0 | { |
51 | 0 | bool files_mode = false; |
52 | 0 | if (!args.empty() && args[0] == "FILES") { |
53 | 0 | files_mode = true; |
54 | 0 | } |
55 | |
|
56 | 0 | std::vector<std::string> files; |
57 | | |
58 | | // two different options |
59 | 0 | if (args.size() > 1 || files_mode) { |
60 | | // for each argument, get the programs |
61 | 0 | auto s = args.begin(); |
62 | 0 | if (files_mode) { |
63 | | // Skip the FILES argument in files mode. |
64 | 0 | ++s; |
65 | 0 | } |
66 | 0 | for (; s != args.end(); ++s) { |
67 | | // add to the result |
68 | 0 | files.push_back(FindInstallSource(makefile, s->c_str())); |
69 | 0 | } |
70 | 0 | } else // reg exp list |
71 | 0 | { |
72 | 0 | std::vector<std::string> programs; |
73 | 0 | cmSystemTools::Glob(makefile.GetCurrentSourceDirectory(), args[0], |
74 | 0 | programs); |
75 | |
|
76 | 0 | auto s = programs.begin(); |
77 | | // for each argument, get the programs |
78 | 0 | for (; s != programs.end(); ++s) { |
79 | 0 | files.push_back(FindInstallSource(makefile, s->c_str())); |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | | // Construct the destination. This command always installs under |
84 | | // the prefix. We skip the leading slash given by the user. |
85 | 0 | std::string destination = dest.substr(1); |
86 | 0 | cmSystemTools::ConvertToUnixSlashes(destination); |
87 | 0 | if (destination.empty()) { |
88 | 0 | destination = "."; |
89 | 0 | } |
90 | | |
91 | | // Use a file install generator. |
92 | 0 | std::string const no_permissions; |
93 | 0 | std::string const no_rename; |
94 | 0 | bool no_exclude_from_all = false; |
95 | 0 | std::string no_component = |
96 | 0 | makefile.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME"); |
97 | 0 | std::vector<std::string> no_configurations; |
98 | 0 | cmInstallGenerator::MessageLevel message = |
99 | 0 | cmInstallGenerator::SelectMessageLevel(&makefile); |
100 | 0 | makefile.AddInstallGenerator(cm::make_unique<cmInstallFilesGenerator>( |
101 | 0 | files, destination, true, no_permissions, no_configurations, no_component, |
102 | 0 | message, no_exclude_from_all, no_rename, false, makefile.GetBacktrace())); |
103 | 0 | } |
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 | | */ |
111 | | static std::string FindInstallSource(cmMakefile& makefile, char const* name) |
112 | 0 | { |
113 | 0 | if (cmSystemTools::FileIsFullPath(name) || |
114 | 0 | cmGeneratorExpression::Find(name) == 0) { |
115 | | // This is a full path. |
116 | 0 | return name; |
117 | 0 | } |
118 | | |
119 | | // This is a relative path. |
120 | 0 | std::string tb = cmStrCat(makefile.GetCurrentBinaryDirectory(), '/', name); |
121 | 0 | std::string ts = cmStrCat(makefile.GetCurrentSourceDirectory(), '/', name); |
122 | |
|
123 | 0 | if (cmSystemTools::FileExists(tb)) { |
124 | | // The file exists in the binary tree. Use it. |
125 | 0 | return tb; |
126 | 0 | } |
127 | 0 | if (cmSystemTools::FileExists(ts)) { |
128 | | // The file exists in the source tree. Use it. |
129 | 0 | return ts; |
130 | 0 | } |
131 | | // The file doesn't exist. Assume it will be present in the |
132 | | // binary tree when the install occurs. |
133 | 0 | return tb; |
134 | 0 | } |