cmLocalNinjaGenerator.cxx 32 KB

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