cmCustomCommandGenerator.cxx 7.6 KB

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