cmLocalNinjaGenerator.cxx 19 KB

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