cmCustomCommandGenerator.cxx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #include "cmCustomCommand.h"
  5. #include "cmCustomCommandLines.h"
  6. #include "cmGeneratorExpression.h"
  7. #include "cmGeneratorTarget.h"
  8. #include "cmLocalGenerator.h"
  9. #include "cmMakefile.h"
  10. #include "cmStateTypes.h"
  11. #include "cmSystemTools.h"
  12. #include <memory> // IWYU pragma: keep
  13. #include <stddef.h>
  14. #include <utility>
  15. cmCustomCommandGenerator::cmCustomCommandGenerator(cmCustomCommand const& cc,
  16. const std::string& config,
  17. cmLocalGenerator* lg)
  18. : CC(cc)
  19. , Config(config)
  20. , LG(lg)
  21. , OldStyle(cc.GetEscapeOldStyle())
  22. , MakeVars(cc.GetEscapeAllowMakeVars())
  23. , GE(new cmGeneratorExpression(cc.GetBacktrace()))
  24. {
  25. const cmCustomCommandLines& cmdlines = this->CC.GetCommandLines();
  26. for (cmCustomCommandLine const& cmdline : cmdlines) {
  27. cmCustomCommandLine argv;
  28. for (std::string const& clarg : cmdline) {
  29. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  30. this->GE->Parse(clarg);
  31. std::string parsed_arg = cge->Evaluate(this->LG, this->Config);
  32. if (this->CC.GetCommandExpandLists()) {
  33. std::vector<std::string> ExpandedArg;
  34. cmSystemTools::ExpandListArgument(parsed_arg, ExpandedArg);
  35. argv.insert(argv.end(), ExpandedArg.begin(), ExpandedArg.end());
  36. } else {
  37. argv.push_back(std::move(parsed_arg));
  38. }
  39. }
  40. // Later code assumes at least one entry exists, but expanding
  41. // lists on an empty command may have left this empty.
  42. // FIXME: Should we define behavior for removing empty commands?
  43. if (argv.empty()) {
  44. argv.push_back(std::string());
  45. }
  46. this->CommandLines.push_back(std::move(argv));
  47. }
  48. std::vector<std::string> depends = this->CC.GetDepends();
  49. for (std::string const& d : depends) {
  50. std::unique_ptr<cmCompiledGeneratorExpression> cge = this->GE->Parse(d);
  51. std::vector<std::string> result;
  52. cmSystemTools::ExpandListArgument(cge->Evaluate(this->LG, this->Config),
  53. result);
  54. for (std::string& it : result) {
  55. if (cmSystemTools::FileIsFullPath(it)) {
  56. it = cmSystemTools::CollapseFullPath(it);
  57. }
  58. }
  59. this->Depends.insert(this->Depends.end(), result.begin(), result.end());
  60. }
  61. const std::string& workingdirectory = this->CC.GetWorkingDirectory();
  62. if (!workingdirectory.empty()) {
  63. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  64. this->GE->Parse(workingdirectory);
  65. this->WorkingDirectory = cge->Evaluate(this->LG, this->Config);
  66. }
  67. }
  68. cmCustomCommandGenerator::~cmCustomCommandGenerator()
  69. {
  70. delete this->GE;
  71. }
  72. unsigned int cmCustomCommandGenerator::GetNumberOfCommands() const
  73. {
  74. return static_cast<unsigned int>(this->CC.GetCommandLines().size());
  75. }
  76. const char* cmCustomCommandGenerator::GetCrossCompilingEmulator(
  77. unsigned int c) const
  78. {
  79. if (!this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING")) {
  80. return nullptr;
  81. }
  82. std::string const& argv0 = this->CommandLines[c][0];
  83. cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(argv0);
  84. if (target && target->GetType() == cmStateEnums::EXECUTABLE &&
  85. !target->IsImported()) {
  86. return target->GetProperty("CROSSCOMPILING_EMULATOR");
  87. }
  88. return nullptr;
  89. }
  90. const char* cmCustomCommandGenerator::GetArgv0Location(unsigned int c) const
  91. {
  92. std::string const& argv0 = this->CommandLines[c][0];
  93. cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(argv0);
  94. if (target && target->GetType() == cmStateEnums::EXECUTABLE &&
  95. (target->IsImported() ||
  96. target->GetProperty("CROSSCOMPILING_EMULATOR") ||
  97. !this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING"))) {
  98. return target->GetLocation(this->Config);
  99. }
  100. return nullptr;
  101. }
  102. bool cmCustomCommandGenerator::HasOnlyEmptyCommandLines() const
  103. {
  104. for (size_t i = 0; i < this->CommandLines.size(); ++i) {
  105. for (size_t j = 0; j < this->CommandLines[i].size(); ++j) {
  106. if (!this->CommandLines[i][j].empty()) {
  107. return false;
  108. }
  109. }
  110. }
  111. return true;
  112. }
  113. std::string cmCustomCommandGenerator::GetCommand(unsigned int c) const
  114. {
  115. if (const char* emulator = this->GetCrossCompilingEmulator(c)) {
  116. return std::string(emulator);
  117. }
  118. if (const char* location = this->GetArgv0Location(c)) {
  119. return std::string(location);
  120. }
  121. return this->CommandLines[c][0];
  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. void cmCustomCommandGenerator::AppendArguments(unsigned int c,
  148. std::string& cmd) const
  149. {
  150. unsigned int offset = 1;
  151. if (this->GetCrossCompilingEmulator(c) != nullptr) {
  152. offset = 0;
  153. }
  154. cmCustomCommandLine const& commandLine = this->CommandLines[c];
  155. for (unsigned int j = offset; j < commandLine.size(); ++j) {
  156. std::string arg;
  157. if (const char* location = j == 0 ? this->GetArgv0Location(c) : nullptr) {
  158. // GetCommand returned the emulator instead of the argv0 location,
  159. // so transform the latter now.
  160. arg = location;
  161. } else {
  162. arg = commandLine[j];
  163. }
  164. cmd += " ";
  165. if (this->OldStyle) {
  166. cmd += escapeForShellOldStyle(arg);
  167. } else {
  168. cmd += this->LG->EscapeForShell(arg, this->MakeVars);
  169. }
  170. }
  171. }
  172. const char* cmCustomCommandGenerator::GetComment() const
  173. {
  174. return this->CC.GetComment();
  175. }
  176. std::string cmCustomCommandGenerator::GetWorkingDirectory() const
  177. {
  178. return this->WorkingDirectory;
  179. }
  180. std::vector<std::string> const& cmCustomCommandGenerator::GetOutputs() const
  181. {
  182. return this->CC.GetOutputs();
  183. }
  184. std::vector<std::string> const& cmCustomCommandGenerator::GetByproducts() const
  185. {
  186. return this->CC.GetByproducts();
  187. }
  188. std::vector<std::string> const& cmCustomCommandGenerator::GetDepends() const
  189. {
  190. return this->Depends;
  191. }