cmCustomCommandGenerator.cxx 5.9 KB

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