cmGlobalNinjaGenerator.cxx 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  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 "cmGlobalNinjaGenerator.h"
  4. #include <algorithm>
  5. #include <cctype>
  6. #include <cstdio>
  7. #include <iterator>
  8. #include <sstream>
  9. #include <cm/memory>
  10. #include <cmext/algorithm>
  11. #include <cmext/memory>
  12. #include "cmsys/FStream.hxx"
  13. #include "cm_jsoncpp_reader.h"
  14. #include "cm_jsoncpp_value.h"
  15. #include "cm_jsoncpp_writer.h"
  16. #include "cmAlgorithms.h"
  17. #include "cmDocumentationEntry.h"
  18. #include "cmFortranParser.h"
  19. #include "cmGeneratedFileStream.h"
  20. #include "cmGeneratorExpressionEvaluationFile.h"
  21. #include "cmGeneratorTarget.h"
  22. #include "cmGlobalGenerator.h"
  23. #include "cmLinkLineComputer.h"
  24. #include "cmListFileCache.h"
  25. #include "cmLocalGenerator.h"
  26. #include "cmLocalNinjaGenerator.h"
  27. #include "cmMakefile.h"
  28. #include "cmMessageType.h"
  29. #include "cmNinjaLinkLineComputer.h"
  30. #include "cmOutputConverter.h"
  31. #include "cmRange.h"
  32. #include "cmState.h"
  33. #include "cmStateDirectory.h"
  34. #include "cmStateSnapshot.h"
  35. #include "cmStateTypes.h"
  36. #include "cmStringAlgorithms.h"
  37. #include "cmSystemTools.h"
  38. #include "cmTarget.h"
  39. #include "cmTargetDepend.h"
  40. #include "cmVersion.h"
  41. #include "cmake.h"
  42. const char* cmGlobalNinjaGenerator::NINJA_BUILD_FILE = "build.ninja";
  43. const char* cmGlobalNinjaGenerator::NINJA_RULES_FILE = "rules.ninja";
  44. const char* cmGlobalNinjaGenerator::INDENT = " ";
  45. #ifdef _WIN32
  46. std::string const cmGlobalNinjaGenerator::SHELL_NOOP = "cd .";
  47. #else
  48. std::string const cmGlobalNinjaGenerator::SHELL_NOOP = ":";
  49. #endif
  50. void cmGlobalNinjaGenerator::Indent(std::ostream& os, int count)
  51. {
  52. for (int i = 0; i < count; ++i) {
  53. os << cmGlobalNinjaGenerator::INDENT;
  54. }
  55. }
  56. void cmGlobalNinjaGenerator::WriteDivider(std::ostream& os)
  57. {
  58. os << "# ======================================"
  59. "=======================================\n";
  60. }
  61. void cmGlobalNinjaGenerator::WriteComment(std::ostream& os,
  62. const std::string& comment)
  63. {
  64. if (comment.empty()) {
  65. return;
  66. }
  67. std::string::size_type lpos = 0;
  68. std::string::size_type rpos;
  69. os << "\n#############################################\n";
  70. while ((rpos = comment.find('\n', lpos)) != std::string::npos) {
  71. os << "# " << comment.substr(lpos, rpos - lpos) << "\n";
  72. lpos = rpos + 1;
  73. }
  74. os << "# " << comment.substr(lpos) << "\n\n";
  75. }
  76. std::unique_ptr<cmLinkLineComputer>
  77. cmGlobalNinjaGenerator::CreateLinkLineComputer(
  78. cmOutputConverter* outputConverter,
  79. cmStateDirectory const& /* stateDir */) const
  80. {
  81. return std::unique_ptr<cmLinkLineComputer>(
  82. cm::make_unique<cmNinjaLinkLineComputer>(
  83. outputConverter,
  84. this->LocalGenerators[0]->GetStateSnapshot().GetDirectory(), this));
  85. }
  86. std::string cmGlobalNinjaGenerator::EncodeRuleName(std::string const& name)
  87. {
  88. // Ninja rule names must match "[a-zA-Z0-9_.-]+". Use ".xx" to encode
  89. // "." and all invalid characters as hexadecimal.
  90. std::string encoded;
  91. for (char i : name) {
  92. if (isalnum(i) || i == '_' || i == '-') {
  93. encoded += i;
  94. } else {
  95. char buf[16];
  96. sprintf(buf, ".%02x", static_cast<unsigned int>(i));
  97. encoded += buf;
  98. }
  99. }
  100. return encoded;
  101. }
  102. std::string cmGlobalNinjaGenerator::EncodeLiteral(const std::string& lit)
  103. {
  104. std::string result = lit;
  105. cmSystemTools::ReplaceString(result, "$", "$$");
  106. cmSystemTools::ReplaceString(result, "\n", "$\n");
  107. if (this->IsMultiConfig()) {
  108. cmSystemTools::ReplaceString(result,
  109. cmStrCat('$', this->GetCMakeCFGIntDir()),
  110. this->GetCMakeCFGIntDir());
  111. }
  112. return result;
  113. }
  114. std::string cmGlobalNinjaGenerator::EncodePath(const std::string& path)
  115. {
  116. std::string result = path;
  117. #ifdef _WIN32
  118. if (this->IsGCCOnWindows())
  119. std::replace(result.begin(), result.end(), '\\', '/');
  120. else
  121. std::replace(result.begin(), result.end(), '/', '\\');
  122. #endif
  123. result = EncodeLiteral(result);
  124. cmSystemTools::ReplaceString(result, " ", "$ ");
  125. cmSystemTools::ReplaceString(result, ":", "$:");
  126. return result;
  127. }
  128. void cmGlobalNinjaGenerator::WriteBuild(std::ostream& os,
  129. cmNinjaBuild const& build,
  130. int cmdLineLimit,
  131. bool* usedResponseFile)
  132. {
  133. // Make sure there is a rule.
  134. if (build.Rule.empty()) {
  135. cmSystemTools::Error("No rule for WriteBuild! called with comment: " +
  136. build.Comment);
  137. return;
  138. }
  139. // Make sure there is at least one output file.
  140. if (build.Outputs.empty()) {
  141. cmSystemTools::Error(
  142. "No output files for WriteBuild! called with comment: " + build.Comment);
  143. return;
  144. }
  145. cmGlobalNinjaGenerator::WriteComment(os, build.Comment);
  146. // Write output files.
  147. std::string buildStr("build");
  148. {
  149. // Write explicit outputs
  150. for (std::string const& output : build.Outputs) {
  151. buildStr += " " + EncodePath(output);
  152. if (this->ComputingUnknownDependencies) {
  153. this->CombinedBuildOutputs.insert(output);
  154. }
  155. }
  156. // Write implicit outputs
  157. if (!build.ImplicitOuts.empty()) {
  158. buildStr += " |";
  159. for (std::string const& implicitOut : build.ImplicitOuts) {
  160. buildStr += " " + EncodePath(implicitOut);
  161. }
  162. }
  163. buildStr += ":";
  164. // Write the rule.
  165. buildStr += " ";
  166. buildStr += build.Rule;
  167. }
  168. std::string arguments;
  169. {
  170. // TODO: Better formatting for when there are multiple input/output files.
  171. // Write explicit dependencies.
  172. for (std::string const& explicitDep : build.ExplicitDeps) {
  173. arguments += " " + EncodePath(explicitDep);
  174. }
  175. // Write implicit dependencies.
  176. if (!build.ImplicitDeps.empty()) {
  177. arguments += " |";
  178. for (std::string const& implicitDep : build.ImplicitDeps) {
  179. arguments += " " + EncodePath(implicitDep);
  180. }
  181. }
  182. // Write order-only dependencies.
  183. if (!build.OrderOnlyDeps.empty()) {
  184. arguments += " ||";
  185. for (std::string const& orderOnlyDep : build.OrderOnlyDeps) {
  186. arguments += " " + EncodePath(orderOnlyDep);
  187. }
  188. }
  189. arguments += "\n";
  190. }
  191. // Write the variables bound to this build statement.
  192. std::string assignments;
  193. {
  194. std::ostringstream variable_assignments;
  195. for (auto const& variable : build.Variables) {
  196. cmGlobalNinjaGenerator::WriteVariable(
  197. variable_assignments, variable.first, variable.second, "", 1);
  198. }
  199. // check if a response file rule should be used
  200. assignments = variable_assignments.str();
  201. bool useResponseFile = false;
  202. if (cmdLineLimit < 0 ||
  203. (cmdLineLimit > 0 &&
  204. (arguments.size() + buildStr.size() + assignments.size() + 1000) >
  205. static_cast<size_t>(cmdLineLimit))) {
  206. variable_assignments.str(std::string());
  207. cmGlobalNinjaGenerator::WriteVariable(variable_assignments, "RSP_FILE",
  208. build.RspFile, "", 1);
  209. assignments += variable_assignments.str();
  210. useResponseFile = true;
  211. }
  212. if (usedResponseFile) {
  213. *usedResponseFile = useResponseFile;
  214. }
  215. }
  216. os << buildStr << arguments << assignments << "\n";
  217. }
  218. void cmGlobalNinjaGenerator::AddCustomCommandRule()
  219. {
  220. cmNinjaRule rule("CUSTOM_COMMAND");
  221. rule.Command = "$COMMAND";
  222. rule.Description = "$DESC";
  223. rule.Comment = "Rule for running custom commands.";
  224. this->AddRule(rule);
  225. }
  226. void cmGlobalNinjaGenerator::WriteCustomCommandBuild(
  227. const std::string& command, const std::string& description,
  228. const std::string& comment, const std::string& depfile,
  229. const std::string& job_pool, bool uses_terminal, bool restat,
  230. const cmNinjaDeps& outputs, const std::string& config,
  231. const cmNinjaDeps& explicitDeps, const cmNinjaDeps& orderOnlyDeps)
  232. {
  233. this->AddCustomCommandRule();
  234. {
  235. cmNinjaBuild build("CUSTOM_COMMAND");
  236. build.Comment = comment;
  237. build.Outputs = outputs;
  238. build.ExplicitDeps = explicitDeps;
  239. build.OrderOnlyDeps = orderOnlyDeps;
  240. cmNinjaVars& vars = build.Variables;
  241. {
  242. std::string cmd = command; // NOLINT(*)
  243. #ifdef _WIN32
  244. if (cmd.empty())
  245. // TODO Shouldn't an empty command be handled by ninja?
  246. cmd = "cmd.exe /c";
  247. #endif
  248. vars["COMMAND"] = std::move(cmd);
  249. }
  250. vars["DESC"] = EncodeLiteral(description);
  251. if (restat) {
  252. vars["restat"] = "1";
  253. }
  254. if (uses_terminal && SupportsConsolePool()) {
  255. vars["pool"] = "console";
  256. } else if (!job_pool.empty()) {
  257. vars["pool"] = job_pool;
  258. }
  259. if (!depfile.empty()) {
  260. vars["depfile"] = depfile;
  261. }
  262. if (config.empty()) {
  263. this->WriteBuild(*this->GetCommonFileStream(), build);
  264. } else {
  265. this->WriteBuild(*this->GetConfigFileStream(config), build);
  266. }
  267. }
  268. if (this->ComputingUnknownDependencies) {
  269. // we need to track every dependency that comes in, since we are trying
  270. // to find dependencies that are side effects of build commands
  271. for (std::string const& dep : explicitDeps) {
  272. this->CombinedCustomCommandExplicitDependencies.insert(dep);
  273. }
  274. }
  275. }
  276. void cmGlobalNinjaGenerator::AddMacOSXContentRule()
  277. {
  278. cmNinjaRule rule("COPY_OSX_CONTENT");
  279. rule.Command = CMakeCmd() + " -E copy $in $out";
  280. rule.Description = "Copying OS X Content $out";
  281. rule.Comment = "Rule for copying OS X bundle content file.";
  282. this->AddRule(rule);
  283. }
  284. void cmGlobalNinjaGenerator::WriteMacOSXContentBuild(std::string input,
  285. std::string output,
  286. const std::string& config)
  287. {
  288. this->AddMacOSXContentRule();
  289. {
  290. cmNinjaBuild build("COPY_OSX_CONTENT");
  291. build.Outputs.push_back(std::move(output));
  292. build.ExplicitDeps.push_back(std::move(input));
  293. this->WriteBuild(*this->GetConfigFileStream(config), build);
  294. }
  295. }
  296. void cmGlobalNinjaGenerator::WriteRule(std::ostream& os,
  297. cmNinjaRule const& rule)
  298. {
  299. // -- Parameter checks
  300. // Make sure the rule has a name.
  301. if (rule.Name.empty()) {
  302. cmSystemTools::Error("No name given for WriteRule! called with comment: " +
  303. rule.Comment);
  304. return;
  305. }
  306. // Make sure a command is given.
  307. if (rule.Command.empty()) {
  308. cmSystemTools::Error(
  309. "No command given for WriteRule! called with comment: " + rule.Comment);
  310. return;
  311. }
  312. // Make sure response file content is given
  313. if (!rule.RspFile.empty() && rule.RspContent.empty()) {
  314. cmSystemTools::Error("rspfile but no rspfile_content given for WriteRule! "
  315. "called with comment: " +
  316. rule.Comment);
  317. return;
  318. }
  319. // -- Write rule
  320. // Write rule intro
  321. cmGlobalNinjaGenerator::WriteComment(os, rule.Comment);
  322. os << "rule " << rule.Name << '\n';
  323. // Write rule key/value pairs
  324. auto writeKV = [&os](const char* key, std::string const& value) {
  325. if (!value.empty()) {
  326. cmGlobalNinjaGenerator::Indent(os, 1);
  327. os << key << " = " << value << '\n';
  328. }
  329. };
  330. writeKV("depfile", rule.DepFile);
  331. writeKV("deps", rule.DepType);
  332. writeKV("command", rule.Command);
  333. writeKV("description", rule.Description);
  334. if (!rule.RspFile.empty()) {
  335. writeKV("rspfile", rule.RspFile);
  336. writeKV("rspfile_content", rule.RspContent);
  337. }
  338. writeKV("restat", rule.Restat);
  339. if (rule.Generator) {
  340. writeKV("generator", "1");
  341. }
  342. // Finish rule
  343. os << '\n';
  344. }
  345. void cmGlobalNinjaGenerator::WriteVariable(std::ostream& os,
  346. const std::string& name,
  347. const std::string& value,
  348. const std::string& comment,
  349. int indent)
  350. {
  351. // Make sure we have a name.
  352. if (name.empty()) {
  353. cmSystemTools::Error("No name given for WriteVariable! called "
  354. "with comment: " +
  355. comment);
  356. return;
  357. }
  358. // Do not add a variable if the value is empty.
  359. std::string val = cmTrimWhitespace(value);
  360. if (val.empty()) {
  361. return;
  362. }
  363. cmGlobalNinjaGenerator::WriteComment(os, comment);
  364. cmGlobalNinjaGenerator::Indent(os, indent);
  365. os << name << " = " << val << "\n";
  366. }
  367. void cmGlobalNinjaGenerator::WriteInclude(std::ostream& os,
  368. const std::string& filename,
  369. const std::string& comment)
  370. {
  371. cmGlobalNinjaGenerator::WriteComment(os, comment);
  372. os << "include " << filename << "\n";
  373. }
  374. void cmGlobalNinjaGenerator::WriteDefault(std::ostream& os,
  375. const cmNinjaDeps& targets,
  376. const std::string& comment)
  377. {
  378. cmGlobalNinjaGenerator::WriteComment(os, comment);
  379. os << "default";
  380. for (std::string const& target : targets) {
  381. os << " " << target;
  382. }
  383. os << "\n";
  384. }
  385. cmGlobalNinjaGenerator::cmGlobalNinjaGenerator(cmake* cm)
  386. : cmGlobalCommonGenerator(cm)
  387. {
  388. #ifdef _WIN32
  389. cm->GetState()->SetWindowsShell(true);
  390. #endif
  391. // // Ninja is not ported to non-Unix OS yet.
  392. // this->ForceUnixPaths = true;
  393. this->FindMakeProgramFile = "CMakeNinjaFindMake.cmake";
  394. }
  395. // Virtual public methods.
  396. std::unique_ptr<cmLocalGenerator> cmGlobalNinjaGenerator::CreateLocalGenerator(
  397. cmMakefile* mf)
  398. {
  399. return std::unique_ptr<cmLocalGenerator>(
  400. cm::make_unique<cmLocalNinjaGenerator>(this, mf));
  401. }
  402. codecvt::Encoding cmGlobalNinjaGenerator::GetMakefileEncoding() const
  403. {
  404. #ifdef _WIN32
  405. // Ninja on Windows does not support non-ANSI characters.
  406. // https://github.com/ninja-build/ninja/issues/1195
  407. return codecvt::ANSI;
  408. #else
  409. // No encoding conversion needed on other platforms.
  410. return codecvt::None;
  411. #endif
  412. }
  413. void cmGlobalNinjaGenerator::GetDocumentation(cmDocumentationEntry& entry)
  414. {
  415. entry.Name = cmGlobalNinjaGenerator::GetActualName();
  416. entry.Brief = "Generates build.ninja files.";
  417. }
  418. // Implemented in all cmGlobaleGenerator sub-classes.
  419. // Used in:
  420. // Source/cmLocalGenerator.cxx
  421. // Source/cmake.cxx
  422. void cmGlobalNinjaGenerator::Generate()
  423. {
  424. // Check minimum Ninja version.
  425. if (cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  426. this->NinjaVersion.c_str(),
  427. RequiredNinjaVersion().c_str())) {
  428. std::ostringstream msg;
  429. msg << "The detected version of Ninja (" << this->NinjaVersion;
  430. msg << ") is less than the version of Ninja required by CMake (";
  431. msg << cmGlobalNinjaGenerator::RequiredNinjaVersion() << ").";
  432. this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  433. msg.str());
  434. return;
  435. }
  436. if (!this->OpenBuildFileStreams()) {
  437. return;
  438. }
  439. if (!this->OpenRulesFileStream()) {
  440. return;
  441. }
  442. for (auto& it : this->Configs) {
  443. it.second.TargetDependsClosures.clear();
  444. }
  445. this->InitOutputPathPrefix();
  446. this->TargetAll = this->NinjaOutputPath("all");
  447. this->CMakeCacheFile = this->NinjaOutputPath("CMakeCache.txt");
  448. this->PolicyCMP0058 =
  449. this->LocalGenerators[0]->GetMakefile()->GetPolicyStatus(
  450. cmPolicies::CMP0058);
  451. this->ComputingUnknownDependencies =
  452. (this->PolicyCMP0058 == cmPolicies::OLD ||
  453. this->PolicyCMP0058 == cmPolicies::WARN);
  454. this->cmGlobalGenerator::Generate();
  455. this->WriteAssumedSourceDependencies();
  456. this->WriteTargetAliases(*this->GetCommonFileStream());
  457. this->WriteFolderTargets(*this->GetCommonFileStream());
  458. this->WriteUnknownExplicitDependencies(*this->GetCommonFileStream());
  459. this->WriteBuiltinTargets(*this->GetCommonFileStream());
  460. if (cmSystemTools::GetErrorOccuredFlag()) {
  461. this->RulesFileStream->setstate(std::ios::failbit);
  462. for (auto const& config : this->Makefiles[0]->GetGeneratorConfigs()) {
  463. this->GetConfigFileStream(config)->setstate(std::ios::failbit);
  464. }
  465. this->GetCommonFileStream()->setstate(std::ios::failbit);
  466. }
  467. this->CloseCompileCommandsStream();
  468. this->CloseRulesFileStream();
  469. this->CloseBuildFileStreams();
  470. if (!this->WriteDefaultBuildFile()) {
  471. return;
  472. }
  473. auto run_ninja_tool = [this](char const* tool) {
  474. std::vector<std::string> command;
  475. command.push_back(this->NinjaCommand);
  476. command.emplace_back("-t");
  477. command.emplace_back(tool);
  478. std::string error;
  479. if (!cmSystemTools::RunSingleCommand(command, nullptr, &error, nullptr,
  480. nullptr,
  481. cmSystemTools::OUTPUT_NONE)) {
  482. this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  483. "Running\n '" +
  484. cmJoin(command, "' '") +
  485. "'\n"
  486. "failed with:\n " +
  487. error);
  488. cmSystemTools::SetFatalErrorOccured();
  489. }
  490. };
  491. if (this->NinjaSupportsCleanDeadTool) {
  492. run_ninja_tool("cleandead");
  493. }
  494. if (this->NinjaSupportsUnconditionalRecompactTool) {
  495. run_ninja_tool("recompact");
  496. }
  497. if (this->NinjaSupportsRestatTool) {
  498. run_ninja_tool("restat");
  499. }
  500. }
  501. bool cmGlobalNinjaGenerator::FindMakeProgram(cmMakefile* mf)
  502. {
  503. if (!this->cmGlobalGenerator::FindMakeProgram(mf)) {
  504. return false;
  505. }
  506. if (const char* ninjaCommand = mf->GetDefinition("CMAKE_MAKE_PROGRAM")) {
  507. this->NinjaCommand = ninjaCommand;
  508. std::vector<std::string> command;
  509. command.push_back(this->NinjaCommand);
  510. command.emplace_back("--version");
  511. std::string version;
  512. std::string error;
  513. if (!cmSystemTools::RunSingleCommand(command, &version, &error, nullptr,
  514. nullptr,
  515. cmSystemTools::OUTPUT_NONE)) {
  516. mf->IssueMessage(MessageType::FATAL_ERROR,
  517. "Running\n '" + cmJoin(command, "' '") +
  518. "'\n"
  519. "failed with:\n " +
  520. error);
  521. cmSystemTools::SetFatalErrorOccured();
  522. return false;
  523. }
  524. this->NinjaVersion = cmTrimWhitespace(version);
  525. this->CheckNinjaFeatures();
  526. }
  527. return true;
  528. }
  529. void cmGlobalNinjaGenerator::CheckNinjaFeatures()
  530. {
  531. this->NinjaSupportsConsolePool = !cmSystemTools::VersionCompare(
  532. cmSystemTools::OP_LESS, this->NinjaVersion.c_str(),
  533. RequiredNinjaVersionForConsolePool().c_str());
  534. this->NinjaSupportsImplicitOuts = !cmSystemTools::VersionCompare(
  535. cmSystemTools::OP_LESS, this->NinjaVersion.c_str(),
  536. cmGlobalNinjaGenerator::RequiredNinjaVersionForImplicitOuts().c_str());
  537. this->NinjaSupportsManifestRestat = !cmSystemTools::VersionCompare(
  538. cmSystemTools::OP_LESS, this->NinjaVersion.c_str(),
  539. RequiredNinjaVersionForManifestRestat().c_str());
  540. this->NinjaSupportsMultilineDepfile = !cmSystemTools::VersionCompare(
  541. cmSystemTools::OP_LESS, this->NinjaVersion.c_str(),
  542. RequiredNinjaVersionForMultilineDepfile().c_str());
  543. this->NinjaSupportsDyndeps = !cmSystemTools::VersionCompare(
  544. cmSystemTools::OP_LESS, this->NinjaVersion.c_str(),
  545. RequiredNinjaVersionForDyndeps().c_str());
  546. if (!this->NinjaSupportsDyndeps) {
  547. // The ninja version number is not new enough to have upstream support.
  548. // Our ninja branch adds ".dyndep-#" to its version number,
  549. // where '#' is a feature-specific version number. Extract it.
  550. static std::string const k_DYNDEP_ = ".dyndep-";
  551. std::string::size_type pos = this->NinjaVersion.find(k_DYNDEP_);
  552. if (pos != std::string::npos) {
  553. const char* fv = &this->NinjaVersion[pos + k_DYNDEP_.size()];
  554. unsigned long dyndep = 0;
  555. cmStrToULong(fv, &dyndep);
  556. if (dyndep == 1) {
  557. this->NinjaSupportsDyndeps = true;
  558. }
  559. }
  560. }
  561. this->NinjaSupportsCleanDeadTool = !cmSystemTools::VersionCompare(
  562. cmSystemTools::OP_LESS, this->NinjaVersion.c_str(),
  563. RequiredNinjaVersionForCleanDeadTool().c_str());
  564. this->NinjaSupportsUnconditionalRecompactTool =
  565. !cmSystemTools::VersionCompare(
  566. cmSystemTools::OP_LESS, this->NinjaVersion.c_str(),
  567. RequiredNinjaVersionForUnconditionalRecompactTool().c_str());
  568. this->NinjaSupportsRestatTool = !cmSystemTools::VersionCompare(
  569. cmSystemTools::OP_LESS, this->NinjaVersion.c_str(),
  570. RequiredNinjaVersionForRestatTool().c_str());
  571. }
  572. bool cmGlobalNinjaGenerator::CheckLanguages(
  573. std::vector<std::string> const& languages, cmMakefile* mf) const
  574. {
  575. if (cmContains(languages, "Fortran")) {
  576. return this->CheckFortran(mf);
  577. }
  578. return true;
  579. }
  580. bool cmGlobalNinjaGenerator::CheckFortran(cmMakefile* mf) const
  581. {
  582. if (this->NinjaSupportsDyndeps) {
  583. return true;
  584. }
  585. std::ostringstream e;
  586. /* clang-format off */
  587. e <<
  588. "The Ninja generator does not support Fortran using Ninja version\n"
  589. " " + this->NinjaVersion + "\n"
  590. "due to lack of required features. "
  591. "Kitware has implemented the required features and they have been "
  592. "merged to upstream ninja for inclusion in Ninja 1.10 and higher. "
  593. "As of this version of CMake, Ninja 1.10 has not been released. "
  594. "Meanwhile, Kitware maintains a branch of Ninja at:\n"
  595. " https://github.com/Kitware/ninja/tree/features-for-fortran#readme\n"
  596. "with the required features. "
  597. "One may build ninja from that branch to get support for Fortran."
  598. ;
  599. /* clang-format on */
  600. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  601. cmSystemTools::SetFatalErrorOccured();
  602. return false;
  603. }
  604. void cmGlobalNinjaGenerator::EnableLanguage(
  605. std::vector<std::string> const& langs, cmMakefile* mf, bool optional)
  606. {
  607. if (this->IsMultiConfig()) {
  608. if (!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES")) {
  609. mf->AddCacheDefinition(
  610. "CMAKE_CONFIGURATION_TYPES", "Debug;Release;MinSizeRel;RelWithDebInfo",
  611. "Semicolon separated list of supported configuration types, only "
  612. "supports Debug, Release, MinSizeRel, and RelWithDebInfo, anything "
  613. "else will be ignored",
  614. cmStateEnums::STRING);
  615. }
  616. }
  617. this->cmGlobalGenerator::EnableLanguage(langs, mf, optional);
  618. for (std::string const& l : langs) {
  619. if (l == "NONE") {
  620. continue;
  621. }
  622. this->ResolveLanguageCompiler(l, mf, optional);
  623. }
  624. #ifdef _WIN32
  625. const bool clangGnuMode =
  626. ((mf->GetSafeDefinition("CMAKE_C_COMPILER_ID") == "Clang") &&
  627. (mf->GetSafeDefinition("CMAKE_C_COMPILER_FRONTEND_VARIANT") == "GNU")) ||
  628. ((mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID") == "Clang") &&
  629. (mf->GetSafeDefinition("CMAKE_CXX_COMPILER_FRONTEND_VARIANT") == "GNU"));
  630. if (clangGnuMode ||
  631. ((mf->GetSafeDefinition("CMAKE_C_SIMULATE_ID") != "MSVC") &&
  632. (mf->GetSafeDefinition("CMAKE_CXX_SIMULATE_ID") != "MSVC") &&
  633. (mf->IsOn("CMAKE_COMPILER_IS_MINGW") ||
  634. (mf->GetSafeDefinition("CMAKE_C_COMPILER_ID") == "GNU") ||
  635. (mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID") == "GNU") ||
  636. (mf->GetSafeDefinition("CMAKE_C_COMPILER_ID") == "Clang") ||
  637. (mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID") == "Clang") ||
  638. (mf->GetSafeDefinition("CMAKE_C_COMPILER_ID") == "QCC") ||
  639. (mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID") == "QCC")))) {
  640. this->UsingGCCOnWindows = true;
  641. }
  642. #endif
  643. }
  644. // Implemented by:
  645. // cmGlobalUnixMakefileGenerator3
  646. // cmGlobalGhsMultiGenerator
  647. // cmGlobalVisualStudio10Generator
  648. // cmGlobalVisualStudio7Generator
  649. // cmGlobalXCodeGenerator
  650. // Called by:
  651. // cmGlobalGenerator::Build()
  652. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  653. cmGlobalNinjaGenerator::GenerateBuildCommand(
  654. const std::string& makeProgram, const std::string& /*projectName*/,
  655. const std::string& /*projectDir*/,
  656. std::vector<std::string> const& targetNames, const std::string& config,
  657. bool /*fast*/, int jobs, bool verbose,
  658. std::vector<std::string> const& makeOptions)
  659. {
  660. GeneratedMakeCommand makeCommand;
  661. makeCommand.Add(this->SelectMakeProgram(makeProgram));
  662. if (verbose) {
  663. makeCommand.Add("-v");
  664. }
  665. if ((jobs != cmake::NO_BUILD_PARALLEL_LEVEL) &&
  666. (jobs != cmake::DEFAULT_BUILD_PARALLEL_LEVEL)) {
  667. makeCommand.Add("-j", std::to_string(jobs));
  668. }
  669. this->AppendNinjaFileArgument(makeCommand,
  670. config.empty() ? "Debug" : config);
  671. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  672. for (const auto& tname : targetNames) {
  673. if (!tname.empty()) {
  674. makeCommand.Add(tname);
  675. }
  676. }
  677. return { std::move(makeCommand) };
  678. }
  679. // Non-virtual public methods.
  680. void cmGlobalNinjaGenerator::AddRule(cmNinjaRule const& rule)
  681. {
  682. // Do not add the same rule twice.
  683. if (!this->Rules.insert(rule.Name).second) {
  684. return;
  685. }
  686. // Store command length
  687. this->RuleCmdLength[rule.Name] = static_cast<int>(rule.Command.size());
  688. // Write rule
  689. cmGlobalNinjaGenerator::WriteRule(*this->RulesFileStream, rule);
  690. }
  691. bool cmGlobalNinjaGenerator::HasRule(const std::string& name)
  692. {
  693. return (this->Rules.find(name) != this->Rules.end());
  694. }
  695. // Private virtual overrides
  696. std::string cmGlobalNinjaGenerator::GetEditCacheCommand() const
  697. {
  698. // Ninja by design does not run interactive tools in the terminal,
  699. // so our only choice is cmake-gui.
  700. return cmSystemTools::GetCMakeGUICommand();
  701. }
  702. void cmGlobalNinjaGenerator::ComputeTargetObjectDirectory(
  703. cmGeneratorTarget* gt) const
  704. {
  705. // Compute full path to object file directory for this target.
  706. std::string dir = cmStrCat(gt->LocalGenerator->GetCurrentBinaryDirectory(),
  707. '/', gt->LocalGenerator->GetTargetDirectory(gt),
  708. '/', this->GetCMakeCFGIntDir(), '/');
  709. gt->ObjectDirectory = dir;
  710. }
  711. // Private methods
  712. bool cmGlobalNinjaGenerator::OpenBuildFileStreams()
  713. {
  714. if (!this->OpenFileStream(this->BuildFileStream,
  715. cmGlobalNinjaGenerator::NINJA_BUILD_FILE)) {
  716. return false;
  717. }
  718. // Write a comment about this file.
  719. *this->BuildFileStream
  720. << "# This file contains all the build statements describing the\n"
  721. << "# compilation DAG.\n\n";
  722. return true;
  723. }
  724. bool cmGlobalNinjaGenerator::OpenFileStream(
  725. std::unique_ptr<cmGeneratedFileStream>& stream, const std::string& name)
  726. {
  727. // Get a stream where to generate things.
  728. if (!stream) {
  729. // Compute Ninja's build file path.
  730. std::string path =
  731. cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(), '/', name);
  732. stream = cm::make_unique<cmGeneratedFileStream>(
  733. path, false, this->GetMakefileEncoding());
  734. if (!(*stream)) {
  735. // An error message is generated by the constructor if it cannot
  736. // open the file.
  737. return false;
  738. }
  739. // Write the do not edit header.
  740. this->WriteDisclaimer(*stream);
  741. }
  742. return true;
  743. }
  744. void cmGlobalNinjaGenerator::CloseBuildFileStreams()
  745. {
  746. if (this->BuildFileStream) {
  747. this->BuildFileStream.reset();
  748. } else {
  749. cmSystemTools::Error("Build file stream was not open.");
  750. }
  751. }
  752. bool cmGlobalNinjaGenerator::OpenRulesFileStream()
  753. {
  754. if (!this->OpenFileStream(this->RulesFileStream,
  755. cmGlobalNinjaGenerator::NINJA_RULES_FILE)) {
  756. return false;
  757. }
  758. // Write comment about this file.
  759. /* clang-format off */
  760. *this->RulesFileStream
  761. << "# This file contains all the rules used to get the outputs files\n"
  762. << "# built from the input files.\n"
  763. << "# It is included in the main '" << NINJA_BUILD_FILE << "'.\n\n"
  764. ;
  765. /* clang-format on */
  766. return true;
  767. }
  768. void cmGlobalNinjaGenerator::CloseRulesFileStream()
  769. {
  770. if (this->RulesFileStream) {
  771. this->RulesFileStream.reset();
  772. } else {
  773. cmSystemTools::Error("Rules file stream was not open.");
  774. }
  775. }
  776. static void EnsureTrailingSlash(std::string& path)
  777. {
  778. if (path.empty()) {
  779. return;
  780. }
  781. std::string::value_type last = path.back();
  782. #ifdef _WIN32
  783. if (last != '\\') {
  784. path += '\\';
  785. }
  786. #else
  787. if (last != '/') {
  788. path += '/';
  789. }
  790. #endif
  791. }
  792. std::string const& cmGlobalNinjaGenerator::ConvertToNinjaPath(
  793. const std::string& path) const
  794. {
  795. auto const f = ConvertToNinjaPathCache.find(path);
  796. if (f != ConvertToNinjaPathCache.end()) {
  797. return f->second;
  798. }
  799. const auto& ng =
  800. cm::static_reference_cast<cmLocalNinjaGenerator>(this->LocalGenerators[0]);
  801. std::string const& bin_dir = ng.GetState()->GetBinaryDirectory();
  802. std::string convPath = ng.MaybeConvertToRelativePath(bin_dir, path);
  803. convPath = this->NinjaOutputPath(convPath);
  804. #ifdef _WIN32
  805. std::replace(convPath.begin(), convPath.end(), '/', '\\');
  806. #endif
  807. return ConvertToNinjaPathCache.emplace(path, std::move(convPath))
  808. .first->second;
  809. }
  810. void cmGlobalNinjaGenerator::AddAdditionalCleanFile(std::string fileName,
  811. const std::string& config)
  812. {
  813. this->Configs[config].AdditionalCleanFiles.emplace(std::move(fileName));
  814. }
  815. void cmGlobalNinjaGenerator::AddCXXCompileCommand(
  816. const std::string& commandLine, const std::string& sourceFile)
  817. {
  818. // Compute Ninja's build file path.
  819. std::string buildFileDir =
  820. this->GetCMakeInstance()->GetHomeOutputDirectory();
  821. if (!this->CompileCommandsStream) {
  822. std::string buildFilePath = buildFileDir + "/compile_commands.json";
  823. if (this->ComputingUnknownDependencies) {
  824. this->CombinedBuildOutputs.insert(
  825. this->NinjaOutputPath("compile_commands.json"));
  826. }
  827. // Get a stream where to generate things.
  828. this->CompileCommandsStream =
  829. cm::make_unique<cmGeneratedFileStream>(buildFilePath);
  830. *this->CompileCommandsStream << "[";
  831. } else {
  832. *this->CompileCommandsStream << "," << std::endl;
  833. }
  834. std::string sourceFileName = sourceFile;
  835. if (!cmSystemTools::FileIsFullPath(sourceFileName)) {
  836. sourceFileName = cmSystemTools::CollapseFullPath(
  837. sourceFileName, this->GetCMakeInstance()->GetHomeOutputDirectory());
  838. }
  839. /* clang-format off */
  840. *this->CompileCommandsStream << "\n{\n"
  841. << R"( "directory": ")"
  842. << cmGlobalGenerator::EscapeJSON(buildFileDir) << "\",\n"
  843. << R"( "command": ")"
  844. << cmGlobalGenerator::EscapeJSON(commandLine) << "\",\n"
  845. << R"( "file": ")"
  846. << cmGlobalGenerator::EscapeJSON(sourceFileName) << "\"\n"
  847. << "}";
  848. /* clang-format on */
  849. }
  850. void cmGlobalNinjaGenerator::CloseCompileCommandsStream()
  851. {
  852. if (this->CompileCommandsStream) {
  853. *this->CompileCommandsStream << "\n]";
  854. this->CompileCommandsStream.reset();
  855. }
  856. }
  857. void cmGlobalNinjaGenerator::WriteDisclaimer(std::ostream& os)
  858. {
  859. os << "# CMAKE generated file: DO NOT EDIT!\n"
  860. << "# Generated by \"" << this->GetName() << "\""
  861. << " Generator, CMake Version " << cmVersion::GetMajorVersion() << "."
  862. << cmVersion::GetMinorVersion() << "\n\n";
  863. }
  864. void cmGlobalNinjaGenerator::WriteAssumedSourceDependencies()
  865. {
  866. for (auto const& asd : this->AssumedSourceDependencies) {
  867. cmNinjaDeps orderOnlyDeps;
  868. std::copy(asd.second.begin(), asd.second.end(),
  869. std::back_inserter(orderOnlyDeps));
  870. WriteCustomCommandBuild(/*command=*/"", /*description=*/"",
  871. "Assume dependencies for generated source file.",
  872. /*depfile*/ "", /*job_pool*/ "",
  873. /*uses_terminal*/ false,
  874. /*restat*/ true, cmNinjaDeps(1, asd.first), "",
  875. cmNinjaDeps(), orderOnlyDeps);
  876. }
  877. }
  878. std::string cmGlobalNinjaGenerator::OrderDependsTargetForTarget(
  879. cmGeneratorTarget const* target, const std::string& config)
  880. {
  881. return "cmake_object_order_depends_target_" + target->GetName() + "_" +
  882. config;
  883. }
  884. void cmGlobalNinjaGenerator::AppendTargetOutputs(
  885. cmGeneratorTarget const* target, cmNinjaDeps& outputs,
  886. const std::string& config, cmNinjaTargetDepends depends)
  887. {
  888. // for frameworks, we want the real name, not smple name
  889. // frameworks always appear versioned, and the build.ninja
  890. // will always attempt to manage symbolic links instead
  891. // of letting cmOSXBundleGenerator do it.
  892. bool realname = target->IsFrameworkOnApple();
  893. switch (target->GetType()) {
  894. case cmStateEnums::SHARED_LIBRARY:
  895. case cmStateEnums::STATIC_LIBRARY:
  896. case cmStateEnums::MODULE_LIBRARY: {
  897. if (depends == DependOnTargetOrdering) {
  898. outputs.push_back(OrderDependsTargetForTarget(target, config));
  899. break;
  900. }
  901. }
  902. // FALLTHROUGH
  903. case cmStateEnums::EXECUTABLE: {
  904. outputs.push_back(this->ConvertToNinjaPath(target->GetFullPath(
  905. config, cmStateEnums::RuntimeBinaryArtifact, realname)));
  906. break;
  907. }
  908. case cmStateEnums::OBJECT_LIBRARY: {
  909. if (depends == DependOnTargetOrdering) {
  910. outputs.push_back(OrderDependsTargetForTarget(target, config));
  911. break;
  912. }
  913. }
  914. // FALLTHROUGH
  915. case cmStateEnums::GLOBAL_TARGET:
  916. case cmStateEnums::UTILITY: {
  917. std::string path =
  918. target->GetLocalGenerator()->GetCurrentBinaryDirectory() +
  919. std::string("/") + target->GetName();
  920. std::string output = this->ConvertToNinjaPath(path);
  921. if (target->Target->IsPerConfig()) {
  922. output = this->BuildAlias(output, config);
  923. }
  924. outputs.push_back(output);
  925. break;
  926. }
  927. default:
  928. return;
  929. }
  930. }
  931. void cmGlobalNinjaGenerator::AppendTargetDepends(
  932. cmGeneratorTarget const* target, cmNinjaDeps& outputs,
  933. const std::string& config, const std::string& fileConfig,
  934. cmNinjaTargetDepends depends)
  935. {
  936. if (target->GetType() == cmStateEnums::GLOBAL_TARGET) {
  937. // These depend only on other CMake-provided targets, e.g. "all".
  938. for (BT<std::string> const& util : target->GetUtilities()) {
  939. std::string d =
  940. target->GetLocalGenerator()->GetCurrentBinaryDirectory() + "/" +
  941. util.Value;
  942. outputs.push_back(this->BuildAlias(this->ConvertToNinjaPath(d), config));
  943. }
  944. } else {
  945. cmNinjaDeps outs;
  946. for (cmTargetDepend const& targetDep :
  947. this->GetTargetDirectDepends(target)) {
  948. if (targetDep->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  949. continue;
  950. }
  951. // For some reason, object libraries show up as "utility" dependencies
  952. // even though they're used for linking. Treat them as link dependencies.
  953. if (targetDep.IsUtil() &&
  954. targetDep->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  955. this->AppendTargetOutputs(targetDep, outs, fileConfig, depends);
  956. } else {
  957. this->AppendTargetOutputs(targetDep, outs, config, depends);
  958. }
  959. }
  960. std::sort(outs.begin(), outs.end());
  961. cm::append(outputs, outs);
  962. }
  963. }
  964. void cmGlobalNinjaGenerator::AppendTargetDependsClosure(
  965. cmGeneratorTarget const* target, cmNinjaDeps& outputs,
  966. const std::string& config)
  967. {
  968. cmNinjaOuts outs;
  969. this->AppendTargetDependsClosure(target, outs, config, true);
  970. cm::append(outputs, outs);
  971. }
  972. void cmGlobalNinjaGenerator::AppendTargetDependsClosure(
  973. cmGeneratorTarget const* target, cmNinjaOuts& outputs,
  974. const std::string& config, bool omit_self)
  975. {
  976. // try to locate the target in the cache
  977. auto find = this->Configs[config].TargetDependsClosures.lower_bound(target);
  978. if (find == this->Configs[config].TargetDependsClosures.end() ||
  979. find->first != target) {
  980. // We now calculate the closure outputs by inspecting the dependent
  981. // targets recursively.
  982. // For that we have to distinguish between a local result set that is only
  983. // relevant for filling the cache entries properly isolated and a global
  984. // result set that is relevant for the result of the top level call to
  985. // AppendTargetDependsClosure.
  986. cmNinjaOuts this_outs; // this will be the new cache entry
  987. for (auto const& dep_target : this->GetTargetDirectDepends(target)) {
  988. if (dep_target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  989. continue;
  990. }
  991. // Collect the dependent targets for _this_ target
  992. this->AppendTargetDependsClosure(dep_target, this_outs, config, false);
  993. }
  994. find = this->Configs[config].TargetDependsClosures.emplace_hint(
  995. find, target, std::move(this_outs));
  996. }
  997. // now fill the outputs of the final result from the newly generated cache
  998. // entry
  999. outputs.insert(find->second.begin(), find->second.end());
  1000. // finally generate the outputs of the target itself, if applicable
  1001. cmNinjaDeps outs;
  1002. if (!omit_self) {
  1003. this->AppendTargetOutputs(target, outs, config);
  1004. }
  1005. outputs.insert(outs.begin(), outs.end());
  1006. }
  1007. void cmGlobalNinjaGenerator::AddTargetAlias(const std::string& alias,
  1008. cmGeneratorTarget* target,
  1009. const std::string& config)
  1010. {
  1011. std::string outputPath = this->NinjaOutputPath(alias);
  1012. std::string buildAlias = this->BuildAlias(outputPath, config);
  1013. cmNinjaDeps outputs;
  1014. this->AppendTargetOutputs(target, outputs, config);
  1015. // Mark the target's outputs as ambiguous to ensure that no other target
  1016. // uses the output as an alias.
  1017. for (std::string const& output : outputs) {
  1018. this->TargetAliases[output].GeneratorTarget = nullptr;
  1019. for (const std::string& config2 :
  1020. this->Makefiles.front()->GetGeneratorConfigs()) {
  1021. this->Configs[config2].TargetAliases[output].GeneratorTarget = nullptr;
  1022. }
  1023. }
  1024. // Insert the alias into the map. If the alias was already present in the
  1025. // map and referred to another target, mark it as ambiguous.
  1026. TargetAlias ta;
  1027. ta.GeneratorTarget = target;
  1028. ta.Config = config;
  1029. std::pair<TargetAliasMap::iterator, bool> newAliasGlobal =
  1030. this->TargetAliases.insert(std::make_pair(buildAlias, ta));
  1031. if (newAliasGlobal.second &&
  1032. newAliasGlobal.first->second.GeneratorTarget != target) {
  1033. newAliasGlobal.first->second.GeneratorTarget = nullptr;
  1034. }
  1035. if (config != "all") {
  1036. std::pair<TargetAliasMap::iterator, bool> newAliasConfig =
  1037. this->Configs[config].TargetAliases.insert(
  1038. std::make_pair(outputPath, ta));
  1039. if (newAliasConfig.second &&
  1040. newAliasConfig.first->second.GeneratorTarget != target) {
  1041. newAliasConfig.first->second.GeneratorTarget = nullptr;
  1042. }
  1043. }
  1044. }
  1045. void cmGlobalNinjaGenerator::WriteTargetAliases(std::ostream& os)
  1046. {
  1047. cmGlobalNinjaGenerator::WriteDivider(os);
  1048. os << "# Target aliases.\n\n";
  1049. cmNinjaBuild build("phony");
  1050. build.Outputs.emplace_back();
  1051. for (auto const& ta : this->TargetAliases) {
  1052. // Don't write ambiguous aliases.
  1053. if (!ta.second.GeneratorTarget) {
  1054. continue;
  1055. }
  1056. // Don't write alias if there is a already a custom command with
  1057. // matching output
  1058. if (this->HasCustomCommandOutput(ta.first)) {
  1059. continue;
  1060. }
  1061. build.Outputs.front() = ta.first;
  1062. build.ExplicitDeps.clear();
  1063. if (ta.second.Config == "all") {
  1064. for (auto const& config :
  1065. ta.second.GeneratorTarget->Makefile->GetGeneratorConfigs()) {
  1066. this->AppendTargetOutputs(ta.second.GeneratorTarget,
  1067. build.ExplicitDeps, config);
  1068. }
  1069. } else {
  1070. this->AppendTargetOutputs(ta.second.GeneratorTarget, build.ExplicitDeps,
  1071. ta.second.Config);
  1072. }
  1073. this->WriteBuild(os, build);
  1074. }
  1075. if (this->IsMultiConfig()) {
  1076. for (auto const& config : this->Makefiles.front()->GetGeneratorConfigs()) {
  1077. for (auto const& ta : this->Configs[config].TargetAliases) {
  1078. // Don't write ambiguous aliases.
  1079. if (!ta.second.GeneratorTarget) {
  1080. continue;
  1081. }
  1082. // Don't write alias if there is a already a custom command with
  1083. // matching output
  1084. if (this->HasCustomCommandOutput(ta.first)) {
  1085. continue;
  1086. }
  1087. build.Outputs.front() = ta.first;
  1088. build.ExplicitDeps.clear();
  1089. this->AppendTargetOutputs(ta.second.GeneratorTarget,
  1090. build.ExplicitDeps, config);
  1091. this->WriteBuild(*this->GetConfigFileStream(config), build);
  1092. }
  1093. }
  1094. }
  1095. }
  1096. void cmGlobalNinjaGenerator::WriteFolderTargets(std::ostream& os)
  1097. {
  1098. cmGlobalNinjaGenerator::WriteDivider(os);
  1099. os << "# Folder targets.\n\n";
  1100. std::map<std::string, DirectoryTarget> dirTargets =
  1101. this->ComputeDirectoryTargets();
  1102. for (auto const& it : dirTargets) {
  1103. cmNinjaBuild build("phony");
  1104. cmGlobalNinjaGenerator::WriteDivider(os);
  1105. std::string const& currentBinaryDir = it.first;
  1106. DirectoryTarget const& dt = it.second;
  1107. std::vector<std::string> configs;
  1108. dt.LG->GetMakefile()->GetConfigurations(configs, true);
  1109. if (configs.empty()) {
  1110. configs.emplace_back();
  1111. }
  1112. // Setup target
  1113. cmNinjaDeps configDeps;
  1114. build.Comment = "Folder: " + currentBinaryDir;
  1115. build.Outputs.emplace_back();
  1116. for (auto const& config : configs) {
  1117. build.ExplicitDeps.clear();
  1118. build.Outputs.front() = this->BuildAlias(
  1119. this->ConvertToNinjaPath(currentBinaryDir + "/all"), config);
  1120. configDeps.emplace_back(build.Outputs.front());
  1121. for (DirectoryTarget::Target const& t : dt.Targets) {
  1122. if (!t.ExcludeFromAll) {
  1123. this->AppendTargetOutputs(t.GT, build.ExplicitDeps, config);
  1124. }
  1125. }
  1126. for (DirectoryTarget::Dir const& d : dt.Children) {
  1127. if (!d.ExcludeFromAll) {
  1128. build.ExplicitDeps.emplace_back(this->BuildAlias(
  1129. this->ConvertToNinjaPath(d.Path + "/all"), config));
  1130. }
  1131. }
  1132. // Write target
  1133. this->WriteBuild(os, build);
  1134. }
  1135. // Add shortcut target
  1136. if (this->IsMultiConfig()) {
  1137. for (auto const& config : configs) {
  1138. build.ExplicitDeps = { this->BuildAlias(
  1139. this->ConvertToNinjaPath(currentBinaryDir + "/all"), config) };
  1140. build.Outputs.front() =
  1141. this->ConvertToNinjaPath(currentBinaryDir + "/all");
  1142. this->WriteBuild(*this->GetConfigFileStream(config), build);
  1143. }
  1144. }
  1145. // Add target for all configs
  1146. if (this->EnableCrossConfigBuild()) {
  1147. build.ExplicitDeps.clear();
  1148. for (auto const& config : configs) {
  1149. build.ExplicitDeps.push_back(this->BuildAlias(
  1150. this->ConvertToNinjaPath(currentBinaryDir + "/all"), config));
  1151. }
  1152. build.Outputs.front() = this->BuildAlias(
  1153. this->ConvertToNinjaPath(currentBinaryDir + "/all"), "all");
  1154. this->WriteBuild(*this->GetCommonFileStream(), build);
  1155. }
  1156. }
  1157. }
  1158. void cmGlobalNinjaGenerator::WriteUnknownExplicitDependencies(std::ostream& os)
  1159. {
  1160. if (!this->ComputingUnknownDependencies) {
  1161. return;
  1162. }
  1163. // We need to collect the set of known build outputs.
  1164. // Start with those generated by WriteBuild calls.
  1165. // No other method needs this so we can take ownership
  1166. // of the set locally and throw it out when we are done.
  1167. std::set<std::string> knownDependencies;
  1168. knownDependencies.swap(this->CombinedBuildOutputs);
  1169. // now write out the unknown explicit dependencies.
  1170. // union the configured files, evaluations files and the
  1171. // CombinedBuildOutputs,
  1172. // and then difference with CombinedExplicitDependencies to find the explicit
  1173. // dependencies that we have no rule for
  1174. cmGlobalNinjaGenerator::WriteDivider(os);
  1175. /* clang-format off */
  1176. os << "# Unknown Build Time Dependencies.\n"
  1177. << "# Tell Ninja that they may appear as side effects of build rules\n"
  1178. << "# otherwise ordered by order-only dependencies.\n\n";
  1179. /* clang-format on */
  1180. // get the list of files that cmake itself has generated as a
  1181. // product of configuration.
  1182. for (const auto& lg : this->LocalGenerators) {
  1183. // get the vector of files created by this makefile and convert them
  1184. // to ninja paths, which are all relative in respect to the build directory
  1185. for (std::string const& file : lg->GetMakefile()->GetOutputFiles()) {
  1186. knownDependencies.insert(this->ConvertToNinjaPath(file));
  1187. }
  1188. if (!this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  1189. // get list files which are implicit dependencies as well and will be
  1190. // phony for rebuild manifest
  1191. for (std::string const& j : lg->GetMakefile()->GetListFiles()) {
  1192. knownDependencies.insert(this->ConvertToNinjaPath(j));
  1193. }
  1194. }
  1195. for (cmGeneratorExpressionEvaluationFile* li :
  1196. lg->GetMakefile()->GetEvaluationFiles()) {
  1197. // get all the files created by generator expressions and convert them
  1198. // to ninja paths
  1199. for (std::string const& evaluationFile : li->GetFiles()) {
  1200. knownDependencies.insert(this->ConvertToNinjaPath(evaluationFile));
  1201. }
  1202. }
  1203. }
  1204. knownDependencies.insert(this->CMakeCacheFile);
  1205. for (auto const& ta : this->TargetAliases) {
  1206. knownDependencies.insert(this->ConvertToNinjaPath(ta.first));
  1207. }
  1208. // remove all source files we know will exist.
  1209. for (auto const& i : this->AssumedSourceDependencies) {
  1210. knownDependencies.insert(this->ConvertToNinjaPath(i.first));
  1211. }
  1212. // now we difference with CombinedCustomCommandExplicitDependencies to find
  1213. // the list of items we know nothing about.
  1214. // We have encoded all the paths in CombinedCustomCommandExplicitDependencies
  1215. // and knownDependencies so no matter if unix or windows paths they
  1216. // should all match now.
  1217. std::vector<std::string> unknownExplicitDepends;
  1218. this->CombinedCustomCommandExplicitDependencies.erase(this->TargetAll);
  1219. std::set_difference(this->CombinedCustomCommandExplicitDependencies.begin(),
  1220. this->CombinedCustomCommandExplicitDependencies.end(),
  1221. knownDependencies.begin(), knownDependencies.end(),
  1222. std::back_inserter(unknownExplicitDepends));
  1223. std::vector<std::string> warnExplicitDepends;
  1224. if (!unknownExplicitDepends.empty()) {
  1225. cmake* cmk = this->GetCMakeInstance();
  1226. std::string const& buildRoot = cmk->GetHomeOutputDirectory();
  1227. bool const inSource = (buildRoot == cmk->GetHomeDirectory());
  1228. bool const warn = (!inSource && (this->PolicyCMP0058 == cmPolicies::WARN));
  1229. cmNinjaBuild build("phony");
  1230. build.Outputs.emplace_back("");
  1231. for (std::string const& ued : unknownExplicitDepends) {
  1232. // verify the file is in the build directory
  1233. std::string const absDepPath =
  1234. cmSystemTools::CollapseFullPath(ued, buildRoot);
  1235. if (cmSystemTools::IsSubDirectory(absDepPath, buildRoot)) {
  1236. // Generate phony build statement
  1237. build.Outputs[0] = ued;
  1238. this->WriteBuild(os, build);
  1239. // Add to warning on demand
  1240. if (warn && warnExplicitDepends.size() < 10) {
  1241. warnExplicitDepends.push_back(ued);
  1242. }
  1243. }
  1244. }
  1245. }
  1246. if (!warnExplicitDepends.empty()) {
  1247. std::ostringstream w;
  1248. /* clang-format off */
  1249. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0058) << "\n"
  1250. "This project specifies custom command DEPENDS on files "
  1251. "in the build tree that are not specified as the OUTPUT or "
  1252. "BYPRODUCTS of any add_custom_command or add_custom_target:\n"
  1253. " " << cmJoin(warnExplicitDepends, "\n ") <<
  1254. "\n"
  1255. "For compatibility with versions of CMake that did not have "
  1256. "the BYPRODUCTS option, CMake is generating phony rules for "
  1257. "such files to convince 'ninja' to build."
  1258. "\n"
  1259. "Project authors should add the missing BYPRODUCTS or OUTPUT "
  1260. "options to the custom commands that produce these files."
  1261. ;
  1262. /* clang-format on */
  1263. this->GetCMakeInstance()->IssueMessage(MessageType::AUTHOR_WARNING,
  1264. w.str());
  1265. }
  1266. }
  1267. void cmGlobalNinjaGenerator::WriteBuiltinTargets(std::ostream& os)
  1268. {
  1269. // Write headers.
  1270. cmGlobalNinjaGenerator::WriteDivider(os);
  1271. os << "# Built-in targets\n\n";
  1272. this->WriteTargetRebuildManifest(os);
  1273. this->WriteTargetClean(os);
  1274. this->WriteTargetHelp(os);
  1275. for (auto const& config : this->Makefiles[0]->GetGeneratorConfigs()) {
  1276. this->WriteTargetDefault(*this->GetConfigFileStream(config));
  1277. }
  1278. }
  1279. void cmGlobalNinjaGenerator::WriteTargetDefault(std::ostream& os)
  1280. {
  1281. if (!this->HasOutputPathPrefix()) {
  1282. cmNinjaDeps all;
  1283. all.push_back(this->TargetAll);
  1284. cmGlobalNinjaGenerator::WriteDefault(os, all,
  1285. "Make the all target the default.");
  1286. }
  1287. }
  1288. void cmGlobalNinjaGenerator::WriteTargetRebuildManifest(std::ostream& os)
  1289. {
  1290. if (this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  1291. return;
  1292. }
  1293. const auto& lg = this->LocalGenerators[0];
  1294. {
  1295. cmNinjaRule rule("RERUN_CMAKE");
  1296. rule.Command =
  1297. cmStrCat(CMakeCmd(), " -S",
  1298. lg->ConvertToOutputFormat(lg->GetSourceDirectory(),
  1299. cmOutputConverter::SHELL),
  1300. " -B",
  1301. lg->ConvertToOutputFormat(lg->GetBinaryDirectory(),
  1302. cmOutputConverter::SHELL));
  1303. rule.Description = "Re-running CMake...";
  1304. rule.Comment = "Rule for re-running cmake.";
  1305. rule.Generator = true;
  1306. WriteRule(*this->RulesFileStream, rule);
  1307. }
  1308. cmNinjaBuild reBuild("RERUN_CMAKE");
  1309. reBuild.Comment = "Re-run CMake if any of its inputs changed.";
  1310. this->AddRebuildManifestOutputs(reBuild.Outputs);
  1311. for (const auto& localGen : this->LocalGenerators) {
  1312. for (std::string const& fi : localGen->GetMakefile()->GetListFiles()) {
  1313. reBuild.ImplicitDeps.push_back(this->ConvertToNinjaPath(fi));
  1314. }
  1315. }
  1316. reBuild.ImplicitDeps.push_back(this->CMakeCacheFile);
  1317. // Use 'console' pool to get non buffered output of the CMake re-run call
  1318. // Available since Ninja 1.5
  1319. if (SupportsConsolePool()) {
  1320. reBuild.Variables["pool"] = "console";
  1321. }
  1322. cmake* cm = this->GetCMakeInstance();
  1323. if (this->SupportsManifestRestat() && cm->DoWriteGlobVerifyTarget()) {
  1324. {
  1325. cmNinjaRule rule("VERIFY_GLOBS");
  1326. rule.Command =
  1327. cmStrCat(CMakeCmd(), " -P ",
  1328. lg->ConvertToOutputFormat(cm->GetGlobVerifyScript(),
  1329. cmOutputConverter::SHELL));
  1330. rule.Description = "Re-checking globbed directories...";
  1331. rule.Comment = "Rule for re-checking globbed directories.";
  1332. rule.Generator = true;
  1333. this->WriteRule(*this->RulesFileStream, rule);
  1334. }
  1335. cmNinjaBuild phonyBuild("phony");
  1336. phonyBuild.Comment = "Phony target to force glob verification run.";
  1337. phonyBuild.Outputs.push_back(cm->GetGlobVerifyScript() + "_force");
  1338. this->WriteBuild(os, phonyBuild);
  1339. reBuild.Variables["restat"] = "1";
  1340. std::string const verifyScriptFile =
  1341. this->NinjaOutputPath(cm->GetGlobVerifyScript());
  1342. std::string const verifyStampFile =
  1343. this->NinjaOutputPath(cm->GetGlobVerifyStamp());
  1344. {
  1345. cmNinjaBuild vgBuild("VERIFY_GLOBS");
  1346. vgBuild.Comment =
  1347. "Re-run CMake to check if globbed directories changed.";
  1348. vgBuild.Outputs.push_back(verifyStampFile);
  1349. vgBuild.ImplicitDeps = phonyBuild.Outputs;
  1350. vgBuild.Variables = reBuild.Variables;
  1351. this->WriteBuild(os, vgBuild);
  1352. }
  1353. reBuild.Variables.erase("restat");
  1354. reBuild.ImplicitDeps.push_back(verifyScriptFile);
  1355. reBuild.ExplicitDeps.push_back(verifyStampFile);
  1356. } else if (!this->SupportsManifestRestat() &&
  1357. cm->DoWriteGlobVerifyTarget()) {
  1358. std::ostringstream msg;
  1359. msg << "The detected version of Ninja:\n"
  1360. << " " << this->NinjaVersion << "\n"
  1361. << "is less than the version of Ninja required by CMake for adding "
  1362. "restat dependencies to the build.ninja manifest regeneration "
  1363. "target:\n"
  1364. << " "
  1365. << cmGlobalNinjaGenerator::RequiredNinjaVersionForManifestRestat()
  1366. << "\n";
  1367. msg << "Any pre-check scripts, such as those generated for file(GLOB "
  1368. "CONFIGURE_DEPENDS), will not be run by Ninja.";
  1369. this->GetCMakeInstance()->IssueMessage(MessageType::AUTHOR_WARNING,
  1370. msg.str());
  1371. }
  1372. std::sort(reBuild.ImplicitDeps.begin(), reBuild.ImplicitDeps.end());
  1373. reBuild.ImplicitDeps.erase(
  1374. std::unique(reBuild.ImplicitDeps.begin(), reBuild.ImplicitDeps.end()),
  1375. reBuild.ImplicitDeps.end());
  1376. this->WriteBuild(os, reBuild);
  1377. {
  1378. cmNinjaBuild build("phony");
  1379. build.Comment = "A missing CMake input file is not an error.";
  1380. std::set_difference(std::make_move_iterator(reBuild.ImplicitDeps.begin()),
  1381. std::make_move_iterator(reBuild.ImplicitDeps.end()),
  1382. CustomCommandOutputs.begin(),
  1383. CustomCommandOutputs.end(),
  1384. std::back_inserter(build.Outputs));
  1385. this->WriteBuild(os, build);
  1386. }
  1387. }
  1388. std::string cmGlobalNinjaGenerator::CMakeCmd() const
  1389. {
  1390. const auto& lgen = this->LocalGenerators.at(0);
  1391. return lgen->ConvertToOutputFormat(cmSystemTools::GetCMakeCommand(),
  1392. cmOutputConverter::SHELL);
  1393. }
  1394. std::string cmGlobalNinjaGenerator::NinjaCmd() const
  1395. {
  1396. const auto& lgen = this->LocalGenerators[0];
  1397. if (lgen != nullptr) {
  1398. return lgen->ConvertToOutputFormat(this->NinjaCommand,
  1399. cmOutputConverter::SHELL);
  1400. }
  1401. return "ninja";
  1402. }
  1403. bool cmGlobalNinjaGenerator::SupportsConsolePool() const
  1404. {
  1405. return this->NinjaSupportsConsolePool;
  1406. }
  1407. bool cmGlobalNinjaGenerator::SupportsImplicitOuts() const
  1408. {
  1409. return this->NinjaSupportsImplicitOuts;
  1410. }
  1411. bool cmGlobalNinjaGenerator::SupportsManifestRestat() const
  1412. {
  1413. return this->NinjaSupportsManifestRestat;
  1414. }
  1415. bool cmGlobalNinjaGenerator::SupportsMultilineDepfile() const
  1416. {
  1417. return this->NinjaSupportsMultilineDepfile;
  1418. }
  1419. bool cmGlobalNinjaGenerator::WriteTargetCleanAdditional(std::ostream& os)
  1420. {
  1421. const auto& lgr = this->LocalGenerators.at(0);
  1422. std::string cleanScriptRel = "CMakeFiles/clean_additional.cmake";
  1423. std::string cleanScriptAbs =
  1424. cmStrCat(lgr->GetBinaryDirectory(), '/', cleanScriptRel);
  1425. std::vector<std::string> configs;
  1426. this->Makefiles[0]->GetConfigurations(configs, true);
  1427. if (configs.empty()) {
  1428. configs.emplace_back();
  1429. }
  1430. // Check if there are additional files to clean
  1431. bool empty = true;
  1432. for (auto const& config : configs) {
  1433. auto const it = this->Configs.find(config);
  1434. if (it != this->Configs.end() &&
  1435. !it->second.AdditionalCleanFiles.empty()) {
  1436. empty = false;
  1437. break;
  1438. }
  1439. }
  1440. if (empty) {
  1441. // Remove cmake clean script file if it exists
  1442. cmSystemTools::RemoveFile(cleanScriptAbs);
  1443. return false;
  1444. }
  1445. // Write cmake clean script file
  1446. {
  1447. cmGeneratedFileStream fout(cleanScriptAbs);
  1448. if (!fout) {
  1449. return false;
  1450. }
  1451. fout << "# Additional clean files\ncmake_minimum_required(VERSION 3.16)\n";
  1452. for (auto const& config : configs) {
  1453. auto const it = this->Configs.find(config);
  1454. if (it != this->Configs.end() &&
  1455. !it->second.AdditionalCleanFiles.empty()) {
  1456. fout << "\nif(\"${CONFIG}\" STREQUAL \"\" OR \"${CONFIG}\" STREQUAL \""
  1457. << config << "\")\n";
  1458. fout << " file(REMOVE_RECURSE\n";
  1459. for (std::string const& acf : it->second.AdditionalCleanFiles) {
  1460. fout << " "
  1461. << cmOutputConverter::EscapeForCMake(ConvertToNinjaPath(acf))
  1462. << '\n';
  1463. }
  1464. fout << " )\n";
  1465. fout << "endif()\n";
  1466. }
  1467. }
  1468. }
  1469. // Register clean script file
  1470. lgr->GetMakefile()->AddCMakeOutputFile(cleanScriptAbs);
  1471. // Write rule
  1472. {
  1473. cmNinjaRule rule("CLEAN_ADDITIONAL");
  1474. rule.Command = cmStrCat(
  1475. CMakeCmd(), " -DCONFIG=$CONFIG -P ",
  1476. lgr->ConvertToOutputFormat(this->NinjaOutputPath(cleanScriptRel),
  1477. cmOutputConverter::SHELL));
  1478. rule.Description = "Cleaning additional files...";
  1479. rule.Comment = "Rule for cleaning additional files.";
  1480. WriteRule(*this->RulesFileStream, rule);
  1481. }
  1482. // Write build
  1483. {
  1484. cmNinjaBuild build("CLEAN_ADDITIONAL");
  1485. build.Comment = "Clean additional files.";
  1486. build.Outputs.emplace_back();
  1487. for (auto const& config : configs) {
  1488. build.Outputs.front() = this->BuildAlias(
  1489. this->NinjaOutputPath(this->GetAdditionalCleanTargetName()), config);
  1490. build.Variables["CONFIG"] = config;
  1491. WriteBuild(os, build);
  1492. }
  1493. if (this->IsMultiConfig()) {
  1494. build.Outputs.front() =
  1495. this->NinjaOutputPath(this->GetAdditionalCleanTargetName());
  1496. build.Variables["CONFIG"] = "";
  1497. WriteBuild(os, build);
  1498. }
  1499. }
  1500. // Return success
  1501. return true;
  1502. }
  1503. void cmGlobalNinjaGenerator::WriteTargetClean(std::ostream& os)
  1504. {
  1505. // -- Additional clean target
  1506. bool additionalFiles = WriteTargetCleanAdditional(os);
  1507. // -- Default clean target
  1508. // Write rule
  1509. {
  1510. cmNinjaRule rule("CLEAN");
  1511. rule.Command = NinjaCmd() + " $FILE_ARG -t clean $TARGETS";
  1512. rule.Description = "Cleaning all built files...";
  1513. rule.Comment = "Rule for cleaning all built files.";
  1514. WriteRule(*this->RulesFileStream, rule);
  1515. }
  1516. auto const configs = this->Makefiles.front()->GetGeneratorConfigs();
  1517. // Write build
  1518. {
  1519. cmNinjaBuild build("CLEAN");
  1520. build.Comment = "Clean all the built files.";
  1521. build.Outputs.emplace_back();
  1522. for (auto const& config : configs) {
  1523. build.Outputs.front() = this->BuildAlias(
  1524. this->NinjaOutputPath(this->GetCleanTargetName()), config);
  1525. if (this->IsMultiConfig()) {
  1526. build.Variables["TARGETS"] =
  1527. cmStrCat(this->BuildAlias(GetByproductsForCleanTargetName(), config),
  1528. " ", GetByproductsForCleanTargetName());
  1529. }
  1530. build.ExplicitDeps.clear();
  1531. if (additionalFiles) {
  1532. build.ExplicitDeps.push_back(this->BuildAlias(
  1533. this->NinjaOutputPath(this->GetAdditionalCleanTargetName()),
  1534. config));
  1535. }
  1536. for (auto const& fileConfig : configs) {
  1537. if (fileConfig != config && !this->EnableCrossConfigBuild()) {
  1538. continue;
  1539. }
  1540. if (this->IsMultiConfig()) {
  1541. build.Variables["FILE_ARG"] = cmStrCat(
  1542. "-f ", cmGlobalNinjaMultiGenerator::GetNinjaFilename(fileConfig));
  1543. }
  1544. this->WriteBuild(*this->GetConfigFileStream(fileConfig), build);
  1545. }
  1546. }
  1547. if (this->EnableCrossConfigBuild()) {
  1548. build.Outputs.front() = this->BuildAlias(
  1549. this->NinjaOutputPath(this->GetCleanTargetName()), "all");
  1550. build.ExplicitDeps.clear();
  1551. if (additionalFiles) {
  1552. build.ExplicitDeps.push_back(
  1553. this->NinjaOutputPath(this->GetAdditionalCleanTargetName()));
  1554. }
  1555. build.Variables["TARGETS"] = "";
  1556. for (auto const& fileConfig : configs) {
  1557. build.Variables["FILE_ARG"] = cmStrCat(
  1558. "-f ", cmGlobalNinjaMultiGenerator::GetNinjaFilename(fileConfig));
  1559. this->WriteBuild(*this->GetConfigFileStream(fileConfig), build);
  1560. }
  1561. }
  1562. }
  1563. if (this->IsMultiConfig()) {
  1564. cmNinjaBuild build("phony");
  1565. build.Outputs.emplace_back(
  1566. this->NinjaOutputPath(this->GetCleanTargetName()));
  1567. build.ExplicitDeps.emplace_back();
  1568. for (auto const& config : configs) {
  1569. build.ExplicitDeps.front() = this->BuildAlias(
  1570. this->NinjaOutputPath(this->GetCleanTargetName()), config);
  1571. this->WriteBuild(*this->GetConfigFileStream(config), build);
  1572. }
  1573. }
  1574. // Write byproducts
  1575. if (this->IsMultiConfig()) {
  1576. cmNinjaBuild build("phony");
  1577. build.Comment = "Clean byproducts.";
  1578. build.Outputs.emplace_back(
  1579. this->ConvertToNinjaPath(GetByproductsForCleanTargetName()));
  1580. build.ExplicitDeps = this->ByproductsForCleanTarget;
  1581. WriteBuild(os, build);
  1582. for (auto const& config : configs) {
  1583. build.Outputs.front() = this->BuildAlias(
  1584. this->ConvertToNinjaPath(GetByproductsForCleanTargetName()), config);
  1585. build.ExplicitDeps = this->Configs[config].ByproductsForCleanTarget;
  1586. WriteBuild(os, build);
  1587. }
  1588. }
  1589. }
  1590. void cmGlobalNinjaGenerator::WriteTargetHelp(std::ostream& os)
  1591. {
  1592. {
  1593. cmNinjaRule rule("HELP");
  1594. rule.Command = NinjaCmd() + " -t targets";
  1595. rule.Description = "All primary targets available:";
  1596. rule.Comment = "Rule for printing all primary targets available.";
  1597. WriteRule(*this->RulesFileStream, rule);
  1598. }
  1599. {
  1600. cmNinjaBuild build("HELP");
  1601. build.Comment = "Print all primary targets available.";
  1602. build.Outputs.push_back(this->NinjaOutputPath("help"));
  1603. WriteBuild(os, build);
  1604. }
  1605. }
  1606. void cmGlobalNinjaGenerator::InitOutputPathPrefix()
  1607. {
  1608. this->OutputPathPrefix =
  1609. this->LocalGenerators[0]->GetMakefile()->GetSafeDefinition(
  1610. "CMAKE_NINJA_OUTPUT_PATH_PREFIX");
  1611. EnsureTrailingSlash(this->OutputPathPrefix);
  1612. }
  1613. std::string cmGlobalNinjaGenerator::NinjaOutputPath(
  1614. std::string const& path) const
  1615. {
  1616. if (!this->HasOutputPathPrefix() || cmSystemTools::FileIsFullPath(path)) {
  1617. return path;
  1618. }
  1619. return this->OutputPathPrefix + path;
  1620. }
  1621. void cmGlobalNinjaGenerator::StripNinjaOutputPathPrefixAsSuffix(
  1622. std::string& path)
  1623. {
  1624. if (path.empty()) {
  1625. return;
  1626. }
  1627. EnsureTrailingSlash(path);
  1628. cmStripSuffixIfExists(path, this->OutputPathPrefix);
  1629. }
  1630. /*
  1631. We use the following approach to support Fortran. Each target already
  1632. has a <target>.dir/ directory used to hold intermediate files for CMake.
  1633. For each target, a FortranDependInfo.json file is generated by CMake with
  1634. information about include directories, module directories, and the locations
  1635. the per-target directories for target dependencies.
  1636. Compilation of source files within a target is split into the following steps:
  1637. 1. Preprocess all sources, scan preprocessed output for module dependencies.
  1638. This step is done with independent build statements for each source,
  1639. and can therefore be done in parallel.
  1640. rule Fortran_PREPROCESS
  1641. depfile = $DEP_FILE
  1642. command = gfortran -cpp $DEFINES $INCLUDES $FLAGS -E $in -o $out &&
  1643. cmake -E cmake_ninja_depends \
  1644. --tdi=FortranDependInfo.json --pp=$out --dep=$DEP_FILE \
  1645. --obj=$OBJ_FILE --ddi=$DYNDEP_INTERMEDIATE_FILE \
  1646. --lang=Fortran
  1647. build src.f90-pp.f90 | src.f90.o.ddi: Fortran_PREPROCESS src.f90
  1648. OBJ_FILE = src.f90.o
  1649. DEP_FILE = src.f90.o.d
  1650. DYNDEP_INTERMEDIATE_FILE = src.f90.o.ddi
  1651. The ``cmake -E cmake_ninja_depends`` tool reads the preprocessed output
  1652. and generates the ninja depfile for preprocessor dependencies. It also
  1653. generates a "ddi" file (in a format private to CMake) that lists the
  1654. object file that compilation will produce along with the module names
  1655. it provides and/or requires. The "ddi" file is an implicit output
  1656. because it should not appear in "$out" but is generated by the rule.
  1657. 2. Consolidate the per-source module dependencies saved in the "ddi"
  1658. files from all sources to produce a ninja "dyndep" file, ``Fortran.dd``.
  1659. rule Fortran_DYNDEP
  1660. command = cmake -E cmake_ninja_dyndep \
  1661. --tdi=FortranDependInfo.json --lang=Fortran --dd=$out $in
  1662. build Fortran.dd: Fortran_DYNDEP src1.f90.o.ddi src2.f90.o.ddi
  1663. The ``cmake -E cmake_ninja_dyndep`` tool reads the "ddi" files from all
  1664. sources in the target and the ``FortranModules.json`` files from targets
  1665. on which the target depends. It computes dependency edges on compilations
  1666. that require modules to those that provide the modules. This information
  1667. is placed in the ``Fortran.dd`` file for ninja to load later. It also
  1668. writes the expected location of modules provided by this target into
  1669. ``FortranModules.json`` for use by dependent targets.
  1670. 3. Compile all sources after loading dynamically discovered dependencies
  1671. of the compilation build statements from their ``dyndep`` bindings.
  1672. rule Fortran_COMPILE
  1673. command = gfortran $INCLUDES $FLAGS -c $in -o $out
  1674. build src1.f90.o: Fortran_COMPILE src1.f90-pp.f90 || Fortran.dd
  1675. dyndep = Fortran.dd
  1676. The "dyndep" binding tells ninja to load dynamically discovered
  1677. dependency information from ``Fortran.dd``. This adds information
  1678. such as:
  1679. build src1.f90.o | mod1.mod: dyndep
  1680. restat = 1
  1681. This tells ninja that ``mod1.mod`` is an implicit output of compiling
  1682. the object file ``src1.f90.o``. The ``restat`` binding tells it that
  1683. the timestamp of the output may not always change. Additionally:
  1684. build src2.f90.o: dyndep | mod1.mod
  1685. This tells ninja that ``mod1.mod`` is a dependency of compiling the
  1686. object file ``src2.f90.o``. This ensures that ``src1.f90.o`` and
  1687. ``mod1.mod`` will always be up to date before ``src2.f90.o`` is built
  1688. (because the latter consumes the module).
  1689. */
  1690. struct cmSourceInfo
  1691. {
  1692. // Set of provided and required modules.
  1693. std::set<std::string> Provides;
  1694. std::set<std::string> Requires;
  1695. // Set of files included in the translation unit.
  1696. std::set<std::string> Includes;
  1697. };
  1698. static std::unique_ptr<cmSourceInfo> cmcmd_cmake_ninja_depends_fortran(
  1699. std::string const& arg_tdi, std::string const& arg_pp);
  1700. int cmcmd_cmake_ninja_depends(std::vector<std::string>::const_iterator argBeg,
  1701. std::vector<std::string>::const_iterator argEnd)
  1702. {
  1703. std::string arg_tdi;
  1704. std::string arg_pp;
  1705. std::string arg_dep;
  1706. std::string arg_obj;
  1707. std::string arg_ddi;
  1708. std::string arg_lang;
  1709. for (std::string const& arg : cmMakeRange(argBeg, argEnd)) {
  1710. if (cmHasLiteralPrefix(arg, "--tdi=")) {
  1711. arg_tdi = arg.substr(6);
  1712. } else if (cmHasLiteralPrefix(arg, "--pp=")) {
  1713. arg_pp = arg.substr(5);
  1714. } else if (cmHasLiteralPrefix(arg, "--dep=")) {
  1715. arg_dep = arg.substr(6);
  1716. } else if (cmHasLiteralPrefix(arg, "--obj=")) {
  1717. arg_obj = arg.substr(6);
  1718. } else if (cmHasLiteralPrefix(arg, "--ddi=")) {
  1719. arg_ddi = arg.substr(6);
  1720. } else if (cmHasLiteralPrefix(arg, "--lang=")) {
  1721. arg_lang = arg.substr(7);
  1722. } else {
  1723. cmSystemTools::Error("-E cmake_ninja_depends unknown argument: " + arg);
  1724. return 1;
  1725. }
  1726. }
  1727. if (arg_tdi.empty()) {
  1728. cmSystemTools::Error("-E cmake_ninja_depends requires value for --tdi=");
  1729. return 1;
  1730. }
  1731. if (arg_pp.empty()) {
  1732. cmSystemTools::Error("-E cmake_ninja_depends requires value for --pp=");
  1733. return 1;
  1734. }
  1735. if (arg_dep.empty()) {
  1736. cmSystemTools::Error("-E cmake_ninja_depends requires value for --dep=");
  1737. return 1;
  1738. }
  1739. if (arg_obj.empty()) {
  1740. cmSystemTools::Error("-E cmake_ninja_depends requires value for --obj=");
  1741. return 1;
  1742. }
  1743. if (arg_ddi.empty()) {
  1744. cmSystemTools::Error("-E cmake_ninja_depends requires value for --ddi=");
  1745. return 1;
  1746. }
  1747. if (arg_lang.empty()) {
  1748. cmSystemTools::Error("-E cmake_ninja_depends requires value for --lang=");
  1749. return 1;
  1750. }
  1751. std::unique_ptr<cmSourceInfo> info;
  1752. if (arg_lang == "Fortran") {
  1753. info = cmcmd_cmake_ninja_depends_fortran(arg_tdi, arg_pp);
  1754. } else {
  1755. cmSystemTools::Error(
  1756. cmStrCat("-E cmake_ninja_depends does not understand the ", arg_lang,
  1757. " language"));
  1758. return 1;
  1759. }
  1760. if (!info) {
  1761. // The error message is already expected to have been output.
  1762. return 1;
  1763. }
  1764. {
  1765. cmGeneratedFileStream depfile(arg_dep);
  1766. depfile << cmSystemTools::ConvertToUnixOutputPath(arg_pp) << ":";
  1767. for (std::string const& include : info->Includes) {
  1768. depfile << " \\\n " << cmSystemTools::ConvertToUnixOutputPath(include);
  1769. }
  1770. depfile << "\n";
  1771. }
  1772. Json::Value ddi(Json::objectValue);
  1773. ddi["object"] = arg_obj;
  1774. Json::Value& ddi_provides = ddi["provides"] = Json::arrayValue;
  1775. for (std::string const& provide : info->Provides) {
  1776. ddi_provides.append(provide);
  1777. }
  1778. Json::Value& ddi_requires = ddi["requires"] = Json::arrayValue;
  1779. for (std::string const& r : info->Requires) {
  1780. // Require modules not provided in the same source.
  1781. if (!info->Provides.count(r)) {
  1782. ddi_requires.append(r);
  1783. }
  1784. }
  1785. cmGeneratedFileStream ddif(arg_ddi);
  1786. ddif << ddi;
  1787. if (!ddif) {
  1788. cmSystemTools::Error("-E cmake_ninja_depends failed to write " + arg_ddi);
  1789. return 1;
  1790. }
  1791. return 0;
  1792. }
  1793. std::unique_ptr<cmSourceInfo> cmcmd_cmake_ninja_depends_fortran(
  1794. std::string const& arg_tdi, std::string const& arg_pp)
  1795. {
  1796. cmFortranCompiler fc;
  1797. std::vector<std::string> includes;
  1798. {
  1799. Json::Value tdio;
  1800. Json::Value const& tdi = tdio;
  1801. {
  1802. cmsys::ifstream tdif(arg_tdi.c_str(), std::ios::in | std::ios::binary);
  1803. Json::Reader reader;
  1804. if (!reader.parse(tdif, tdio, false)) {
  1805. cmSystemTools::Error(
  1806. cmStrCat("-E cmake_ninja_depends failed to parse ", arg_tdi,
  1807. reader.getFormattedErrorMessages()));
  1808. return nullptr;
  1809. }
  1810. }
  1811. Json::Value const& tdi_include_dirs = tdi["include-dirs"];
  1812. if (tdi_include_dirs.isArray()) {
  1813. for (auto const& tdi_include_dir : tdi_include_dirs) {
  1814. includes.push_back(tdi_include_dir.asString());
  1815. }
  1816. }
  1817. Json::Value const& tdi_compiler_id = tdi["compiler-id"];
  1818. fc.Id = tdi_compiler_id.asString();
  1819. Json::Value const& tdi_submodule_sep = tdi["submodule-sep"];
  1820. fc.SModSep = tdi_submodule_sep.asString();
  1821. Json::Value const& tdi_submodule_ext = tdi["submodule-ext"];
  1822. fc.SModExt = tdi_submodule_ext.asString();
  1823. }
  1824. cmFortranSourceInfo finfo;
  1825. std::set<std::string> defines;
  1826. cmFortranParser parser(fc, includes, defines, finfo);
  1827. if (!cmFortranParser_FilePush(&parser, arg_pp.c_str())) {
  1828. cmSystemTools::Error("-E cmake_ninja_depends failed to open " + arg_pp);
  1829. return nullptr;
  1830. }
  1831. if (cmFortran_yyparse(parser.Scanner) != 0) {
  1832. // Failed to parse the file.
  1833. return nullptr;
  1834. }
  1835. auto info = cm::make_unique<cmSourceInfo>();
  1836. info->Provides = finfo.Provides;
  1837. info->Requires = finfo.Requires;
  1838. info->Includes = finfo.Includes;
  1839. return info;
  1840. }
  1841. struct cmDyndepObjectInfo
  1842. {
  1843. std::string Object;
  1844. std::vector<std::string> Provides;
  1845. std::vector<std::string> Requires;
  1846. };
  1847. bool cmGlobalNinjaGenerator::WriteDyndepFile(
  1848. std::string const& dir_top_src, std::string const& dir_top_bld,
  1849. std::string const& dir_cur_src, std::string const& dir_cur_bld,
  1850. std::string const& arg_dd, std::vector<std::string> const& arg_ddis,
  1851. std::string const& module_dir,
  1852. std::vector<std::string> const& linked_target_dirs,
  1853. std::string const& arg_lang)
  1854. {
  1855. // Setup path conversions.
  1856. {
  1857. cmStateSnapshot snapshot = this->GetCMakeInstance()->GetCurrentSnapshot();
  1858. snapshot.GetDirectory().SetCurrentSource(dir_cur_src);
  1859. snapshot.GetDirectory().SetCurrentBinary(dir_cur_bld);
  1860. snapshot.GetDirectory().SetRelativePathTopSource(dir_top_src.c_str());
  1861. snapshot.GetDirectory().SetRelativePathTopBinary(dir_top_bld.c_str());
  1862. auto mfd = cm::make_unique<cmMakefile>(this, snapshot);
  1863. auto lgd = this->CreateLocalGenerator(mfd.get());
  1864. this->Makefiles.push_back(std::move(mfd));
  1865. this->LocalGenerators.push_back(std::move(lgd));
  1866. }
  1867. std::vector<cmDyndepObjectInfo> objects;
  1868. for (std::string const& arg_ddi : arg_ddis) {
  1869. // Load the ddi file and compute the module file paths it provides.
  1870. Json::Value ddio;
  1871. Json::Value const& ddi = ddio;
  1872. cmsys::ifstream ddif(arg_ddi.c_str(), std::ios::in | std::ios::binary);
  1873. Json::Reader reader;
  1874. if (!reader.parse(ddif, ddio, false)) {
  1875. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
  1876. arg_ddi,
  1877. reader.getFormattedErrorMessages()));
  1878. return false;
  1879. }
  1880. cmDyndepObjectInfo info;
  1881. info.Object = ddi["object"].asString();
  1882. Json::Value const& ddi_provides = ddi["provides"];
  1883. if (ddi_provides.isArray()) {
  1884. for (auto const& ddi_provide : ddi_provides) {
  1885. info.Provides.push_back(ddi_provide.asString());
  1886. }
  1887. }
  1888. Json::Value const& ddi_requires = ddi["requires"];
  1889. if (ddi_requires.isArray()) {
  1890. for (auto const& ddi_require : ddi_requires) {
  1891. info.Requires.push_back(ddi_require.asString());
  1892. }
  1893. }
  1894. objects.push_back(std::move(info));
  1895. }
  1896. // Map from module name to module file path, if known.
  1897. std::map<std::string, std::string> mod_files;
  1898. // Populate the module map with those provided by linked targets first.
  1899. for (std::string const& linked_target_dir : linked_target_dirs) {
  1900. std::string const ltmn =
  1901. cmStrCat(linked_target_dir, "/", arg_lang, "Modules.json");
  1902. Json::Value ltm;
  1903. cmsys::ifstream ltmf(ltmn.c_str(), std::ios::in | std::ios::binary);
  1904. Json::Reader reader;
  1905. if (ltmf && !reader.parse(ltmf, ltm, false)) {
  1906. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
  1907. linked_target_dir,
  1908. reader.getFormattedErrorMessages()));
  1909. return false;
  1910. }
  1911. if (ltm.isObject()) {
  1912. for (Json::Value::iterator i = ltm.begin(); i != ltm.end(); ++i) {
  1913. mod_files[i.key().asString()] = i->asString();
  1914. }
  1915. }
  1916. }
  1917. // Extend the module map with those provided by this target.
  1918. // We do this after loading the modules provided by linked targets
  1919. // in case we have one of the same name that must be preferred.
  1920. Json::Value tm = Json::objectValue;
  1921. for (cmDyndepObjectInfo const& object : objects) {
  1922. for (std::string const& p : object.Provides) {
  1923. std::string const mod = module_dir + p;
  1924. mod_files[p] = mod;
  1925. tm[p] = mod;
  1926. }
  1927. }
  1928. cmGeneratedFileStream ddf(arg_dd);
  1929. ddf << "ninja_dyndep_version = 1.0\n";
  1930. {
  1931. cmNinjaBuild build("dyndep");
  1932. build.Outputs.emplace_back("");
  1933. for (cmDyndepObjectInfo const& object : objects) {
  1934. build.Outputs[0] = object.Object;
  1935. build.ImplicitOuts.clear();
  1936. for (std::string const& p : object.Provides) {
  1937. build.ImplicitOuts.push_back(this->ConvertToNinjaPath(mod_files[p]));
  1938. }
  1939. build.ImplicitDeps.clear();
  1940. for (std::string const& r : object.Requires) {
  1941. auto mit = mod_files.find(r);
  1942. if (mit != mod_files.end()) {
  1943. build.ImplicitDeps.push_back(this->ConvertToNinjaPath(mit->second));
  1944. }
  1945. }
  1946. build.Variables.clear();
  1947. if (!object.Provides.empty()) {
  1948. build.Variables.emplace("restat", "1");
  1949. }
  1950. this->WriteBuild(ddf, build);
  1951. }
  1952. }
  1953. // Store the map of modules provided by this target in a file for
  1954. // use by dependents that reference this target in linked-target-dirs.
  1955. std::string const target_mods_file =
  1956. cmSystemTools::GetFilenamePath(arg_dd) + "/" + arg_lang + "Modules.json";
  1957. cmGeneratedFileStream tmf(target_mods_file);
  1958. tmf << tm;
  1959. return true;
  1960. }
  1961. bool cmGlobalNinjaGenerator::EnableCrossConfigBuild() const
  1962. {
  1963. return this->IsMultiConfig() &&
  1964. this->Makefiles.front()->IsOn("CMAKE_NINJA_CROSS_CONFIG_ENABLE");
  1965. }
  1966. int cmcmd_cmake_ninja_dyndep(std::vector<std::string>::const_iterator argBeg,
  1967. std::vector<std::string>::const_iterator argEnd)
  1968. {
  1969. std::vector<std::string> arg_full =
  1970. cmSystemTools::HandleResponseFile(argBeg, argEnd);
  1971. std::string arg_dd;
  1972. std::string arg_lang;
  1973. std::string arg_tdi;
  1974. std::vector<std::string> arg_ddis;
  1975. for (std::string const& arg : arg_full) {
  1976. if (cmHasLiteralPrefix(arg, "--tdi=")) {
  1977. arg_tdi = arg.substr(6);
  1978. } else if (cmHasLiteralPrefix(arg, "--lang=")) {
  1979. arg_lang = arg.substr(7);
  1980. } else if (cmHasLiteralPrefix(arg, "--dd=")) {
  1981. arg_dd = arg.substr(5);
  1982. } else if (!cmHasLiteralPrefix(arg, "--") &&
  1983. cmHasLiteralSuffix(arg, ".ddi")) {
  1984. arg_ddis.push_back(arg);
  1985. } else {
  1986. cmSystemTools::Error("-E cmake_ninja_dyndep unknown argument: " + arg);
  1987. return 1;
  1988. }
  1989. }
  1990. if (arg_tdi.empty()) {
  1991. cmSystemTools::Error("-E cmake_ninja_dyndep requires value for --tdi=");
  1992. return 1;
  1993. }
  1994. if (arg_lang.empty()) {
  1995. cmSystemTools::Error("-E cmake_ninja_dyndep requires value for --lang=");
  1996. return 1;
  1997. }
  1998. if (arg_dd.empty()) {
  1999. cmSystemTools::Error("-E cmake_ninja_dyndep requires value for --dd=");
  2000. return 1;
  2001. }
  2002. Json::Value tdio;
  2003. Json::Value const& tdi = tdio;
  2004. {
  2005. cmsys::ifstream tdif(arg_tdi.c_str(), std::ios::in | std::ios::binary);
  2006. Json::Reader reader;
  2007. if (!reader.parse(tdif, tdio, false)) {
  2008. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
  2009. arg_tdi,
  2010. reader.getFormattedErrorMessages()));
  2011. return 1;
  2012. }
  2013. }
  2014. std::string const dir_cur_bld = tdi["dir-cur-bld"].asString();
  2015. std::string const dir_cur_src = tdi["dir-cur-src"].asString();
  2016. std::string const dir_top_bld = tdi["dir-top-bld"].asString();
  2017. std::string const dir_top_src = tdi["dir-top-src"].asString();
  2018. std::string module_dir = tdi["module-dir"].asString();
  2019. if (!module_dir.empty() && !cmHasLiteralSuffix(module_dir, "/")) {
  2020. module_dir += "/";
  2021. }
  2022. std::vector<std::string> linked_target_dirs;
  2023. Json::Value const& tdi_linked_target_dirs = tdi["linked-target-dirs"];
  2024. if (tdi_linked_target_dirs.isArray()) {
  2025. for (auto const& tdi_linked_target_dir : tdi_linked_target_dirs) {
  2026. linked_target_dirs.push_back(tdi_linked_target_dir.asString());
  2027. }
  2028. }
  2029. cmake cm(cmake::RoleInternal, cmState::Unknown);
  2030. cm.SetHomeDirectory(dir_top_src);
  2031. cm.SetHomeOutputDirectory(dir_top_bld);
  2032. auto ggd = cm.CreateGlobalGenerator("Ninja");
  2033. if (!ggd ||
  2034. !cm::static_reference_cast<cmGlobalNinjaGenerator>(ggd).WriteDyndepFile(
  2035. dir_top_src, dir_top_bld, dir_cur_src, dir_cur_bld, arg_dd, arg_ddis,
  2036. module_dir, linked_target_dirs, arg_lang)) {
  2037. return 1;
  2038. }
  2039. return 0;
  2040. }
  2041. void cmGlobalNinjaGenerator::AppendDirectoryForConfig(
  2042. const std::string& prefix, const std::string& config,
  2043. const std::string& suffix, std::string& dir)
  2044. {
  2045. if (!config.empty() && this->IsMultiConfig()) {
  2046. dir += prefix;
  2047. dir += config;
  2048. dir += suffix;
  2049. }
  2050. }
  2051. const char* cmGlobalNinjaMultiGenerator::NINJA_COMMON_FILE = "common.ninja";
  2052. const char* cmGlobalNinjaMultiGenerator::NINJA_FILE_EXTENSION = ".ninja";
  2053. cmGlobalNinjaMultiGenerator::cmGlobalNinjaMultiGenerator(cmake* cm)
  2054. : cmGlobalNinjaGenerator(cm)
  2055. {
  2056. cm->GetState()->SetIsGeneratorMultiConfig(true);
  2057. cm->GetState()->SetNinjaMulti(true);
  2058. }
  2059. void cmGlobalNinjaMultiGenerator::GetDocumentation(cmDocumentationEntry& entry)
  2060. {
  2061. entry.Name = cmGlobalNinjaMultiGenerator::GetActualName();
  2062. entry.Brief = "Generates build-<Config>.ninja files.";
  2063. }
  2064. std::string cmGlobalNinjaMultiGenerator::ExpandCFGIntDir(
  2065. const std::string& str, const std::string& config) const
  2066. {
  2067. std::string result = str;
  2068. cmSystemTools::ReplaceString(result, this->GetCMakeCFGIntDir(), config);
  2069. return result;
  2070. }
  2071. bool cmGlobalNinjaMultiGenerator::OpenBuildFileStreams()
  2072. {
  2073. if (!this->OpenFileStream(this->CommonFileStream,
  2074. cmGlobalNinjaMultiGenerator::NINJA_COMMON_FILE)) {
  2075. return false;
  2076. }
  2077. // Write a comment about this file.
  2078. *this->CommonFileStream
  2079. << "# This file contains build statements common to all "
  2080. "configurations.\n\n";
  2081. for (auto const& config : this->Makefiles[0]->GetGeneratorConfigs()) {
  2082. if (!this->OpenFileStream(this->ConfigFileStreams[config],
  2083. GetNinjaFilename(config))) {
  2084. return false;
  2085. }
  2086. // Write a comment about this file.
  2087. *this->ConfigFileStreams[config]
  2088. << "# This file contains build statements specific to the \"" << config
  2089. << "\"\n# configuration.\n\n";
  2090. }
  2091. return true;
  2092. }
  2093. void cmGlobalNinjaMultiGenerator::CloseBuildFileStreams()
  2094. {
  2095. if (this->CommonFileStream) {
  2096. this->CommonFileStream.reset();
  2097. } else {
  2098. cmSystemTools::Error("Common file stream was not open.");
  2099. }
  2100. for (auto const& config : this->Makefiles[0]->GetGeneratorConfigs()) {
  2101. if (this->ConfigFileStreams[config]) {
  2102. this->ConfigFileStreams[config].reset();
  2103. } else {
  2104. cmSystemTools::Error(
  2105. cmStrCat("Config file stream for \"", config, "\" was not open."));
  2106. }
  2107. }
  2108. }
  2109. void cmGlobalNinjaMultiGenerator::AppendNinjaFileArgument(
  2110. GeneratedMakeCommand& command, const std::string& config) const
  2111. {
  2112. command.Add("-f");
  2113. command.Add(GetNinjaFilename(config));
  2114. }
  2115. std::string cmGlobalNinjaMultiGenerator::GetNinjaFilename(
  2116. const std::string& config)
  2117. {
  2118. return cmStrCat("build-", config,
  2119. cmGlobalNinjaMultiGenerator::NINJA_FILE_EXTENSION);
  2120. }
  2121. void cmGlobalNinjaMultiGenerator::AddRebuildManifestOutputs(
  2122. cmNinjaDeps& outputs) const
  2123. {
  2124. for (auto const& config : this->Makefiles.front()->GetGeneratorConfigs()) {
  2125. outputs.push_back(this->NinjaOutputPath(GetNinjaFilename(config)));
  2126. }
  2127. if (this->Makefiles.front()->GetDefinition(
  2128. "CMAKE_NINJA_MULTI_DEFAULT_BUILD_TYPE")) {
  2129. outputs.push_back(this->NinjaOutputPath(NINJA_BUILD_FILE));
  2130. }
  2131. }
  2132. void cmGlobalNinjaMultiGenerator::GetQtAutoGenConfigs(
  2133. std::vector<std::string>& configs) const
  2134. {
  2135. auto const oldSize = configs.size();
  2136. this->Makefiles.front()->GetConfigurations(configs);
  2137. if (configs.size() == oldSize) {
  2138. configs.emplace_back();
  2139. }
  2140. }
  2141. bool cmGlobalNinjaMultiGenerator::WriteDefaultBuildFile()
  2142. {
  2143. auto const* defaultConfig = this->Makefiles.front()->GetDefinition(
  2144. "CMAKE_NINJA_MULTI_DEFAULT_BUILD_TYPE");
  2145. if (defaultConfig) {
  2146. std::unique_ptr<cmGeneratedFileStream> defaultStream;
  2147. if (!this->OpenFileStream(defaultStream, NINJA_BUILD_FILE)) {
  2148. return false;
  2149. }
  2150. *defaultStream << "# This file is a convenience file generated by\n"
  2151. << "# CMAKE_NINJA_MULTI_DEFAULT_BUILD_TYPE.\n\n"
  2152. << "include " << this->GetNinjaFilename(defaultConfig)
  2153. << "\n";
  2154. }
  2155. return true;
  2156. }