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