cmCustomCommandGenerator.cxx 11 KB

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