cmLocalNinjaGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmLocalNinjaGenerator.h"
  4. #include <algorithm>
  5. #include <assert.h>
  6. #include <iterator>
  7. #include <memory> // IWYU pragma: keep
  8. #include <sstream>
  9. #include <stdio.h>
  10. #include <utility>
  11. #include "cmCryptoHash.h"
  12. #include "cmCustomCommand.h"
  13. #include "cmCustomCommandGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGeneratorTarget.h"
  16. #include "cmGlobalGenerator.h"
  17. #include "cmGlobalNinjaGenerator.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmMakefile.h"
  20. #include "cmNinjaTargetGenerator.h"
  21. #include "cmRulePlaceholderExpander.h"
  22. #include "cmSourceFile.h"
  23. #include "cmState.h"
  24. #include "cmStateTypes.h"
  25. #include "cmSystemTools.h"
  26. #include "cmake.h"
  27. #include "cmsys/FStream.hxx"
  28. cmLocalNinjaGenerator::cmLocalNinjaGenerator(cmGlobalGenerator* gg,
  29. cmMakefile* mf)
  30. : cmLocalCommonGenerator(gg, mf, mf->GetState()->GetBinaryDirectory())
  31. , HomeRelativeOutputPath("")
  32. {
  33. }
  34. // Virtual public methods.
  35. cmRulePlaceholderExpander*
  36. cmLocalNinjaGenerator::CreateRulePlaceholderExpander() const
  37. {
  38. cmRulePlaceholderExpander* ret =
  39. this->cmLocalGenerator::CreateRulePlaceholderExpander();
  40. ret->SetTargetImpLib("$TARGET_IMPLIB");
  41. return ret;
  42. }
  43. cmLocalNinjaGenerator::~cmLocalNinjaGenerator() = default;
  44. void cmLocalNinjaGenerator::Generate()
  45. {
  46. // Compute the path to use when referencing the current output
  47. // directory from the top output directory.
  48. this->HomeRelativeOutputPath = this->MaybeConvertToRelativePath(
  49. this->GetBinaryDirectory(), this->GetCurrentBinaryDirectory());
  50. if (this->HomeRelativeOutputPath == ".") {
  51. this->HomeRelativeOutputPath.clear();
  52. }
  53. this->WriteProcessedMakefile(this->GetBuildFileStream());
  54. #ifdef NINJA_GEN_VERBOSE_FILES
  55. this->WriteProcessedMakefile(this->GetRulesFileStream());
  56. #endif
  57. // We do that only once for the top CMakeLists.txt file.
  58. if (this->IsRootMakefile()) {
  59. this->WriteBuildFileTop();
  60. this->WritePools(this->GetRulesFileStream());
  61. const std::string& showIncludesPrefix =
  62. this->GetMakefile()->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  63. if (!showIncludesPrefix.empty()) {
  64. cmGlobalNinjaGenerator::WriteComment(this->GetRulesFileStream(),
  65. "localized /showIncludes string");
  66. this->GetRulesFileStream()
  67. << "msvc_deps_prefix = " << showIncludesPrefix << "\n\n";
  68. }
  69. }
  70. for (cmGeneratorTarget* target : this->GetGeneratorTargets()) {
  71. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  72. continue;
  73. }
  74. auto tg = cmNinjaTargetGenerator::New(target);
  75. if (tg) {
  76. tg->Generate();
  77. // Add the target to "all" if required.
  78. if (!this->GetGlobalNinjaGenerator()->IsExcluded(target)) {
  79. this->GetGlobalNinjaGenerator()->AddDependencyToAll(target);
  80. }
  81. }
  82. }
  83. this->WriteCustomCommandBuildStatements();
  84. }
  85. // TODO: Picked up from cmLocalUnixMakefileGenerator3. Refactor it.
  86. std::string cmLocalNinjaGenerator::GetTargetDirectory(
  87. cmGeneratorTarget const* target) const
  88. {
  89. std::string dir = "CMakeFiles/";
  90. dir += target->GetName();
  91. #if defined(__VMS)
  92. dir += "_dir";
  93. #else
  94. dir += ".dir";
  95. #endif
  96. return dir;
  97. }
  98. // Non-virtual public methods.
  99. const cmGlobalNinjaGenerator* cmLocalNinjaGenerator::GetGlobalNinjaGenerator()
  100. const
  101. {
  102. return static_cast<const cmGlobalNinjaGenerator*>(
  103. this->GetGlobalGenerator());
  104. }
  105. cmGlobalNinjaGenerator* cmLocalNinjaGenerator::GetGlobalNinjaGenerator()
  106. {
  107. return static_cast<cmGlobalNinjaGenerator*>(this->GetGlobalGenerator());
  108. }
  109. // Virtual protected methods.
  110. std::string cmLocalNinjaGenerator::ConvertToIncludeReference(
  111. std::string const& path, cmOutputConverter::OutputFormat format,
  112. bool forceFullPaths)
  113. {
  114. if (forceFullPaths) {
  115. return this->ConvertToOutputFormat(cmSystemTools::CollapseFullPath(path),
  116. format);
  117. }
  118. return this->ConvertToOutputFormat(
  119. this->MaybeConvertToRelativePath(this->GetBinaryDirectory(), path),
  120. format);
  121. }
  122. // Private methods.
  123. cmGeneratedFileStream& cmLocalNinjaGenerator::GetBuildFileStream() const
  124. {
  125. return *this->GetGlobalNinjaGenerator()->GetBuildFileStream();
  126. }
  127. cmGeneratedFileStream& cmLocalNinjaGenerator::GetRulesFileStream() const
  128. {
  129. return *this->GetGlobalNinjaGenerator()->GetRulesFileStream();
  130. }
  131. const cmake* cmLocalNinjaGenerator::GetCMakeInstance() const
  132. {
  133. return this->GetGlobalGenerator()->GetCMakeInstance();
  134. }
  135. cmake* cmLocalNinjaGenerator::GetCMakeInstance()
  136. {
  137. return this->GetGlobalGenerator()->GetCMakeInstance();
  138. }
  139. void cmLocalNinjaGenerator::WriteBuildFileTop()
  140. {
  141. // For the build file.
  142. this->WriteProjectHeader(this->GetBuildFileStream());
  143. this->WriteNinjaRequiredVersion(this->GetBuildFileStream());
  144. this->WriteNinjaFilesInclusion(this->GetBuildFileStream());
  145. // For the rule file.
  146. this->WriteProjectHeader(this->GetRulesFileStream());
  147. }
  148. void cmLocalNinjaGenerator::WriteProjectHeader(std::ostream& os)
  149. {
  150. cmGlobalNinjaGenerator::WriteDivider(os);
  151. os << "# Project: " << this->GetProjectName() << std::endl
  152. << "# Configuration: " << this->ConfigName << std::endl;
  153. cmGlobalNinjaGenerator::WriteDivider(os);
  154. }
  155. void cmLocalNinjaGenerator::WriteNinjaRequiredVersion(std::ostream& os)
  156. {
  157. // Default required version
  158. std::string requiredVersion = cmGlobalNinjaGenerator::RequiredNinjaVersion();
  159. // Ninja generator uses the 'console' pool if available (>= 1.5)
  160. if (this->GetGlobalNinjaGenerator()->SupportsConsolePool()) {
  161. requiredVersion =
  162. cmGlobalNinjaGenerator::RequiredNinjaVersionForConsolePool();
  163. }
  164. // The Ninja generator writes rules which require support for restat
  165. // when rebuilding build.ninja manifest (>= 1.8)
  166. if (this->GetGlobalNinjaGenerator()->SupportsManifestRestat() &&
  167. this->GetCMakeInstance()->DoWriteGlobVerifyTarget() &&
  168. !this->GetGlobalNinjaGenerator()->GlobalSettingIsOn(
  169. "CMAKE_SUPPRESS_REGENERATION")) {
  170. requiredVersion =
  171. cmGlobalNinjaGenerator::RequiredNinjaVersionForManifestRestat();
  172. }
  173. cmGlobalNinjaGenerator::WriteComment(
  174. os, "Minimal version of Ninja required by this file");
  175. os << "ninja_required_version = " << requiredVersion << std::endl
  176. << std::endl;
  177. }
  178. void cmLocalNinjaGenerator::WritePools(std::ostream& os)
  179. {
  180. cmGlobalNinjaGenerator::WriteDivider(os);
  181. const char* jobpools =
  182. this->GetCMakeInstance()->GetState()->GetGlobalProperty("JOB_POOLS");
  183. if (!jobpools) {
  184. jobpools = this->GetMakefile()->GetDefinition("CMAKE_JOB_POOLS");
  185. }
  186. if (jobpools) {
  187. cmGlobalNinjaGenerator::WriteComment(
  188. os, "Pools defined by global property JOB_POOLS");
  189. std::vector<std::string> pools;
  190. cmSystemTools::ExpandListArgument(jobpools, pools);
  191. for (std::string const& pool : pools) {
  192. const std::string::size_type eq = pool.find('=');
  193. unsigned int jobs;
  194. if (eq != std::string::npos &&
  195. sscanf(pool.c_str() + eq, "=%u", &jobs) == 1) {
  196. os << "pool " << pool.substr(0, eq) << std::endl;
  197. os << " depth = " << jobs << std::endl;
  198. os << std::endl;
  199. } else {
  200. cmSystemTools::Error("Invalid pool defined by property 'JOB_POOLS': " +
  201. pool);
  202. }
  203. }
  204. }
  205. }
  206. void cmLocalNinjaGenerator::WriteNinjaFilesInclusion(std::ostream& os)
  207. {
  208. cmGlobalNinjaGenerator::WriteDivider(os);
  209. os << "# Include auxiliary files.\n"
  210. << "\n";
  211. cmGlobalNinjaGenerator* ng = this->GetGlobalNinjaGenerator();
  212. std::string const ninjaRulesFile =
  213. ng->NinjaOutputPath(cmGlobalNinjaGenerator::NINJA_RULES_FILE);
  214. std::string const rulesFilePath = ng->EncodePath(ninjaRulesFile);
  215. cmGlobalNinjaGenerator::WriteInclude(os, rulesFilePath,
  216. "Include rules file.");
  217. os << "\n";
  218. }
  219. void cmLocalNinjaGenerator::WriteProcessedMakefile(std::ostream& os)
  220. {
  221. cmGlobalNinjaGenerator::WriteDivider(os);
  222. os << "# Write statements declared in CMakeLists.txt:" << std::endl
  223. << "# " << this->Makefile->GetDefinition("CMAKE_CURRENT_LIST_FILE")
  224. << std::endl;
  225. if (this->IsRootMakefile()) {
  226. os << "# Which is the root file." << std::endl;
  227. }
  228. cmGlobalNinjaGenerator::WriteDivider(os);
  229. os << std::endl;
  230. }
  231. void cmLocalNinjaGenerator::AppendTargetOutputs(cmGeneratorTarget* target,
  232. cmNinjaDeps& outputs)
  233. {
  234. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(target, outputs);
  235. }
  236. void cmLocalNinjaGenerator::AppendTargetDepends(cmGeneratorTarget* target,
  237. cmNinjaDeps& outputs,
  238. cmNinjaTargetDepends depends)
  239. {
  240. this->GetGlobalNinjaGenerator()->AppendTargetDepends(target, outputs,
  241. depends);
  242. }
  243. void cmLocalNinjaGenerator::AppendCustomCommandDeps(
  244. cmCustomCommandGenerator const& ccg, cmNinjaDeps& ninjaDeps)
  245. {
  246. for (std::string const& i : ccg.GetDepends()) {
  247. std::string dep;
  248. if (this->GetRealDependency(i, this->GetConfigName(), dep)) {
  249. ninjaDeps.push_back(
  250. this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(dep));
  251. }
  252. }
  253. }
  254. std::string cmLocalNinjaGenerator::WriteCommandScript(
  255. std::vector<std::string> const& cmdLines, std::string const& customStep,
  256. cmGeneratorTarget const* target) const
  257. {
  258. std::string scriptPath;
  259. if (target) {
  260. scriptPath = target->GetSupportDirectory();
  261. } else {
  262. scriptPath = this->GetCurrentBinaryDirectory();
  263. scriptPath += "/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 (std::vector<std::string>::const_iterator li = cmdLines.begin();
  342. li != cmdLines.end(); ++li)
  343. #ifdef _WIN32
  344. {
  345. if (li != cmdLines.begin()) {
  346. cmd << " && ";
  347. } else if (cmdLines.size() > 1) {
  348. cmd << "cmd.exe /C \"";
  349. }
  350. // Put current cmdLine in brackets if it contains "||" because it has
  351. // higher precedence than "&&" in cmd.exe
  352. if (li->find("||") != std::string::npos) {
  353. cmd << "( " << *li << " )";
  354. } else {
  355. cmd << *li;
  356. }
  357. }
  358. if (cmdLines.size() > 1) {
  359. cmd << "\"";
  360. }
  361. #else
  362. {
  363. if (li != cmdLines.begin()) {
  364. cmd << " && ";
  365. }
  366. cmd << *li;
  367. }
  368. #endif
  369. return cmd.str();
  370. }
  371. void cmLocalNinjaGenerator::AppendCustomCommandLines(
  372. cmCustomCommandGenerator const& ccg, std::vector<std::string>& cmdLines)
  373. {
  374. if (ccg.GetNumberOfCommands() > 0) {
  375. std::string wd = ccg.GetWorkingDirectory();
  376. if (wd.empty()) {
  377. wd = this->GetCurrentBinaryDirectory();
  378. }
  379. std::ostringstream cdCmd;
  380. #ifdef _WIN32
  381. std::string cdStr = "cd /D ";
  382. #else
  383. std::string cdStr = "cd ";
  384. #endif
  385. cdCmd << cdStr
  386. << this->ConvertToOutputFormat(wd, cmOutputConverter::SHELL);
  387. cmdLines.push_back(cdCmd.str());
  388. }
  389. std::string launcher = this->MakeCustomLauncher(ccg);
  390. for (unsigned i = 0; i != ccg.GetNumberOfCommands(); ++i) {
  391. cmdLines.push_back(launcher +
  392. this->ConvertToOutputFormat(ccg.GetCommand(i),
  393. cmOutputConverter::SHELL));
  394. std::string& cmd = cmdLines.back();
  395. ccg.AppendArguments(i, cmd);
  396. }
  397. }
  398. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatement(
  399. cmCustomCommand const* cc, const cmNinjaDeps& orderOnlyDeps)
  400. {
  401. if (this->GetGlobalNinjaGenerator()->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. cmNinjaDeps ninjaOutputs(outputs.size() + byproducts.size()), ninjaDeps;
  408. bool symbolic = false;
  409. for (std::string const& output : outputs) {
  410. if (cmSourceFile* sf = this->Makefile->GetSource(output)) {
  411. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  412. if (symbolic) {
  413. break;
  414. }
  415. }
  416. }
  417. #if 0
  418. # error TODO: Once CC in an ExternalProject target must provide the \
  419. file of each imported target that has an add_dependencies pointing \
  420. at us. How to know which ExternalProject step actually provides it?
  421. #endif
  422. std::transform(outputs.begin(), outputs.end(), ninjaOutputs.begin(),
  423. this->GetGlobalNinjaGenerator()->MapToNinjaPath());
  424. std::transform(byproducts.begin(), byproducts.end(),
  425. ninjaOutputs.begin() + outputs.size(),
  426. this->GetGlobalNinjaGenerator()->MapToNinjaPath());
  427. this->AppendCustomCommandDeps(ccg, ninjaDeps);
  428. for (std::string const& ninjaOutput : ninjaOutputs) {
  429. this->GetGlobalNinjaGenerator()->SeenCustomCommandOutput(ninjaOutput);
  430. }
  431. std::vector<std::string> cmdLines;
  432. this->AppendCustomCommandLines(ccg, cmdLines);
  433. if (cmdLines.empty()) {
  434. this->GetGlobalNinjaGenerator()->WritePhonyBuild(
  435. this->GetBuildFileStream(),
  436. "Phony custom command for " + ninjaOutputs[0], ninjaOutputs, ninjaDeps,
  437. cmNinjaDeps(), orderOnlyDeps, cmNinjaVars());
  438. } else {
  439. std::string customStep = cmSystemTools::GetFilenameName(ninjaOutputs[0]);
  440. // Hash full path to make unique.
  441. customStep += '-';
  442. cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
  443. customStep += hash.HashString(ninjaOutputs[0]).substr(0, 7);
  444. this->GetGlobalNinjaGenerator()->WriteCustomCommandBuild(
  445. this->BuildCommandLine(cmdLines, customStep),
  446. this->ConstructComment(ccg), "Custom command for " + ninjaOutputs[0],
  447. cc->GetDepfile(), cc->GetUsesTerminal(),
  448. /*restat*/ !symbolic || !byproducts.empty(), ninjaOutputs, ninjaDeps,
  449. orderOnlyDeps);
  450. }
  451. }
  452. void cmLocalNinjaGenerator::AddCustomCommandTarget(cmCustomCommand const* cc,
  453. cmGeneratorTarget* target)
  454. {
  455. CustomCommandTargetMap::value_type v(cc, std::set<cmGeneratorTarget*>());
  456. std::pair<CustomCommandTargetMap::iterator, bool> ins =
  457. this->CustomCommandTargets.insert(v);
  458. if (ins.second) {
  459. this->CustomCommands.push_back(cc);
  460. }
  461. ins.first->second.insert(target);
  462. }
  463. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatements()
  464. {
  465. for (cmCustomCommand const* customCommand : this->CustomCommands) {
  466. CustomCommandTargetMap::iterator i =
  467. this->CustomCommandTargets.find(customCommand);
  468. assert(i != this->CustomCommandTargets.end());
  469. // A custom command may appear on multiple targets. However, some build
  470. // systems exist where the target dependencies on some of the targets are
  471. // overspecified, leading to a dependency cycle. If we assume all target
  472. // dependencies are a superset of the true target dependencies for this
  473. // custom command, we can take the set intersection of all target
  474. // dependencies to obtain a correct dependency list.
  475. //
  476. // FIXME: This won't work in certain obscure scenarios involving indirect
  477. // dependencies.
  478. std::set<cmGeneratorTarget*>::iterator j = i->second.begin();
  479. assert(j != i->second.end());
  480. std::vector<std::string> ccTargetDeps;
  481. this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure(*j,
  482. ccTargetDeps);
  483. std::sort(ccTargetDeps.begin(), ccTargetDeps.end());
  484. ++j;
  485. for (; j != i->second.end(); ++j) {
  486. std::vector<std::string> jDeps, depsIntersection;
  487. this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure(*j, jDeps);
  488. std::sort(jDeps.begin(), jDeps.end());
  489. std::set_intersection(ccTargetDeps.begin(), ccTargetDeps.end(),
  490. jDeps.begin(), jDeps.end(),
  491. std::back_inserter(depsIntersection));
  492. ccTargetDeps = depsIntersection;
  493. }
  494. this->WriteCustomCommandBuildStatement(i->first, ccTargetDeps);
  495. }
  496. }
  497. std::string cmLocalNinjaGenerator::MakeCustomLauncher(
  498. cmCustomCommandGenerator const& ccg)
  499. {
  500. const char* property_value =
  501. this->Makefile->GetProperty("RULE_LAUNCH_CUSTOM");
  502. if (!property_value || !*property_value) {
  503. return std::string();
  504. }
  505. // Expand rule variables referenced in the given launcher command.
  506. cmRulePlaceholderExpander::RuleVariables vars;
  507. std::string output;
  508. const std::vector<std::string>& outputs = ccg.GetOutputs();
  509. if (!outputs.empty()) {
  510. output = outputs[0];
  511. if (ccg.GetWorkingDirectory().empty()) {
  512. output = this->MaybeConvertToRelativePath(
  513. this->GetCurrentBinaryDirectory(), output);
  514. }
  515. output = this->ConvertToOutputFormat(output, cmOutputConverter::SHELL);
  516. }
  517. vars.Output = output.c_str();
  518. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  519. this->CreateRulePlaceholderExpander());
  520. std::string launcher = property_value;
  521. rulePlaceholderExpander->ExpandRuleVariables(this, launcher, vars);
  522. if (!launcher.empty()) {
  523. launcher += " ";
  524. }
  525. return launcher;
  526. }