cmCustomCommandGenerator.cxx 7.9 KB

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