cmLocalNinjaGenerator.cxx 19 KB

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