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"
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"
14 #include <memory> // IWYU pragma: keep
18 cmCustomCommandGenerator::cmCustomCommandGenerator(cmCustomCommand const& cc,
19 const std::string& config,
24 , OldStyle(cc.GetEscapeOldStyle())
25 , MakeVars(cc.GetEscapeAllowMakeVars())
26 , GE(new cmGeneratorExpression(cc.GetBacktrace()))
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());
40 argv.push_back(std::move(parsed_arg));
43 this->CommandLines.push_back(std::move(argv));
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),
52 for (std::string& it : result) {
53 if (cmSystemTools::FileIsFullPath(it)) {
54 it = cmSystemTools::CollapseFullPath(it);
57 this->Depends.insert(this->Depends.end(), result.begin(), result.end());
61 cmCustomCommandGenerator::~cmCustomCommandGenerator()
66 unsigned int cmCustomCommandGenerator::GetNumberOfCommands() const
68 return static_cast<unsigned int>(this->CC.GetCommandLines().size());
71 const char* cmCustomCommandGenerator::GetCrossCompilingEmulator(
74 if (!this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING")) {
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");
86 const char* cmCustomCommandGenerator::GetArgv0Location(unsigned int c) const
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);
99 bool cmCustomCommandGenerator::HasOnlyEmptyCommandLines() const
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()) {
111 std::string cmCustomCommandGenerator::GetCommand(unsigned int c) const
113 if (const char* emulator = this->GetCrossCompilingEmulator(c)) {
114 return std::string(emulator);
116 if (const char* location = this->GetArgv0Location(c)) {
117 return std::string(location);
120 return this->CommandLines[c][0];
123 std::string escapeForShellOldStyle(const std::string& str)
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) {
138 for (const char* ch = str.c_str(); *ch != '\0'; ++ch) {
148 void cmCustomCommandGenerator::AppendArguments(unsigned int c,
149 std::string& cmd) const
151 unsigned int offset = 1;
152 if (this->GetCrossCompilingEmulator(c) != nullptr) {
155 cmCustomCommandLine const& commandLine = this->CommandLines[c];
156 for (unsigned int j = offset; j < commandLine.size(); ++j) {
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.
163 arg = commandLine[j];
166 if (this->OldStyle) {
167 cmd += escapeForShellOldStyle(arg);
169 cmd += this->LG->EscapeForShell(arg, this->MakeVars);
174 const char* cmCustomCommandGenerator::GetComment() const
176 return this->CC.GetComment();
179 std::string cmCustomCommandGenerator::GetWorkingDirectory() const
181 return this->CC.GetWorkingDirectory();
184 std::vector<std::string> const& cmCustomCommandGenerator::GetOutputs() const
186 return this->CC.GetOutputs();
189 std::vector<std::string> const& cmCustomCommandGenerator::GetByproducts() const
191 return this->CC.GetByproducts();
194 std::vector<std::string> const& cmCustomCommandGenerator::GetDepends() const
196 return this->Depends;