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