cmLocalNinjaGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 "cmLocalNinjaGenerator.h"
  4. #include <algorithm>
  5. #include <assert.h>
  6. #include <iterator>
  7. #include <memory> // IWYU pragma: keep
  8. #include <sstream>
  9. #include <stdio.h>
  10. #include <utility>
  11. #include "cmCryptoHash.h"
  12. #include "cmCustomCommand.h"
  13. #include "cmCustomCommandGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGeneratorTarget.h"
  16. #include "cmGlobalGenerator.h"
  17. #include "cmGlobalNinjaGenerator.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmMakefile.h"
  20. #include "cmNinjaTargetGenerator.h"
  21. #include "cmRulePlaceholderExpander.h"
  22. #include "cmSourceFile.h"
  23. #include "cmState.h"
  24. #include "cmStateTypes.h"
  25. #include "cmSystemTools.h"
  26. #include "cmake.h"
  27. #include "cmsys/FStream.hxx"
  28. cmLocalNinjaGenerator::cmLocalNinjaGenerator(cmGlobalGenerator* gg,
  29. cmMakefile* mf)
  30. : cmLocalCommonGenerator(gg, mf, mf->GetState()->GetBinaryDirectory())
  31. , HomeRelativeOutputPath("")
  32. {
  33. }
  34. // Virtual public methods.
  35. cmRulePlaceholderExpander*
  36. cmLocalNinjaGenerator::CreateRulePlaceholderExpander() const
  37. {
  38. cmRulePlaceholderExpander* ret =
  39. this->cmLocalGenerator::CreateRulePlaceholderExpander();
  40. ret->SetTargetImpLib("$TARGET_IMPLIB");
  41. return ret;
  42. }
  43. cmLocalNinjaGenerator::~cmLocalNinjaGenerator()
  44. {
  45. }
  46. void cmLocalNinjaGenerator::Generate()
  47. {
  48. // Compute the path to use when referencing the current output
  49. // directory from the top output directory.
  50. this->HomeRelativeOutputPath = this->ConvertToRelativePath(
  51. this->GetBinaryDirectory(), this->GetCurrentBinaryDirectory());
  52. if (this->HomeRelativeOutputPath == ".") {
  53. this->HomeRelativeOutputPath.clear();
  54. }
  55. this->WriteProcessedMakefile(this->GetBuildFileStream());
  56. #ifdef NINJA_GEN_VERBOSE_FILES
  57. this->WriteProcessedMakefile(this->GetRulesFileStream());
  58. #endif
  59. // We do that only once for the top CMakeLists.txt file.
  60. if (this->IsRootMakefile()) {
  61. this->WriteBuildFileTop();
  62. this->WritePools(this->GetRulesFileStream());
  63. const std::string showIncludesPrefix =
  64. this->GetMakefile()->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  65. if (!showIncludesPrefix.empty()) {
  66. cmGlobalNinjaGenerator::WriteComment(this->GetRulesFileStream(),
  67. "localized /showIncludes string");
  68. this->GetRulesFileStream()
  69. << "msvc_deps_prefix = " << showIncludesPrefix << "\n\n";
  70. }
  71. }
  72. const std::vector<cmGeneratorTarget*>& targets = this->GetGeneratorTargets();
  73. for (cmGeneratorTarget* target : targets) {
  74. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  75. continue;
  76. }
  77. cmNinjaTargetGenerator* tg = cmNinjaTargetGenerator::New(target);
  78. if (tg) {
  79. tg->Generate();
  80. // Add the target to "all" if required.
  81. if (!this->GetGlobalNinjaGenerator()->IsExcluded(
  82. this->GetGlobalNinjaGenerator()->GetLocalGenerators()[0],
  83. target)) {
  84. this->GetGlobalNinjaGenerator()->AddDependencyToAll(target);
  85. }
  86. delete tg;
  87. }
  88. }
  89. this->WriteCustomCommandBuildStatements();
  90. }
  91. // TODO: Picked up from cmLocalUnixMakefileGenerator3. Refactor it.
  92. std::string cmLocalNinjaGenerator::GetTargetDirectory(
  93. cmGeneratorTarget const* target) const
  94. {
  95. std::string dir = "CMakeFiles/";
  96. dir += target->GetName();
  97. #if defined(__VMS)
  98. dir += "_dir";
  99. #else
  100. dir += ".dir";
  101. #endif
  102. return dir;
  103. }
  104. // Non-virtual public methods.
  105. const cmGlobalNinjaGenerator* cmLocalNinjaGenerator::GetGlobalNinjaGenerator()
  106. const
  107. {
  108. return static_cast<const cmGlobalNinjaGenerator*>(
  109. this->GetGlobalGenerator());
  110. }
  111. cmGlobalNinjaGenerator* cmLocalNinjaGenerator::GetGlobalNinjaGenerator()
  112. {
  113. return static_cast<cmGlobalNinjaGenerator*>(this->GetGlobalGenerator());
  114. }
  115. // Virtual protected methods.
  116. std::string cmLocalNinjaGenerator::ConvertToIncludeReference(
  117. std::string const& path, cmOutputConverter::OutputFormat format,
  118. bool forceFullPaths)
  119. {
  120. if (forceFullPaths) {
  121. return this->ConvertToOutputFormat(cmSystemTools::CollapseFullPath(path),
  122. format);
  123. }
  124. return this->ConvertToOutputFormat(
  125. this->ConvertToRelativePath(this->GetBinaryDirectory(), path), format);
  126. }
  127. // Private methods.
  128. cmGeneratedFileStream& cmLocalNinjaGenerator::GetBuildFileStream() const
  129. {
  130. return *this->GetGlobalNinjaGenerator()->GetBuildFileStream();
  131. }
  132. cmGeneratedFileStream& cmLocalNinjaGenerator::GetRulesFileStream() const
  133. {
  134. return *this->GetGlobalNinjaGenerator()->GetRulesFileStream();
  135. }
  136. const cmake* cmLocalNinjaGenerator::GetCMakeInstance() const
  137. {
  138. return this->GetGlobalGenerator()->GetCMakeInstance();
  139. }
  140. cmake* cmLocalNinjaGenerator::GetCMakeInstance()
  141. {
  142. return this->GetGlobalGenerator()->GetCMakeInstance();
  143. }
  144. void cmLocalNinjaGenerator::WriteBuildFileTop()
  145. {
  146. // For the build file.
  147. this->WriteProjectHeader(this->GetBuildFileStream());
  148. this->WriteNinjaRequiredVersion(this->GetBuildFileStream());
  149. this->WriteNinjaFilesInclusion(this->GetBuildFileStream());
  150. // For the rule file.
  151. this->WriteProjectHeader(this->GetRulesFileStream());
  152. }
  153. void cmLocalNinjaGenerator::WriteProjectHeader(std::ostream& os)
  154. {
  155. cmGlobalNinjaGenerator::WriteDivider(os);
  156. os << "# Project: " << this->GetProjectName() << std::endl
  157. << "# Configuration: " << this->ConfigName << std::endl;
  158. cmGlobalNinjaGenerator::WriteDivider(os);
  159. }
  160. void cmLocalNinjaGenerator::WriteNinjaRequiredVersion(std::ostream& os)
  161. {
  162. // Default required version
  163. std::string requiredVersion = cmGlobalNinjaGenerator::RequiredNinjaVersion();
  164. // Ninja generator uses the 'console' pool if available (>= 1.5)
  165. if (this->GetGlobalNinjaGenerator()->SupportsConsolePool()) {
  166. requiredVersion =
  167. cmGlobalNinjaGenerator::RequiredNinjaVersionForConsolePool();
  168. }
  169. // The Ninja generator writes rules which require support for restat
  170. // when rebuilding build.ninja manifest (>= 1.8)
  171. if (this->GetGlobalNinjaGenerator()->SupportsManifestRestat() &&
  172. this->GetCMakeInstance()->DoWriteGlobVerifyTarget() &&
  173. !this->GetGlobalNinjaGenerator()->GlobalSettingIsOn(
  174. "CMAKE_SUPPRESS_REGENERATION")) {
  175. requiredVersion =
  176. cmGlobalNinjaGenerator::RequiredNinjaVersionForManifestRestat();
  177. }
  178. cmGlobalNinjaGenerator::WriteComment(
  179. os, "Minimal version of Ninja required by this file");
  180. os << "ninja_required_version = " << requiredVersion << std::endl
  181. << std::endl;
  182. }
  183. void cmLocalNinjaGenerator::WritePools(std::ostream& os)
  184. {
  185. cmGlobalNinjaGenerator::WriteDivider(os);
  186. const char* jobpools =
  187. this->GetCMakeInstance()->GetState()->GetGlobalProperty("JOB_POOLS");
  188. if (!jobpools) {
  189. jobpools = this->GetMakefile()->GetDefinition("CMAKE_JOB_POOLS");
  190. }
  191. if (jobpools) {
  192. cmGlobalNinjaGenerator::WriteComment(
  193. os, "Pools defined by global property JOB_POOLS");
  194. std::vector<std::string> pools;
  195. cmSystemTools::ExpandListArgument(jobpools, pools);
  196. for (std::string const& pool : pools) {
  197. const std::string::size_type eq = pool.find('=');
  198. unsigned int jobs;
  199. if (eq != std::string::npos &&
  200. sscanf(pool.c_str() + eq, "=%u", &jobs) == 1) {
  201. os << "pool " << pool.substr(0, eq) << std::endl;
  202. os << " depth = " << jobs << std::endl;
  203. os << std::endl;
  204. } else {
  205. cmSystemTools::Error("Invalid pool defined by property 'JOB_POOLS': ",
  206. pool.c_str());
  207. }
  208. }
  209. }
  210. }
  211. void cmLocalNinjaGenerator::WriteNinjaFilesInclusion(std::ostream& os)
  212. {
  213. cmGlobalNinjaGenerator::WriteDivider(os);
  214. os << "# Include auxiliary files.\n"
  215. << "\n";
  216. cmGlobalNinjaGenerator* ng = this->GetGlobalNinjaGenerator();
  217. std::string const ninjaRulesFile =
  218. ng->NinjaOutputPath(cmGlobalNinjaGenerator::NINJA_RULES_FILE);
  219. std::string const rulesFilePath = ng->EncodePath(ninjaRulesFile);
  220. cmGlobalNinjaGenerator::WriteInclude(os, rulesFilePath,
  221. "Include rules file.");
  222. os << "\n";
  223. }
  224. void cmLocalNinjaGenerator::WriteProcessedMakefile(std::ostream& os)
  225. {
  226. cmGlobalNinjaGenerator::WriteDivider(os);
  227. os << "# Write statements declared in CMakeLists.txt:" << std::endl
  228. << "# " << this->Makefile->GetDefinition("CMAKE_CURRENT_LIST_FILE")
  229. << std::endl;
  230. if (this->IsRootMakefile()) {
  231. os << "# Which is the root file." << std::endl;
  232. }
  233. cmGlobalNinjaGenerator::WriteDivider(os);
  234. os << std::endl;
  235. }
  236. void cmLocalNinjaGenerator::AppendTargetOutputs(cmGeneratorTarget* target,
  237. cmNinjaDeps& outputs)
  238. {
  239. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(target, outputs);
  240. }
  241. void cmLocalNinjaGenerator::AppendTargetDepends(cmGeneratorTarget* target,
  242. cmNinjaDeps& outputs,
  243. cmNinjaTargetDepends depends)
  244. {
  245. this->GetGlobalNinjaGenerator()->AppendTargetDepends(target, outputs,
  246. depends);
  247. }
  248. void cmLocalNinjaGenerator::AppendCustomCommandDeps(
  249. cmCustomCommandGenerator const& ccg, cmNinjaDeps& ninjaDeps)
  250. {
  251. const std::vector<std::string>& deps = ccg.GetDepends();
  252. for (std::string const& i : deps) {
  253. std::string dep;
  254. if (this->GetRealDependency(i, this->GetConfigName(), dep)) {
  255. ninjaDeps.push_back(
  256. this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(dep));
  257. }
  258. }
  259. }
  260. std::string cmLocalNinjaGenerator::WriteCommandScript(
  261. std::vector<std::string> const& cmdLines, std::string const& customStep,
  262. cmGeneratorTarget const* target) const
  263. {
  264. std::string scriptPath;
  265. if (target) {
  266. scriptPath = target->GetSupportDirectory();
  267. } else {
  268. scriptPath = this->GetCurrentBinaryDirectory();
  269. scriptPath += "/CMakeFiles";
  270. }
  271. cmSystemTools::MakeDirectory(scriptPath);
  272. scriptPath += '/';
  273. scriptPath += customStep;
  274. #ifdef _WIN32
  275. scriptPath += ".bat";
  276. #else
  277. scriptPath += ".sh";
  278. #endif
  279. cmsys::ofstream script(scriptPath.c_str());
  280. #ifdef _WIN32
  281. script << "@echo off\n";
  282. int line = 1;
  283. #else
  284. script << "set -e\n\n";
  285. #endif
  286. for (auto const& i : cmdLines) {
  287. std::string cmd = i;
  288. // The command line was built assuming it would be written to
  289. // the build.ninja file, so it uses '$$' for '$'. Remove this
  290. // for the raw shell script.
  291. cmSystemTools::ReplaceString(cmd, "$$", "$");
  292. #ifdef _WIN32
  293. script << cmd << " || (set FAIL_LINE=" << ++line << "& goto :ABORT)"
  294. << '\n';
  295. #else
  296. script << cmd << '\n';
  297. #endif
  298. }
  299. #ifdef _WIN32
  300. script << "goto :EOF\n\n"
  301. ":ABORT\n"
  302. "set ERROR_CODE=%ERRORLEVEL%\n"
  303. "echo Batch file failed at line %FAIL_LINE% "
  304. "with errorcode %ERRORLEVEL%\n"
  305. "exit /b %ERROR_CODE%";
  306. #endif
  307. return scriptPath;
  308. }
  309. std::string cmLocalNinjaGenerator::BuildCommandLine(
  310. std::vector<std::string> const& cmdLines, std::string const& customStep,
  311. cmGeneratorTarget const* target) const
  312. {
  313. // If we have no commands but we need to build a command anyway, use noop.
  314. // This happens when building a POST_BUILD value for link targets that
  315. // don't use POST_BUILD.
  316. if (cmdLines.empty()) {
  317. return cmGlobalNinjaGenerator::SHELL_NOOP;
  318. }
  319. // If this is a custom step then we will have no '$VAR' ninja placeholders.
  320. // This means we can deal with long command sequences by writing to a script.
  321. // Do this if the command lines are on the scale of the OS limit.
  322. if (!customStep.empty()) {
  323. size_t cmdLinesTotal = 0;
  324. for (std::string const& cmd : cmdLines) {
  325. cmdLinesTotal += cmd.length() + 6;
  326. }
  327. if (cmdLinesTotal > cmSystemTools::CalculateCommandLineLengthLimit() / 2) {
  328. std::string const scriptPath =
  329. this->WriteCommandScript(cmdLines, customStep, target);
  330. std::string cmd
  331. #ifndef _WIN32
  332. = "/bin/sh "
  333. #endif
  334. ;
  335. cmd += this->ConvertToOutputFormat(
  336. this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(scriptPath),
  337. cmOutputConverter::SHELL);
  338. // Add an unused argument based on script content so that Ninja
  339. // knows when the command lines change.
  340. cmd += " ";
  341. cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
  342. cmd += hash.HashFile(scriptPath).substr(0, 16);
  343. return cmd;
  344. }
  345. }
  346. std::ostringstream cmd;
  347. for (std::vector<std::string>::const_iterator li = cmdLines.begin();
  348. li != cmdLines.end(); ++li)
  349. #ifdef _WIN32
  350. {
  351. if (li != cmdLines.begin()) {
  352. cmd << " && ";
  353. } else if (cmdLines.size() > 1) {
  354. cmd << "cmd.exe /C \"";
  355. }
  356. // Put current cmdLine in brackets if it contains "||" because it has
  357. // higher precedence than "&&" in cmd.exe
  358. if (li->find("||") != std::string::npos) {
  359. cmd << "( " << *li << " )";
  360. } else {
  361. cmd << *li;
  362. }
  363. }
  364. if (cmdLines.size() > 1) {
  365. cmd << "\"";
  366. }
  367. #else
  368. {
  369. if (li != cmdLines.begin()) {
  370. cmd << " && ";
  371. }
  372. cmd << *li;
  373. }
  374. #endif
  375. return cmd.str();
  376. }
  377. void cmLocalNinjaGenerator::AppendCustomCommandLines(
  378. cmCustomCommandGenerator const& ccg, std::vector<std::string>& cmdLines)
  379. {
  380. if (ccg.GetNumberOfCommands() > 0) {
  381. std::string wd = ccg.GetWorkingDirectory();
  382. if (wd.empty()) {
  383. wd = this->GetCurrentBinaryDirectory();
  384. }
  385. std::ostringstream cdCmd;
  386. #ifdef _WIN32
  387. std::string cdStr = "cd /D ";
  388. #else
  389. std::string cdStr = "cd ";
  390. #endif
  391. cdCmd << cdStr
  392. << this->ConvertToOutputFormat(wd, cmOutputConverter::SHELL);
  393. cmdLines.push_back(cdCmd.str());
  394. }
  395. std::string launcher = this->MakeCustomLauncher(ccg);
  396. for (unsigned i = 0; i != ccg.GetNumberOfCommands(); ++i) {
  397. cmdLines.push_back(launcher +
  398. this->ConvertToOutputFormat(ccg.GetCommand(i),
  399. cmOutputConverter::SHELL));
  400. std::string& cmd = cmdLines.back();
  401. ccg.AppendArguments(i, cmd);
  402. }
  403. }
  404. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatement(
  405. cmCustomCommand const* cc, const cmNinjaDeps& orderOnlyDeps)
  406. {
  407. if (this->GetGlobalNinjaGenerator()->SeenCustomCommand(cc)) {
  408. return;
  409. }
  410. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(), this);
  411. const std::vector<std::string>& outputs = ccg.GetOutputs();
  412. const std::vector<std::string>& byproducts = ccg.GetByproducts();
  413. cmNinjaDeps ninjaOutputs(outputs.size() + byproducts.size()), ninjaDeps;
  414. bool symbolic = false;
  415. for (std::vector<std::string>::const_iterator o = outputs.begin();
  416. !symbolic && o != outputs.end(); ++o) {
  417. if (cmSourceFile* sf = this->Makefile->GetSource(*o)) {
  418. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  419. }
  420. }
  421. #if 0
  422. # error TODO: Once CC in an ExternalProject target must provide the \
  423. file of each imported target that has an add_dependencies pointing \
  424. at us. How to know which ExternalProject step actually provides it?
  425. #endif
  426. std::transform(outputs.begin(), outputs.end(), ninjaOutputs.begin(),
  427. this->GetGlobalNinjaGenerator()->MapToNinjaPath());
  428. std::transform(byproducts.begin(), byproducts.end(),
  429. ninjaOutputs.begin() + outputs.size(),
  430. this->GetGlobalNinjaGenerator()->MapToNinjaPath());
  431. this->AppendCustomCommandDeps(ccg, ninjaDeps);
  432. for (std::string const& ninjaOutput : ninjaOutputs) {
  433. this->GetGlobalNinjaGenerator()->SeenCustomCommandOutput(ninjaOutput);
  434. }
  435. std::vector<std::string> cmdLines;
  436. this->AppendCustomCommandLines(ccg, cmdLines);
  437. if (cmdLines.empty()) {
  438. this->GetGlobalNinjaGenerator()->WritePhonyBuild(
  439. this->GetBuildFileStream(),
  440. "Phony custom command for " + ninjaOutputs[0], ninjaOutputs, ninjaDeps,
  441. cmNinjaDeps(), orderOnlyDeps, cmNinjaVars());
  442. } else {
  443. std::string customStep = cmSystemTools::GetFilenameName(ninjaOutputs[0]);
  444. // Hash full path to make unique.
  445. customStep += '-';
  446. cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
  447. customStep += hash.HashString(ninjaOutputs[0]).substr(0, 7);
  448. this->GetGlobalNinjaGenerator()->WriteCustomCommandBuild(
  449. this->BuildCommandLine(cmdLines, customStep),
  450. this->ConstructComment(ccg), "Custom command for " + ninjaOutputs[0],
  451. cc->GetDepfile(), cc->GetUsesTerminal(),
  452. /*restat*/ !symbolic || !byproducts.empty(), ninjaOutputs, ninjaDeps,
  453. orderOnlyDeps);
  454. }
  455. }
  456. void cmLocalNinjaGenerator::AddCustomCommandTarget(cmCustomCommand const* cc,
  457. cmGeneratorTarget* target)
  458. {
  459. CustomCommandTargetMap::value_type v(cc, std::set<cmGeneratorTarget*>());
  460. std::pair<CustomCommandTargetMap::iterator, bool> ins =
  461. this->CustomCommandTargets.insert(v);
  462. if (ins.second) {
  463. this->CustomCommands.push_back(cc);
  464. }
  465. ins.first->second.insert(target);
  466. }
  467. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatements()
  468. {
  469. for (cmCustomCommand const* customCommand : this->CustomCommands) {
  470. CustomCommandTargetMap::iterator i =
  471. this->CustomCommandTargets.find(customCommand);
  472. assert(i != this->CustomCommandTargets.end());
  473. // A custom command may appear on multiple targets. However, some build
  474. // systems exist where the target dependencies on some of the targets are
  475. // overspecified, leading to a dependency cycle. If we assume all target
  476. // dependencies are a superset of the true target dependencies for this
  477. // custom command, we can take the set intersection of all target
  478. // dependencies to obtain a correct dependency list.
  479. //
  480. // FIXME: This won't work in certain obscure scenarios involving indirect
  481. // dependencies.
  482. std::set<cmGeneratorTarget*>::iterator j = i->second.begin();
  483. assert(j != i->second.end());
  484. std::vector<std::string> ccTargetDeps;
  485. this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure(*j,
  486. ccTargetDeps);
  487. std::sort(ccTargetDeps.begin(), ccTargetDeps.end());
  488. ++j;
  489. for (; j != i->second.end(); ++j) {
  490. std::vector<std::string> jDeps, depsIntersection;
  491. this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure(*j, jDeps);
  492. std::sort(jDeps.begin(), jDeps.end());
  493. std::set_intersection(ccTargetDeps.begin(), ccTargetDeps.end(),
  494. jDeps.begin(), jDeps.end(),
  495. std::back_inserter(depsIntersection));
  496. ccTargetDeps = depsIntersection;
  497. }
  498. this->WriteCustomCommandBuildStatement(i->first, ccTargetDeps);
  499. }
  500. }
  501. std::string cmLocalNinjaGenerator::MakeCustomLauncher(
  502. cmCustomCommandGenerator const& ccg)
  503. {
  504. const char* property_value =
  505. this->Makefile->GetProperty("RULE_LAUNCH_CUSTOM");
  506. if (!property_value || !*property_value) {
  507. return std::string();
  508. }
  509. // Expand rule variables referenced in the given launcher command.
  510. cmRulePlaceholderExpander::RuleVariables vars;
  511. std::string output;
  512. const std::vector<std::string>& outputs = ccg.GetOutputs();
  513. if (!outputs.empty()) {
  514. output = outputs[0];
  515. if (ccg.GetWorkingDirectory().empty()) {
  516. output =
  517. this->ConvertToRelativePath(this->GetCurrentBinaryDirectory(), output);
  518. }
  519. output = this->ConvertToOutputFormat(output, cmOutputConverter::SHELL);
  520. }
  521. vars.Output = output.c_str();
  522. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  523. this->CreateRulePlaceholderExpander());
  524. std::string launcher = property_value;
  525. rulePlaceholderExpander->ExpandRuleVariables(this, launcher, vars);
  526. if (!launcher.empty()) {
  527. launcher += " ";
  528. }
  529. return launcher;
  530. }