cmCustomCommandGenerator.cxx 11 KB

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