1
0

cmCustomCommandGenerator.cxx 11 KB

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