cmCustomCommandGenerator.cxx 7.2 KB

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