cmLocalNinjaGenerator.cxx 32 KB

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