cmLocalNinjaGenerator.cxx 22 KB

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