cmCustomCommandGenerator.cxx 7.5 KB

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