cmLocalNinjaGenerator.cxx 31 KB

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