cmCustomCommandGenerator.cxx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "cmAlgorithms.h"
  5. #include "cmCustomCommand.h"
  6. #include "cmCustomCommandLines.h"
  7. #include "cmGeneratorExpression.h"
  8. #include "cmGeneratorTarget.h"
  9. #include "cmLocalGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmStateTypes.h"
  12. #include "cmSystemTools.h"
  13. #include <memory>
  14. #include <stddef.h>
  15. #include <utility>
  16. cmCustomCommandGenerator::cmCustomCommandGenerator(cmCustomCommand const& cc,
  17. std::string config,
  18. cmLocalGenerator* lg)
  19. : CC(cc)
  20. , Config(std::move(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. cmAppend(argv, cmSystemTools::ExpandedListArgument(parsed_arg));
  35. } else {
  36. argv.push_back(std::move(parsed_arg));
  37. }
  38. }
  39. // Later code assumes at least one entry exists, but expanding
  40. // lists on an empty command may have left this empty.
  41. // FIXME: Should we define behavior for removing empty commands?
  42. if (argv.empty()) {
  43. argv.push_back(std::string());
  44. }
  45. this->CommandLines.push_back(std::move(argv));
  46. }
  47. std::vector<std::string> depends = this->CC.GetDepends();
  48. for (std::string const& d : depends) {
  49. std::unique_ptr<cmCompiledGeneratorExpression> cge = this->GE->Parse(d);
  50. std::vector<std::string> result = cmSystemTools::ExpandedListArgument(
  51. cge->Evaluate(this->LG, this->Config));
  52. for (std::string& it : result) {
  53. if (cmSystemTools::FileIsFullPath(it)) {
  54. it = cmSystemTools::CollapseFullPath(it);
  55. }
  56. }
  57. cmAppend(this->Depends, result);
  58. }
  59. const std::string& workingdirectory = this->CC.GetWorkingDirectory();
  60. if (!workingdirectory.empty()) {
  61. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  62. this->GE->Parse(workingdirectory);
  63. this->WorkingDirectory = cge->Evaluate(this->LG, this->Config);
  64. // Convert working directory to a full path.
  65. if (!this->WorkingDirectory.empty()) {
  66. std::string const& build_dir = this->LG->GetCurrentBinaryDirectory();
  67. this->WorkingDirectory =
  68. cmSystemTools::CollapseFullPath(this->WorkingDirectory, build_dir);
  69. }
  70. }
  71. this->FillEmulatorsWithArguments();
  72. }
  73. cmCustomCommandGenerator::~cmCustomCommandGenerator()
  74. {
  75. delete this->GE;
  76. }
  77. unsigned int cmCustomCommandGenerator::GetNumberOfCommands() const
  78. {
  79. return static_cast<unsigned int>(this->CC.GetCommandLines().size());
  80. }
  81. void cmCustomCommandGenerator::FillEmulatorsWithArguments()
  82. {
  83. if (!this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING")) {
  84. return;
  85. }
  86. for (unsigned int c = 0; c < this->GetNumberOfCommands(); ++c) {
  87. std::string const& argv0 = this->CommandLines[c][0];
  88. cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(argv0);
  89. if (target && target->GetType() == cmStateEnums::EXECUTABLE &&
  90. !target->IsImported()) {
  91. const char* emulator_property =
  92. target->GetProperty("CROSSCOMPILING_EMULATOR");
  93. if (!emulator_property) {
  94. continue;
  95. }
  96. this->EmulatorsWithArguments.emplace_back();
  97. cmSystemTools::ExpandListArgument(emulator_property,
  98. this->EmulatorsWithArguments[c]);
  99. }
  100. }
  101. }
  102. std::vector<std::string> cmCustomCommandGenerator::GetCrossCompilingEmulator(
  103. unsigned int c) const
  104. {
  105. if (c >= this->EmulatorsWithArguments.size()) {
  106. return std::vector<std::string>();
  107. }
  108. return this->EmulatorsWithArguments[c];
  109. }
  110. const char* cmCustomCommandGenerator::GetArgv0Location(unsigned int c) const
  111. {
  112. std::string const& argv0 = this->CommandLines[c][0];
  113. cmGeneratorTarget* target = this->LG->FindGeneratorTargetToUse(argv0);
  114. if (target && target->GetType() == cmStateEnums::EXECUTABLE &&
  115. (target->IsImported() ||
  116. target->GetProperty("CROSSCOMPILING_EMULATOR") ||
  117. !this->LG->GetMakefile()->IsOn("CMAKE_CROSSCOMPILING"))) {
  118. return target->GetLocation(this->Config);
  119. }
  120. return nullptr;
  121. }
  122. bool cmCustomCommandGenerator::HasOnlyEmptyCommandLines() const
  123. {
  124. for (size_t i = 0; i < this->CommandLines.size(); ++i) {
  125. for (size_t j = 0; j < this->CommandLines[i].size(); ++j) {
  126. if (!this->CommandLines[i][j].empty()) {
  127. return false;
  128. }
  129. }
  130. }
  131. return true;
  132. }
  133. std::string cmCustomCommandGenerator::GetCommand(unsigned int c) const
  134. {
  135. std::vector<std::string> emulator = this->GetCrossCompilingEmulator(c);
  136. if (!emulator.empty()) {
  137. return emulator[0];
  138. }
  139. if (const char* location = this->GetArgv0Location(c)) {
  140. return std::string(location);
  141. }
  142. return this->CommandLines[c][0];
  143. }
  144. std::string escapeForShellOldStyle(const std::string& str)
  145. {
  146. std::string result;
  147. #if defined(_WIN32) && !defined(__CYGWIN__)
  148. // if there are spaces
  149. std::string temp = str;
  150. if (temp.find(" ") != std::string::npos &&
  151. temp.find("\"") == std::string::npos) {
  152. result = "\"";
  153. result += str;
  154. result += "\"";
  155. return result;
  156. }
  157. return str;
  158. #else
  159. for (const char* ch = str.c_str(); *ch != '\0'; ++ch) {
  160. if (*ch == ' ') {
  161. result += '\\';
  162. }
  163. result += *ch;
  164. }
  165. return result;
  166. #endif
  167. }
  168. void cmCustomCommandGenerator::AppendArguments(unsigned int c,
  169. std::string& cmd) const
  170. {
  171. unsigned int offset = 1;
  172. std::vector<std::string> emulator = this->GetCrossCompilingEmulator(c);
  173. if (!emulator.empty()) {
  174. for (unsigned j = 1; j < emulator.size(); ++j) {
  175. cmd += " ";
  176. if (this->OldStyle) {
  177. cmd += escapeForShellOldStyle(emulator[j]);
  178. } else {
  179. cmd += this->LG->EscapeForShell(emulator[j], this->MakeVars);
  180. }
  181. }
  182. offset = 0;
  183. }
  184. cmCustomCommandLine const& commandLine = this->CommandLines[c];
  185. for (unsigned int j = offset; j < commandLine.size(); ++j) {
  186. std::string arg;
  187. if (const char* location = j == 0 ? this->GetArgv0Location(c) : nullptr) {
  188. // GetCommand returned the emulator instead of the argv0 location,
  189. // so transform the latter now.
  190. arg = location;
  191. } else {
  192. arg = commandLine[j];
  193. }
  194. cmd += " ";
  195. if (this->OldStyle) {
  196. cmd += escapeForShellOldStyle(arg);
  197. } else {
  198. cmd += this->LG->EscapeForShell(arg, this->MakeVars);
  199. }
  200. }
  201. }
  202. const char* cmCustomCommandGenerator::GetComment() const
  203. {
  204. return this->CC.GetComment();
  205. }
  206. std::string cmCustomCommandGenerator::GetWorkingDirectory() const
  207. {
  208. return this->WorkingDirectory;
  209. }
  210. std::vector<std::string> const& cmCustomCommandGenerator::GetOutputs() const
  211. {
  212. return this->CC.GetOutputs();
  213. }
  214. std::vector<std::string> const& cmCustomCommandGenerator::GetByproducts() const
  215. {
  216. return this->CC.GetByproducts();
  217. }
  218. std::vector<std::string> const& cmCustomCommandGenerator::GetDepends() const
  219. {
  220. return this->Depends;
  221. }