cmGlobalNinjaGenerator.cxx 91 KB

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