cmLocalNinjaGenerator.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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 <cmext/string_view>
  12. #include "cmsys/FStream.hxx"
  13. #include "cmCryptoHash.h"
  14. #include "cmCustomCommand.h"
  15. #include "cmCustomCommandGenerator.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmGeneratorExpression.h"
  18. #include "cmGeneratorTarget.h"
  19. #include "cmGlobalGenerator.h"
  20. #include "cmGlobalNinjaGenerator.h"
  21. #include "cmLocalGenerator.h"
  22. #include "cmMakefile.h"
  23. #include "cmMessageType.h"
  24. #include "cmNinjaTargetGenerator.h"
  25. #include "cmPolicies.h"
  26. #include "cmProperty.h"
  27. #include "cmRulePlaceholderExpander.h"
  28. #include "cmSourceFile.h"
  29. #include "cmState.h"
  30. #include "cmStateTypes.h"
  31. #include "cmStringAlgorithms.h"
  32. #include "cmSystemTools.h"
  33. #include "cmTarget.h"
  34. #include "cmake.h"
  35. cmLocalNinjaGenerator::cmLocalNinjaGenerator(cmGlobalGenerator* gg,
  36. cmMakefile* mf)
  37. : cmLocalCommonGenerator(gg, mf, mf->GetState()->GetBinaryDirectory())
  38. , HomeRelativeOutputPath("")
  39. {
  40. }
  41. // Virtual public methods.
  42. cmRulePlaceholderExpander*
  43. cmLocalNinjaGenerator::CreateRulePlaceholderExpander() const
  44. {
  45. cmRulePlaceholderExpander* ret =
  46. this->cmLocalGenerator::CreateRulePlaceholderExpander();
  47. ret->SetTargetImpLib("$TARGET_IMPLIB");
  48. return ret;
  49. }
  50. cmLocalNinjaGenerator::~cmLocalNinjaGenerator() = default;
  51. void cmLocalNinjaGenerator::Generate()
  52. {
  53. // Compute the path to use when referencing the current output
  54. // directory from the top output directory.
  55. this->HomeRelativeOutputPath = this->MaybeConvertToRelativePath(
  56. this->GetBinaryDirectory(), this->GetCurrentBinaryDirectory());
  57. if (this->HomeRelativeOutputPath == ".") {
  58. this->HomeRelativeOutputPath.clear();
  59. }
  60. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  61. for (auto const& config : this->GetConfigNames()) {
  62. this->WriteProcessedMakefile(this->GetImplFileStream(config));
  63. }
  64. }
  65. this->WriteProcessedMakefile(this->GetCommonFileStream());
  66. #ifdef NINJA_GEN_VERBOSE_FILES
  67. this->WriteProcessedMakefile(this->GetRulesFileStream());
  68. #endif
  69. // We do that only once for the top CMakeLists.txt file.
  70. if (this->IsRootMakefile()) {
  71. this->WriteBuildFileTop();
  72. this->WritePools(this->GetRulesFileStream());
  73. const std::string& showIncludesPrefix =
  74. this->GetMakefile()->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  75. if (!showIncludesPrefix.empty()) {
  76. cmGlobalNinjaGenerator::WriteComment(this->GetRulesFileStream(),
  77. "localized /showIncludes string");
  78. this->GetRulesFileStream() << "msvc_deps_prefix = ";
  79. #ifdef WIN32
  80. // Ninja uses the ANSI Windows APIs, so strings in the rules file
  81. // typically need to be ANSI encoded. However, in this case the compiler
  82. // is being invoked using the UTF-8 codepage so the /showIncludes prefix
  83. // will be UTF-8 encoded on stdout. Ninja can't successfully compare this
  84. // UTF-8 encoded prefix to the ANSI encoded msvc_deps_prefix if it
  85. // contains any non-ASCII characters and dependency checking will fail.
  86. // As a workaround, leave the msvc_deps_prefix UTF-8 encoded even though
  87. // the rest of the file is ANSI encoded.
  88. if (GetConsoleOutputCP() == CP_UTF8 && GetACP() != CP_UTF8) {
  89. this->GetRulesFileStream().WriteRaw(showIncludesPrefix);
  90. } else {
  91. this->GetRulesFileStream() << showIncludesPrefix;
  92. }
  93. #else
  94. // It's safe to use the standard encoding on other platforms.
  95. this->GetRulesFileStream() << showIncludesPrefix;
  96. #endif
  97. this->GetRulesFileStream() << "\n\n";
  98. }
  99. }
  100. for (const auto& target : this->GetGeneratorTargets()) {
  101. if (!target->IsInBuildSystem()) {
  102. continue;
  103. }
  104. auto tg = cmNinjaTargetGenerator::New(target.get());
  105. if (tg) {
  106. if (target->Target->IsPerConfig()) {
  107. for (auto const& config : this->GetConfigNames()) {
  108. tg->Generate(config);
  109. if (target->GetType() == cmStateEnums::GLOBAL_TARGET &&
  110. this->GetGlobalGenerator()->IsMultiConfig()) {
  111. cmNinjaBuild phonyAlias("phony");
  112. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(
  113. target.get(), phonyAlias.Outputs, "", DependOnTargetArtifact);
  114. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(
  115. target.get(), phonyAlias.ExplicitDeps, config,
  116. DependOnTargetArtifact);
  117. this->GetGlobalNinjaGenerator()->WriteBuild(
  118. *this->GetGlobalNinjaGenerator()->GetConfigFileStream(config),
  119. phonyAlias);
  120. }
  121. }
  122. if (target->GetType() == cmStateEnums::GLOBAL_TARGET &&
  123. this->GetGlobalGenerator()->IsMultiConfig()) {
  124. if (!this->GetGlobalNinjaGenerator()->GetDefaultConfigs().empty()) {
  125. cmNinjaBuild phonyAlias("phony");
  126. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(
  127. target.get(), phonyAlias.Outputs, "", DependOnTargetArtifact);
  128. for (auto const& config :
  129. this->GetGlobalNinjaGenerator()->GetDefaultConfigs()) {
  130. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(
  131. target.get(), phonyAlias.ExplicitDeps, config,
  132. DependOnTargetArtifact);
  133. }
  134. this->GetGlobalNinjaGenerator()->WriteBuild(
  135. *this->GetGlobalNinjaGenerator()->GetDefaultFileStream(),
  136. phonyAlias);
  137. }
  138. cmNinjaBuild phonyAlias("phony");
  139. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(
  140. target.get(), phonyAlias.Outputs, "all", DependOnTargetArtifact);
  141. for (auto const& config : this->GetConfigNames()) {
  142. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(
  143. target.get(), phonyAlias.ExplicitDeps, config,
  144. DependOnTargetArtifact);
  145. }
  146. this->GetGlobalNinjaGenerator()->WriteBuild(
  147. *this->GetGlobalNinjaGenerator()->GetDefaultFileStream(),
  148. phonyAlias);
  149. }
  150. } else {
  151. tg->Generate("");
  152. }
  153. }
  154. }
  155. for (auto const& config : this->GetConfigNames()) {
  156. this->WriteCustomCommandBuildStatements(config);
  157. this->AdditionalCleanFiles(config);
  158. }
  159. }
  160. // TODO: Picked up from cmLocalUnixMakefileGenerator3. Refactor it.
  161. std::string cmLocalNinjaGenerator::GetTargetDirectory(
  162. cmGeneratorTarget const* target) const
  163. {
  164. std::string dir = cmStrCat("CMakeFiles/", target->GetName());
  165. #if defined(__VMS)
  166. dir += "_dir";
  167. #else
  168. dir += ".dir";
  169. #endif
  170. return dir;
  171. }
  172. // Non-virtual public methods.
  173. const cmGlobalNinjaGenerator* cmLocalNinjaGenerator::GetGlobalNinjaGenerator()
  174. const
  175. {
  176. return static_cast<const cmGlobalNinjaGenerator*>(
  177. this->GetGlobalGenerator());
  178. }
  179. cmGlobalNinjaGenerator* cmLocalNinjaGenerator::GetGlobalNinjaGenerator()
  180. {
  181. return static_cast<cmGlobalNinjaGenerator*>(this->GetGlobalGenerator());
  182. }
  183. // Virtual protected methods.
  184. std::string cmLocalNinjaGenerator::ConvertToIncludeReference(
  185. std::string const& path, cmOutputConverter::OutputFormat format,
  186. bool forceFullPaths)
  187. {
  188. if (forceFullPaths) {
  189. return this->ConvertToOutputFormat(
  190. cmSystemTools::CollapseFullPath(path, this->GetCurrentBinaryDirectory()),
  191. format);
  192. }
  193. return this->ConvertToOutputFormat(
  194. this->MaybeConvertToRelativePath(this->GetBinaryDirectory(), path),
  195. format);
  196. }
  197. // Private methods.
  198. cmGeneratedFileStream& cmLocalNinjaGenerator::GetImplFileStream(
  199. const std::string& config) const
  200. {
  201. return *this->GetGlobalNinjaGenerator()->GetImplFileStream(config);
  202. }
  203. cmGeneratedFileStream& cmLocalNinjaGenerator::GetCommonFileStream() const
  204. {
  205. return *this->GetGlobalNinjaGenerator()->GetCommonFileStream();
  206. }
  207. cmGeneratedFileStream& cmLocalNinjaGenerator::GetRulesFileStream() const
  208. {
  209. return *this->GetGlobalNinjaGenerator()->GetRulesFileStream();
  210. }
  211. const cmake* cmLocalNinjaGenerator::GetCMakeInstance() const
  212. {
  213. return this->GetGlobalGenerator()->GetCMakeInstance();
  214. }
  215. cmake* cmLocalNinjaGenerator::GetCMakeInstance()
  216. {
  217. return this->GetGlobalGenerator()->GetCMakeInstance();
  218. }
  219. void cmLocalNinjaGenerator::WriteBuildFileTop()
  220. {
  221. this->WriteProjectHeader(this->GetCommonFileStream());
  222. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  223. for (auto const& config : this->GetConfigNames()) {
  224. auto& stream = this->GetImplFileStream(config);
  225. this->WriteProjectHeader(stream);
  226. this->WriteNinjaRequiredVersion(stream);
  227. this->WriteNinjaConfigurationVariable(stream, config);
  228. this->WriteNinjaFilesInclusionConfig(stream);
  229. }
  230. } else {
  231. this->WriteNinjaRequiredVersion(this->GetCommonFileStream());
  232. this->WriteNinjaConfigurationVariable(this->GetCommonFileStream(),
  233. this->GetConfigNames().front());
  234. }
  235. this->WriteNinjaFilesInclusionCommon(this->GetCommonFileStream());
  236. // For the rule file.
  237. this->WriteProjectHeader(this->GetRulesFileStream());
  238. }
  239. void cmLocalNinjaGenerator::WriteProjectHeader(std::ostream& os)
  240. {
  241. cmGlobalNinjaGenerator::WriteDivider(os);
  242. os << "# Project: " << this->GetProjectName() << '\n'
  243. << "# Configurations: " << cmJoin(this->GetConfigNames(), ", ") << '\n';
  244. cmGlobalNinjaGenerator::WriteDivider(os);
  245. }
  246. void cmLocalNinjaGenerator::WriteNinjaRequiredVersion(std::ostream& os)
  247. {
  248. // Default required version
  249. std::string requiredVersion = cmGlobalNinjaGenerator::RequiredNinjaVersion();
  250. // Ninja generator uses the 'console' pool if available (>= 1.5)
  251. if (this->GetGlobalNinjaGenerator()->SupportsConsolePool()) {
  252. requiredVersion =
  253. cmGlobalNinjaGenerator::RequiredNinjaVersionForConsolePool();
  254. }
  255. // The Ninja generator writes rules which require support for restat
  256. // when rebuilding build.ninja manifest (>= 1.8)
  257. if (this->GetGlobalNinjaGenerator()->SupportsManifestRestat() &&
  258. this->GetCMakeInstance()->DoWriteGlobVerifyTarget() &&
  259. !this->GetGlobalNinjaGenerator()->GlobalSettingIsOn(
  260. "CMAKE_SUPPRESS_REGENERATION")) {
  261. requiredVersion =
  262. cmGlobalNinjaGenerator::RequiredNinjaVersionForManifestRestat();
  263. }
  264. cmGlobalNinjaGenerator::WriteComment(
  265. os, "Minimal version of Ninja required by this file");
  266. os << "ninja_required_version = " << requiredVersion << "\n\n";
  267. }
  268. void cmLocalNinjaGenerator::WriteNinjaConfigurationVariable(
  269. std::ostream& os, const std::string& config)
  270. {
  271. cmGlobalNinjaGenerator::WriteVariable(
  272. os, "CONFIGURATION", config,
  273. "Set configuration variable for custom commands.");
  274. }
  275. void cmLocalNinjaGenerator::WritePools(std::ostream& os)
  276. {
  277. cmGlobalNinjaGenerator::WriteDivider(os);
  278. cmProp jobpools =
  279. this->GetCMakeInstance()->GetState()->GetGlobalProperty("JOB_POOLS");
  280. if (!jobpools) {
  281. jobpools = this->GetMakefile()->GetDefinition("CMAKE_JOB_POOLS");
  282. }
  283. if (jobpools) {
  284. cmGlobalNinjaGenerator::WriteComment(
  285. os, "Pools defined by global property JOB_POOLS");
  286. std::vector<std::string> pools = cmExpandedList(*jobpools);
  287. for (std::string const& pool : pools) {
  288. const std::string::size_type eq = pool.find('=');
  289. unsigned int jobs;
  290. if (eq != std::string::npos &&
  291. sscanf(pool.c_str() + eq, "=%u", &jobs) == 1) {
  292. os << "pool " << pool.substr(0, eq) << "\n depth = " << jobs
  293. << "\n\n";
  294. } else {
  295. cmSystemTools::Error("Invalid pool defined by property 'JOB_POOLS': " +
  296. pool);
  297. }
  298. }
  299. }
  300. }
  301. void cmLocalNinjaGenerator::WriteNinjaFilesInclusionConfig(std::ostream& os)
  302. {
  303. cmGlobalNinjaGenerator::WriteDivider(os);
  304. os << "# Include auxiliary files.\n\n";
  305. cmGlobalNinjaGenerator* ng = this->GetGlobalNinjaGenerator();
  306. std::string const ninjaCommonFile =
  307. ng->NinjaOutputPath(cmGlobalNinjaMultiGenerator::NINJA_COMMON_FILE);
  308. std::string const commonFilePath = ng->EncodePath(ninjaCommonFile);
  309. cmGlobalNinjaGenerator::WriteInclude(os, commonFilePath,
  310. "Include common file.");
  311. os << "\n";
  312. }
  313. void cmLocalNinjaGenerator::WriteNinjaFilesInclusionCommon(std::ostream& os)
  314. {
  315. cmGlobalNinjaGenerator::WriteDivider(os);
  316. os << "# Include auxiliary files.\n\n";
  317. cmGlobalNinjaGenerator* ng = this->GetGlobalNinjaGenerator();
  318. std::string const ninjaRulesFile =
  319. ng->NinjaOutputPath(cmGlobalNinjaGenerator::NINJA_RULES_FILE);
  320. std::string const rulesFilePath = ng->EncodePath(ninjaRulesFile);
  321. cmGlobalNinjaGenerator::WriteInclude(os, rulesFilePath,
  322. "Include rules file.");
  323. os << "\n";
  324. }
  325. void cmLocalNinjaGenerator::WriteProcessedMakefile(std::ostream& os)
  326. {
  327. cmGlobalNinjaGenerator::WriteDivider(os);
  328. os << "# Write statements declared in CMakeLists.txt:\n"
  329. << "# " << this->Makefile->GetSafeDefinition("CMAKE_CURRENT_LIST_FILE")
  330. << '\n';
  331. if (this->IsRootMakefile()) {
  332. os << "# Which is the root file.\n";
  333. }
  334. cmGlobalNinjaGenerator::WriteDivider(os);
  335. os << '\n';
  336. }
  337. void cmLocalNinjaGenerator::AppendTargetOutputs(cmGeneratorTarget* target,
  338. cmNinjaDeps& outputs,
  339. const std::string& config)
  340. {
  341. this->GetGlobalNinjaGenerator()->AppendTargetOutputs(target, outputs, config,
  342. DependOnTargetArtifact);
  343. }
  344. void cmLocalNinjaGenerator::AppendTargetDepends(cmGeneratorTarget* target,
  345. cmNinjaDeps& outputs,
  346. const std::string& config,
  347. const std::string& fileConfig,
  348. cmNinjaTargetDepends depends)
  349. {
  350. this->GetGlobalNinjaGenerator()->AppendTargetDepends(target, outputs, config,
  351. fileConfig, depends);
  352. }
  353. void cmLocalNinjaGenerator::AppendCustomCommandDeps(
  354. cmCustomCommandGenerator const& ccg, cmNinjaDeps& ninjaDeps,
  355. const std::string& config)
  356. {
  357. for (std::string const& i : ccg.GetDepends()) {
  358. std::string dep;
  359. if (this->GetRealDependency(i, config, dep)) {
  360. ninjaDeps.push_back(
  361. this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(dep));
  362. }
  363. }
  364. }
  365. std::string cmLocalNinjaGenerator::WriteCommandScript(
  366. std::vector<std::string> const& cmdLines, std::string const& customStep,
  367. cmGeneratorTarget const* target) const
  368. {
  369. std::string scriptPath;
  370. if (target) {
  371. scriptPath = target->GetSupportDirectory();
  372. } else {
  373. scriptPath = cmStrCat(this->GetCurrentBinaryDirectory(), "/CMakeFiles");
  374. }
  375. cmSystemTools::MakeDirectory(scriptPath);
  376. scriptPath += '/';
  377. scriptPath += customStep;
  378. #ifdef _WIN32
  379. scriptPath += ".bat";
  380. #else
  381. scriptPath += ".sh";
  382. #endif
  383. cmsys::ofstream script(scriptPath.c_str());
  384. #ifdef _WIN32
  385. script << "@echo off\n";
  386. int line = 1;
  387. #else
  388. script << "set -e\n\n";
  389. #endif
  390. for (auto const& i : cmdLines) {
  391. std::string cmd = i;
  392. // The command line was built assuming it would be written to
  393. // the build.ninja file, so it uses '$$' for '$'. Remove this
  394. // for the raw shell script.
  395. cmSystemTools::ReplaceString(cmd, "$$", "$");
  396. #ifdef _WIN32
  397. script << cmd << " || (set FAIL_LINE=" << ++line << "& goto :ABORT)"
  398. << '\n';
  399. #else
  400. script << cmd << '\n';
  401. #endif
  402. }
  403. #ifdef _WIN32
  404. script << "goto :EOF\n\n"
  405. ":ABORT\n"
  406. "set ERROR_CODE=%ERRORLEVEL%\n"
  407. "echo Batch file failed at line %FAIL_LINE% "
  408. "with errorcode %ERRORLEVEL%\n"
  409. "exit /b %ERROR_CODE%";
  410. #endif
  411. return scriptPath;
  412. }
  413. std::string cmLocalNinjaGenerator::BuildCommandLine(
  414. std::vector<std::string> const& cmdLines, std::string const& customStep,
  415. cmGeneratorTarget const* target) const
  416. {
  417. // If we have no commands but we need to build a command anyway, use noop.
  418. // This happens when building a POST_BUILD value for link targets that
  419. // don't use POST_BUILD.
  420. if (cmdLines.empty()) {
  421. return cmGlobalNinjaGenerator::SHELL_NOOP;
  422. }
  423. // If this is a custom step then we will have no '$VAR' ninja placeholders.
  424. // This means we can deal with long command sequences by writing to a script.
  425. // Do this if the command lines are on the scale of the OS limit.
  426. if (!customStep.empty()) {
  427. size_t cmdLinesTotal = 0;
  428. for (std::string const& cmd : cmdLines) {
  429. cmdLinesTotal += cmd.length() + 6;
  430. }
  431. if (cmdLinesTotal > cmSystemTools::CalculateCommandLineLengthLimit() / 2) {
  432. std::string const scriptPath =
  433. this->WriteCommandScript(cmdLines, customStep, target);
  434. std::string cmd
  435. #ifndef _WIN32
  436. = "/bin/sh "
  437. #endif
  438. ;
  439. cmd += this->ConvertToOutputFormat(
  440. this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(scriptPath),
  441. cmOutputConverter::SHELL);
  442. // Add an unused argument based on script content so that Ninja
  443. // knows when the command lines change.
  444. cmd += " ";
  445. cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
  446. cmd += hash.HashFile(scriptPath).substr(0, 16);
  447. return cmd;
  448. }
  449. }
  450. std::ostringstream cmd;
  451. for (auto li = cmdLines.begin(); li != cmdLines.end(); ++li)
  452. #ifdef _WIN32
  453. {
  454. if (li != cmdLines.begin()) {
  455. cmd << " && ";
  456. } else if (cmdLines.size() > 1) {
  457. cmd << "cmd.exe /C \"";
  458. }
  459. // Put current cmdLine in brackets if it contains "||" because it has
  460. // higher precedence than "&&" in cmd.exe
  461. if (li->find("||") != std::string::npos) {
  462. cmd << "( " << *li << " )";
  463. } else {
  464. cmd << *li;
  465. }
  466. }
  467. if (cmdLines.size() > 1) {
  468. cmd << "\"";
  469. }
  470. #else
  471. {
  472. if (li != cmdLines.begin()) {
  473. cmd << " && ";
  474. }
  475. cmd << *li;
  476. }
  477. #endif
  478. return cmd.str();
  479. }
  480. void cmLocalNinjaGenerator::AppendCustomCommandLines(
  481. cmCustomCommandGenerator const& ccg, std::vector<std::string>& cmdLines)
  482. {
  483. auto* gg = this->GetGlobalNinjaGenerator();
  484. if (ccg.GetNumberOfCommands() > 0) {
  485. std::string wd = ccg.GetWorkingDirectory();
  486. if (wd.empty()) {
  487. wd = this->GetCurrentBinaryDirectory();
  488. }
  489. std::ostringstream cdCmd;
  490. #ifdef _WIN32
  491. std::string cdStr = "cd /D ";
  492. #else
  493. std::string cdStr = "cd ";
  494. #endif
  495. cdCmd << cdStr
  496. << this->ConvertToOutputFormat(wd, cmOutputConverter::SHELL);
  497. cmdLines.push_back(cdCmd.str());
  498. }
  499. std::string launcher = this->MakeCustomLauncher(ccg);
  500. for (unsigned i = 0; i != ccg.GetNumberOfCommands(); ++i) {
  501. std::string c = ccg.GetCommand(i);
  502. if (c.empty()) {
  503. continue;
  504. }
  505. cmdLines.push_back(launcher +
  506. this->ConvertToOutputFormat(
  507. c,
  508. gg->IsMultiConfig() ? cmOutputConverter::NINJAMULTI
  509. : cmOutputConverter::SHELL));
  510. std::string& cmd = cmdLines.back();
  511. ccg.AppendArguments(i, cmd);
  512. }
  513. }
  514. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatement(
  515. cmCustomCommand const* cc, const std::set<cmGeneratorTarget*>& targets,
  516. const std::string& fileConfig)
  517. {
  518. cmGlobalNinjaGenerator* gg = this->GetGlobalNinjaGenerator();
  519. if (gg->SeenCustomCommand(cc, fileConfig)) {
  520. return;
  521. }
  522. auto ccgs = this->MakeCustomCommandGenerators(*cc, fileConfig);
  523. for (cmCustomCommandGenerator const& ccg : ccgs) {
  524. cmNinjaDeps orderOnlyDeps;
  525. // A custom command may appear on multiple targets. However, some build
  526. // systems exist where the target dependencies on some of the targets are
  527. // overspecified, leading to a dependency cycle. If we assume all target
  528. // dependencies are a superset of the true target dependencies for this
  529. // custom command, we can take the set intersection of all target
  530. // dependencies to obtain a correct dependency list.
  531. //
  532. // FIXME: This won't work in certain obscure scenarios involving indirect
  533. // dependencies.
  534. auto j = targets.begin();
  535. assert(j != targets.end());
  536. this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure(
  537. *j, orderOnlyDeps, ccg.GetOutputConfig(), fileConfig, ccgs.size() > 1);
  538. std::sort(orderOnlyDeps.begin(), orderOnlyDeps.end());
  539. ++j;
  540. for (; j != targets.end(); ++j) {
  541. std::vector<std::string> jDeps;
  542. std::vector<std::string> depsIntersection;
  543. this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure(
  544. *j, jDeps, ccg.GetOutputConfig(), fileConfig, ccgs.size() > 1);
  545. std::sort(jDeps.begin(), jDeps.end());
  546. std::set_intersection(orderOnlyDeps.begin(), orderOnlyDeps.end(),
  547. jDeps.begin(), jDeps.end(),
  548. std::back_inserter(depsIntersection));
  549. orderOnlyDeps = depsIntersection;
  550. }
  551. const std::vector<std::string>& outputs = ccg.GetOutputs();
  552. const std::vector<std::string>& byproducts = ccg.GetByproducts();
  553. bool symbolic = false;
  554. for (std::string const& output : outputs) {
  555. if (cmSourceFile* sf = this->Makefile->GetSource(output)) {
  556. if (sf->GetPropertyAsBool("SYMBOLIC")) {
  557. symbolic = true;
  558. break;
  559. }
  560. }
  561. }
  562. cmNinjaDeps ninjaOutputs(outputs.size() + byproducts.size());
  563. std::transform(outputs.begin(), outputs.end(), ninjaOutputs.begin(),
  564. gg->MapToNinjaPath());
  565. std::transform(byproducts.begin(), byproducts.end(),
  566. ninjaOutputs.begin() + outputs.size(),
  567. gg->MapToNinjaPath());
  568. for (std::string const& ninjaOutput : ninjaOutputs) {
  569. gg->SeenCustomCommandOutput(ninjaOutput);
  570. }
  571. cmNinjaDeps ninjaDeps;
  572. this->AppendCustomCommandDeps(ccg, ninjaDeps, fileConfig);
  573. std::vector<std::string> cmdLines;
  574. this->AppendCustomCommandLines(ccg, cmdLines);
  575. if (cmdLines.empty()) {
  576. cmNinjaBuild build("phony");
  577. build.Comment = "Phony custom command for " + ninjaOutputs[0];
  578. build.Outputs = std::move(ninjaOutputs);
  579. build.ExplicitDeps = std::move(ninjaDeps);
  580. build.OrderOnlyDeps = orderOnlyDeps;
  581. gg->WriteBuild(this->GetImplFileStream(fileConfig), build);
  582. } else {
  583. std::string customStep = cmSystemTools::GetFilenameName(ninjaOutputs[0]);
  584. // Hash full path to make unique.
  585. customStep += '-';
  586. cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
  587. customStep += hash.HashString(ninjaOutputs[0]).substr(0, 7);
  588. std::string depfile = cc->GetDepfile();
  589. if (!depfile.empty()) {
  590. switch (this->GetPolicyStatus(cmPolicies::CMP0116)) {
  591. case cmPolicies::WARN:
  592. if (this->GetCurrentBinaryDirectory() !=
  593. this->GetBinaryDirectory() ||
  594. this->Makefile->PolicyOptionalWarningEnabled(
  595. "CMAKE_POLICY_WARNING_CMP0116")) {
  596. this->GetCMakeInstance()->IssueMessage(
  597. MessageType::AUTHOR_WARNING,
  598. cmPolicies::GetPolicyWarning(cmPolicies::CMP0116),
  599. cc->GetBacktrace());
  600. }
  601. CM_FALLTHROUGH;
  602. case cmPolicies::OLD:
  603. break;
  604. case cmPolicies::REQUIRED_IF_USED:
  605. case cmPolicies::REQUIRED_ALWAYS:
  606. case cmPolicies::NEW:
  607. cmSystemTools::MakeDirectory(
  608. cmStrCat(this->GetBinaryDirectory(), "/CMakeFiles/d"));
  609. depfile = ccg.GetInternalDepfile();
  610. break;
  611. }
  612. }
  613. gg->WriteCustomCommandBuild(
  614. this->BuildCommandLine(cmdLines, customStep),
  615. this->ConstructComment(ccg), "Custom command for " + ninjaOutputs[0],
  616. depfile, cc->GetJobPool(), cc->GetUsesTerminal(),
  617. /*restat*/ !symbolic || !byproducts.empty(), ninjaOutputs, fileConfig,
  618. ninjaDeps, orderOnlyDeps);
  619. }
  620. }
  621. }
  622. namespace {
  623. bool HasUniqueByproducts(cmLocalGenerator& lg,
  624. std::vector<std::string> const& byproducts,
  625. cmListFileBacktrace const& bt)
  626. {
  627. std::vector<std::string> configs =
  628. lg.GetMakefile()->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  629. cmGeneratorExpression ge(bt);
  630. for (std::string const& p : byproducts) {
  631. if (cmGeneratorExpression::Find(p) == std::string::npos) {
  632. return false;
  633. }
  634. std::set<std::string> seen;
  635. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(p);
  636. for (std::string const& config : configs) {
  637. for (std::string const& b :
  638. lg.ExpandCustomCommandOutputPaths(*cge, config)) {
  639. if (!seen.insert(b).second) {
  640. return false;
  641. }
  642. }
  643. }
  644. }
  645. return true;
  646. }
  647. bool HasUniqueOutputs(std::vector<cmCustomCommandGenerator> const& ccgs)
  648. {
  649. std::set<std::string> allOutputs;
  650. std::set<std::string> allByproducts;
  651. for (cmCustomCommandGenerator const& ccg : ccgs) {
  652. for (std::string const& output : ccg.GetOutputs()) {
  653. if (!allOutputs.insert(output).second) {
  654. return false;
  655. }
  656. }
  657. for (std::string const& byproduct : ccg.GetByproducts()) {
  658. if (!allByproducts.insert(byproduct).second) {
  659. return false;
  660. }
  661. }
  662. }
  663. return true;
  664. }
  665. }
  666. std::string cmLocalNinjaGenerator::CreateUtilityOutput(
  667. std::string const& targetName, std::vector<std::string> const& byproducts,
  668. cmListFileBacktrace const& bt)
  669. {
  670. // In Ninja Multi-Config, we can only produce cross-config utility
  671. // commands if all byproducts are per-config.
  672. if (!this->GetGlobalGenerator()->IsMultiConfig() ||
  673. !HasUniqueByproducts(*this, byproducts, bt)) {
  674. return this->cmLocalGenerator::CreateUtilityOutput(targetName, byproducts,
  675. bt);
  676. }
  677. std::string const base = cmStrCat(this->GetCurrentBinaryDirectory(),
  678. "/CMakeFiles/", targetName, '-');
  679. // The output is not actually created so mark it symbolic.
  680. for (std::string const& config :
  681. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig)) {
  682. std::string const force = cmStrCat(base, config);
  683. if (cmSourceFile* sf = this->Makefile->GetOrCreateGeneratedSource(force)) {
  684. sf->SetProperty("SYMBOLIC", "1");
  685. } else {
  686. cmSystemTools::Error("Could not get source file entry for " + force);
  687. }
  688. }
  689. this->GetGlobalNinjaGenerator()->AddPerConfigUtilityTarget(targetName);
  690. return cmStrCat(base, "$<CONFIG>"_s);
  691. }
  692. std::vector<cmCustomCommandGenerator>
  693. cmLocalNinjaGenerator::MakeCustomCommandGenerators(
  694. cmCustomCommand const& cc, std::string const& fileConfig)
  695. {
  696. cmGlobalNinjaGenerator const* gg = this->GetGlobalNinjaGenerator();
  697. bool transformDepfile = false;
  698. switch (this->GetPolicyStatus(cmPolicies::CMP0116)) {
  699. case cmPolicies::OLD:
  700. case cmPolicies::WARN:
  701. break;
  702. case cmPolicies::REQUIRED_IF_USED:
  703. case cmPolicies::REQUIRED_ALWAYS:
  704. case cmPolicies::NEW:
  705. transformDepfile = true;
  706. break;
  707. }
  708. // Start with the build graph's configuration.
  709. std::vector<cmCustomCommandGenerator> ccgs;
  710. ccgs.emplace_back(cc, fileConfig, this, transformDepfile);
  711. // Consider adding cross configurations.
  712. if (!gg->EnableCrossConfigBuild()) {
  713. return ccgs;
  714. }
  715. // Outputs and byproducts must be expressed using generator expressions.
  716. for (std::string const& output : cc.GetOutputs()) {
  717. if (cmGeneratorExpression::Find(output) == std::string::npos) {
  718. return ccgs;
  719. }
  720. }
  721. for (std::string const& byproduct : cc.GetByproducts()) {
  722. if (cmGeneratorExpression::Find(byproduct) == std::string::npos) {
  723. return ccgs;
  724. }
  725. }
  726. // Tentatively add the other cross configurations.
  727. for (std::string const& config : gg->GetCrossConfigs(fileConfig)) {
  728. if (fileConfig != config) {
  729. ccgs.emplace_back(cc, fileConfig, this, transformDepfile, config);
  730. }
  731. }
  732. // If outputs and byproducts are not unique to each configuration,
  733. // drop the cross configurations.
  734. if (!HasUniqueOutputs(ccgs)) {
  735. ccgs.erase(ccgs.begin() + 1, ccgs.end());
  736. }
  737. return ccgs;
  738. }
  739. void cmLocalNinjaGenerator::AddCustomCommandTarget(cmCustomCommand const* cc,
  740. cmGeneratorTarget* target)
  741. {
  742. CustomCommandTargetMap::value_type v(cc, std::set<cmGeneratorTarget*>());
  743. std::pair<CustomCommandTargetMap::iterator, bool> ins =
  744. this->CustomCommandTargets.insert(v);
  745. if (ins.second) {
  746. this->CustomCommands.push_back(cc);
  747. }
  748. ins.first->second.insert(target);
  749. }
  750. void cmLocalNinjaGenerator::WriteCustomCommandBuildStatements(
  751. const std::string& fileConfig)
  752. {
  753. for (cmCustomCommand const* customCommand : this->CustomCommands) {
  754. auto i = this->CustomCommandTargets.find(customCommand);
  755. assert(i != this->CustomCommandTargets.end());
  756. this->WriteCustomCommandBuildStatement(i->first, i->second, fileConfig);
  757. }
  758. }
  759. std::string cmLocalNinjaGenerator::MakeCustomLauncher(
  760. cmCustomCommandGenerator const& ccg)
  761. {
  762. cmProp property_value = this->Makefile->GetProperty("RULE_LAUNCH_CUSTOM");
  763. if (!cmNonempty(property_value)) {
  764. return std::string();
  765. }
  766. // Expand rule variables referenced in the given launcher command.
  767. cmRulePlaceholderExpander::RuleVariables vars;
  768. std::string output;
  769. const std::vector<std::string>& outputs = ccg.GetOutputs();
  770. if (!outputs.empty()) {
  771. output = outputs[0];
  772. if (ccg.GetWorkingDirectory().empty()) {
  773. output = this->MaybeConvertToRelativePath(
  774. this->GetCurrentBinaryDirectory(), output);
  775. }
  776. output = this->ConvertToOutputFormat(output, cmOutputConverter::SHELL);
  777. }
  778. vars.Output = output.c_str();
  779. std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
  780. this->CreateRulePlaceholderExpander());
  781. std::string launcher = *property_value;
  782. rulePlaceholderExpander->ExpandRuleVariables(this, launcher, vars);
  783. if (!launcher.empty()) {
  784. launcher += " ";
  785. }
  786. return launcher;
  787. }
  788. void cmLocalNinjaGenerator::AdditionalCleanFiles(const std::string& config)
  789. {
  790. if (cmProp prop_value =
  791. this->Makefile->GetProperty("ADDITIONAL_CLEAN_FILES")) {
  792. std::vector<std::string> cleanFiles;
  793. {
  794. cmExpandList(cmGeneratorExpression::Evaluate(*prop_value, this, config),
  795. cleanFiles);
  796. }
  797. std::string const& binaryDir = this->GetCurrentBinaryDirectory();
  798. cmGlobalNinjaGenerator* gg = this->GetGlobalNinjaGenerator();
  799. for (std::string const& cleanFile : cleanFiles) {
  800. // Support relative paths
  801. gg->AddAdditionalCleanFile(
  802. cmSystemTools::CollapseFullPath(cleanFile, binaryDir), config);
  803. }
  804. }
  805. }