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(target)) {
  80. this->GetGlobalNinjaGenerator()->AddDependencyToAll(target);
  81. }
  82. delete tg;
  83. }
  84. }
  85. this->WriteCustomCommandBuildStatements();
  86. }
  87. // TODO: Picked up from cmLocalUnixMakefileGenerator3. Refactor it.
  88. std::string cmLocalNinjaGenerator::GetTargetDirectory(
  89. cmGeneratorTarget const* target) const
  90. {
  91. std::string dir = "CMakeFiles/";
  92. dir += target->GetName();
  93. #if defined(__VMS)
  94. dir += "_dir";
  95. #else
  96. dir += ".dir";
  97. #endif
  98. return dir;
  99. }
  100. // Non-virtual public methods.
  101. const cmGlobalNinjaGenerator* cmLocalNinjaGenerator::GetGlobalNinjaGenerator()
  102. const
  103. {
  104. return static_cast<const cmGlobalNinjaGenerator*>(
  105. this->GetGlobalGenerator());
  106. }
  107. cmGlobalNinjaGenerator* cmLocalNinjaGenerator::GetGlobalNinjaGenerator()
  108. {
  109. return static_cast<cmGlobalNinjaGenerator*>(this->GetGlobalGenerator());
  110. }
  111. // Virtual protected methods.
  112. std::string cmLocalNinjaGenerator::ConvertToIncludeReference(
  113. std::string const& path, cmOutputConverter::OutputFormat format,
  114. bool forceFullPaths)
  115. {
  116. if (forceFullPaths) {
  117. return this->ConvertToOutputFormat(cmSystemTools::CollapseFullPath(path),
  118. format);
  119. }
  120. return this->ConvertToOutputFormat(
  121. this->MaybeConvertToRelativePath(this->GetBinaryDirectory(), path),
  122. format);
  123. }
  124. // Private methods.
  125. cmGeneratedFileStream& cmLocalNinjaGenerator::GetBuildFileStream() const
  126. {
  127. return *this->GetGlobalNinjaGenerator()->GetBuildFileStream();
  128. }
  129. cmGeneratedFileStream& cmLocalNinjaGenerator::GetRulesFileStream() const
  130. {
  131. return *this->GetGlobalNinjaGenerator()->GetRulesFileStream();
  132. }
  133. const cmake* cmLocalNinjaGenerator::GetCMakeInstance() const
  134. {
  135. return this->GetGlobalGenerator()->GetCMakeInstance();
  136. }
  137. cmake* cmLocalNinjaGenerator::GetCMakeInstance()
  138. {
  139. return this->GetGlobalGenerator()->GetCMakeInstance();
  140. }
  141. void cmLocalNinjaGenerator::WriteBuildFileTop()
  142. {
  143. // For the build file.
  144. this->WriteProjectHeader(this->GetBuildFileStream());
  145. this->WriteNinjaRequiredVersion(this->GetBuildFileStream());
  146. this->WriteNinjaFilesInclusion(this->GetBuildFileStream());
  147. // For the rule file.
  148. this->WriteProjectHeader(this->GetRulesFileStream());
  149. }
  150. void cmLocalNinjaGenerator::WriteProjectHeader(std::ostream& os)
  151. {
  152. cmGlobalNinjaGenerator::WriteDivider(os);
  153. os << "# Project: " << this->GetProjectName() << std::endl
  154. << "# Configuration: " << this->ConfigName << std::endl;
  155. cmGlobalNinjaGenerator::WriteDivider(os);
  156. }
  157. void cmLocalNinjaGenerator::WriteNinjaRequiredVersion(std::ostream& os)
  158. {
  159. // Default required version
  160. std::string requiredVersion = cmGlobalNinjaGenerator::RequiredNinjaVersion();
  161. // Ninja generator uses the 'console' pool if available (>= 1.5)
  162. if (this->GetGlobalNinjaGenerator()->SupportsConsolePool()) {
  163. requiredVersion =
  164. cmGlobalNinjaGenerator::RequiredNinjaVersionForConsolePool();
  165. }
  166. // The Ninja generator writes rules which require support for restat
  167. // when rebuilding build.ninja manifest (>= 1.8)
  168. if (this->GetGlobalNinjaGenerator()->SupportsManifestRestat() &&
  169. this->GetCMakeInstance()->DoWriteGlobVerifyTarget() &&
  170. !this->GetGlobalNinjaGenerator()->GlobalSettingIsOn(
  171. "CMAKE_SUPPRESS_REGENERATION")) {
  172. requiredVersion =
  173. cmGlobalNinjaGenerator::RequiredNinjaVersionForManifestRestat();
  174. }
  175. cmGlobalNinjaGenerator::WriteComment(
  176. os, "Minimal version of Ninja required by this file");
  177. os << "ninja_required_version = " << requiredVersion << std::endl
  178. << std::endl;
  179. }
  180. void cmLocalNinjaGenerator::WritePools(std::ostream& os)
  181. {
  182. cmGlobalNinjaGenerator::WriteDivider(os);
  183. const char* jobpools =
  184. this->GetCMakeInstance()->GetState()->GetGlobalProperty("JOB_POOLS");
  185. if (!jobpools) {
  186. jobpools = this->GetMakefile()->GetDefinition("CMAKE_JOB_POOLS");
  187. }
  188. if (jobpools) {
  189. cmGlobalNinjaGenerator::WriteComment(
  190. os, "Pools defined by global property JOB_POOLS");
  191. std::vector<std::string> pools;
  192. cmSystemTools::ExpandListArgument(jobpools, pools);
  193. for (std::string const& pool : pools) {
  194. const std::string::size_type eq = pool.find('=');
  195. unsigned int jobs;
  196. if (eq != std::string::npos &&
  197. sscanf(pool.c_str() + eq, "=%u", &jobs) == 1) {
  198. os << "pool " << pool.substr(0, eq) << std::endl;
  199. os << " depth = " << jobs << std::endl;
  200. os << std::endl;
  201. } else {
  202. cmSystemTools::Error("Invalid pool defined by property 'JOB_POOLS': ",
  203. pool.c_str());
  204. }
  205. }
  206. }
  207. }
  208. void cmLocalNinjaGenerator::WriteNinjaFilesInclusion(std::ostream& os)
  209. {
  210. cmGlobalNinjaGenerator::WriteDivider(os);
  211. os << "# Include auxiliary files.\n"
  212. << "\n";
  213. cmGlobalNinjaGenerator* ng = this->GetGlobalNinjaGenerator();
  214. std::string const ninjaRulesFile =
  215. ng->NinjaOutputPath(cmGlobalNinjaGenerator::NINJA_RULES_FILE);
  216. std::string const rulesFilePath = ng->EncodePath(ninjaRulesFile);
  217. cmGlobalNinjaGenerator::WriteInclude(os, rulesFilePath,
  218. "Include rules file.");
  219. os << "\n";
  220. }
  221. void cmLocalNinjaGenerator::WriteProcessedMakefile(std::ostream& os)
  222. {
  223. cmGlobalNinjaGenerator::WriteDivider(os);
  224. os << "# Write statements declared in CMakeLists.txt:" << std::endl
  225. << "# " << this->Makefile->GetDefinition("CMAKE_CURRENT_LIST_FILE")
  226. << std::endl;
  227. if (this->IsRootMakefile()) {
  228. os << "# Which is the root file." << std::endl;
  229. }
  230. cmGlobalNinjaGenerator::WriteDivider(os);
  231. os << std::endl;
  232. }
  233. void cmLocalNinjaGenerator::AppendTargetOutputs(cmGeneratorTarget* target,
  234. cmNinjaDeps& outputs)
  235. {
  236. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(target, outputs);
  237. }
  238. void cmLocalNinjaGenerator::AppendTargetDepends(cmGeneratorTarget* target,
  239. cmNinjaDeps& outputs,
  240. cmNinjaTargetDepends depends)
  241. {
  242. this->GetGlobalNinjaGenerator()->AppendTargetDepends(target, outputs,
  243. depends);
  244. }
  245. void cmLocalNinjaGenerator::AppendCustomCommandDeps(
  246. cmCustomCommandGenerator const& ccg, cmNinjaDeps& ninjaDeps)
  247. {
  248. const std::vector<std::string>& deps = ccg.GetDepends();
  249. for (std::string const& i : deps) {
  250. std::string dep;
  251. if (this->GetRealDependency(i, this->GetConfigName(), dep)) {
  252. ninjaDeps.push_back(
  253. this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(dep));
  254. }
  255. }
  256. }
  257. std::string cmLocalNinjaGenerator::WriteCommandScript(
  258. std::vector<std::string> const& cmdLines, std::string const& customStep,
  259. cmGeneratorTarget const* target) const
  260. {
  261. std::string scriptPath;
  262. if (target) {
  263. scriptPath = target->GetSupportDirectory();
  264. } else {
  265. scriptPath = this->GetCurrentBinaryDirectory();
  266. scriptPath += "/CMakeFiles";
  267. }
  268. cmSystemTools::MakeDirectory(scriptPath);
  269. scriptPath += '/';
  270. scriptPath += customStep;
  271. #ifdef _WIN32
  272. scriptPath += ".bat";
  273. #else
  274. scriptPath += ".sh";
  275. #endif
  276. cmsys::ofstream script(scriptPath.c_str());
  277. #ifdef _WIN32
  278. script << "@echo off\n";
  279. int line = 1;
  280. #else
  281. script << "set -e\n\n";
  282. #endif
  283. for (auto const& i : cmdLines) {
  284. std::string cmd = i;
  285. // The command line was built assuming it would be written to
  286. // the build.ninja file, so it uses '$$' for '$'. Remove this
  287. // for the raw shell script.
  288. cmSystemTools::ReplaceString(cmd, "$$", "$");
  289. #ifdef _WIN32
  290. script << cmd << " || (set FAIL_LINE=" << ++line << "& goto :ABORT)"
  291. << '\n';
  292. #else
  293. script << cmd << '\n';
  294. #endif
  295. }
  296. #ifdef _WIN32
  297. script << "goto :EOF\n\n"
  298. ":ABORT\n"
  299. "set ERROR_CODE=%ERRORLEVEL%\n"
  300. "echo Batch file failed at line %FAIL_LINE% "
  301. "with errorcode %ERRORLEVEL%\n"
  302. "exit /b %ERROR_CODE%";
  303. #endif
  304. return scriptPath;
  305. }
  306. std::string cmLocalNinjaGenerator::BuildCommandLine(
  307. std::vector<std::string> const& cmdLines, std::string const& customStep,
  308. cmGeneratorTarget const* target) const
  309. {
  310. // If we have no commands but we need to build a command anyway, use noop.
  311. // This happens when building a POST_BUILD value for link targets that
  312. // don't use POST_BUILD.
  313. if (cmdLines.empty()) {
  314. return cmGlobalNinjaGenerator::SHELL_NOOP;
  315. }
  316. // If this is a custom step then we will have no '$VAR' ninja placeholders.
  317. // This means we can deal with long command sequences by writing to a script.
  318. // Do this if the command lines are on the scale of the OS limit.
  319. if (!customStep.empty()) {
  320. size_t cmdLinesTotal = 0;
  321. for (std::string const& cmd : cmdLines) {
  322. cmdLinesTotal += cmd.length() + 6;
  323. }
  324. if (cmdLinesTotal > cmSystemTools::CalculateCommandLineLengthLimit() / 2) {
  325. std::string const scriptPath =
  326. this->WriteCommandScript(cmdLines, customStep, target);
  327. std::string cmd
  328. #ifndef _WIN32
  329. = "/bin/sh "
  330. #endif
  331. ;
  332. cmd += this->ConvertToOutputFormat(
  333. this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(scriptPath),
  334. cmOutputConverter::SHELL);
  335. // Add an unused argument based on script content so that Ninja
  336. // knows when the command lines change.
  337. cmd += " ";
  338. cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
  339. cmd += hash.HashFile(scriptPath).substr(0, 16);
  340. return cmd;
  341. }
  342. }
  343. std::ostringstream cmd;
  344. for (std::vector<std::string>::const_iterator li = cmdLines.begin();
  345. li != cmdLines.end(); ++li)
  346. #ifdef _WIN32
  347. {
  348. if (li != cmdLines.begin()) {
  349. cmd << " && ";
  350. } else if (cmdLines.size() > 1) {
  351. cmd << "cmd.exe /C \"";
  352. }
  353. // Put current cmdLine in brackets if it contains "||" because it has
  354. // higher precedence than "&&" in cmd.exe
  355. if (li->find("||") != std::string::npos) {
  356. cmd << "( " << *li << " )";
  357. } else {
  358. cmd << *li;
  359. }
  360. }
  361. if (cmdLines.size() > 1) {
  362. cmd << "\"";
  363. }
  364. #else
  365. {
  366. if (li != cmdLines.begin()) {
  367. cmd << " && ";
  368. }
  369. cmd << *li;
  370. }
  371. #endif
  372. return cmd.str();
  373. }
  374. void cmLocalNinjaGenerator::AppendCustomCommandLines(
  375. cmCustomCommandGenerator const& ccg, std::vector<std::string>& cmdLines)
  376. {
  377. if (ccg.GetNumberOfCommands() > 0) {
  378. std::string wd = ccg.GetWorkingDirectory();
  379. if (wd.empty()) {
  380. wd = this->GetCurrentBinaryDirectory();
  381. }
  382. std::ostringstream cdCmd;
  383. #ifdef _WIN32
  384. std::string cdStr = "cd /D ";
  385. #else
  386. std::string cdStr = "cd ";
  387. #endif
  388. cdCmd << cdStr
  389. << this->ConvertToOutputFormat(wd, cmOutputConverter::SHELL);
  390. cmdLines.push_back(cdCmd.str());
  391. }
  392. std::string launcher = this->MakeCustomLauncher(ccg);
  393. for (unsigned i = 0; i != ccg.GetNumberOfCommands(); ++i) {
  394. cmdLines.push_back(launcher +
  395. this->ConvertToOutputFormat(ccg.GetCommand(i),
  396. cmOutputConverter::SHELL));
  397. std::string& cmd = cmdLines.back();
  398. ccg.AppendArguments(i, cmd);
  399. }
  400. }
  401. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatement(
  402. cmCustomCommand const* cc, const cmNinjaDeps& orderOnlyDeps)
  403. {
  404. if (this->GetGlobalNinjaGenerator()->SeenCustomCommand(cc)) {
  405. return;
  406. }
  407. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(), this);
  408. const std::vector<std::string>& outputs = ccg.GetOutputs();
  409. const std::vector<std::string>& byproducts = ccg.GetByproducts();
  410. cmNinjaDeps ninjaOutputs(outputs.size() + byproducts.size()), ninjaDeps;
  411. bool symbolic = false;
  412. for (std::string const& output : outputs) {
  413. if (cmSourceFile* sf = this->Makefile->GetSource(output)) {
  414. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  415. if (symbolic) {
  416. break;
  417. }
  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. }