136cf39bea60b5a8072c409c0876a03ac052258f
[platform/upstream/cmake.git] / Source / cmCustomCommandGenerator.cxx
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 "cmCustomCommandGenerator.h"
4
5 #include "cmCustomCommand.h"
6 #include "cmCustomCommandLines.h"
7 #include "cmGeneratorExpression.h"
8 #include "cmGeneratorTarget.h"
9 #include "cmLocalGenerator.h"
10 #include "cmMakefile.h"
11 #include "cmStateTypes.h"
12 #include "cmSystemTools.h"
13
14 #include <memory> // IWYU pragma: keep
15 #include <stddef.h>
16 #include <utility>
17
18 cmCustomCommandGenerator::cmCustomCommandGenerator(cmCustomCommand const& cc,
19                                                    const std::string& config,
20                                                    cmLocalGenerator* lg)
21   : CC(cc)
22   , Config(config)
23   , LG(lg)
24   , OldStyle(cc.GetEscapeOldStyle())
25   , MakeVars(cc.GetEscapeAllowMakeVars())
26   , GE(new cmGeneratorExpression(cc.GetBacktrace()))
27 {
28   const cmCustomCommandLines& cmdlines = this->CC.GetCommandLines();
29   for (cmCustomCommandLine const& cmdline : cmdlines) {
30     cmCustomCommandLine argv;
31     for (std::string const& clarg : cmdline) {
32       std::unique_ptr<cmCompiledGeneratorExpression> cge =
33         this->GE->Parse(clarg);
34       std::string parsed_arg = cge->Evaluate(this->LG, this->Config);
35       if (this->CC.GetCommandExpandLists()) {
36         std::vector<std::string> ExpandedArg;
37         cmSystemTools::ExpandListArgument(parsed_arg, ExpandedArg);
38         argv.insert(argv.end(), ExpandedArg.begin(), ExpandedArg.end());
39       } else {
40         argv.push_back(std::move(parsed_arg));
41       }
42     }
43     this->CommandLines.push_back(std::move(argv));
44   }
45
46   std::vector<std::string> depends = this->CC.GetDepends();
47   for (std::string const& d : depends) {
48     std::unique_ptr<cmCompiledGeneratorExpression> cge = this->GE->Parse(d);
49     std::vector<std::string> result;
50     cmSystemTools::ExpandListArgument(cge->Evaluate(this->LG, this->Config),
51                                       result);
52     for (std::string& it : result) {
53       if (cmSystemTools::FileIsFullPath(it)) {
54         it = cmSystemTools::CollapseFullPath(it);
55       }
56     }
57     this->Depends.insert(this->Depends.end(), result.begin(), result.end());
58   }
59 }
60
61 cmCustomCommandGenerator::~cmCustomCommandGenerator()
62 {
63   delete this->GE;
64 }
65
66 unsigned int cmCustomCommandGenerator::GetNumberOfCommands() const
67 {
68   return static_cast<unsigned int>(this->CC.GetCommandLines().size());
69 }
70
71 const char* cmCustomCommandGenerator::GetCrossCompilingEmulator(
72   unsigned int c) const
73 {
74   if (!this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING")) {
75     return nullptr;
76   }
77   std::string const& argv0 = this->CommandLines[c][0];
78   cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(argv0);
79   if (target && target->GetType() == cmStateEnums::EXECUTABLE &&
80       !target->IsImported()) {
81     return target->GetProperty("CROSSCOMPILING_EMULATOR");
82   }
83   return nullptr;
84 }
85
86 const char* cmCustomCommandGenerator::GetArgv0Location(unsigned int c) const
87 {
88   std::string const& argv0 = this->CommandLines[c][0];
89   cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(argv0);
90   if (target && target->GetType() == cmStateEnums::EXECUTABLE &&
91       (target->IsImported() ||
92        target->GetProperty("CROSSCOMPILING_EMULATOR") ||
93        !this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING"))) {
94     return target->GetLocation(this->Config);
95   }
96   return nullptr;
97 }
98
99 bool cmCustomCommandGenerator::HasOnlyEmptyCommandLines() const
100 {
101   for (size_t i = 0; i < this->CommandLines.size(); ++i) {
102     for (size_t j = 0; j < this->CommandLines[i].size(); ++j) {
103       if (!this->CommandLines[i][j].empty()) {
104         return false;
105       }
106     }
107   }
108   return true;
109 }
110
111 std::string cmCustomCommandGenerator::GetCommand(unsigned int c) const
112 {
113   if (const char* emulator = this->GetCrossCompilingEmulator(c)) {
114     return std::string(emulator);
115   }
116   if (const char* location = this->GetArgv0Location(c)) {
117     return std::string(location);
118   }
119
120   return this->CommandLines[c][0];
121 }
122
123 std::string escapeForShellOldStyle(const std::string& str)
124 {
125   std::string result;
126 #if defined(_WIN32) && !defined(__CYGWIN__)
127   // if there are spaces
128   std::string temp = str;
129   if (temp.find(" ") != std::string::npos &&
130       temp.find("\"") == std::string::npos) {
131     result = "\"";
132     result += str;
133     result += "\"";
134     return result;
135   }
136   return str;
137 #else
138   for (const char* ch = str.c_str(); *ch != '\0'; ++ch) {
139     if (*ch == ' ') {
140       result += '\\';
141     }
142     result += *ch;
143   }
144   return result;
145 #endif
146 }
147
148 void cmCustomCommandGenerator::AppendArguments(unsigned int c,
149                                                std::string& cmd) const
150 {
151   unsigned int offset = 1;
152   if (this->GetCrossCompilingEmulator(c) != nullptr) {
153     offset = 0;
154   }
155   cmCustomCommandLine const& commandLine = this->CommandLines[c];
156   for (unsigned int j = offset; j < commandLine.size(); ++j) {
157     std::string arg;
158     if (const char* location = j == 0 ? this->GetArgv0Location(c) : nullptr) {
159       // GetCommand returned the emulator instead of the argv0 location,
160       // so transform the latter now.
161       arg = location;
162     } else {
163       arg = commandLine[j];
164     }
165     cmd += " ";
166     if (this->OldStyle) {
167       cmd += escapeForShellOldStyle(arg);
168     } else {
169       cmd += this->LG->EscapeForShell(arg, this->MakeVars);
170     }
171   }
172 }
173
174 const char* cmCustomCommandGenerator::GetComment() const
175 {
176   return this->CC.GetComment();
177 }
178
179 std::string cmCustomCommandGenerator::GetWorkingDirectory() const
180 {
181   return this->CC.GetWorkingDirectory();
182 }
183
184 std::vector<std::string> const& cmCustomCommandGenerator::GetOutputs() const
185 {
186   return this->CC.GetOutputs();
187 }
188
189 std::vector<std::string> const& cmCustomCommandGenerator::GetByproducts() const
190 {
191   return this->CC.GetByproducts();
192 }
193
194 std::vector<std::string> const& cmCustomCommandGenerator::GetDepends() const
195 {
196   return this->Depends;
197 }