cmCustomCommandGenerator.cxx 9.8 KB

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