cmLocalNinjaGenerator.cxx 19 KB

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