cmCustomCommandGenerator.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. }
  62. cmCustomCommandGenerator::~cmCustomCommandGenerator()
  63. {
  64. delete this->GE;
  65. }
  66. unsigned int cmCustomCommandGenerator::GetNumberOfCommands() const
  67. {
  68. return static_cast<unsigned int>(this->CC.GetCommandLines().size());
  69. }
  70. const char* cmCustomCommandGenerator::GetCrossCompilingEmulator(
  71. unsigned int c) const
  72. {
  73. if (!this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING")) {
  74. return nullptr;
  75. }
  76. std::string const& argv0 = this->CommandLines[c][0];
  77. cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(argv0);
  78. if (target && target->GetType() == cmStateEnums::EXECUTABLE &&
  79. !target->IsImported()) {
  80. return target->GetProperty("CROSSCOMPILING_EMULATOR");
  81. }
  82. return nullptr;
  83. }
  84. const char* cmCustomCommandGenerator::GetArgv0Location(unsigned int c) const
  85. {
  86. std::string const& argv0 = this->CommandLines[c][0];
  87. cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(argv0);
  88. if (target && target->GetType() == cmStateEnums::EXECUTABLE &&
  89. (target->IsImported() ||
  90. target->GetProperty("CROSSCOMPILING_EMULATOR") ||
  91. !this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING"))) {
  92. return target->GetLocation(this->Config);
  93. }
  94. return nullptr;
  95. }
  96. bool cmCustomCommandGenerator::HasOnlyEmptyCommandLines() const
  97. {
  98. for (size_t i = 0; i < this->CommandLines.size(); ++i) {
  99. for (size_t j = 0; j < this->CommandLines[i].size(); ++j) {
  100. if (!this->CommandLines[i][j].empty()) {
  101. return false;
  102. }
  103. }
  104. }
  105. return true;
  106. }
  107. std::string cmCustomCommandGenerator::GetCommand(unsigned int c) const
  108. {
  109. if (const char* emulator = this->GetCrossCompilingEmulator(c)) {
  110. return std::string(emulator);
  111. }
  112. if (const char* location = this->GetArgv0Location(c)) {
  113. return std::string(location);
  114. }
  115. return this->CommandLines[c][0];
  116. }
  117. std::string escapeForShellOldStyle(const std::string& str)
  118. {
  119. std::string result;
  120. #if defined(_WIN32) && !defined(__CYGWIN__)
  121. // if there are spaces
  122. std::string temp = str;
  123. if (temp.find(" ") != std::string::npos &&
  124. temp.find("\"") == std::string::npos) {
  125. result = "\"";
  126. result += str;
  127. result += "\"";
  128. return result;
  129. }
  130. return str;
  131. #else
  132. for (const char* ch = str.c_str(); *ch != '\0'; ++ch) {
  133. if (*ch == ' ') {
  134. result += '\\';
  135. }
  136. result += *ch;
  137. }
  138. return result;
  139. #endif
  140. }
  141. void cmCustomCommandGenerator::AppendArguments(unsigned int c,
  142. std::string& cmd) const
  143. {
  144. unsigned int offset = 1;
  145. if (this->GetCrossCompilingEmulator(c) != nullptr) {
  146. offset = 0;
  147. }
  148. cmCustomCommandLine const& commandLine = this->CommandLines[c];
  149. for (unsigned int j = offset; j < commandLine.size(); ++j) {
  150. std::string arg;
  151. if (const char* location = j == 0 ? this->GetArgv0Location(c) : nullptr) {
  152. // GetCommand returned the emulator instead of the argv0 location,
  153. // so transform the latter now.
  154. arg = location;
  155. } else {
  156. arg = commandLine[j];
  157. }
  158. cmd += " ";
  159. if (this->OldStyle) {
  160. cmd += escapeForShellOldStyle(arg);
  161. } else {
  162. cmd += this->LG->EscapeForShell(arg, this->MakeVars);
  163. }
  164. }
  165. }
  166. const char* cmCustomCommandGenerator::GetComment() const
  167. {
  168. return this->CC.GetComment();
  169. }
  170. std::string cmCustomCommandGenerator::GetWorkingDirectory() const
  171. {
  172. return this->CC.GetWorkingDirectory();
  173. }
  174. std::vector<std::string> const& cmCustomCommandGenerator::GetOutputs() const
  175. {
  176. return this->CC.GetOutputs();
  177. }
  178. std::vector<std::string> const& cmCustomCommandGenerator::GetByproducts() const
  179. {
  180. return this->CC.GetByproducts();
  181. }
  182. std::vector<std::string> const& cmCustomCommandGenerator::GetDepends() const
  183. {
  184. return this->Depends;
  185. }