cmLocalNinjaGenerator.cxx 19 KB

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