cmLocalNinjaGenerator.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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>
  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 "cmGeneratorExpression.h"
  16. #include "cmGeneratorTarget.h"
  17. #include "cmGlobalGenerator.h"
  18. #include "cmGlobalNinjaGenerator.h"
  19. #include "cmLocalGenerator.h"
  20. #include "cmMakefile.h"
  21. #include "cmNinjaTargetGenerator.h"
  22. #include "cmRulePlaceholderExpander.h"
  23. #include "cmSourceFile.h"
  24. #include "cmState.h"
  25. #include "cmStateTypes.h"
  26. #include "cmStringAlgorithms.h"
  27. #include "cmSystemTools.h"
  28. #include "cmake.h"
  29. #include "cmsys/FStream.hxx"
  30. cmLocalNinjaGenerator::cmLocalNinjaGenerator(cmGlobalGenerator* gg,
  31. cmMakefile* mf)
  32. : cmLocalCommonGenerator(gg, mf, mf->GetState()->GetBinaryDirectory())
  33. , HomeRelativeOutputPath("")
  34. {
  35. }
  36. // Virtual public methods.
  37. cmRulePlaceholderExpander*
  38. cmLocalNinjaGenerator::CreateRulePlaceholderExpander() const
  39. {
  40. cmRulePlaceholderExpander* ret =
  41. this->cmLocalGenerator::CreateRulePlaceholderExpander();
  42. ret->SetTargetImpLib("$TARGET_IMPLIB");
  43. return ret;
  44. }
  45. cmLocalNinjaGenerator::~cmLocalNinjaGenerator() = default;
  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->MaybeConvertToRelativePath(
  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. for (cmGeneratorTarget* target : this->GetGeneratorTargets()) {
  73. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  74. continue;
  75. }
  76. auto tg = cmNinjaTargetGenerator::New(target);
  77. if (tg) {
  78. tg->Generate();
  79. // Add the target to "all" if required.
  80. if (!this->GetGlobalNinjaGenerator()->IsExcluded(target)) {
  81. this->GetGlobalNinjaGenerator()->AddDependencyToAll(target);
  82. }
  83. }
  84. }
  85. this->WriteCustomCommandBuildStatements();
  86. this->AdditionalCleanFiles();
  87. }
  88. // TODO: Picked up from cmLocalUnixMakefileGenerator3. Refactor it.
  89. std::string cmLocalNinjaGenerator::GetTargetDirectory(
  90. cmGeneratorTarget const* target) const
  91. {
  92. std::string dir = cmStrCat("CMakeFiles/", 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 = cmExpandedList(jobpools);
  192. for (std::string const& pool : pools) {
  193. const std::string::size_type eq = pool.find('=');
  194. unsigned int jobs;
  195. if (eq != std::string::npos &&
  196. sscanf(pool.c_str() + eq, "=%u", &jobs) == 1) {
  197. os << "pool " << pool.substr(0, eq) << std::endl;
  198. os << " depth = " << jobs << std::endl;
  199. os << std::endl;
  200. } else {
  201. cmSystemTools::Error("Invalid pool defined by property 'JOB_POOLS': " +
  202. pool);
  203. }
  204. }
  205. }
  206. }
  207. void cmLocalNinjaGenerator::WriteNinjaFilesInclusion(std::ostream& os)
  208. {
  209. cmGlobalNinjaGenerator::WriteDivider(os);
  210. os << "# Include auxiliary files.\n"
  211. << "\n";
  212. cmGlobalNinjaGenerator* ng = this->GetGlobalNinjaGenerator();
  213. std::string const ninjaRulesFile =
  214. ng->NinjaOutputPath(cmGlobalNinjaGenerator::NINJA_RULES_FILE);
  215. std::string const rulesFilePath = ng->EncodePath(ninjaRulesFile);
  216. cmGlobalNinjaGenerator::WriteInclude(os, rulesFilePath,
  217. "Include rules file.");
  218. os << "\n";
  219. }
  220. void cmLocalNinjaGenerator::WriteProcessedMakefile(std::ostream& os)
  221. {
  222. cmGlobalNinjaGenerator::WriteDivider(os);
  223. os << "# Write statements declared in CMakeLists.txt:" << std::endl
  224. << "# " << this->Makefile->GetDefinition("CMAKE_CURRENT_LIST_FILE")
  225. << std::endl;
  226. if (this->IsRootMakefile()) {
  227. os << "# Which is the root file." << std::endl;
  228. }
  229. cmGlobalNinjaGenerator::WriteDivider(os);
  230. os << std::endl;
  231. }
  232. void cmLocalNinjaGenerator::AppendTargetOutputs(cmGeneratorTarget* target,
  233. cmNinjaDeps& outputs)
  234. {
  235. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(target, outputs);
  236. }
  237. void cmLocalNinjaGenerator::AppendTargetDepends(cmGeneratorTarget* target,
  238. cmNinjaDeps& outputs,
  239. cmNinjaTargetDepends depends)
  240. {
  241. this->GetGlobalNinjaGenerator()->AppendTargetDepends(target, outputs,
  242. depends);
  243. }
  244. void cmLocalNinjaGenerator::AppendCustomCommandDeps(
  245. cmCustomCommandGenerator const& ccg, cmNinjaDeps& ninjaDeps)
  246. {
  247. for (std::string const& i : ccg.GetDepends()) {
  248. std::string dep;
  249. if (this->GetRealDependency(i, this->GetConfigName(), dep)) {
  250. ninjaDeps.push_back(
  251. this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(dep));
  252. }
  253. }
  254. }
  255. std::string cmLocalNinjaGenerator::WriteCommandScript(
  256. std::vector<std::string> const& cmdLines, std::string const& customStep,
  257. cmGeneratorTarget const* target) const
  258. {
  259. std::string scriptPath;
  260. if (target) {
  261. scriptPath = target->GetSupportDirectory();
  262. } else {
  263. scriptPath = cmStrCat(this->GetCurrentBinaryDirectory(), "/CMakeFiles");
  264. }
  265. cmSystemTools::MakeDirectory(scriptPath);
  266. scriptPath += '/';
  267. scriptPath += customStep;
  268. #ifdef _WIN32
  269. scriptPath += ".bat";
  270. #else
  271. scriptPath += ".sh";
  272. #endif
  273. cmsys::ofstream script(scriptPath.c_str());
  274. #ifdef _WIN32
  275. script << "@echo off\n";
  276. int line = 1;
  277. #else
  278. script << "set -e\n\n";
  279. #endif
  280. for (auto const& i : cmdLines) {
  281. std::string cmd = i;
  282. // The command line was built assuming it would be written to
  283. // the build.ninja file, so it uses '$$' for '$'. Remove this
  284. // for the raw shell script.
  285. cmSystemTools::ReplaceString(cmd, "$$", "$");
  286. #ifdef _WIN32
  287. script << cmd << " || (set FAIL_LINE=" << ++line << "& goto :ABORT)"
  288. << '\n';
  289. #else
  290. script << cmd << '\n';
  291. #endif
  292. }
  293. #ifdef _WIN32
  294. script << "goto :EOF\n\n"
  295. ":ABORT\n"
  296. "set ERROR_CODE=%ERRORLEVEL%\n"
  297. "echo Batch file failed at line %FAIL_LINE% "
  298. "with errorcode %ERRORLEVEL%\n"
  299. "exit /b %ERROR_CODE%";
  300. #endif
  301. return scriptPath;
  302. }
  303. std::string cmLocalNinjaGenerator::BuildCommandLine(
  304. std::vector<std::string> const& cmdLines, std::string const& customStep,
  305. cmGeneratorTarget const* target) const
  306. {
  307. // If we have no commands but we need to build a command anyway, use noop.
  308. // This happens when building a POST_BUILD value for link targets that
  309. // don't use POST_BUILD.
  310. if (cmdLines.empty()) {
  311. return cmGlobalNinjaGenerator::SHELL_NOOP;
  312. }
  313. // If this is a custom step then we will have no '$VAR' ninja placeholders.
  314. // This means we can deal with long command sequences by writing to a script.
  315. // Do this if the command lines are on the scale of the OS limit.
  316. if (!customStep.empty()) {
  317. size_t cmdLinesTotal = 0;
  318. for (std::string const& cmd : cmdLines) {
  319. cmdLinesTotal += cmd.length() + 6;
  320. }
  321. if (cmdLinesTotal > cmSystemTools::CalculateCommandLineLengthLimit() / 2) {
  322. std::string const scriptPath =
  323. this->WriteCommandScript(cmdLines, customStep, target);
  324. std::string cmd
  325. #ifndef _WIN32
  326. = "/bin/sh "
  327. #endif
  328. ;
  329. cmd += this->ConvertToOutputFormat(
  330. this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(scriptPath),
  331. cmOutputConverter::SHELL);
  332. // Add an unused argument based on script content so that Ninja
  333. // knows when the command lines change.
  334. cmd += " ";
  335. cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
  336. cmd += hash.HashFile(scriptPath).substr(0, 16);
  337. return cmd;
  338. }
  339. }
  340. std::ostringstream cmd;
  341. for (auto li = cmdLines.begin(); li != cmdLines.end(); ++li)
  342. #ifdef _WIN32
  343. {
  344. if (li != cmdLines.begin()) {
  345. cmd << " && ";
  346. } else if (cmdLines.size() > 1) {
  347. cmd << "cmd.exe /C \"";
  348. }
  349. // Put current cmdLine in brackets if it contains "||" because it has
  350. // higher precedence than "&&" in cmd.exe
  351. if (li->find("||") != std::string::npos) {
  352. cmd << "( " << *li << " )";
  353. } else {
  354. cmd << *li;
  355. }
  356. }
  357. if (cmdLines.size() > 1) {
  358. cmd << "\"";
  359. }
  360. #else
  361. {
  362. if (li != cmdLines.begin()) {
  363. cmd << " && ";
  364. }
  365. cmd << *li;
  366. }
  367. #endif
  368. return cmd.str();
  369. }
  370. void cmLocalNinjaGenerator::AppendCustomCommandLines(
  371. cmCustomCommandGenerator const& ccg, std::vector<std::string>& cmdLines)
  372. {
  373. if (ccg.GetNumberOfCommands() > 0) {
  374. std::string wd = ccg.GetWorkingDirectory();
  375. if (wd.empty()) {
  376. wd = this->GetCurrentBinaryDirectory();
  377. }
  378. std::ostringstream cdCmd;
  379. #ifdef _WIN32
  380. std::string cdStr = "cd /D ";
  381. #else
  382. std::string cdStr = "cd ";
  383. #endif
  384. cdCmd << cdStr
  385. << this->ConvertToOutputFormat(wd, cmOutputConverter::SHELL);
  386. cmdLines.push_back(cdCmd.str());
  387. }
  388. std::string launcher = this->MakeCustomLauncher(ccg);
  389. for (unsigned i = 0; i != ccg.GetNumberOfCommands(); ++i) {
  390. cmdLines.push_back(launcher +
  391. this->ConvertToOutputFormat(ccg.GetCommand(i),
  392. cmOutputConverter::SHELL));
  393. std::string& cmd = cmdLines.back();
  394. ccg.AppendArguments(i, cmd);
  395. }
  396. }
  397. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatement(
  398. cmCustomCommand const* cc, const cmNinjaDeps& orderOnlyDeps)
  399. {
  400. cmGlobalNinjaGenerator* gg = this->GetGlobalNinjaGenerator();
  401. if (gg->SeenCustomCommand(cc)) {
  402. return;
  403. }
  404. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(), this);
  405. const std::vector<std::string>& outputs = ccg.GetOutputs();
  406. const std::vector<std::string>& byproducts = ccg.GetByproducts();
  407. bool symbolic = false;
  408. for (std::string const& output : outputs) {
  409. if (cmSourceFile* sf = this->Makefile->GetSource(output)) {
  410. if (sf->GetPropertyAsBool("SYMBOLIC")) {
  411. symbolic = true;
  412. break;
  413. }
  414. }
  415. }
  416. #if 0
  417. # error TODO: Once CC in an ExternalProject target must provide the \
  418. file of each imported target that has an add_dependencies pointing \
  419. at us. How to know which ExternalProject step actually provides it?
  420. #endif
  421. cmNinjaDeps ninjaOutputs(outputs.size() + byproducts.size());
  422. std::transform(outputs.begin(), outputs.end(), ninjaOutputs.begin(),
  423. gg->MapToNinjaPath());
  424. std::transform(byproducts.begin(), byproducts.end(),
  425. ninjaOutputs.begin() + outputs.size(), gg->MapToNinjaPath());
  426. for (std::string const& ninjaOutput : ninjaOutputs) {
  427. gg->SeenCustomCommandOutput(ninjaOutput);
  428. }
  429. cmNinjaDeps ninjaDeps;
  430. this->AppendCustomCommandDeps(ccg, ninjaDeps);
  431. std::vector<std::string> cmdLines;
  432. this->AppendCustomCommandLines(ccg, cmdLines);
  433. if (cmdLines.empty()) {
  434. cmNinjaBuild build("phony");
  435. build.Comment = "Phony custom command for " + ninjaOutputs[0];
  436. build.Outputs = std::move(ninjaOutputs);
  437. build.ExplicitDeps = std::move(ninjaDeps);
  438. build.OrderOnlyDeps = orderOnlyDeps;
  439. gg->WriteBuild(this->GetBuildFileStream(), build);
  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. gg->WriteCustomCommandBuild(
  447. this->BuildCommandLine(cmdLines, customStep),
  448. this->ConstructComment(ccg), "Custom command for " + ninjaOutputs[0],
  449. cc->GetDepfile(), cc->GetJobPool(), 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. auto i = this->CustomCommandTargets.find(customCommand);
  469. assert(i != this->CustomCommandTargets.end());
  470. // A custom command may appear on multiple targets. However, some build
  471. // systems exist where the target dependencies on some of the targets are
  472. // overspecified, leading to a dependency cycle. If we assume all target
  473. // dependencies are a superset of the true target dependencies for this
  474. // custom command, we can take the set intersection of all target
  475. // dependencies to obtain a correct dependency list.
  476. //
  477. // FIXME: This won't work in certain obscure scenarios involving indirect
  478. // dependencies.
  479. auto j = i->second.begin();
  480. assert(j != i->second.end());
  481. std::vector<std::string> ccTargetDeps;
  482. this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure(*j,
  483. ccTargetDeps);
  484. std::sort(ccTargetDeps.begin(), ccTargetDeps.end());
  485. ++j;
  486. for (; j != i->second.end(); ++j) {
  487. std::vector<std::string> jDeps;
  488. std::vector<std::string> 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 = this->MaybeConvertToRelativePath(
  515. 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. }
  529. void cmLocalNinjaGenerator::AdditionalCleanFiles()
  530. {
  531. if (const char* prop_value =
  532. this->Makefile->GetProperty("ADDITIONAL_CLEAN_FILES")) {
  533. std::vector<std::string> cleanFiles;
  534. {
  535. cmGeneratorExpression ge;
  536. auto cge = ge.Parse(prop_value);
  537. cmExpandList(
  538. cge->Evaluate(this,
  539. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE")),
  540. cleanFiles);
  541. }
  542. std::string const& binaryDir = this->GetCurrentBinaryDirectory();
  543. cmGlobalNinjaGenerator* gg = this->GetGlobalNinjaGenerator();
  544. for (std::string const& cleanFile : cleanFiles) {
  545. // Support relative paths
  546. gg->AddAdditionalCleanFile(
  547. cmSystemTools::CollapseFullPath(cleanFile, binaryDir));
  548. }
  549. }
  550. }