cmLocalNinjaGenerator.cxx 31 KB

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