cmCustomCommandGenerator.cxx 5.8 KB

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