cmCustomCommandGenerator.cxx 5.9 KB

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