cmLocalNinjaGenerator.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 "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 "cmSystemTools.h"
  27. #include "cmake.h"
  28. #include "cmsys/FStream.hxx"
  29. cmLocalNinjaGenerator::cmLocalNinjaGenerator(cmGlobalGenerator* gg,
  30. cmMakefile* mf)
  31. : cmLocalCommonGenerator(gg, mf, mf->GetState()->GetBinaryDirectory())
  32. , HomeRelativeOutputPath("")
  33. {
  34. }
  35. // Virtual public methods.
  36. cmRulePlaceholderExpander*
  37. cmLocalNinjaGenerator::CreateRulePlaceholderExpander() const
  38. {
  39. cmRulePlaceholderExpander* ret =
  40. this->cmLocalGenerator::CreateRulePlaceholderExpander();
  41. ret->SetTargetImpLib("$TARGET_IMPLIB");
  42. return ret;
  43. }
  44. cmLocalNinjaGenerator::~cmLocalNinjaGenerator() = default;
  45. void cmLocalNinjaGenerator::Generate()
  46. {
  47. // Compute the path to use when referencing the current output
  48. // directory from the top output directory.
  49. this->HomeRelativeOutputPath = this->MaybeConvertToRelativePath(
  50. this->GetBinaryDirectory(), this->GetCurrentBinaryDirectory());
  51. if (this->HomeRelativeOutputPath == ".") {
  52. this->HomeRelativeOutputPath.clear();
  53. }
  54. this->WriteProcessedMakefile(this->GetBuildFileStream());
  55. #ifdef NINJA_GEN_VERBOSE_FILES
  56. this->WriteProcessedMakefile(this->GetRulesFileStream());
  57. #endif
  58. // We do that only once for the top CMakeLists.txt file.
  59. if (this->IsRootMakefile()) {
  60. this->WriteBuildFileTop();
  61. this->WritePools(this->GetRulesFileStream());
  62. const std::string showIncludesPrefix =
  63. this->GetMakefile()->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  64. if (!showIncludesPrefix.empty()) {
  65. cmGlobalNinjaGenerator::WriteComment(this->GetRulesFileStream(),
  66. "localized /showIncludes string");
  67. this->GetRulesFileStream()
  68. << "msvc_deps_prefix = " << showIncludesPrefix << "\n\n";
  69. }
  70. }
  71. for (cmGeneratorTarget* target : this->GetGeneratorTargets()) {
  72. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  73. continue;
  74. }
  75. auto 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. }
  83. }
  84. this->WriteCustomCommandBuildStatements();
  85. this->AdditionalCleanFiles();
  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);
  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. for (std::string const& i : ccg.GetDepends()) {
  249. std::string dep;
  250. if (this->GetRealDependency(i, this->GetConfigName(), dep)) {
  251. ninjaDeps.push_back(
  252. this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(dep));
  253. }
  254. }
  255. }
  256. std::string cmLocalNinjaGenerator::WriteCommandScript(
  257. std::vector<std::string> const& cmdLines, std::string const& customStep,
  258. cmGeneratorTarget const* target) const
  259. {
  260. std::string scriptPath;
  261. if (target) {
  262. scriptPath = target->GetSupportDirectory();
  263. } else {
  264. scriptPath = this->GetCurrentBinaryDirectory();
  265. scriptPath += "/CMakeFiles";
  266. }
  267. cmSystemTools::MakeDirectory(scriptPath);
  268. scriptPath += '/';
  269. scriptPath += customStep;
  270. #ifdef _WIN32
  271. scriptPath += ".bat";
  272. #else
  273. scriptPath += ".sh";
  274. #endif
  275. cmsys::ofstream script(scriptPath.c_str());
  276. #ifdef _WIN32
  277. script << "@echo off\n";
  278. int line = 1;
  279. #else
  280. script << "set -e\n\n";
  281. #endif
  282. for (auto const& i : cmdLines) {
  283. std::string cmd = i;
  284. // The command line was built assuming it would be written to
  285. // the build.ninja file, so it uses '$$' for '$'. Remove this
  286. // for the raw shell script.
  287. cmSystemTools::ReplaceString(cmd, "$$", "$");
  288. #ifdef _WIN32
  289. script << cmd << " || (set FAIL_LINE=" << ++line << "& goto :ABORT)"
  290. << '\n';
  291. #else
  292. script << cmd << '\n';
  293. #endif
  294. }
  295. #ifdef _WIN32
  296. script << "goto :EOF\n\n"
  297. ":ABORT\n"
  298. "set ERROR_CODE=%ERRORLEVEL%\n"
  299. "echo Batch file failed at line %FAIL_LINE% "
  300. "with errorcode %ERRORLEVEL%\n"
  301. "exit /b %ERROR_CODE%";
  302. #endif
  303. return scriptPath;
  304. }
  305. std::string cmLocalNinjaGenerator::BuildCommandLine(
  306. std::vector<std::string> const& cmdLines, std::string const& customStep,
  307. cmGeneratorTarget const* target) const
  308. {
  309. // If we have no commands but we need to build a command anyway, use noop.
  310. // This happens when building a POST_BUILD value for link targets that
  311. // don't use POST_BUILD.
  312. if (cmdLines.empty()) {
  313. return cmGlobalNinjaGenerator::SHELL_NOOP;
  314. }
  315. // If this is a custom step then we will have no '$VAR' ninja placeholders.
  316. // This means we can deal with long command sequences by writing to a script.
  317. // Do this if the command lines are on the scale of the OS limit.
  318. if (!customStep.empty()) {
  319. size_t cmdLinesTotal = 0;
  320. for (std::string const& cmd : cmdLines) {
  321. cmdLinesTotal += cmd.length() + 6;
  322. }
  323. if (cmdLinesTotal > cmSystemTools::CalculateCommandLineLengthLimit() / 2) {
  324. std::string const scriptPath =
  325. this->WriteCommandScript(cmdLines, customStep, target);
  326. std::string cmd
  327. #ifndef _WIN32
  328. = "/bin/sh "
  329. #endif
  330. ;
  331. cmd += this->ConvertToOutputFormat(
  332. this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(scriptPath),
  333. cmOutputConverter::SHELL);
  334. // Add an unused argument based on script content so that Ninja
  335. // knows when the command lines change.
  336. cmd += " ";
  337. cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
  338. cmd += hash.HashFile(scriptPath).substr(0, 16);
  339. return cmd;
  340. }
  341. }
  342. std::ostringstream cmd;
  343. for (std::vector<std::string>::const_iterator li = cmdLines.begin();
  344. li != cmdLines.end(); ++li)
  345. #ifdef _WIN32
  346. {
  347. if (li != cmdLines.begin()) {
  348. cmd << " && ";
  349. } else if (cmdLines.size() > 1) {
  350. cmd << "cmd.exe /C \"";
  351. }
  352. // Put current cmdLine in brackets if it contains "||" because it has
  353. // higher precedence than "&&" in cmd.exe
  354. if (li->find("||") != std::string::npos) {
  355. cmd << "( " << *li << " )";
  356. } else {
  357. cmd << *li;
  358. }
  359. }
  360. if (cmdLines.size() > 1) {
  361. cmd << "\"";
  362. }
  363. #else
  364. {
  365. if (li != cmdLines.begin()) {
  366. cmd << " && ";
  367. }
  368. cmd << *li;
  369. }
  370. #endif
  371. return cmd.str();
  372. }
  373. void cmLocalNinjaGenerator::AppendCustomCommandLines(
  374. cmCustomCommandGenerator const& ccg, std::vector<std::string>& cmdLines)
  375. {
  376. if (ccg.GetNumberOfCommands() > 0) {
  377. std::string wd = ccg.GetWorkingDirectory();
  378. if (wd.empty()) {
  379. wd = this->GetCurrentBinaryDirectory();
  380. }
  381. std::ostringstream cdCmd;
  382. #ifdef _WIN32
  383. std::string cdStr = "cd /D ";
  384. #else
  385. std::string cdStr = "cd ";
  386. #endif
  387. cdCmd << cdStr
  388. << this->ConvertToOutputFormat(wd, cmOutputConverter::SHELL);
  389. cmdLines.push_back(cdCmd.str());
  390. }
  391. std::string launcher = this->MakeCustomLauncher(ccg);
  392. for (unsigned i = 0; i != ccg.GetNumberOfCommands(); ++i) {
  393. cmdLines.push_back(launcher +
  394. this->ConvertToOutputFormat(ccg.GetCommand(i),
  395. cmOutputConverter::SHELL));
  396. std::string& cmd = cmdLines.back();
  397. ccg.AppendArguments(i, cmd);
  398. }
  399. }
  400. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatement(
  401. cmCustomCommand const* cc, const cmNinjaDeps& orderOnlyDeps)
  402. {
  403. if (this->GetGlobalNinjaGenerator()->SeenCustomCommand(cc)) {
  404. return;
  405. }
  406. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(), this);
  407. const std::vector<std::string>& outputs = ccg.GetOutputs();
  408. const std::vector<std::string>& byproducts = ccg.GetByproducts();
  409. cmNinjaDeps ninjaOutputs(outputs.size() + byproducts.size()), ninjaDeps;
  410. bool symbolic = false;
  411. for (std::string const& output : outputs) {
  412. if (cmSourceFile* sf = this->Makefile->GetSource(output)) {
  413. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  414. if (symbolic) {
  415. break;
  416. }
  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 = 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. cmSystemTools::ExpandListArgument(
  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. }