cmCustomCommandGenerator.cxx 11 KB

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