cmGlobalNinjaGenerator.cxx 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmGlobalNinjaGenerator.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cctype>
  7. #include <cstdio>
  8. #include <functional>
  9. #include <iterator>
  10. #include <sstream>
  11. #include <type_traits>
  12. #include <utility>
  13. #include <cm/memory>
  14. #include <cm/optional>
  15. #include <cm/string_view>
  16. #include <cmext/algorithm>
  17. #include <cmext/memory>
  18. #include <cmext/string_view>
  19. #include <cm3p/json/reader.h>
  20. #include <cm3p/json/value.h>
  21. #include <cm3p/json/writer.h>
  22. #include "cmsys/FStream.hxx"
  23. #include "cmCustomCommand.h"
  24. #include "cmCxxModuleMapper.h"
  25. #include "cmDyndepCollation.h"
  26. #include "cmFortranParser.h"
  27. #include "cmGeneratedFileStream.h"
  28. #include "cmGeneratorTarget.h"
  29. #include "cmGlobalGenerator.h"
  30. #include "cmInstrumentation.h"
  31. #include "cmLinkLineComputer.h"
  32. #include "cmList.h"
  33. #include "cmListFileCache.h"
  34. #include "cmLocalGenerator.h"
  35. #include "cmLocalNinjaGenerator.h"
  36. #include "cmMakefile.h"
  37. #include "cmMessageType.h"
  38. #include "cmNinjaLinkLineComputer.h"
  39. #include "cmOutputConverter.h"
  40. #include "cmRange.h"
  41. #include "cmScanDepFormat.h"
  42. #include "cmSourceFile.h"
  43. #include "cmState.h"
  44. #include "cmStateDirectory.h"
  45. #include "cmStateSnapshot.h"
  46. #include "cmStateTypes.h"
  47. #include "cmStringAlgorithms.h"
  48. #include "cmSystemTools.h"
  49. #include "cmTarget.h"
  50. #include "cmTargetDepend.h"
  51. #include "cmValue.h"
  52. #include "cmVersion.h"
  53. #include "cmake.h"
  54. char const* cmGlobalNinjaGenerator::NINJA_BUILD_FILE = "build.ninja";
  55. char const* cmGlobalNinjaGenerator::NINJA_RULES_FILE =
  56. "CMakeFiles/rules.ninja";
  57. char const* cmGlobalNinjaGenerator::INDENT = " ";
  58. #ifdef _WIN32
  59. std::string const cmGlobalNinjaGenerator::SHELL_NOOP = "cd .";
  60. #else
  61. std::string const cmGlobalNinjaGenerator::SHELL_NOOP = ":";
  62. #endif
  63. namespace {
  64. #ifdef _WIN32
  65. bool DetectGCCOnWindows(cm::string_view compilerId, cm::string_view simulateId,
  66. cm::string_view compilerFrontendVariant)
  67. {
  68. return ((compilerId == "Clang"_s && compilerFrontendVariant == "GNU"_s) ||
  69. (simulateId != "MSVC"_s &&
  70. (compilerId == "GNU"_s || compilerId == "QCC"_s ||
  71. cmHasLiteralSuffix(compilerId, "Clang"))));
  72. }
  73. #endif
  74. }
  75. bool operator==(
  76. cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey const& lhs,
  77. cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey const& rhs)
  78. {
  79. return lhs.Target == rhs.Target && lhs.Config == rhs.Config &&
  80. lhs.GenexOutput == rhs.GenexOutput;
  81. }
  82. bool operator!=(
  83. cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey const& lhs,
  84. cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey const& rhs)
  85. {
  86. return !(lhs == rhs);
  87. }
  88. bool operator<(
  89. cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey const& lhs,
  90. cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey const& rhs)
  91. {
  92. return lhs.Target < rhs.Target ||
  93. (lhs.Target == rhs.Target &&
  94. (lhs.Config < rhs.Config ||
  95. (lhs.Config == rhs.Config && lhs.GenexOutput < rhs.GenexOutput)));
  96. }
  97. bool operator>(
  98. cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey const& lhs,
  99. cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey const& rhs)
  100. {
  101. return rhs < lhs;
  102. }
  103. bool operator<=(
  104. cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey const& lhs,
  105. cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey const& rhs)
  106. {
  107. return !(lhs > rhs);
  108. }
  109. bool operator>=(
  110. cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey const& lhs,
  111. cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey const& rhs)
  112. {
  113. return rhs <= lhs;
  114. }
  115. void cmGlobalNinjaGenerator::Indent(std::ostream& os, int count)
  116. {
  117. for (int i = 0; i < count; ++i) {
  118. os << cmGlobalNinjaGenerator::INDENT;
  119. }
  120. }
  121. void cmGlobalNinjaGenerator::WriteDivider(std::ostream& os)
  122. {
  123. os << "# ======================================"
  124. "=======================================\n";
  125. }
  126. void cmGlobalNinjaGenerator::WriteComment(std::ostream& os,
  127. std::string const& comment)
  128. {
  129. if (comment.empty()) {
  130. return;
  131. }
  132. std::string::size_type lpos = 0;
  133. std::string::size_type rpos;
  134. os << "\n#############################################\n";
  135. while ((rpos = comment.find('\n', lpos)) != std::string::npos) {
  136. os << "# " << comment.substr(lpos, rpos - lpos) << "\n";
  137. lpos = rpos + 1;
  138. }
  139. os << "# " << comment.substr(lpos) << "\n\n";
  140. }
  141. std::unique_ptr<cmLinkLineComputer>
  142. cmGlobalNinjaGenerator::CreateLinkLineComputer(
  143. cmOutputConverter* outputConverter,
  144. cmStateDirectory const& /* stateDir */) const
  145. {
  146. return std::unique_ptr<cmLinkLineComputer>(
  147. cm::make_unique<cmNinjaLinkLineComputer>(
  148. outputConverter,
  149. this->LocalGenerators[0]->GetStateSnapshot().GetDirectory(), this));
  150. }
  151. std::string cmGlobalNinjaGenerator::EncodeRuleName(std::string const& name)
  152. {
  153. // Ninja rule names must match "[a-zA-Z0-9_.-]+". Use ".xx" to encode
  154. // "." and all invalid characters as hexadecimal.
  155. std::string encoded;
  156. for (char i : name) {
  157. if (isalnum(i) || i == '_' || i == '-') {
  158. encoded += i;
  159. } else {
  160. char buf[16];
  161. snprintf(buf, sizeof(buf), ".%02x", static_cast<unsigned int>(i));
  162. encoded += buf;
  163. }
  164. }
  165. return encoded;
  166. }
  167. std::string& cmGlobalNinjaGenerator::EncodeLiteral(std::string& lit)
  168. {
  169. cmSystemTools::ReplaceString(lit, "$", "$$");
  170. cmSystemTools::ReplaceString(lit, "\n", "$\n");
  171. if (this->IsMultiConfig()) {
  172. cmSystemTools::ReplaceString(lit, cmStrCat('$', this->GetCMakeCFGIntDir()),
  173. this->GetCMakeCFGIntDir());
  174. }
  175. return lit;
  176. }
  177. std::string cmGlobalNinjaGenerator::EncodePath(std::string const& path)
  178. {
  179. std::string result = path;
  180. #ifdef _WIN32
  181. if (this->IsGCCOnWindows())
  182. std::replace(result.begin(), result.end(), '\\', '/');
  183. else
  184. std::replace(result.begin(), result.end(), '/', '\\');
  185. #endif
  186. this->EncodeLiteral(result);
  187. cmSystemTools::ReplaceString(result, " ", "$ ");
  188. cmSystemTools::ReplaceString(result, ":", "$:");
  189. return result;
  190. }
  191. void cmGlobalNinjaGenerator::WriteBuild(std::ostream& os,
  192. cmNinjaBuild const& build,
  193. int cmdLineLimit,
  194. bool* usedResponseFile)
  195. {
  196. // Make sure there is a rule.
  197. if (build.Rule.empty()) {
  198. cmSystemTools::Error(cmStrCat(
  199. "No rule for WriteBuild! called with comment: ", build.Comment));
  200. return;
  201. }
  202. // Make sure there is at least one output file.
  203. if (build.Outputs.empty()) {
  204. cmSystemTools::Error(cmStrCat(
  205. "No output files for WriteBuild! called with comment: ", build.Comment));
  206. return;
  207. }
  208. cmGlobalNinjaGenerator::WriteComment(os, build.Comment);
  209. // Write output files.
  210. std::string buildStr("build");
  211. {
  212. // Write explicit outputs
  213. for (std::string const& output : build.Outputs) {
  214. buildStr = cmStrCat(buildStr, ' ', this->EncodePath(output));
  215. }
  216. // Write implicit outputs
  217. if (!build.ImplicitOuts.empty()) {
  218. // Assume Ninja is new enough to support implicit outputs.
  219. // Callers should not populate this field otherwise.
  220. buildStr = cmStrCat(buildStr, " |");
  221. for (std::string const& implicitOut : build.ImplicitOuts) {
  222. buildStr = cmStrCat(buildStr, ' ', this->EncodePath(implicitOut));
  223. }
  224. }
  225. // Repeat some outputs, but expressed as absolute paths.
  226. // This helps Ninja handle absolute paths found in a depfile.
  227. // FIXME: Unfortunately this causes Ninja to stat the file twice.
  228. // We could avoid this if Ninja Issue 1251 were fixed.
  229. if (!build.WorkDirOuts.empty()) {
  230. if (this->SupportsImplicitOuts() && build.ImplicitOuts.empty()) {
  231. // Make them implicit outputs if supported by this version of Ninja.
  232. buildStr = cmStrCat(buildStr, " |");
  233. }
  234. for (std::string const& workdirOut : build.WorkDirOuts) {
  235. buildStr = cmStrCat(buildStr, " ${cmake_ninja_workdir}",
  236. this->EncodePath(workdirOut));
  237. }
  238. }
  239. // Write the rule.
  240. buildStr = cmStrCat(buildStr, ": ", build.Rule);
  241. }
  242. std::string arguments;
  243. {
  244. // TODO: Better formatting for when there are multiple input/output files.
  245. // Write explicit dependencies.
  246. for (std::string const& explicitDep : build.ExplicitDeps) {
  247. arguments += cmStrCat(' ', this->EncodePath(explicitDep));
  248. }
  249. // Write implicit dependencies.
  250. if (!build.ImplicitDeps.empty()) {
  251. arguments += " |";
  252. for (std::string const& implicitDep : build.ImplicitDeps) {
  253. arguments += cmStrCat(' ', this->EncodePath(implicitDep));
  254. }
  255. }
  256. // Write order-only dependencies.
  257. if (!build.OrderOnlyDeps.empty()) {
  258. arguments += " ||";
  259. for (std::string const& orderOnlyDep : build.OrderOnlyDeps) {
  260. arguments += cmStrCat(' ', this->EncodePath(orderOnlyDep));
  261. }
  262. }
  263. arguments += '\n';
  264. }
  265. // Write the variables bound to this build statement.
  266. std::string assignments;
  267. {
  268. std::ostringstream variable_assignments;
  269. for (auto const& variable : build.Variables) {
  270. cmGlobalNinjaGenerator::WriteVariable(
  271. variable_assignments, variable.first, variable.second, "", 1);
  272. }
  273. // check if a response file rule should be used
  274. assignments = variable_assignments.str();
  275. bool useResponseFile = false;
  276. if (cmdLineLimit < 0 ||
  277. (cmdLineLimit > 0 &&
  278. (arguments.size() + buildStr.size() + assignments.size() + 1000) >
  279. static_cast<size_t>(cmdLineLimit))) {
  280. variable_assignments.str(std::string());
  281. cmGlobalNinjaGenerator::WriteVariable(variable_assignments, "RSP_FILE",
  282. build.RspFile, "", 1);
  283. assignments += variable_assignments.str();
  284. useResponseFile = true;
  285. }
  286. if (usedResponseFile) {
  287. *usedResponseFile = useResponseFile;
  288. }
  289. }
  290. os << buildStr << arguments << assignments << "\n";
  291. }
  292. void cmGlobalNinjaGenerator::AddCustomCommandRule()
  293. {
  294. cmNinjaRule rule("CUSTOM_COMMAND");
  295. rule.Command = "$COMMAND";
  296. rule.Description = "$DESC";
  297. rule.Comment = "Rule for running custom commands.";
  298. this->AddRule(rule);
  299. }
  300. void cmGlobalNinjaGenerator::CCOutputs::Add(
  301. std::vector<std::string> const& paths)
  302. {
  303. for (std::string const& path : paths) {
  304. std::string out = this->GG->ConvertToNinjaPath(path);
  305. if (!cmSystemTools::FileIsFullPath(out)) {
  306. // This output is expressed as a relative path. Repeat it,
  307. // but expressed as an absolute path for Ninja Issue 1251.
  308. this->WorkDirOuts.emplace_back(out);
  309. this->GG->SeenCustomCommandOutput(this->GG->ConvertToNinjaAbsPath(path));
  310. }
  311. this->GG->SeenCustomCommandOutput(out);
  312. this->ExplicitOuts.emplace_back(std::move(out));
  313. }
  314. }
  315. void cmGlobalNinjaGenerator::WriteCustomCommandBuild(
  316. std::string const& command, std::string const& description,
  317. std::string const& comment, std::string const& depfile,
  318. std::string const& job_pool, bool uses_terminal, bool restat,
  319. std::string const& config, CCOutputs outputs, cmNinjaDeps explicitDeps,
  320. cmNinjaDeps orderOnlyDeps)
  321. {
  322. this->AddCustomCommandRule();
  323. {
  324. std::string ninjaDepfilePath;
  325. bool depfileIsOutput = false;
  326. if (!depfile.empty()) {
  327. ninjaDepfilePath = this->ConvertToNinjaPath(depfile);
  328. depfileIsOutput =
  329. std::find(outputs.ExplicitOuts.begin(), outputs.ExplicitOuts.end(),
  330. ninjaDepfilePath) != outputs.ExplicitOuts.end();
  331. }
  332. cmNinjaBuild build("CUSTOM_COMMAND");
  333. build.Comment = comment;
  334. build.Outputs = std::move(outputs.ExplicitOuts);
  335. build.WorkDirOuts = std::move(outputs.WorkDirOuts);
  336. build.ExplicitDeps = std::move(explicitDeps);
  337. build.OrderOnlyDeps = std::move(orderOnlyDeps);
  338. cmNinjaVars& vars = build.Variables;
  339. {
  340. std::string cmd = command; // NOLINT(*)
  341. #ifdef _WIN32
  342. if (cmd.empty())
  343. cmd = "cmd.exe /c";
  344. #endif
  345. vars["COMMAND"] = std::move(cmd);
  346. }
  347. vars["DESC"] = this->GetEncodedLiteral(description);
  348. if (restat) {
  349. vars["restat"] = "1";
  350. }
  351. if (uses_terminal && this->SupportsDirectConsole()) {
  352. vars["pool"] = "console";
  353. } else if (!job_pool.empty()) {
  354. vars["pool"] = job_pool;
  355. }
  356. if (!depfile.empty()) {
  357. vars["depfile"] = ninjaDepfilePath;
  358. // Add the depfile to the `.ninja_deps` database. Since this (generally)
  359. // removes the file, it cannot be declared as an output or byproduct of
  360. // the command.
  361. if (!depfileIsOutput) {
  362. vars["deps"] = "gcc";
  363. }
  364. }
  365. if (config.empty()) {
  366. this->WriteBuild(*this->GetCommonFileStream(), build);
  367. } else {
  368. this->WriteBuild(*this->GetImplFileStream(config), build);
  369. }
  370. }
  371. }
  372. void cmGlobalNinjaGenerator::AddMacOSXContentRule()
  373. {
  374. {
  375. cmNinjaRule rule("COPY_OSX_CONTENT_FILE");
  376. rule.Command = cmStrCat(this->CMakeCmd(), " -E copy $in $out");
  377. rule.Description = "Copying OS X Content $out";
  378. rule.Comment = "Rule for copying OS X bundle content file, with style.";
  379. this->AddRule(rule);
  380. }
  381. {
  382. cmNinjaRule rule("COPY_OSX_CONTENT_DIR");
  383. rule.Command = cmStrCat(this->CMakeCmd(), " -E copy_directory $in $out");
  384. rule.Description = "Copying OS X Content $out";
  385. rule.Comment = "Rule for copying OS X bundle content dir, with style.";
  386. this->AddRule(rule);
  387. }
  388. }
  389. void cmGlobalNinjaGenerator::WriteMacOSXContentBuild(std::string input,
  390. std::string output,
  391. std::string const& config)
  392. {
  393. this->AddMacOSXContentRule();
  394. {
  395. cmNinjaBuild build(cmSystemTools::FileIsDirectory(input)
  396. ? "COPY_OSX_CONTENT_DIR"
  397. : "COPY_OSX_CONTENT_FILE");
  398. build.Outputs.push_back(std::move(output));
  399. build.ExplicitDeps.push_back(std::move(input));
  400. this->WriteBuild(*this->GetImplFileStream(config), build);
  401. }
  402. }
  403. void cmGlobalNinjaGenerator::WriteRule(std::ostream& os,
  404. cmNinjaRule const& rule)
  405. {
  406. // -- Parameter checks
  407. // Make sure the rule has a name.
  408. if (rule.Name.empty()) {
  409. cmSystemTools::Error(cmStrCat(
  410. "No name given for WriteRule! called with comment: ", rule.Comment));
  411. return;
  412. }
  413. // Make sure a command is given.
  414. if (rule.Command.empty()) {
  415. cmSystemTools::Error(cmStrCat(
  416. "No command given for WriteRule! called with comment: ", rule.Comment));
  417. return;
  418. }
  419. // Make sure response file content is given
  420. if (!rule.RspFile.empty() && rule.RspContent.empty()) {
  421. cmSystemTools::Error(
  422. cmStrCat("rspfile but no rspfile_content given for WriteRule! "
  423. "called with comment: ",
  424. rule.Comment));
  425. return;
  426. }
  427. // -- Write rule
  428. // Write rule intro
  429. cmGlobalNinjaGenerator::WriteComment(os, rule.Comment);
  430. os << "rule " << rule.Name << '\n';
  431. // Write rule key/value pairs
  432. auto writeKV = [&os](char const* key, std::string const& value) {
  433. if (!value.empty()) {
  434. cmGlobalNinjaGenerator::Indent(os, 1);
  435. os << key << " = " << value << '\n';
  436. }
  437. };
  438. writeKV("depfile", rule.DepFile);
  439. writeKV("deps", rule.DepType);
  440. writeKV("command", rule.Command);
  441. writeKV("description", rule.Description);
  442. if (!rule.RspFile.empty()) {
  443. writeKV("rspfile", rule.RspFile);
  444. writeKV("rspfile_content", rule.RspContent);
  445. }
  446. writeKV("restat", rule.Restat);
  447. if (rule.Generator) {
  448. writeKV("generator", "1");
  449. }
  450. // Finish rule
  451. os << '\n';
  452. }
  453. void cmGlobalNinjaGenerator::WriteVariable(std::ostream& os,
  454. std::string const& name,
  455. std::string const& value,
  456. std::string const& comment,
  457. int indent)
  458. {
  459. // Make sure we have a name.
  460. if (name.empty()) {
  461. cmSystemTools::Error(cmStrCat("No name given for WriteVariable! called "
  462. "with comment: ",
  463. comment));
  464. return;
  465. }
  466. std::string val;
  467. static std::unordered_set<std::string> const variablesShouldNotBeTrimmed = {
  468. "CODE_CHECK", "LAUNCHER"
  469. };
  470. if (variablesShouldNotBeTrimmed.find(name) ==
  471. variablesShouldNotBeTrimmed.end()) {
  472. val = cmTrimWhitespace(value);
  473. // If the value ends with `\n` and a `$` was left at the end of the trimmed
  474. // value, put the newline back. Otherwise the next stanza is hidden by the
  475. // trailing `$` escaping the newline.
  476. if (cmSystemTools::StringEndsWith(value, "\n") &&
  477. cmSystemTools::StringEndsWith(val, "$")) {
  478. val += '\n';
  479. }
  480. } else {
  481. val = value;
  482. }
  483. // Do not add a variable if the value is empty.
  484. if (val.empty()) {
  485. return;
  486. }
  487. cmGlobalNinjaGenerator::WriteComment(os, comment);
  488. cmGlobalNinjaGenerator::Indent(os, indent);
  489. os << name << " = " << val << "\n";
  490. }
  491. void cmGlobalNinjaGenerator::WriteInclude(std::ostream& os,
  492. std::string const& filename,
  493. std::string const& comment)
  494. {
  495. cmGlobalNinjaGenerator::WriteComment(os, comment);
  496. os << "include " << filename << "\n";
  497. }
  498. void cmGlobalNinjaGenerator::WriteDefault(std::ostream& os,
  499. cmNinjaDeps const& targets,
  500. std::string const& comment)
  501. {
  502. cmGlobalNinjaGenerator::WriteComment(os, comment);
  503. os << "default";
  504. for (std::string const& target : targets) {
  505. os << " " << target;
  506. }
  507. os << "\n";
  508. }
  509. cmGlobalNinjaGenerator::cmGlobalNinjaGenerator(cmake* cm)
  510. : cmGlobalCommonGenerator(cm)
  511. {
  512. #ifdef _WIN32
  513. cm->GetState()->SetWindowsShell(true);
  514. // Attempt to use full path to COMSPEC, default "cmd.exe"
  515. this->Comspec = cmSystemTools::GetComspec();
  516. #endif
  517. cm->GetState()->SetNinja(true);
  518. this->FindMakeProgramFile = "CMakeNinjaFindMake.cmake";
  519. }
  520. // Virtual public methods.
  521. std::unique_ptr<cmLocalGenerator> cmGlobalNinjaGenerator::CreateLocalGenerator(
  522. cmMakefile* mf)
  523. {
  524. return std::unique_ptr<cmLocalGenerator>(
  525. cm::make_unique<cmLocalNinjaGenerator>(this, mf));
  526. }
  527. codecvt_Encoding cmGlobalNinjaGenerator::GetMakefileEncoding() const
  528. {
  529. return this->NinjaExpectedEncoding;
  530. }
  531. cmDocumentationEntry cmGlobalNinjaGenerator::GetDocumentation()
  532. {
  533. return { cmGlobalNinjaGenerator::GetActualName(),
  534. "Generates build.ninja files." };
  535. }
  536. std::vector<std::string> const& cmGlobalNinjaGenerator::GetConfigNames() const
  537. {
  538. return static_cast<cmLocalNinjaGenerator const*>(
  539. this->LocalGenerators.front().get())
  540. ->GetConfigNames();
  541. }
  542. // Implemented in all cmGlobaleGenerator sub-classes.
  543. // Used in:
  544. // Source/cmLocalGenerator.cxx
  545. // Source/cmake.cxx
  546. void cmGlobalNinjaGenerator::Generate()
  547. {
  548. // Check minimum Ninja version.
  549. if (cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
  550. RequiredNinjaVersion())) {
  551. std::ostringstream msg;
  552. msg << "The detected version of Ninja (" << this->NinjaVersion;
  553. msg << ") is less than the version of Ninja required by CMake (";
  554. msg << cmGlobalNinjaGenerator::RequiredNinjaVersion() << ").";
  555. this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  556. msg.str());
  557. return;
  558. }
  559. this->InitOutputPathPrefix();
  560. if (!this->OpenBuildFileStreams()) {
  561. return;
  562. }
  563. if (!this->OpenRulesFileStream()) {
  564. return;
  565. }
  566. for (auto& it : this->Configs) {
  567. it.second.TargetDependsClosures.clear();
  568. }
  569. this->TargetAll = this->NinjaOutputPath("all");
  570. this->CMakeCacheFile = this->NinjaOutputPath("CMakeCache.txt");
  571. this->DiagnosedCxxModuleNinjaSupport = false;
  572. this->ClangTidyExportFixesDirs.clear();
  573. this->ClangTidyExportFixesFiles.clear();
  574. this->cmGlobalGenerator::Generate();
  575. this->WriteAssumedSourceDependencies();
  576. this->WriteTargetAliases(*this->GetCommonFileStream());
  577. this->WriteFolderTargets(*this->GetCommonFileStream());
  578. this->WriteBuiltinTargets(*this->GetCommonFileStream());
  579. if (cmSystemTools::GetErrorOccurredFlag()) {
  580. this->RulesFileStream->setstate(std::ios::failbit);
  581. for (std::string const& config : this->GetConfigNames()) {
  582. this->GetImplFileStream(config)->setstate(std::ios::failbit);
  583. this->GetConfigFileStream(config)->setstate(std::ios::failbit);
  584. }
  585. this->GetCommonFileStream()->setstate(std::ios::failbit);
  586. }
  587. this->CloseCompileCommandsStream();
  588. this->CloseRulesFileStream();
  589. this->CloseBuildFileStreams();
  590. #ifdef _WIN32
  591. // Older ninja tools will not be able to update metadata on Windows
  592. // when we are re-generating inside an existing 'ninja' invocation
  593. // because the outer tool has the files open for write.
  594. if (this->NinjaSupportsMetadataOnRegeneration ||
  595. !this->GetCMakeInstance()->GetRegenerateDuringBuild())
  596. #endif
  597. {
  598. this->CleanMetaData();
  599. }
  600. this->RemoveUnknownClangTidyExportFixesFiles();
  601. }
  602. void cmGlobalNinjaGenerator::CleanMetaData()
  603. {
  604. constexpr size_t ninja_tool_arg_size = 8; // 2 `-_` flags and 4 separators
  605. auto run_ninja_tool = [this](std::vector<char const*> const& args) {
  606. std::vector<std::string> command;
  607. command.push_back(this->NinjaCommand);
  608. command.emplace_back("-C");
  609. command.emplace_back(this->GetCMakeInstance()->GetHomeOutputDirectory());
  610. command.emplace_back("-t");
  611. for (auto const& arg : args) {
  612. command.emplace_back(arg);
  613. }
  614. std::string error;
  615. if (!cmSystemTools::RunSingleCommand(command, nullptr, &error, nullptr,
  616. nullptr,
  617. cmSystemTools::OUTPUT_NONE)) {
  618. this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  619. cmStrCat("Running\n '",
  620. cmJoin(command, "' '"),
  621. "'\n"
  622. "failed with:\n ",
  623. error));
  624. cmSystemTools::SetFatalErrorOccurred();
  625. }
  626. };
  627. // Can the tools below expect 'build.ninja' to be loadable?
  628. bool const expectBuildManifest =
  629. !this->IsMultiConfig() && this->OutputPathPrefix.empty();
  630. // Skip some ninja tools if they need 'build.ninja' but it is missing.
  631. bool const missingBuildManifest = expectBuildManifest &&
  632. this->NinjaSupportsUnconditionalRecompactTool &&
  633. !cmSystemTools::FileExists("build.ninja");
  634. // The `recompact` tool loads the manifest. As above, we don't have a single
  635. // `build.ninja` to load for this in Ninja-Multi. This may be relaxed in the
  636. // future pending further investigation into how Ninja works upstream
  637. // (ninja#1721).
  638. if (this->NinjaSupportsUnconditionalRecompactTool &&
  639. !this->GetCMakeInstance()->GetRegenerateDuringBuild() &&
  640. expectBuildManifest && !missingBuildManifest) {
  641. run_ninja_tool({ "recompact" });
  642. }
  643. if (this->NinjaSupportsRestatTool && this->OutputPathPrefix.empty()) {
  644. cmNinjaDeps outputs;
  645. this->AddRebuildManifestOutputs(outputs);
  646. auto output_it = outputs.begin();
  647. size_t static_arg_size = ninja_tool_arg_size + this->NinjaCommand.size() +
  648. this->GetCMakeInstance()->GetHomeOutputDirectory().size();
  649. // The Windows command-line length limit is 32768, but if `ninja` is
  650. // wrapped by a `.bat` file, the limit is 8192. Leave plenty.
  651. constexpr size_t maximum_arg_size = 8000;
  652. while (output_it != outputs.end()) {
  653. size_t total_arg_size = static_arg_size;
  654. std::vector<char const*> args;
  655. args.reserve(std::distance(output_it, outputs.end()) + 1);
  656. args.push_back("restat");
  657. total_arg_size += 7; // restat + 1
  658. while (output_it != outputs.end() &&
  659. total_arg_size + output_it->size() + 1 < maximum_arg_size) {
  660. args.push_back(output_it->c_str());
  661. total_arg_size += output_it->size() + 1;
  662. ++output_it;
  663. }
  664. run_ninja_tool(args);
  665. }
  666. }
  667. }
  668. bool cmGlobalNinjaGenerator::FindMakeProgram(cmMakefile* mf)
  669. {
  670. if (!this->cmGlobalGenerator::FindMakeProgram(mf)) {
  671. return false;
  672. }
  673. if (cmValue ninjaCommand = mf->GetDefinition("CMAKE_MAKE_PROGRAM")) {
  674. this->NinjaCommand = *ninjaCommand;
  675. std::vector<std::string> command;
  676. command.push_back(this->NinjaCommand);
  677. command.emplace_back("--version");
  678. std::string version;
  679. std::string error;
  680. if (!cmSystemTools::RunSingleCommand(command, &version, &error, nullptr,
  681. nullptr,
  682. cmSystemTools::OUTPUT_NONE)) {
  683. mf->IssueMessage(MessageType::FATAL_ERROR,
  684. cmStrCat("Running\n '", cmJoin(command, "' '"),
  685. "'\n"
  686. "failed with:\n ",
  687. error));
  688. cmSystemTools::SetFatalErrorOccurred();
  689. return false;
  690. }
  691. this->NinjaVersion = cmTrimWhitespace(version);
  692. this->CheckNinjaFeatures();
  693. }
  694. return true;
  695. }
  696. void cmGlobalNinjaGenerator::CheckNinjaFeatures()
  697. {
  698. this->NinjaSupportsConsolePool =
  699. !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
  700. RequiredNinjaVersionForConsolePool());
  701. this->NinjaSupportsImplicitOuts = !cmSystemTools::VersionCompare(
  702. cmSystemTools::OP_LESS, this->NinjaVersion,
  703. cmGlobalNinjaGenerator::RequiredNinjaVersionForImplicitOuts());
  704. this->NinjaSupportsManifestRestat =
  705. !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
  706. RequiredNinjaVersionForManifestRestat());
  707. this->NinjaSupportsMultilineDepfile =
  708. !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
  709. RequiredNinjaVersionForMultilineDepfile());
  710. this->NinjaSupportsDyndepsCxx =
  711. !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
  712. RequiredNinjaVersionForDyndepsCxx());
  713. this->NinjaSupportsDyndepsFortran =
  714. !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
  715. RequiredNinjaVersionForDyndepsFortran());
  716. if (!this->NinjaSupportsDyndepsFortran) {
  717. // The ninja version number is not new enough to have upstream support.
  718. // Our ninja branch adds ".dyndep-#" to its version number,
  719. // where '#' is a feature-specific version number. Extract it.
  720. static std::string const k_DYNDEP_ = ".dyndep-";
  721. std::string::size_type pos = this->NinjaVersion.find(k_DYNDEP_);
  722. if (pos != std::string::npos) {
  723. char const* fv = &this->NinjaVersion[pos + k_DYNDEP_.size()];
  724. unsigned long dyndep = 0;
  725. cmStrToULong(fv, &dyndep);
  726. if (dyndep == 1) {
  727. this->NinjaSupportsDyndepsFortran = true;
  728. }
  729. }
  730. }
  731. this->NinjaSupportsUnconditionalRecompactTool =
  732. !cmSystemTools::VersionCompare(
  733. cmSystemTools::OP_LESS, this->NinjaVersion,
  734. RequiredNinjaVersionForUnconditionalRecompactTool());
  735. this->NinjaSupportsRestatTool =
  736. !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
  737. RequiredNinjaVersionForRestatTool());
  738. this->NinjaSupportsMultipleOutputs =
  739. !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
  740. RequiredNinjaVersionForMultipleOutputs());
  741. this->NinjaSupportsMetadataOnRegeneration = !cmSystemTools::VersionCompare(
  742. cmSystemTools::OP_LESS, this->NinjaVersion,
  743. RequiredNinjaVersionForMetadataOnRegeneration());
  744. #ifdef _WIN32
  745. this->NinjaSupportsCodePage =
  746. !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
  747. RequiredNinjaVersionForCodePage());
  748. if (this->NinjaSupportsCodePage) {
  749. this->CheckNinjaCodePage();
  750. } else {
  751. this->NinjaExpectedEncoding = codecvt_Encoding::ANSI;
  752. }
  753. #endif
  754. this->NinjaSupportsCWDDepend =
  755. !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
  756. RequiredNinjaVersionForCWDDepend());
  757. }
  758. void cmGlobalNinjaGenerator::CheckNinjaCodePage()
  759. {
  760. std::vector<std::string> command{ this->NinjaCommand, "-t", "wincodepage" };
  761. std::string output;
  762. std::string error;
  763. int result;
  764. if (!cmSystemTools::RunSingleCommand(command, &output, &error, &result,
  765. nullptr, cmSystemTools::OUTPUT_NONE)) {
  766. this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  767. cmStrCat("Running\n '",
  768. cmJoin(command, "' '"),
  769. "'\n"
  770. "failed with:\n ",
  771. error));
  772. cmSystemTools::SetFatalErrorOccurred();
  773. } else if (result == 0) {
  774. std::istringstream outputStream(output);
  775. std::string line;
  776. bool found = false;
  777. while (cmSystemTools::GetLineFromStream(outputStream, line)) {
  778. if (cmHasLiteralPrefix(line, "Build file encoding: ")) {
  779. cm::string_view lineView(line);
  780. cm::string_view encoding =
  781. lineView.substr(cmStrLen("Build file encoding: "));
  782. if (encoding == "UTF-8") {
  783. // Ninja expects UTF-8. We use that internally. No conversion needed.
  784. this->NinjaExpectedEncoding = codecvt_Encoding::None;
  785. } else {
  786. this->NinjaExpectedEncoding = codecvt_Encoding::ANSI;
  787. }
  788. found = true;
  789. break;
  790. }
  791. }
  792. if (!found) {
  793. this->GetCMakeInstance()->IssueMessage(
  794. MessageType::WARNING,
  795. "Could not determine Ninja's code page, defaulting to UTF-8");
  796. this->NinjaExpectedEncoding = codecvt_Encoding::None;
  797. }
  798. } else {
  799. this->NinjaExpectedEncoding = codecvt_Encoding::ANSI;
  800. }
  801. }
  802. bool cmGlobalNinjaGenerator::CheckLanguages(
  803. std::vector<std::string> const& languages, cmMakefile* mf) const
  804. {
  805. if (cm::contains(languages, "Fortran")) {
  806. return this->CheckFortran(mf);
  807. }
  808. if (cm::contains(languages, "ISPC")) {
  809. return this->CheckISPC(mf);
  810. }
  811. if (cm::contains(languages, "Swift")) {
  812. std::string const architectures =
  813. mf->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES");
  814. if (architectures.find_first_of(';') != std::string::npos) {
  815. mf->IssueMessage(MessageType::FATAL_ERROR,
  816. "multiple values for CMAKE_OSX_ARCHITECTURES not "
  817. "supported with Swift");
  818. cmSystemTools::SetFatalErrorOccurred();
  819. return false;
  820. }
  821. }
  822. return true;
  823. }
  824. bool cmGlobalNinjaGenerator::CheckCxxModuleSupport(CxxModuleSupportQuery query)
  825. {
  826. if (this->NinjaSupportsDyndepsCxx) {
  827. return true;
  828. }
  829. bool const diagnose = !this->DiagnosedCxxModuleNinjaSupport &&
  830. !this->CMakeInstance->GetIsInTryCompile() &&
  831. query == CxxModuleSupportQuery::Expected;
  832. if (diagnose) {
  833. std::ostringstream e;
  834. /* clang-format off */
  835. e <<
  836. "The Ninja generator does not support C++20 modules "
  837. "using Ninja version \n"
  838. " " << this->NinjaVersion << "\n"
  839. "due to lack of required features. "
  840. "Ninja " << RequiredNinjaVersionForDyndepsCxx() <<
  841. " or higher is required."
  842. ;
  843. /* clang-format on */
  844. this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR, e.str());
  845. cmSystemTools::SetFatalErrorOccurred();
  846. }
  847. return false;
  848. }
  849. bool cmGlobalNinjaGenerator::CheckFortran(cmMakefile* mf) const
  850. {
  851. if (this->NinjaSupportsDyndepsFortran) {
  852. return true;
  853. }
  854. std::ostringstream e;
  855. /* clang-format off */
  856. e <<
  857. "The Ninja generator does not support Fortran using Ninja version\n"
  858. " " << this->NinjaVersion << "\n"
  859. "due to lack of required features. "
  860. "Ninja " << RequiredNinjaVersionForDyndepsFortran() <<
  861. " or higher is required."
  862. ;
  863. /* clang-format on */
  864. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  865. cmSystemTools::SetFatalErrorOccurred();
  866. return false;
  867. }
  868. bool cmGlobalNinjaGenerator::CheckISPC(cmMakefile* mf) const
  869. {
  870. if (this->NinjaSupportsMultipleOutputs) {
  871. return true;
  872. }
  873. std::ostringstream e;
  874. /* clang-format off */
  875. e <<
  876. "The Ninja generator does not support ISPC using Ninja version\n"
  877. " " << this->NinjaVersion << "\n"
  878. "due to lack of required features. "
  879. "Ninja " << RequiredNinjaVersionForMultipleOutputs() <<
  880. " or higher is required."
  881. ;
  882. /* clang-format on */
  883. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  884. cmSystemTools::SetFatalErrorOccurred();
  885. return false;
  886. }
  887. void cmGlobalNinjaGenerator::EnableLanguage(
  888. std::vector<std::string> const& langs, cmMakefile* mf, bool optional)
  889. {
  890. if (this->IsMultiConfig()) {
  891. mf->InitCMAKE_CONFIGURATION_TYPES("Debug;Release;RelWithDebInfo");
  892. }
  893. this->cmGlobalGenerator::EnableLanguage(langs, mf, optional);
  894. for (std::string const& l : langs) {
  895. if (l == "NONE") {
  896. continue;
  897. }
  898. this->ResolveLanguageCompiler(l, mf, optional);
  899. #ifdef _WIN32
  900. std::string const& compilerId =
  901. mf->GetSafeDefinition(cmStrCat("CMAKE_", l, "_COMPILER_ID"));
  902. std::string const& simulateId =
  903. mf->GetSafeDefinition(cmStrCat("CMAKE_", l, "_SIMULATE_ID"));
  904. std::string const& compilerFrontendVariant = mf->GetSafeDefinition(
  905. cmStrCat("CMAKE_", l, "_COMPILER_FRONTEND_VARIANT"));
  906. if (DetectGCCOnWindows(compilerId, simulateId, compilerFrontendVariant)) {
  907. this->MarkAsGCCOnWindows();
  908. }
  909. #endif
  910. }
  911. }
  912. // Implemented by:
  913. // cmGlobalUnixMakefileGenerator3
  914. // cmGlobalGhsMultiGenerator
  915. // cmGlobalVisualStudio10Generator
  916. // cmGlobalVisualStudio7Generator
  917. // cmGlobalXCodeGenerator
  918. // Called by:
  919. // cmGlobalGenerator::Build()
  920. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  921. cmGlobalNinjaGenerator::GenerateBuildCommand(
  922. std::string const& makeProgram, std::string const& /*projectName*/,
  923. std::string const& /*projectDir*/,
  924. std::vector<std::string> const& targetNames, std::string const& config,
  925. int jobs, bool verbose, cmBuildOptions const& /*buildOptions*/,
  926. std::vector<std::string> const& makeOptions)
  927. {
  928. GeneratedMakeCommand makeCommand;
  929. makeCommand.Add(this->SelectMakeProgram(makeProgram));
  930. if (verbose) {
  931. makeCommand.Add("-v");
  932. }
  933. if ((jobs != cmake::NO_BUILD_PARALLEL_LEVEL) &&
  934. (jobs != cmake::DEFAULT_BUILD_PARALLEL_LEVEL)) {
  935. makeCommand.Add("-j", std::to_string(jobs));
  936. }
  937. this->AppendNinjaFileArgument(makeCommand, config);
  938. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  939. for (auto const& tname : targetNames) {
  940. if (!tname.empty()) {
  941. makeCommand.Add(tname);
  942. }
  943. }
  944. return { std::move(makeCommand) };
  945. }
  946. // Non-virtual public methods.
  947. void cmGlobalNinjaGenerator::AddRule(cmNinjaRule const& rule)
  948. {
  949. // Do not add the same rule twice.
  950. if (!this->Rules.insert(rule.Name).second) {
  951. return;
  952. }
  953. // Store command length
  954. this->RuleCmdLength[rule.Name] = static_cast<int>(rule.Command.size());
  955. // Write rule
  956. cmGlobalNinjaGenerator::WriteRule(*this->RulesFileStream, rule);
  957. }
  958. bool cmGlobalNinjaGenerator::HasRule(std::string const& name)
  959. {
  960. return (this->Rules.find(name) != this->Rules.end());
  961. }
  962. // Private virtual overrides
  963. void cmGlobalNinjaGenerator::ComputeTargetObjectDirectory(
  964. cmGeneratorTarget* gt) const
  965. {
  966. // Compute full path to object file directory for this target.
  967. std::string dir =
  968. cmStrCat(gt->GetSupportDirectory(), '/', this->GetCMakeCFGIntDir(), '/');
  969. gt->ObjectDirectory = dir;
  970. }
  971. // Private methods
  972. bool cmGlobalNinjaGenerator::OpenBuildFileStreams()
  973. {
  974. if (!this->OpenFileStream(this->BuildFileStream,
  975. cmGlobalNinjaGenerator::NINJA_BUILD_FILE)) {
  976. return false;
  977. }
  978. // Write a comment about this file.
  979. *this->BuildFileStream
  980. << "# This file contains all the build statements describing the\n"
  981. << "# compilation DAG.\n\n";
  982. return true;
  983. }
  984. bool cmGlobalNinjaGenerator::OpenFileStream(
  985. std::unique_ptr<cmGeneratedFileStream>& stream, std::string const& name)
  986. {
  987. // Get a stream where to generate things.
  988. if (!stream) {
  989. // Compute Ninja's build file path.
  990. std::string path =
  991. cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(), '/', name);
  992. stream = cm::make_unique<cmGeneratedFileStream>(
  993. path, false, this->GetMakefileEncoding());
  994. if (!(*stream)) {
  995. // An error message is generated by the constructor if it cannot
  996. // open the file.
  997. return false;
  998. }
  999. // Write the do not edit header.
  1000. this->WriteDisclaimer(*stream);
  1001. }
  1002. return true;
  1003. }
  1004. cm::optional<std::set<std::string>> cmGlobalNinjaGenerator::ListSubsetWithAll(
  1005. std::set<std::string> const& all, std::set<std::string> const& defaults,
  1006. std::vector<std::string> const& items)
  1007. {
  1008. std::set<std::string> result;
  1009. for (auto const& item : items) {
  1010. if (item == "all") {
  1011. if (items.size() == 1) {
  1012. result = defaults;
  1013. } else {
  1014. return cm::nullopt;
  1015. }
  1016. } else if (all.count(item)) {
  1017. result.insert(item);
  1018. } else {
  1019. return cm::nullopt;
  1020. }
  1021. }
  1022. return cm::make_optional(result);
  1023. }
  1024. void cmGlobalNinjaGenerator::CloseBuildFileStreams()
  1025. {
  1026. if (this->BuildFileStream) {
  1027. this->BuildFileStream.reset();
  1028. } else {
  1029. cmSystemTools::Error("Build file stream was not open.");
  1030. }
  1031. }
  1032. bool cmGlobalNinjaGenerator::OpenRulesFileStream()
  1033. {
  1034. if (!this->OpenFileStream(this->RulesFileStream,
  1035. cmGlobalNinjaGenerator::NINJA_RULES_FILE)) {
  1036. return false;
  1037. }
  1038. // Write comment about this file.
  1039. /* clang-format off */
  1040. *this->RulesFileStream
  1041. << "# This file contains all the rules used to get the outputs files\n"
  1042. << "# built from the input files.\n"
  1043. << "# It is included in the main '" << NINJA_BUILD_FILE << "'.\n\n"
  1044. ;
  1045. /* clang-format on */
  1046. return true;
  1047. }
  1048. void cmGlobalNinjaGenerator::CloseRulesFileStream()
  1049. {
  1050. if (this->RulesFileStream) {
  1051. this->RulesFileStream.reset();
  1052. } else {
  1053. cmSystemTools::Error("Rules file stream was not open.");
  1054. }
  1055. }
  1056. static void EnsureTrailingSlash(std::string& path)
  1057. {
  1058. if (path.empty()) {
  1059. return;
  1060. }
  1061. std::string::value_type last = path.back();
  1062. #ifdef _WIN32
  1063. if (last != '\\') {
  1064. path += '\\';
  1065. }
  1066. #else
  1067. if (last != '/') {
  1068. path += '/';
  1069. }
  1070. #endif
  1071. }
  1072. std::string const& cmGlobalNinjaGenerator::ConvertToNinjaPath(
  1073. std::string const& path) const
  1074. {
  1075. auto const f = this->ConvertToNinjaPathCache.find(path);
  1076. if (f != this->ConvertToNinjaPathCache.end()) {
  1077. return f->second;
  1078. }
  1079. std::string convPath =
  1080. this->LocalGenerators[0]->MaybeRelativeToTopBinDir(path);
  1081. convPath = this->NinjaOutputPath(convPath);
  1082. #ifdef _WIN32
  1083. std::replace(convPath.begin(), convPath.end(), '/', '\\');
  1084. #endif
  1085. return this->ConvertToNinjaPathCache.emplace(path, std::move(convPath))
  1086. .first->second;
  1087. }
  1088. std::string cmGlobalNinjaGenerator::ConvertToNinjaAbsPath(
  1089. std::string path) const
  1090. {
  1091. #ifdef _WIN32
  1092. std::replace(path.begin(), path.end(), '/', '\\');
  1093. #endif
  1094. return path;
  1095. }
  1096. void cmGlobalNinjaGenerator::AddAdditionalCleanFile(std::string fileName,
  1097. std::string const& config)
  1098. {
  1099. this->Configs[config].AdditionalCleanFiles.emplace(std::move(fileName));
  1100. }
  1101. void cmGlobalNinjaGenerator::AddCXXCompileCommand(
  1102. std::string const& commandLine, std::string const& sourceFile,
  1103. std::string const& objPath)
  1104. {
  1105. // Compute Ninja's build file path.
  1106. std::string buildFileDir =
  1107. this->GetCMakeInstance()->GetHomeOutputDirectory();
  1108. if (!this->CompileCommandsStream) {
  1109. std::string buildFilePath =
  1110. cmStrCat(buildFileDir, "/compile_commands.json");
  1111. // Get a stream where to generate things.
  1112. this->CompileCommandsStream =
  1113. cm::make_unique<cmGeneratedFileStream>(buildFilePath);
  1114. *this->CompileCommandsStream << "[\n";
  1115. } else {
  1116. *this->CompileCommandsStream << ",\n";
  1117. }
  1118. std::string sourceFileName =
  1119. cmSystemTools::CollapseFullPath(sourceFile, buildFileDir);
  1120. /* clang-format off */
  1121. *this->CompileCommandsStream << "{\n"
  1122. << R"( "directory": ")"
  1123. << cmGlobalGenerator::EscapeJSON(buildFileDir) << "\",\n"
  1124. << R"( "command": ")"
  1125. << cmGlobalGenerator::EscapeJSON(commandLine) << "\",\n"
  1126. << R"( "file": ")"
  1127. << cmGlobalGenerator::EscapeJSON(sourceFileName) << "\",\n"
  1128. << R"( "output": ")"
  1129. << cmGlobalGenerator::EscapeJSON(
  1130. cmSystemTools::CollapseFullPath(objPath, buildFileDir))
  1131. << "\"\n"
  1132. << "}";
  1133. /* clang-format on */
  1134. }
  1135. void cmGlobalNinjaGenerator::CloseCompileCommandsStream()
  1136. {
  1137. if (this->CompileCommandsStream) {
  1138. *this->CompileCommandsStream << "\n]\n";
  1139. this->CompileCommandsStream.reset();
  1140. }
  1141. }
  1142. void cmGlobalNinjaGenerator::WriteDisclaimer(std::ostream& os) const
  1143. {
  1144. os << "# CMAKE generated file: DO NOT EDIT!\n"
  1145. << "# Generated by \"" << this->GetName() << "\""
  1146. << " Generator, CMake Version " << cmVersion::GetMajorVersion() << "."
  1147. << cmVersion::GetMinorVersion() << "\n\n";
  1148. }
  1149. void cmGlobalNinjaGenerator::WriteAssumedSourceDependencies()
  1150. {
  1151. for (auto const& asd : this->AssumedSourceDependencies) {
  1152. CCOutputs outputs(this);
  1153. outputs.ExplicitOuts.emplace_back(asd.first);
  1154. cmNinjaDeps orderOnlyDeps;
  1155. std::copy(asd.second.begin(), asd.second.end(),
  1156. std::back_inserter(orderOnlyDeps));
  1157. this->WriteCustomCommandBuild(
  1158. /*command=*/"", /*description=*/"",
  1159. "Assume dependencies for generated source file.",
  1160. /*depfile*/ "", /*job_pool*/ "",
  1161. /*uses_terminal*/ false,
  1162. /*restat*/ true, std::string(), outputs, cmNinjaDeps(),
  1163. std::move(orderOnlyDeps));
  1164. }
  1165. }
  1166. std::string cmGlobalNinjaGenerator::OrderDependsTargetForTarget(
  1167. cmGeneratorTarget const* target, std::string const& /*config*/) const
  1168. {
  1169. return cmStrCat("cmake_object_order_depends_target_", target->GetName());
  1170. }
  1171. std::string cmGlobalNinjaGenerator::OrderDependsTargetForTargetPrivate(
  1172. cmGeneratorTarget const* target, std::string const& config) const
  1173. {
  1174. return cmStrCat(this->OrderDependsTargetForTarget(target, config),
  1175. "_private");
  1176. }
  1177. void cmGlobalNinjaGenerator::AppendTargetOutputs(
  1178. cmGeneratorTarget const* target, cmNinjaDeps& outputs,
  1179. std::string const& config, cmNinjaTargetDepends depends) const
  1180. {
  1181. // for frameworks, we want the real name, not sample name
  1182. // frameworks always appear versioned, and the build.ninja
  1183. // will always attempt to manage symbolic links instead
  1184. // of letting cmOSXBundleGenerator do it.
  1185. bool realname = target->IsFrameworkOnApple();
  1186. switch (target->GetType()) {
  1187. case cmStateEnums::SHARED_LIBRARY:
  1188. case cmStateEnums::STATIC_LIBRARY:
  1189. case cmStateEnums::MODULE_LIBRARY: {
  1190. if (depends == DependOnTargetOrdering) {
  1191. outputs.push_back(this->OrderDependsTargetForTarget(target, config));
  1192. break;
  1193. }
  1194. }
  1195. CM_FALLTHROUGH;
  1196. case cmStateEnums::EXECUTABLE: {
  1197. if (target->IsApple() && target->HasImportLibrary(config)) {
  1198. outputs.push_back(this->ConvertToNinjaPath(target->GetFullPath(
  1199. config, cmStateEnums::ImportLibraryArtifact, realname)));
  1200. }
  1201. outputs.push_back(this->ConvertToNinjaPath(target->GetFullPath(
  1202. config, cmStateEnums::RuntimeBinaryArtifact, realname)));
  1203. break;
  1204. }
  1205. case cmStateEnums::OBJECT_LIBRARY: {
  1206. if (depends == DependOnTargetOrdering) {
  1207. outputs.push_back(this->OrderDependsTargetForTarget(target, config));
  1208. break;
  1209. }
  1210. }
  1211. CM_FALLTHROUGH;
  1212. case cmStateEnums::GLOBAL_TARGET:
  1213. case cmStateEnums::INTERFACE_LIBRARY:
  1214. case cmStateEnums::UTILITY: {
  1215. std::string path =
  1216. cmStrCat(target->GetLocalGenerator()->GetCurrentBinaryDirectory(), '/',
  1217. target->GetName());
  1218. std::string output = this->ConvertToNinjaPath(path);
  1219. if (target->Target->IsPerConfig()) {
  1220. output = this->BuildAlias(output, config);
  1221. }
  1222. outputs.push_back(output);
  1223. break;
  1224. }
  1225. case cmStateEnums::UNKNOWN_LIBRARY:
  1226. break;
  1227. }
  1228. }
  1229. void cmGlobalNinjaGenerator::AppendTargetDepends(
  1230. cmGeneratorTarget const* target, cmNinjaDeps& outputs,
  1231. std::string const& config, std::string const& fileConfig,
  1232. cmNinjaTargetDepends depends)
  1233. {
  1234. if (target->GetType() == cmStateEnums::GLOBAL_TARGET) {
  1235. // These depend only on other CMake-provided targets, e.g. "all".
  1236. for (BT<std::pair<std::string, bool>> const& util :
  1237. target->GetUtilities()) {
  1238. std::string d =
  1239. cmStrCat(target->GetLocalGenerator()->GetCurrentBinaryDirectory(), '/',
  1240. util.Value.first);
  1241. outputs.push_back(this->BuildAlias(this->ConvertToNinjaPath(d), config));
  1242. }
  1243. } else {
  1244. cmNinjaDeps outs;
  1245. auto computeISPCOutputs = [](cmGlobalNinjaGenerator* gg,
  1246. cmGeneratorTarget const* depTarget,
  1247. cmNinjaDeps& outputDeps,
  1248. std::string const& targetConfig) {
  1249. if (depTarget->CanCompileSources()) {
  1250. auto headers = depTarget->GetGeneratedISPCHeaders(targetConfig);
  1251. if (!headers.empty()) {
  1252. std::transform(headers.begin(), headers.end(), headers.begin(),
  1253. gg->MapToNinjaPath());
  1254. outputDeps.insert(outputDeps.end(), headers.begin(), headers.end());
  1255. }
  1256. auto objs = depTarget->GetGeneratedISPCObjects(targetConfig);
  1257. if (!objs.empty()) {
  1258. std::transform(objs.begin(), objs.end(), objs.begin(),
  1259. gg->MapToNinjaPath());
  1260. outputDeps.insert(outputDeps.end(), objs.begin(), objs.end());
  1261. }
  1262. }
  1263. };
  1264. for (cmTargetDepend const& targetDep :
  1265. this->GetTargetDirectDepends(target)) {
  1266. if (!targetDep->IsInBuildSystem()) {
  1267. continue;
  1268. }
  1269. if (targetDep.IsCross()) {
  1270. this->AppendTargetOutputs(targetDep, outs, fileConfig, depends);
  1271. computeISPCOutputs(this, targetDep, outs, fileConfig);
  1272. } else {
  1273. this->AppendTargetOutputs(targetDep, outs, config, depends);
  1274. computeISPCOutputs(this, targetDep, outs, config);
  1275. }
  1276. }
  1277. std::sort(outs.begin(), outs.end());
  1278. cm::append(outputs, outs);
  1279. }
  1280. }
  1281. void cmGlobalNinjaGenerator::AppendTargetDependsClosure(
  1282. cmGeneratorTarget const* target, std::unordered_set<std::string>& outputs,
  1283. std::string const& config, std::string const& fileConfig, bool genexOutput,
  1284. bool omit_self)
  1285. {
  1286. // try to locate the target in the cache
  1287. ByConfig::TargetDependsClosureKey key{
  1288. target,
  1289. config,
  1290. genexOutput,
  1291. };
  1292. auto find = this->Configs[fileConfig].TargetDependsClosures.lower_bound(key);
  1293. if (find == this->Configs[fileConfig].TargetDependsClosures.end() ||
  1294. find->first != key) {
  1295. // We now calculate the closure outputs by inspecting the dependent
  1296. // targets recursively.
  1297. // For that we have to distinguish between a local result set that is only
  1298. // relevant for filling the cache entries properly isolated and a global
  1299. // result set that is relevant for the result of the top level call to
  1300. // AppendTargetDependsClosure.
  1301. std::unordered_set<std::string>
  1302. this_outs; // this will be the new cache entry
  1303. for (auto const& dep_target : this->GetTargetDirectDepends(target)) {
  1304. if (!dep_target->IsInBuildSystem()) {
  1305. continue;
  1306. }
  1307. if (!this->IsSingleConfigUtility(target) &&
  1308. !this->IsSingleConfigUtility(dep_target) &&
  1309. this->EnableCrossConfigBuild() && !dep_target.IsCross() &&
  1310. !genexOutput) {
  1311. continue;
  1312. }
  1313. if (dep_target.IsCross()) {
  1314. this->AppendTargetDependsClosure(dep_target, this_outs, fileConfig,
  1315. fileConfig, genexOutput, false);
  1316. } else {
  1317. this->AppendTargetDependsClosure(dep_target, this_outs, config,
  1318. fileConfig, genexOutput, false);
  1319. }
  1320. }
  1321. find = this->Configs[fileConfig].TargetDependsClosures.emplace_hint(
  1322. find, key, std::move(this_outs));
  1323. }
  1324. // now fill the outputs of the final result from the newly generated cache
  1325. // entry
  1326. outputs.insert(find->second.begin(), find->second.end());
  1327. // finally generate the outputs of the target itself, if applicable
  1328. cmNinjaDeps outs;
  1329. if (!omit_self) {
  1330. this->AppendTargetOutputs(target, outs, config, DependOnTargetArtifact);
  1331. }
  1332. outputs.insert(outs.begin(), outs.end());
  1333. }
  1334. void cmGlobalNinjaGenerator::AddTargetAlias(std::string const& alias,
  1335. cmGeneratorTarget* target,
  1336. std::string const& config)
  1337. {
  1338. std::string outputPath = this->NinjaOutputPath(alias);
  1339. std::string buildAlias = this->BuildAlias(outputPath, config);
  1340. cmNinjaDeps outputs;
  1341. if (config != "all") {
  1342. this->AppendTargetOutputs(target, outputs, config, DependOnTargetArtifact);
  1343. }
  1344. // Mark the target's outputs as ambiguous to ensure that no other target
  1345. // uses the output as an alias.
  1346. for (std::string const& output : outputs) {
  1347. this->TargetAliases[output].GeneratorTarget = nullptr;
  1348. this->DefaultTargetAliases[output].GeneratorTarget = nullptr;
  1349. for (std::string const& config2 : this->GetConfigNames()) {
  1350. this->Configs[config2].TargetAliases[output].GeneratorTarget = nullptr;
  1351. }
  1352. }
  1353. // Insert the alias into the map. If the alias was already present in the
  1354. // map and referred to another target, mark it as ambiguous.
  1355. TargetAlias ta;
  1356. ta.GeneratorTarget = target;
  1357. ta.Config = config;
  1358. auto newAliasGlobal =
  1359. this->TargetAliases.insert(std::make_pair(buildAlias, ta));
  1360. if (newAliasGlobal.second &&
  1361. newAliasGlobal.first->second.GeneratorTarget != target) {
  1362. newAliasGlobal.first->second.GeneratorTarget = nullptr;
  1363. }
  1364. auto newAliasConfig =
  1365. this->Configs[config].TargetAliases.insert(std::make_pair(outputPath, ta));
  1366. if (newAliasConfig.second &&
  1367. newAliasConfig.first->second.GeneratorTarget != target) {
  1368. newAliasConfig.first->second.GeneratorTarget = nullptr;
  1369. }
  1370. if (this->DefaultConfigs.count(config)) {
  1371. auto newAliasDefaultGlobal =
  1372. this->DefaultTargetAliases.insert(std::make_pair(outputPath, ta));
  1373. if (newAliasDefaultGlobal.second &&
  1374. newAliasDefaultGlobal.first->second.GeneratorTarget != target) {
  1375. newAliasDefaultGlobal.first->second.GeneratorTarget = nullptr;
  1376. }
  1377. }
  1378. }
  1379. void cmGlobalNinjaGenerator::WriteTargetAliases(std::ostream& os)
  1380. {
  1381. cmGlobalNinjaGenerator::WriteDivider(os);
  1382. os << "# Target aliases.\n\n";
  1383. cmNinjaBuild build("phony");
  1384. build.Outputs.emplace_back();
  1385. for (auto const& ta : this->TargetAliases) {
  1386. // Don't write ambiguous aliases.
  1387. if (!ta.second.GeneratorTarget) {
  1388. continue;
  1389. }
  1390. // Don't write alias if there is a already a custom command with
  1391. // matching output
  1392. if (this->HasCustomCommandOutput(ta.first)) {
  1393. continue;
  1394. }
  1395. build.Outputs.front() = ta.first;
  1396. build.ExplicitDeps.clear();
  1397. if (ta.second.Config == "all") {
  1398. for (auto const& config : this->CrossConfigs) {
  1399. this->AppendTargetOutputs(ta.second.GeneratorTarget,
  1400. build.ExplicitDeps, config,
  1401. DependOnTargetArtifact);
  1402. }
  1403. } else {
  1404. this->AppendTargetOutputs(ta.second.GeneratorTarget, build.ExplicitDeps,
  1405. ta.second.Config, DependOnTargetArtifact);
  1406. }
  1407. this->WriteBuild(this->EnableCrossConfigBuild() &&
  1408. (ta.second.Config == "all" ||
  1409. this->CrossConfigs.count(ta.second.Config))
  1410. ? os
  1411. : *this->GetImplFileStream(ta.second.Config),
  1412. build);
  1413. }
  1414. if (this->IsMultiConfig()) {
  1415. for (std::string const& config : this->GetConfigNames()) {
  1416. for (auto const& ta : this->Configs[config].TargetAliases) {
  1417. // Don't write ambiguous aliases.
  1418. if (!ta.second.GeneratorTarget) {
  1419. continue;
  1420. }
  1421. // Don't write alias if there is a already a custom command with
  1422. // matching output
  1423. if (this->HasCustomCommandOutput(ta.first)) {
  1424. continue;
  1425. }
  1426. build.Outputs.front() = ta.first;
  1427. build.ExplicitDeps.clear();
  1428. this->AppendTargetOutputs(ta.second.GeneratorTarget,
  1429. build.ExplicitDeps, config,
  1430. DependOnTargetArtifact);
  1431. this->WriteBuild(*this->GetConfigFileStream(config), build);
  1432. }
  1433. }
  1434. if (!this->DefaultConfigs.empty()) {
  1435. for (auto const& ta : this->DefaultTargetAliases) {
  1436. // Don't write ambiguous aliases.
  1437. if (!ta.second.GeneratorTarget) {
  1438. continue;
  1439. }
  1440. // Don't write alias if there is a already a custom command with
  1441. // matching output
  1442. if (this->HasCustomCommandOutput(ta.first)) {
  1443. continue;
  1444. }
  1445. build.Outputs.front() = ta.first;
  1446. build.ExplicitDeps.clear();
  1447. for (auto const& config : this->DefaultConfigs) {
  1448. this->AppendTargetOutputs(ta.second.GeneratorTarget,
  1449. build.ExplicitDeps, config,
  1450. DependOnTargetArtifact);
  1451. }
  1452. this->WriteBuild(*this->GetDefaultFileStream(), build);
  1453. }
  1454. }
  1455. }
  1456. }
  1457. void cmGlobalNinjaGenerator::WriteFolderTargets(std::ostream& os)
  1458. {
  1459. cmGlobalNinjaGenerator::WriteDivider(os);
  1460. os << "# Folder targets.\n\n";
  1461. std::map<std::string, DirectoryTarget> dirTargets =
  1462. this->ComputeDirectoryTargets();
  1463. // Codegen target
  1464. if (this->CheckCMP0171()) {
  1465. for (auto const& it : dirTargets) {
  1466. cmNinjaBuild build("phony");
  1467. cmGlobalNinjaGenerator::WriteDivider(os);
  1468. std::string const& currentBinaryDir = it.first;
  1469. DirectoryTarget const& dt = it.second;
  1470. std::vector<std::string> configs =
  1471. static_cast<cmLocalNinjaGenerator const*>(dt.LG)->GetConfigNames();
  1472. // Setup target
  1473. build.Comment = cmStrCat("Folder: ", currentBinaryDir);
  1474. build.Outputs.emplace_back();
  1475. std::string const buildDirCodegenTarget =
  1476. this->ConvertToNinjaPath(cmStrCat(currentBinaryDir, "/codegen"));
  1477. for (auto const& config : configs) {
  1478. build.ExplicitDeps.clear();
  1479. build.Outputs.front() =
  1480. this->BuildAlias(buildDirCodegenTarget, config);
  1481. for (DirectoryTarget::Target const& t : dt.Targets) {
  1482. if (this->IsExcludedFromAllInConfig(t, config)) {
  1483. continue;
  1484. }
  1485. std::vector<cmSourceFile const*> customCommandSources;
  1486. t.GT->GetCustomCommands(customCommandSources, config);
  1487. for (cmSourceFile const* sf : customCommandSources) {
  1488. cmCustomCommand const* cc = sf->GetCustomCommand();
  1489. if (cc->GetCodegen()) {
  1490. auto const& outputs = cc->GetOutputs();
  1491. std::transform(outputs.begin(), outputs.end(),
  1492. std::back_inserter(build.ExplicitDeps),
  1493. this->MapToNinjaPath());
  1494. }
  1495. }
  1496. }
  1497. for (DirectoryTarget::Dir const& d : dt.Children) {
  1498. if (!d.ExcludeFromAll) {
  1499. build.ExplicitDeps.emplace_back(this->BuildAlias(
  1500. this->ConvertToNinjaPath(cmStrCat(d.Path, "/codegen")), config));
  1501. }
  1502. }
  1503. // Write target
  1504. this->WriteBuild(this->EnableCrossConfigBuild() &&
  1505. this->CrossConfigs.count(config)
  1506. ? os
  1507. : *this->GetImplFileStream(config),
  1508. build);
  1509. }
  1510. // Add shortcut target
  1511. if (this->IsMultiConfig()) {
  1512. for (auto const& config : configs) {
  1513. build.ExplicitDeps = { this->BuildAlias(buildDirCodegenTarget,
  1514. config) };
  1515. build.Outputs.front() = buildDirCodegenTarget;
  1516. this->WriteBuild(*this->GetConfigFileStream(config), build);
  1517. }
  1518. if (!this->DefaultFileConfig.empty()) {
  1519. build.ExplicitDeps.clear();
  1520. for (auto const& config : this->DefaultConfigs) {
  1521. build.ExplicitDeps.push_back(
  1522. this->BuildAlias(buildDirCodegenTarget, config));
  1523. }
  1524. build.Outputs.front() = buildDirCodegenTarget;
  1525. this->WriteBuild(*this->GetDefaultFileStream(), build);
  1526. }
  1527. }
  1528. // Add target for all configs
  1529. if (this->EnableCrossConfigBuild()) {
  1530. build.ExplicitDeps.clear();
  1531. for (auto const& config : this->CrossConfigs) {
  1532. build.ExplicitDeps.push_back(
  1533. this->BuildAlias(buildDirCodegenTarget, config));
  1534. }
  1535. build.Outputs.front() =
  1536. this->BuildAlias(buildDirCodegenTarget, "codegen");
  1537. this->WriteBuild(os, build);
  1538. }
  1539. }
  1540. }
  1541. // All target
  1542. for (auto const& it : dirTargets) {
  1543. cmNinjaBuild build("phony");
  1544. cmGlobalNinjaGenerator::WriteDivider(os);
  1545. std::string const& currentBinaryDir = it.first;
  1546. DirectoryTarget const& dt = it.second;
  1547. std::vector<std::string> configs =
  1548. static_cast<cmLocalNinjaGenerator const*>(dt.LG)->GetConfigNames();
  1549. // Setup target
  1550. build.Comment = cmStrCat("Folder: ", currentBinaryDir);
  1551. build.Outputs.emplace_back();
  1552. std::string const buildDirAllTarget =
  1553. this->ConvertToNinjaPath(cmStrCat(currentBinaryDir, "/all"));
  1554. for (auto const& config : configs) {
  1555. build.ExplicitDeps.clear();
  1556. build.Outputs.front() = this->BuildAlias(buildDirAllTarget, config);
  1557. for (DirectoryTarget::Target const& t : dt.Targets) {
  1558. if (!this->IsExcludedFromAllInConfig(t, config)) {
  1559. this->AppendTargetOutputs(t.GT, build.ExplicitDeps, config,
  1560. DependOnTargetArtifact);
  1561. }
  1562. }
  1563. for (DirectoryTarget::Dir const& d : dt.Children) {
  1564. if (!d.ExcludeFromAll) {
  1565. build.ExplicitDeps.emplace_back(this->BuildAlias(
  1566. this->ConvertToNinjaPath(cmStrCat(d.Path, "/all")), config));
  1567. }
  1568. }
  1569. // Write target
  1570. this->WriteBuild(this->EnableCrossConfigBuild() &&
  1571. this->CrossConfigs.count(config)
  1572. ? os
  1573. : *this->GetImplFileStream(config),
  1574. build);
  1575. }
  1576. // Add shortcut target
  1577. if (this->IsMultiConfig()) {
  1578. for (auto const& config : configs) {
  1579. build.ExplicitDeps = { this->BuildAlias(buildDirAllTarget, config) };
  1580. build.Outputs.front() = buildDirAllTarget;
  1581. this->WriteBuild(*this->GetConfigFileStream(config), build);
  1582. }
  1583. if (!this->DefaultFileConfig.empty()) {
  1584. build.ExplicitDeps.clear();
  1585. for (auto const& config : this->DefaultConfigs) {
  1586. build.ExplicitDeps.push_back(
  1587. this->BuildAlias(buildDirAllTarget, config));
  1588. }
  1589. build.Outputs.front() = buildDirAllTarget;
  1590. this->WriteBuild(*this->GetDefaultFileStream(), build);
  1591. }
  1592. }
  1593. // Add target for all configs
  1594. if (this->EnableCrossConfigBuild()) {
  1595. build.ExplicitDeps.clear();
  1596. for (auto const& config : this->CrossConfigs) {
  1597. build.ExplicitDeps.push_back(
  1598. this->BuildAlias(buildDirAllTarget, config));
  1599. }
  1600. build.Outputs.front() = this->BuildAlias(buildDirAllTarget, "all");
  1601. this->WriteBuild(os, build);
  1602. }
  1603. }
  1604. }
  1605. void cmGlobalNinjaGenerator::WriteBuiltinTargets(std::ostream& os)
  1606. {
  1607. // Write headers.
  1608. cmGlobalNinjaGenerator::WriteDivider(os);
  1609. os << "# Built-in targets\n\n";
  1610. this->WriteTargetRebuildManifest(os);
  1611. this->WriteTargetClean(os);
  1612. this->WriteTargetHelp(os);
  1613. #if !defined(CMAKE_BOOTSTRAP) && !defined(_WIN32)
  1614. // FIXME(#26668) This does not work on Windows
  1615. if (this->GetCMakeInstance()
  1616. ->GetInstrumentation()
  1617. ->HasPreOrPostBuildHook()) {
  1618. this->WriteTargetInstrument(os);
  1619. }
  1620. #endif
  1621. for (std::string const& config : this->GetConfigNames()) {
  1622. this->WriteTargetDefault(*this->GetConfigFileStream(config));
  1623. }
  1624. if (!this->DefaultFileConfig.empty()) {
  1625. this->WriteTargetDefault(*this->GetDefaultFileStream());
  1626. }
  1627. if (this->InstallTargetEnabled &&
  1628. this->GetCMakeInstance()->GetState()->GetGlobalPropertyAsBool(
  1629. "INSTALL_PARALLEL") &&
  1630. !this->Makefiles[0]->IsOn("CMAKE_SKIP_INSTALL_RULES")) {
  1631. cmNinjaBuild build("phony");
  1632. build.Comment = "Install every subdirectory in parallel";
  1633. build.Outputs.emplace_back(this->GetInstallParallelTargetName());
  1634. for (auto const& mf : this->Makefiles) {
  1635. build.ExplicitDeps.emplace_back(
  1636. this->ConvertToNinjaPath(cmStrCat(mf->GetCurrentBinaryDirectory(), '/',
  1637. this->GetInstallLocalTargetName())));
  1638. }
  1639. WriteBuild(os, build);
  1640. }
  1641. }
  1642. void cmGlobalNinjaGenerator::WriteTargetDefault(std::ostream& os)
  1643. {
  1644. if (!this->HasOutputPathPrefix()) {
  1645. cmNinjaDeps all;
  1646. all.push_back(this->TargetAll);
  1647. cmGlobalNinjaGenerator::WriteDefault(os, all,
  1648. "Make the all target the default.");
  1649. }
  1650. }
  1651. void cmGlobalNinjaGenerator::WriteTargetRebuildManifest(std::ostream& os)
  1652. {
  1653. if (this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  1654. return;
  1655. }
  1656. cmake* cm = this->GetCMakeInstance();
  1657. auto const& lg = this->LocalGenerators[0];
  1658. {
  1659. cmNinjaRule rule("RERUN_CMAKE");
  1660. rule.Command = cmStrCat(
  1661. this->CMakeCmd(), " --regenerate-during-build",
  1662. cm->GetIgnoreCompileWarningAsError() ? " --compile-no-warning-as-error"
  1663. : "",
  1664. cm->GetIgnoreLinkWarningAsError() ? " --link-no-warning-as-error" : "",
  1665. " -S",
  1666. lg->ConvertToOutputFormat(lg->GetSourceDirectory(),
  1667. cmOutputConverter::SHELL),
  1668. " -B",
  1669. lg->ConvertToOutputFormat(lg->GetBinaryDirectory(),
  1670. cmOutputConverter::SHELL));
  1671. rule.Description = "Re-running CMake...";
  1672. rule.Comment = "Rule for re-running cmake.";
  1673. rule.Generator = true;
  1674. WriteRule(*this->RulesFileStream, rule);
  1675. }
  1676. cmNinjaBuild reBuild("RERUN_CMAKE");
  1677. reBuild.Comment = "Re-run CMake if any of its inputs changed.";
  1678. this->AddRebuildManifestOutputs(reBuild.Outputs);
  1679. for (auto const& localGen : this->LocalGenerators) {
  1680. for (std::string const& fi : localGen->GetMakefile()->GetListFiles()) {
  1681. reBuild.ImplicitDeps.push_back(this->ConvertToNinjaPath(fi));
  1682. }
  1683. }
  1684. reBuild.ImplicitDeps.push_back(this->CMakeCacheFile);
  1685. #if !defined(CMAKE_BOOTSTRAP) && !defined(_WIN32)
  1686. // FIXME(#26668) This does not work on Windows
  1687. if (this->GetCMakeInstance()
  1688. ->GetInstrumentation()
  1689. ->HasPreOrPostBuildHook()) {
  1690. reBuild.ExplicitDeps.push_back(this->NinjaOutputPath("start_instrument"));
  1691. }
  1692. #endif
  1693. // Use 'console' pool to get non buffered output of the CMake re-run call
  1694. // Available since Ninja 1.5
  1695. if (this->SupportsDirectConsole()) {
  1696. reBuild.Variables["pool"] = "console";
  1697. }
  1698. if (this->SupportsManifestRestat() && cm->DoWriteGlobVerifyTarget()) {
  1699. {
  1700. cmNinjaRule rule("VERIFY_GLOBS");
  1701. rule.Command =
  1702. cmStrCat(this->CMakeCmd(), " -P ",
  1703. lg->ConvertToOutputFormat(cm->GetGlobVerifyScript(),
  1704. cmOutputConverter::SHELL));
  1705. rule.Description = "Re-checking globbed directories...";
  1706. rule.Comment = "Rule for re-checking globbed directories.";
  1707. rule.Generator = true;
  1708. this->WriteRule(*this->RulesFileStream, rule);
  1709. }
  1710. cmNinjaBuild phonyBuild("phony");
  1711. phonyBuild.Comment = "Phony target to force glob verification run.";
  1712. phonyBuild.Outputs.push_back(
  1713. cmStrCat(cm->GetGlobVerifyScript(), "_force"));
  1714. this->WriteBuild(os, phonyBuild);
  1715. reBuild.Variables["restat"] = "1";
  1716. std::string const verifyScriptFile =
  1717. this->NinjaOutputPath(cm->GetGlobVerifyScript());
  1718. std::string const verifyStampFile =
  1719. this->NinjaOutputPath(cm->GetGlobVerifyStamp());
  1720. {
  1721. cmNinjaBuild vgBuild("VERIFY_GLOBS");
  1722. vgBuild.Comment =
  1723. "Re-run CMake to check if globbed directories changed.";
  1724. vgBuild.Outputs.push_back(verifyStampFile);
  1725. vgBuild.ImplicitDeps = phonyBuild.Outputs;
  1726. vgBuild.Variables = reBuild.Variables;
  1727. this->WriteBuild(os, vgBuild);
  1728. }
  1729. reBuild.Variables.erase("restat");
  1730. reBuild.ImplicitDeps.push_back(verifyScriptFile);
  1731. reBuild.ExplicitDeps.push_back(verifyStampFile);
  1732. } else if (!this->SupportsManifestRestat() &&
  1733. cm->DoWriteGlobVerifyTarget()) {
  1734. std::ostringstream msg;
  1735. msg << "The detected version of Ninja:\n"
  1736. << " " << this->NinjaVersion << "\n"
  1737. << "is less than the version of Ninja required by CMake for adding "
  1738. "restat dependencies to the build.ninja manifest regeneration "
  1739. "target:\n"
  1740. << " "
  1741. << cmGlobalNinjaGenerator::RequiredNinjaVersionForManifestRestat()
  1742. << "\n";
  1743. msg << "Any pre-check scripts, such as those generated for file(GLOB "
  1744. "CONFIGURE_DEPENDS), will not be run by Ninja.";
  1745. this->GetCMakeInstance()->IssueMessage(MessageType::AUTHOR_WARNING,
  1746. msg.str());
  1747. }
  1748. std::sort(reBuild.ImplicitDeps.begin(), reBuild.ImplicitDeps.end());
  1749. reBuild.ImplicitDeps.erase(
  1750. std::unique(reBuild.ImplicitDeps.begin(), reBuild.ImplicitDeps.end()),
  1751. reBuild.ImplicitDeps.end());
  1752. this->WriteBuild(os, reBuild);
  1753. {
  1754. cmNinjaBuild build("phony");
  1755. build.Comment = "A missing CMake input file is not an error.";
  1756. std::set_difference(std::make_move_iterator(reBuild.ImplicitDeps.begin()),
  1757. std::make_move_iterator(reBuild.ImplicitDeps.end()),
  1758. this->CustomCommandOutputs.begin(),
  1759. this->CustomCommandOutputs.end(),
  1760. std::back_inserter(build.Outputs));
  1761. this->WriteBuild(os, build);
  1762. }
  1763. }
  1764. std::string cmGlobalNinjaGenerator::CMakeCmd() const
  1765. {
  1766. auto const& lgen = this->LocalGenerators.at(0);
  1767. return lgen->ConvertToOutputFormat(cmSystemTools::GetCMakeCommand(),
  1768. cmOutputConverter::SHELL);
  1769. }
  1770. std::string cmGlobalNinjaGenerator::NinjaCmd() const
  1771. {
  1772. auto const& lgen = this->LocalGenerators[0];
  1773. if (lgen) {
  1774. return lgen->ConvertToOutputFormat(this->NinjaCommand,
  1775. cmOutputConverter::SHELL);
  1776. }
  1777. return "ninja";
  1778. }
  1779. bool cmGlobalNinjaGenerator::SupportsDirectConsole() const
  1780. {
  1781. return this->NinjaSupportsConsolePool;
  1782. }
  1783. bool cmGlobalNinjaGenerator::SupportsImplicitOuts() const
  1784. {
  1785. return this->NinjaSupportsImplicitOuts;
  1786. }
  1787. bool cmGlobalNinjaGenerator::SupportsManifestRestat() const
  1788. {
  1789. return this->NinjaSupportsManifestRestat;
  1790. }
  1791. bool cmGlobalNinjaGenerator::SupportsMultilineDepfile() const
  1792. {
  1793. return this->NinjaSupportsMultilineDepfile;
  1794. }
  1795. bool cmGlobalNinjaGenerator::SupportsCWDDepend() const
  1796. {
  1797. return this->NinjaSupportsCWDDepend;
  1798. }
  1799. bool cmGlobalNinjaGenerator::WriteTargetCleanAdditional(std::ostream& os)
  1800. {
  1801. auto const& lgr = this->LocalGenerators.at(0);
  1802. std::string cleanScriptRel = "CMakeFiles/clean_additional.cmake";
  1803. std::string cleanScriptAbs =
  1804. cmStrCat(lgr->GetBinaryDirectory(), '/', cleanScriptRel);
  1805. std::vector<std::string> const& configs = this->GetConfigNames();
  1806. // Check if there are additional files to clean
  1807. bool empty = true;
  1808. for (auto const& config : configs) {
  1809. auto const it = this->Configs.find(config);
  1810. if (it != this->Configs.end() &&
  1811. !it->second.AdditionalCleanFiles.empty()) {
  1812. empty = false;
  1813. break;
  1814. }
  1815. }
  1816. if (empty) {
  1817. // Remove cmake clean script file if it exists
  1818. cmSystemTools::RemoveFile(cleanScriptAbs);
  1819. return false;
  1820. }
  1821. // Write cmake clean script file
  1822. {
  1823. cmGeneratedFileStream fout(cleanScriptAbs);
  1824. if (!fout) {
  1825. return false;
  1826. }
  1827. fout << "# Additional clean files\ncmake_minimum_required(VERSION 3.16)\n";
  1828. for (auto const& config : configs) {
  1829. auto const it = this->Configs.find(config);
  1830. if (it != this->Configs.end() &&
  1831. !it->second.AdditionalCleanFiles.empty()) {
  1832. fout << "\nif(\"${CONFIG}\" STREQUAL \"\" OR \"${CONFIG}\" STREQUAL \""
  1833. << config << "\")\n";
  1834. fout << " file(REMOVE_RECURSE\n";
  1835. for (std::string const& acf : it->second.AdditionalCleanFiles) {
  1836. fout << " "
  1837. << cmOutputConverter::EscapeForCMake(
  1838. this->ConvertToNinjaPath(acf))
  1839. << '\n';
  1840. }
  1841. fout << " )\n";
  1842. fout << "endif()\n";
  1843. }
  1844. }
  1845. }
  1846. // Register clean script file
  1847. lgr->GetMakefile()->AddCMakeOutputFile(cleanScriptAbs);
  1848. // Write rule
  1849. {
  1850. cmNinjaRule rule("CLEAN_ADDITIONAL");
  1851. rule.Command = cmStrCat(
  1852. this->CMakeCmd(), " -DCONFIG=$CONFIG -P ",
  1853. lgr->ConvertToOutputFormat(this->NinjaOutputPath(cleanScriptRel),
  1854. cmOutputConverter::SHELL));
  1855. rule.Description = "Cleaning additional files...";
  1856. rule.Comment = "Rule for cleaning additional files.";
  1857. WriteRule(*this->RulesFileStream, rule);
  1858. }
  1859. // Write build
  1860. {
  1861. cmNinjaBuild build("CLEAN_ADDITIONAL");
  1862. build.Comment = "Clean additional files.";
  1863. build.Outputs.emplace_back();
  1864. for (auto const& config : configs) {
  1865. build.Outputs.front() = this->BuildAlias(
  1866. this->NinjaOutputPath(this->GetAdditionalCleanTargetName()), config);
  1867. build.Variables["CONFIG"] = config;
  1868. this->WriteBuild(os, build);
  1869. }
  1870. if (this->IsMultiConfig()) {
  1871. build.Outputs.front() =
  1872. this->NinjaOutputPath(this->GetAdditionalCleanTargetName());
  1873. build.Variables["CONFIG"] = "";
  1874. this->WriteBuild(os, build);
  1875. }
  1876. }
  1877. // Return success
  1878. return true;
  1879. }
  1880. void cmGlobalNinjaGenerator::WriteTargetClean(std::ostream& os)
  1881. {
  1882. // -- Additional clean target
  1883. bool additionalFiles = this->WriteTargetCleanAdditional(os);
  1884. // -- Default clean target
  1885. // Write rule
  1886. {
  1887. cmNinjaRule rule("CLEAN");
  1888. rule.Command = cmStrCat(this->NinjaCmd(), " $FILE_ARG -t clean $TARGETS");
  1889. rule.Description = "Cleaning all built files...";
  1890. rule.Comment = "Rule for cleaning all built files.";
  1891. WriteRule(*this->RulesFileStream, rule);
  1892. }
  1893. // Write build
  1894. {
  1895. cmNinjaBuild build("CLEAN");
  1896. build.Comment = "Clean all the built files.";
  1897. build.Outputs.emplace_back();
  1898. for (std::string const& config : this->GetConfigNames()) {
  1899. build.Outputs.front() = this->BuildAlias(
  1900. this->NinjaOutputPath(this->GetCleanTargetName()), config);
  1901. if (this->IsMultiConfig()) {
  1902. build.Variables["TARGETS"] = cmStrCat(
  1903. this->BuildAlias(
  1904. this->NinjaOutputPath(GetByproductsForCleanTargetName()), config),
  1905. ' ', this->NinjaOutputPath(GetByproductsForCleanTargetName()));
  1906. }
  1907. build.ExplicitDeps.clear();
  1908. if (additionalFiles) {
  1909. build.ExplicitDeps.push_back(this->BuildAlias(
  1910. this->NinjaOutputPath(this->GetAdditionalCleanTargetName()),
  1911. config));
  1912. }
  1913. for (std::string const& fileConfig : this->GetConfigNames()) {
  1914. if (fileConfig != config && !this->EnableCrossConfigBuild()) {
  1915. continue;
  1916. }
  1917. if (this->IsMultiConfig()) {
  1918. build.Variables["FILE_ARG"] = cmStrCat(
  1919. "-f ",
  1920. this->NinjaOutputPath(
  1921. cmGlobalNinjaMultiGenerator::GetNinjaImplFilename(fileConfig)));
  1922. }
  1923. this->WriteBuild(*this->GetImplFileStream(fileConfig), build);
  1924. }
  1925. }
  1926. if (this->EnableCrossConfigBuild()) {
  1927. build.Outputs.front() = this->BuildAlias(
  1928. this->NinjaOutputPath(this->GetCleanTargetName()), "all");
  1929. build.ExplicitDeps.clear();
  1930. if (additionalFiles) {
  1931. for (auto const& config : this->CrossConfigs) {
  1932. build.ExplicitDeps.push_back(this->BuildAlias(
  1933. this->NinjaOutputPath(this->GetAdditionalCleanTargetName()),
  1934. config));
  1935. }
  1936. }
  1937. std::vector<std::string> byproducts;
  1938. byproducts.reserve(this->CrossConfigs.size());
  1939. for (auto const& config : this->CrossConfigs) {
  1940. byproducts.push_back(this->BuildAlias(
  1941. this->NinjaOutputPath(GetByproductsForCleanTargetName()), config));
  1942. }
  1943. byproducts.emplace_back(GetByproductsForCleanTargetName());
  1944. build.Variables["TARGETS"] = cmJoin(byproducts, " ");
  1945. for (std::string const& fileConfig : this->GetConfigNames()) {
  1946. build.Variables["FILE_ARG"] = cmStrCat(
  1947. "-f ",
  1948. this->NinjaOutputPath(
  1949. cmGlobalNinjaMultiGenerator::GetNinjaImplFilename(fileConfig)));
  1950. this->WriteBuild(*this->GetImplFileStream(fileConfig), build);
  1951. }
  1952. }
  1953. }
  1954. if (this->IsMultiConfig()) {
  1955. cmNinjaBuild build("phony");
  1956. build.Outputs.emplace_back(
  1957. this->NinjaOutputPath(this->GetCleanTargetName()));
  1958. build.ExplicitDeps.emplace_back();
  1959. for (std::string const& config : this->GetConfigNames()) {
  1960. build.ExplicitDeps.front() = this->BuildAlias(
  1961. this->NinjaOutputPath(this->GetCleanTargetName()), config);
  1962. this->WriteBuild(*this->GetConfigFileStream(config), build);
  1963. }
  1964. if (!this->DefaultConfigs.empty()) {
  1965. build.ExplicitDeps.clear();
  1966. for (auto const& config : this->DefaultConfigs) {
  1967. build.ExplicitDeps.push_back(this->BuildAlias(
  1968. this->NinjaOutputPath(this->GetCleanTargetName()), config));
  1969. }
  1970. this->WriteBuild(*this->GetDefaultFileStream(), build);
  1971. }
  1972. }
  1973. // Write byproducts
  1974. if (this->IsMultiConfig()) {
  1975. cmNinjaBuild build("phony");
  1976. build.Comment = "Clean byproducts.";
  1977. build.Outputs.emplace_back(
  1978. this->ConvertToNinjaPath(GetByproductsForCleanTargetName()));
  1979. build.ExplicitDeps = this->ByproductsForCleanTarget;
  1980. this->WriteBuild(os, build);
  1981. for (std::string const& config : this->GetConfigNames()) {
  1982. build.Outputs.front() = this->BuildAlias(
  1983. this->ConvertToNinjaPath(GetByproductsForCleanTargetName()), config);
  1984. build.ExplicitDeps = this->Configs[config].ByproductsForCleanTarget;
  1985. this->WriteBuild(os, build);
  1986. }
  1987. }
  1988. }
  1989. void cmGlobalNinjaGenerator::WriteTargetHelp(std::ostream& os)
  1990. {
  1991. {
  1992. cmNinjaRule rule("HELP");
  1993. rule.Command = cmStrCat(this->NinjaCmd(), " -t targets");
  1994. rule.Description = "All primary targets available:";
  1995. rule.Comment = "Rule for printing all primary targets available.";
  1996. WriteRule(*this->RulesFileStream, rule);
  1997. }
  1998. {
  1999. cmNinjaBuild build("HELP");
  2000. build.Comment = "Print all primary targets available.";
  2001. build.Outputs.push_back(this->NinjaOutputPath("help"));
  2002. this->WriteBuild(os, build);
  2003. }
  2004. }
  2005. #if !defined(CMAKE_BOOTSTRAP) && !defined(_WIN32)
  2006. // FIXME(#26668) This does not work on Windows
  2007. void cmGlobalNinjaGenerator::WriteTargetInstrument(std::ostream& os)
  2008. {
  2009. // Write rule
  2010. {
  2011. cmNinjaRule rule("START_INSTRUMENT");
  2012. rule.Command = cmStrCat(
  2013. '"', cmSystemTools::GetCTestCommand(), "\" --start-instrumentation \"",
  2014. this->GetCMakeInstance()->GetHomeOutputDirectory(), '"');
  2015. /*
  2016. * On Unix systems, Ninja will prefix the command with `/bin/sh -c`.
  2017. * Use exec so that Ninja is the parent process of the command.
  2018. */
  2019. rule.Command = cmStrCat("exec ", rule.Command);
  2020. rule.Description = "Collecting build metrics";
  2021. rule.Comment = "Rule to initialize instrumentation daemon.";
  2022. rule.Restat = "1";
  2023. WriteRule(*this->RulesFileStream, rule);
  2024. }
  2025. // Write build
  2026. {
  2027. cmNinjaBuild phony("phony");
  2028. phony.Comment = "Phony target to keep START_INSTRUMENTATION out of date.";
  2029. phony.Outputs.push_back(this->NinjaOutputPath("CMakeFiles/instrument"));
  2030. cmNinjaBuild instrument("START_INSTRUMENT");
  2031. instrument.Comment = "Start instrumentation daemon.";
  2032. instrument.Outputs.push_back(this->NinjaOutputPath("start_instrument"));
  2033. instrument.ExplicitDeps.push_back(
  2034. this->NinjaOutputPath("CMakeFiles/instrument"));
  2035. WriteBuild(os, phony);
  2036. WriteBuild(os, instrument);
  2037. }
  2038. }
  2039. #endif
  2040. void cmGlobalNinjaGenerator::InitOutputPathPrefix()
  2041. {
  2042. this->OutputPathPrefix =
  2043. this->LocalGenerators[0]->GetMakefile()->GetSafeDefinition(
  2044. "CMAKE_NINJA_OUTPUT_PATH_PREFIX");
  2045. EnsureTrailingSlash(this->OutputPathPrefix);
  2046. }
  2047. std::string cmGlobalNinjaGenerator::NinjaOutputPath(
  2048. std::string const& path) const
  2049. {
  2050. if (!this->HasOutputPathPrefix() || cmSystemTools::FileIsFullPath(path)) {
  2051. return path;
  2052. }
  2053. return cmStrCat(this->OutputPathPrefix, path);
  2054. }
  2055. void cmGlobalNinjaGenerator::StripNinjaOutputPathPrefixAsSuffix(
  2056. std::string& path)
  2057. {
  2058. if (path.empty()) {
  2059. return;
  2060. }
  2061. EnsureTrailingSlash(path);
  2062. cmStripSuffixIfExists(path, this->OutputPathPrefix);
  2063. }
  2064. #if !defined(CMAKE_BOOTSTRAP)
  2065. /*
  2066. We use the following approach to support Fortran. Each target already
  2067. has a <target>.dir/ directory used to hold intermediate files for CMake.
  2068. For each target, a FortranDependInfo.json file is generated by CMake with
  2069. information about include directories, module directories, and the locations
  2070. the per-target directories for target dependencies.
  2071. Compilation of source files within a target is split into the following steps:
  2072. 1. Preprocess all sources, scan preprocessed output for module dependencies.
  2073. This step is done with independent build statements for each source,
  2074. and can therefore be done in parallel.
  2075. rule Fortran_PREPROCESS
  2076. depfile = $DEP_FILE
  2077. command = gfortran -cpp $DEFINES $INCLUDES $FLAGS -E $in -o $out &&
  2078. cmake -E cmake_ninja_depends \
  2079. --tdi=FortranDependInfo.json --lang=Fortran \
  2080. --src=$out --out=$out --dep=$DEP_FILE --obj=$OBJ_FILE \
  2081. --ddi=$DYNDEP_INTERMEDIATE_FILE
  2082. build src.f90-pp.f90 | src.f90.o.ddi: Fortran_PREPROCESS src.f90
  2083. OBJ_FILE = src.f90.o
  2084. DEP_FILE = src.f90.o.d
  2085. DYNDEP_INTERMEDIATE_FILE = src.f90.o.ddi
  2086. The ``cmake -E cmake_ninja_depends`` tool reads the preprocessed output
  2087. and generates the ninja depfile for preprocessor dependencies. It also
  2088. generates a "ddi" file (in a format private to CMake) that lists the
  2089. object file that compilation will produce along with the module names
  2090. it provides and/or requires. The "ddi" file is an implicit output
  2091. because it should not appear in "$out" but is generated by the rule.
  2092. 2. Consolidate the per-source module dependencies saved in the "ddi"
  2093. files from all sources to produce a ninja "dyndep" file, ``Fortran.dd``.
  2094. rule Fortran_DYNDEP
  2095. command = cmake -E cmake_ninja_dyndep \
  2096. --tdi=FortranDependInfo.json --lang=Fortran --dd=$out $in
  2097. build Fortran.dd: Fortran_DYNDEP src1.f90.o.ddi src2.f90.o.ddi
  2098. The ``cmake -E cmake_ninja_dyndep`` tool reads the "ddi" files from all
  2099. sources in the target and the ``FortranModules.json`` files from targets
  2100. on which the target depends. It computes dependency edges on compilations
  2101. that require modules to those that provide the modules. This information
  2102. is placed in the ``Fortran.dd`` file for ninja to load later. It also
  2103. writes the expected location of modules provided by this target into
  2104. ``FortranModules.json`` for use by dependent targets.
  2105. 3. Compile all sources after loading dynamically discovered dependencies
  2106. of the compilation build statements from their ``dyndep`` bindings.
  2107. rule Fortran_COMPILE
  2108. command = gfortran $INCLUDES $FLAGS -c $in -o $out
  2109. build src1.f90.o: Fortran_COMPILE src1.f90-pp.f90 || Fortran.dd
  2110. dyndep = Fortran.dd
  2111. The "dyndep" binding tells ninja to load dynamically discovered
  2112. dependency information from ``Fortran.dd``. This adds information
  2113. such as:
  2114. build src1.f90.o | mod1.mod: dyndep
  2115. restat = 1
  2116. This tells ninja that ``mod1.mod`` is an implicit output of compiling
  2117. the object file ``src1.f90.o``. The ``restat`` binding tells it that
  2118. the timestamp of the output may not always change. Additionally:
  2119. build src2.f90.o: dyndep | mod1.mod
  2120. This tells ninja that ``mod1.mod`` is a dependency of compiling the
  2121. object file ``src2.f90.o``. This ensures that ``src1.f90.o`` and
  2122. ``mod1.mod`` will always be up to date before ``src2.f90.o`` is built
  2123. (because the latter consumes the module).
  2124. */
  2125. namespace {
  2126. struct cmSourceInfo
  2127. {
  2128. cmScanDepInfo ScanDep;
  2129. std::vector<std::string> Includes;
  2130. };
  2131. cm::optional<cmSourceInfo> cmcmd_cmake_ninja_depends_fortran(
  2132. std::string const& arg_tdi, std::string const& arg_src,
  2133. std::string const& arg_src_orig);
  2134. }
  2135. int cmcmd_cmake_ninja_depends(std::vector<std::string>::const_iterator argBeg,
  2136. std::vector<std::string>::const_iterator argEnd)
  2137. {
  2138. std::string arg_tdi;
  2139. std::string arg_src;
  2140. std::string arg_src_orig;
  2141. std::string arg_out;
  2142. std::string arg_dep;
  2143. std::string arg_obj;
  2144. std::string arg_ddi;
  2145. std::string arg_lang;
  2146. for (std::string const& arg : cmMakeRange(argBeg, argEnd)) {
  2147. if (cmHasLiteralPrefix(arg, "--tdi=")) {
  2148. arg_tdi = arg.substr(6);
  2149. } else if (cmHasLiteralPrefix(arg, "--src=")) {
  2150. arg_src = arg.substr(6);
  2151. } else if (cmHasLiteralPrefix(arg, "--src-orig=")) {
  2152. arg_src_orig = arg.substr(11);
  2153. } else if (cmHasLiteralPrefix(arg, "--out=")) {
  2154. arg_out = arg.substr(6);
  2155. } else if (cmHasLiteralPrefix(arg, "--dep=")) {
  2156. arg_dep = arg.substr(6);
  2157. } else if (cmHasLiteralPrefix(arg, "--obj=")) {
  2158. arg_obj = arg.substr(6);
  2159. } else if (cmHasLiteralPrefix(arg, "--ddi=")) {
  2160. arg_ddi = arg.substr(6);
  2161. } else if (cmHasLiteralPrefix(arg, "--lang=")) {
  2162. arg_lang = arg.substr(7);
  2163. } else if (cmHasLiteralPrefix(arg, "--pp=")) {
  2164. // CMake 3.26 and below used '--pp=' instead of '--src=' and '--out='.
  2165. arg_src = arg.substr(5);
  2166. arg_out = arg_src;
  2167. } else {
  2168. cmSystemTools::Error(
  2169. cmStrCat("-E cmake_ninja_depends unknown argument: ", arg));
  2170. return 1;
  2171. }
  2172. }
  2173. if (arg_tdi.empty()) {
  2174. cmSystemTools::Error("-E cmake_ninja_depends requires value for --tdi=");
  2175. return 1;
  2176. }
  2177. if (arg_src.empty()) {
  2178. cmSystemTools::Error("-E cmake_ninja_depends requires value for --src=");
  2179. return 1;
  2180. }
  2181. if (arg_out.empty()) {
  2182. cmSystemTools::Error("-E cmake_ninja_depends requires value for --out=");
  2183. return 1;
  2184. }
  2185. if (arg_dep.empty()) {
  2186. cmSystemTools::Error("-E cmake_ninja_depends requires value for --dep=");
  2187. return 1;
  2188. }
  2189. if (arg_obj.empty()) {
  2190. cmSystemTools::Error("-E cmake_ninja_depends requires value for --obj=");
  2191. return 1;
  2192. }
  2193. if (arg_ddi.empty()) {
  2194. cmSystemTools::Error("-E cmake_ninja_depends requires value for --ddi=");
  2195. return 1;
  2196. }
  2197. if (arg_lang.empty()) {
  2198. cmSystemTools::Error("-E cmake_ninja_depends requires value for --lang=");
  2199. return 1;
  2200. }
  2201. cm::optional<cmSourceInfo> info;
  2202. if (arg_lang == "Fortran") {
  2203. info = cmcmd_cmake_ninja_depends_fortran(arg_tdi, arg_src, arg_src_orig);
  2204. } else {
  2205. cmSystemTools::Error(
  2206. cmStrCat("-E cmake_ninja_depends does not understand the ", arg_lang,
  2207. " language"));
  2208. return 1;
  2209. }
  2210. if (!info) {
  2211. // The error message is already expected to have been output.
  2212. return 1;
  2213. }
  2214. info->ScanDep.PrimaryOutput = arg_obj;
  2215. {
  2216. cmGeneratedFileStream depfile(arg_dep);
  2217. depfile << cmSystemTools::ConvertToUnixOutputPath(arg_out) << ":";
  2218. for (std::string const& include : info->Includes) {
  2219. depfile << " \\\n " << cmSystemTools::ConvertToUnixOutputPath(include);
  2220. }
  2221. depfile << "\n";
  2222. }
  2223. if (!cmScanDepFormat_P1689_Write(arg_ddi, info->ScanDep)) {
  2224. cmSystemTools::Error(
  2225. cmStrCat("-E cmake_ninja_depends failed to write ", arg_ddi));
  2226. return 1;
  2227. }
  2228. return 0;
  2229. }
  2230. namespace {
  2231. cm::optional<cmSourceInfo> cmcmd_cmake_ninja_depends_fortran(
  2232. std::string const& arg_tdi, std::string const& arg_src,
  2233. std::string const& arg_src_orig)
  2234. {
  2235. cm::optional<cmSourceInfo> info;
  2236. cmFortranCompiler fc;
  2237. std::vector<std::string> includes;
  2238. std::string dir_top_bld;
  2239. std::string module_dir;
  2240. if (!arg_src_orig.empty()) {
  2241. // Prepend the original source file's directory as an include directory
  2242. // so Fortran INCLUDE statements can look for files in it.
  2243. std::string src_orig_dir = cmSystemTools::GetParentDirectory(arg_src_orig);
  2244. if (!src_orig_dir.empty()) {
  2245. includes.push_back(src_orig_dir);
  2246. }
  2247. }
  2248. {
  2249. Json::Value tdio;
  2250. Json::Value const& tdi = tdio;
  2251. {
  2252. cmsys::ifstream tdif(arg_tdi.c_str(), std::ios::in | std::ios::binary);
  2253. Json::Reader reader;
  2254. if (!reader.parse(tdif, tdio, false)) {
  2255. cmSystemTools::Error(
  2256. cmStrCat("-E cmake_ninja_depends failed to parse ", arg_tdi,
  2257. reader.getFormattedErrorMessages()));
  2258. return info;
  2259. }
  2260. }
  2261. dir_top_bld = tdi["dir-top-bld"].asString();
  2262. if (!dir_top_bld.empty() && !cmHasLiteralSuffix(dir_top_bld, "/")) {
  2263. dir_top_bld += '/';
  2264. }
  2265. Json::Value const& tdi_include_dirs = tdi["include-dirs"];
  2266. if (tdi_include_dirs.isArray()) {
  2267. for (auto const& tdi_include_dir : tdi_include_dirs) {
  2268. includes.push_back(tdi_include_dir.asString());
  2269. }
  2270. }
  2271. Json::Value const& tdi_module_dir = tdi["module-dir"];
  2272. module_dir = tdi_module_dir.asString();
  2273. if (!module_dir.empty() && !cmHasLiteralSuffix(module_dir, "/")) {
  2274. module_dir += '/';
  2275. }
  2276. Json::Value const& tdi_compiler_id = tdi["compiler-id"];
  2277. fc.Id = tdi_compiler_id.asString();
  2278. Json::Value const& tdi_submodule_sep = tdi["submodule-sep"];
  2279. fc.SModSep = tdi_submodule_sep.asString();
  2280. Json::Value const& tdi_submodule_ext = tdi["submodule-ext"];
  2281. fc.SModExt = tdi_submodule_ext.asString();
  2282. }
  2283. cmFortranSourceInfo finfo;
  2284. std::set<std::string> defines;
  2285. cmFortranParser parser(fc, includes, defines, finfo);
  2286. if (!cmFortranParser_FilePush(&parser, arg_src.c_str())) {
  2287. cmSystemTools::Error(
  2288. cmStrCat("-E cmake_ninja_depends failed to open ", arg_src));
  2289. return info;
  2290. }
  2291. if (cmFortran_yyparse(parser.Scanner) != 0) {
  2292. // Failed to parse the file.
  2293. return info;
  2294. }
  2295. info = cmSourceInfo();
  2296. for (std::string const& provide : finfo.Provides) {
  2297. cmSourceReqInfo src_info;
  2298. src_info.LogicalName = provide;
  2299. if (!module_dir.empty()) {
  2300. std::string mod = cmStrCat(module_dir, provide);
  2301. if (!dir_top_bld.empty() && cmHasPrefix(mod, dir_top_bld)) {
  2302. mod = mod.substr(dir_top_bld.size());
  2303. }
  2304. src_info.CompiledModulePath = std::move(mod);
  2305. }
  2306. info->ScanDep.Provides.emplace_back(src_info);
  2307. }
  2308. for (std::string const& require : finfo.Requires) {
  2309. // Require modules not provided in the same source.
  2310. if (finfo.Provides.count(require)) {
  2311. continue;
  2312. }
  2313. cmSourceReqInfo src_info;
  2314. src_info.LogicalName = require;
  2315. info->ScanDep.Requires.emplace_back(src_info);
  2316. }
  2317. for (std::string const& include : finfo.Includes) {
  2318. info->Includes.push_back(include);
  2319. }
  2320. return info;
  2321. }
  2322. }
  2323. bool cmGlobalNinjaGenerator::WriteDyndepFile(
  2324. std::string const& dir_top_src, std::string const& dir_top_bld,
  2325. std::string const& dir_cur_src, std::string const& dir_cur_bld,
  2326. std::string const& arg_dd, std::vector<std::string> const& arg_ddis,
  2327. std::string const& module_dir,
  2328. std::vector<std::string> const& linked_target_dirs,
  2329. std::vector<std::string> const& forward_modules_from_target_dirs,
  2330. std::string const& arg_lang, std::string const& arg_modmapfmt,
  2331. cmCxxModuleExportInfo const& export_info)
  2332. {
  2333. // Setup path conversions.
  2334. {
  2335. cmStateSnapshot snapshot = this->GetCMakeInstance()->GetCurrentSnapshot();
  2336. snapshot.GetDirectory().SetCurrentSource(dir_cur_src);
  2337. snapshot.GetDirectory().SetCurrentBinary(dir_cur_bld);
  2338. auto mfd = cm::make_unique<cmMakefile>(this, snapshot);
  2339. auto lgd = this->CreateLocalGenerator(mfd.get());
  2340. lgd->SetRelativePathTop(dir_top_src, dir_top_bld);
  2341. this->Makefiles.push_back(std::move(mfd));
  2342. this->LocalGenerators.push_back(std::move(lgd));
  2343. }
  2344. std::vector<cmScanDepInfo> objects;
  2345. for (std::string const& arg_ddi : arg_ddis) {
  2346. cmScanDepInfo info;
  2347. if (!cmScanDepFormat_P1689_Parse(arg_ddi, &info)) {
  2348. cmSystemTools::Error(
  2349. cmStrCat("-E cmake_ninja_dyndep failed to parse ddi file ", arg_ddi));
  2350. return false;
  2351. }
  2352. objects.push_back(std::move(info));
  2353. }
  2354. CxxModuleUsage usages;
  2355. // Map from module name to module file path, if known.
  2356. struct AvailableModuleInfo
  2357. {
  2358. std::string BmiPath;
  2359. bool IsPrivate;
  2360. };
  2361. std::map<std::string, AvailableModuleInfo> mod_files;
  2362. // Populate the module map with those provided by linked targets first.
  2363. for (std::string const& linked_target_dir : linked_target_dirs) {
  2364. std::string const ltmn =
  2365. cmStrCat(linked_target_dir, '/', arg_lang, "Modules.json");
  2366. Json::Value ltm;
  2367. cmsys::ifstream ltmf(ltmn.c_str(), std::ios::in | std::ios::binary);
  2368. if (!ltmf) {
  2369. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to open ",
  2370. ltmn, " for module information"));
  2371. return false;
  2372. }
  2373. Json::Reader reader;
  2374. if (!reader.parse(ltmf, ltm, false)) {
  2375. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
  2376. linked_target_dir,
  2377. reader.getFormattedErrorMessages()));
  2378. return false;
  2379. }
  2380. if (ltm.isObject()) {
  2381. Json::Value const& target_modules = ltm["modules"];
  2382. if (target_modules.isObject()) {
  2383. for (auto i = target_modules.begin(); i != target_modules.end(); ++i) {
  2384. Json::Value const& visible_module = *i;
  2385. if (visible_module.isObject()) {
  2386. Json::Value const& bmi_path = visible_module["bmi"];
  2387. Json::Value const& is_private = visible_module["is-private"];
  2388. mod_files[i.key().asString()] = AvailableModuleInfo{
  2389. bmi_path.asString(),
  2390. is_private.asBool(),
  2391. };
  2392. }
  2393. }
  2394. }
  2395. Json::Value const& target_modules_references = ltm["references"];
  2396. if (target_modules_references.isObject()) {
  2397. for (auto i = target_modules_references.begin();
  2398. i != target_modules_references.end(); ++i) {
  2399. if (i->isObject()) {
  2400. Json::Value const& reference_path = (*i)["path"];
  2401. CxxModuleReference module_reference;
  2402. if (reference_path.isString()) {
  2403. module_reference.Path = reference_path.asString();
  2404. }
  2405. Json::Value const& reference_method = (*i)["lookup-method"];
  2406. if (reference_method.isString()) {
  2407. std::string reference = reference_method.asString();
  2408. if (reference == "by-name") {
  2409. module_reference.Method = LookupMethod::ByName;
  2410. } else if (reference == "include-angle") {
  2411. module_reference.Method = LookupMethod::IncludeAngle;
  2412. } else if (reference == "include-quote") {
  2413. module_reference.Method = LookupMethod::IncludeQuote;
  2414. }
  2415. }
  2416. usages.Reference[i.key().asString()] = module_reference;
  2417. }
  2418. }
  2419. }
  2420. Json::Value const& target_modules_usage = ltm["usages"];
  2421. if (target_modules_usage.isObject()) {
  2422. for (auto i = target_modules_usage.begin();
  2423. i != target_modules_usage.end(); ++i) {
  2424. if (i->isArray()) {
  2425. for (auto j = i->begin(); j != i->end(); ++j) {
  2426. usages.Usage[i.key().asString()].insert(j->asString());
  2427. }
  2428. }
  2429. }
  2430. }
  2431. }
  2432. }
  2433. cm::optional<CxxModuleMapFormat> modmap_fmt;
  2434. if (arg_modmapfmt.empty()) {
  2435. // nothing to do.
  2436. } else if (arg_modmapfmt == "clang") {
  2437. modmap_fmt = CxxModuleMapFormat::Clang;
  2438. } else if (arg_modmapfmt == "gcc") {
  2439. modmap_fmt = CxxModuleMapFormat::Gcc;
  2440. } else if (arg_modmapfmt == "msvc") {
  2441. modmap_fmt = CxxModuleMapFormat::Msvc;
  2442. } else {
  2443. cmSystemTools::Error(
  2444. cmStrCat("-E cmake_ninja_dyndep does not understand the ", arg_modmapfmt,
  2445. " module map format"));
  2446. return false;
  2447. }
  2448. auto module_ext = CxxModuleMapExtension(modmap_fmt);
  2449. // Extend the module map with those provided by this target.
  2450. // We do this after loading the modules provided by linked targets
  2451. // in case we have one of the same name that must be preferred.
  2452. Json::Value target_modules = Json::objectValue;
  2453. for (cmScanDepInfo const& object : objects) {
  2454. for (auto const& p : object.Provides) {
  2455. std::string mod;
  2456. if (cmDyndepCollation::IsBmiOnly(export_info, object.PrimaryOutput)) {
  2457. mod = object.PrimaryOutput;
  2458. } else if (!p.CompiledModulePath.empty()) {
  2459. // The scanner provided the path to the module file.
  2460. mod = p.CompiledModulePath;
  2461. if (!cmSystemTools::FileIsFullPath(mod)) {
  2462. // Treat relative to work directory (top of build tree).
  2463. mod = cmSystemTools::CollapseFullPath(mod, dir_top_bld);
  2464. }
  2465. } else {
  2466. // Assume the module file path matches the logical module name.
  2467. std::string safe_logical_name =
  2468. p.LogicalName; // TODO: needs fixing for header units
  2469. cmSystemTools::ReplaceString(safe_logical_name, ":", "-");
  2470. mod = cmStrCat(module_dir, safe_logical_name, module_ext);
  2471. }
  2472. mod_files[p.LogicalName] = AvailableModuleInfo{
  2473. mod,
  2474. false, // Always visible within our own target.
  2475. };
  2476. Json::Value& module_info = target_modules[p.LogicalName] =
  2477. Json::objectValue;
  2478. module_info["bmi"] = mod;
  2479. module_info["is-private"] =
  2480. cmDyndepCollation::IsObjectPrivate(object.PrimaryOutput, export_info);
  2481. }
  2482. }
  2483. cmGeneratedFileStream ddf(arg_dd);
  2484. ddf << "ninja_dyndep_version = 1.0\n";
  2485. {
  2486. CxxModuleLocations locs;
  2487. locs.RootDirectory = ".";
  2488. locs.PathForGenerator = [this](std::string path) -> std::string {
  2489. path = this->ConvertToNinjaPath(path);
  2490. # ifdef _WIN32
  2491. if (this->IsGCCOnWindows()) {
  2492. std::replace(path.begin(), path.end(), '\\', '/');
  2493. }
  2494. # endif
  2495. return path;
  2496. };
  2497. locs.BmiLocationForModule =
  2498. [&mod_files](std::string const& logical) -> CxxBmiLocation {
  2499. auto m = mod_files.find(logical);
  2500. if (m != mod_files.end()) {
  2501. if (m->second.IsPrivate) {
  2502. return CxxBmiLocation::Private();
  2503. }
  2504. return CxxBmiLocation::Known(m->second.BmiPath);
  2505. }
  2506. return CxxBmiLocation::Unknown();
  2507. };
  2508. // Insert information about the current target's modules.
  2509. if (modmap_fmt) {
  2510. bool private_usage_found = false;
  2511. auto cycle_modules =
  2512. CxxModuleUsageSeed(locs, objects, usages, private_usage_found);
  2513. if (!cycle_modules.empty()) {
  2514. cmSystemTools::Error(
  2515. cmStrCat("Circular dependency detected in the C++ module import "
  2516. "graph. See modules named: \"",
  2517. cmJoin(cycle_modules, R"(", ")"_s), '"'));
  2518. return false;
  2519. }
  2520. if (private_usage_found) {
  2521. // Already errored in the function.
  2522. return false;
  2523. }
  2524. }
  2525. cmNinjaBuild build("dyndep");
  2526. build.Outputs.emplace_back("");
  2527. for (cmScanDepInfo const& object : objects) {
  2528. build.Outputs[0] = this->ConvertToNinjaPath(object.PrimaryOutput);
  2529. build.ImplicitOuts.clear();
  2530. for (auto const& p : object.Provides) {
  2531. auto const implicitOut =
  2532. this->ConvertToNinjaPath(mod_files[p.LogicalName].BmiPath);
  2533. // Ignore the `provides` when the BMI is the output.
  2534. if (implicitOut != build.Outputs[0]) {
  2535. build.ImplicitOuts.emplace_back(implicitOut);
  2536. }
  2537. }
  2538. build.ImplicitDeps.clear();
  2539. for (auto const& r : object.Requires) {
  2540. auto mit = mod_files.find(r.LogicalName);
  2541. if (mit != mod_files.end()) {
  2542. build.ImplicitDeps.push_back(
  2543. this->ConvertToNinjaPath(mit->second.BmiPath));
  2544. }
  2545. }
  2546. build.Variables.clear();
  2547. if (!object.Provides.empty()) {
  2548. build.Variables.emplace("restat", "1");
  2549. }
  2550. if (modmap_fmt) {
  2551. auto mm = CxxModuleMapContent(*modmap_fmt, locs, object, usages);
  2552. // XXX(modmap): If changing this path construction, change
  2553. // `cmNinjaTargetGenerator::WriteObjectBuildStatements` and
  2554. // `cmNinjaTargetGenerator::ExportObjectCompileCommand` to generate the
  2555. // corresponding file path.
  2556. cmGeneratedFileStream mmf;
  2557. mmf.Open(cmStrCat(object.PrimaryOutput, ".modmap"), false,
  2558. CxxModuleMapOpenMode(*modmap_fmt) ==
  2559. CxxModuleMapMode::Binary);
  2560. mmf.SetCopyIfDifferent(true);
  2561. mmf << mm;
  2562. }
  2563. this->WriteBuild(ddf, build);
  2564. }
  2565. }
  2566. Json::Value target_module_info = Json::objectValue;
  2567. target_module_info["modules"] = target_modules;
  2568. auto& target_usages = target_module_info["usages"] = Json::objectValue;
  2569. for (auto const& u : usages.Usage) {
  2570. auto& mod_usage = target_usages[u.first] = Json::arrayValue;
  2571. for (auto const& v : u.second) {
  2572. mod_usage.append(v);
  2573. }
  2574. }
  2575. auto name_for_method = [](LookupMethod method) -> cm::static_string_view {
  2576. switch (method) {
  2577. case LookupMethod::ByName:
  2578. return "by-name"_s;
  2579. case LookupMethod::IncludeAngle:
  2580. return "include-angle"_s;
  2581. case LookupMethod::IncludeQuote:
  2582. return "include-quote"_s;
  2583. }
  2584. assert(false && "unsupported lookup method");
  2585. return ""_s;
  2586. };
  2587. auto& target_references = target_module_info["references"] =
  2588. Json::objectValue;
  2589. for (auto const& r : usages.Reference) {
  2590. auto& mod_ref = target_references[r.first] = Json::objectValue;
  2591. mod_ref["path"] = r.second.Path;
  2592. mod_ref["lookup-method"] = std::string(name_for_method(r.second.Method));
  2593. }
  2594. // Store the map of modules provided by this target in a file for
  2595. // use by dependents that reference this target in linked-target-dirs.
  2596. std::string const target_mods_file = cmStrCat(
  2597. cmSystemTools::GetFilenamePath(arg_dd), '/', arg_lang, "Modules.json");
  2598. // Populate the module map with those provided by linked targets first.
  2599. for (std::string const& forward_modules_from_target_dir :
  2600. forward_modules_from_target_dirs) {
  2601. std::string const fmftn =
  2602. cmStrCat(forward_modules_from_target_dir, '/', arg_lang, "Modules.json");
  2603. Json::Value fmft;
  2604. cmsys::ifstream fmftf(fmftn.c_str(), std::ios::in | std::ios::binary);
  2605. if (!fmftf) {
  2606. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to open ",
  2607. fmftn, " for module information"));
  2608. return false;
  2609. }
  2610. Json::Reader reader;
  2611. if (!reader.parse(fmftf, fmft, false)) {
  2612. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
  2613. forward_modules_from_target_dir,
  2614. reader.getFormattedErrorMessages()));
  2615. return false;
  2616. }
  2617. if (!fmft.isObject()) {
  2618. continue;
  2619. }
  2620. auto forward_info = [](Json::Value& target, Json::Value const& source) {
  2621. if (!source.isObject()) {
  2622. return;
  2623. }
  2624. for (auto i = source.begin(); i != source.end(); ++i) {
  2625. std::string const key = i.key().asString();
  2626. if (target.isMember(key)) {
  2627. continue;
  2628. }
  2629. target[key] = *i;
  2630. }
  2631. };
  2632. // Forward info from forwarding targets into our collation.
  2633. Json::Value& tmi_target_modules = target_module_info["modules"];
  2634. forward_info(tmi_target_modules, fmft["modules"]);
  2635. forward_info(target_references, fmft["references"]);
  2636. forward_info(target_usages, fmft["usages"]);
  2637. }
  2638. cmGeneratedFileStream tmf(target_mods_file);
  2639. tmf.SetCopyIfDifferent(true);
  2640. tmf << target_module_info;
  2641. cmDyndepMetadataCallbacks cb;
  2642. cb.ModuleFile =
  2643. [mod_files](std::string const& name) -> cm::optional<std::string> {
  2644. auto m = mod_files.find(name);
  2645. if (m != mod_files.end()) {
  2646. return m->second.BmiPath;
  2647. }
  2648. return {};
  2649. };
  2650. return cmDyndepCollation::WriteDyndepMetadata(arg_lang, objects, export_info,
  2651. cb);
  2652. }
  2653. int cmcmd_cmake_ninja_dyndep(std::vector<std::string>::const_iterator argBeg,
  2654. std::vector<std::string>::const_iterator argEnd)
  2655. {
  2656. std::vector<std::string> arg_full =
  2657. cmSystemTools::HandleResponseFile(argBeg, argEnd);
  2658. std::string arg_dd;
  2659. std::string arg_lang;
  2660. std::string arg_tdi;
  2661. std::string arg_modmapfmt;
  2662. std::vector<std::string> arg_ddis;
  2663. for (std::string const& arg : arg_full) {
  2664. if (cmHasLiteralPrefix(arg, "--tdi=")) {
  2665. arg_tdi = arg.substr(6);
  2666. } else if (cmHasLiteralPrefix(arg, "--lang=")) {
  2667. arg_lang = arg.substr(7);
  2668. } else if (cmHasLiteralPrefix(arg, "--dd=")) {
  2669. arg_dd = arg.substr(5);
  2670. } else if (cmHasLiteralPrefix(arg, "--modmapfmt=")) {
  2671. arg_modmapfmt = arg.substr(12);
  2672. } else if (!cmHasLiteralPrefix(arg, "--") &&
  2673. cmHasLiteralSuffix(arg, ".ddi")) {
  2674. arg_ddis.push_back(arg);
  2675. } else {
  2676. cmSystemTools::Error(
  2677. cmStrCat("-E cmake_ninja_dyndep unknown argument: ", arg));
  2678. return 1;
  2679. }
  2680. }
  2681. if (arg_tdi.empty()) {
  2682. cmSystemTools::Error("-E cmake_ninja_dyndep requires value for --tdi=");
  2683. return 1;
  2684. }
  2685. if (arg_lang.empty()) {
  2686. cmSystemTools::Error("-E cmake_ninja_dyndep requires value for --lang=");
  2687. return 1;
  2688. }
  2689. if (arg_dd.empty()) {
  2690. cmSystemTools::Error("-E cmake_ninja_dyndep requires value for --dd=");
  2691. return 1;
  2692. }
  2693. Json::Value tdio;
  2694. Json::Value const& tdi = tdio;
  2695. {
  2696. cmsys::ifstream tdif(arg_tdi.c_str(), std::ios::in | std::ios::binary);
  2697. Json::Reader reader;
  2698. if (!reader.parse(tdif, tdio, false)) {
  2699. cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
  2700. arg_tdi,
  2701. reader.getFormattedErrorMessages()));
  2702. return 1;
  2703. }
  2704. }
  2705. std::string const dir_cur_bld = tdi["dir-cur-bld"].asString();
  2706. std::string const dir_cur_src = tdi["dir-cur-src"].asString();
  2707. std::string const dir_top_bld = tdi["dir-top-bld"].asString();
  2708. std::string const dir_top_src = tdi["dir-top-src"].asString();
  2709. std::string module_dir = tdi["module-dir"].asString();
  2710. if (!module_dir.empty() && !cmHasLiteralSuffix(module_dir, "/")) {
  2711. module_dir += '/';
  2712. }
  2713. std::vector<std::string> linked_target_dirs;
  2714. Json::Value const& tdi_linked_target_dirs = tdi["linked-target-dirs"];
  2715. if (tdi_linked_target_dirs.isArray()) {
  2716. for (auto const& tdi_linked_target_dir : tdi_linked_target_dirs) {
  2717. linked_target_dirs.push_back(tdi_linked_target_dir.asString());
  2718. }
  2719. }
  2720. std::vector<std::string> forward_modules_from_target_dirs;
  2721. Json::Value const& tdi_forward_modules_from_target_dirs =
  2722. tdi["forward-modules-from-target-dirs"];
  2723. if (tdi_forward_modules_from_target_dirs.isArray()) {
  2724. for (auto const& tdi_forward_modules_from_target_dir :
  2725. tdi_forward_modules_from_target_dirs) {
  2726. forward_modules_from_target_dirs.push_back(
  2727. tdi_forward_modules_from_target_dir.asString());
  2728. }
  2729. }
  2730. std::string const compilerId = tdi["compiler-id"].asString();
  2731. std::string const simulateId = tdi["compiler-simulate-id"].asString();
  2732. std::string const compilerFrontendVariant =
  2733. tdi["compiler-frontend-variant"].asString();
  2734. auto export_info = cmDyndepCollation::ParseExportInfo(tdi);
  2735. cmake cm(cmake::RoleInternal, cmState::Unknown);
  2736. cm.SetHomeDirectory(dir_top_src);
  2737. cm.SetHomeOutputDirectory(dir_top_bld);
  2738. auto ggd = cm.CreateGlobalGenerator("Ninja");
  2739. if (!ggd) {
  2740. return 1;
  2741. }
  2742. cmGlobalNinjaGenerator& gg =
  2743. cm::static_reference_cast<cmGlobalNinjaGenerator>(ggd);
  2744. # ifdef _WIN32
  2745. if (DetectGCCOnWindows(compilerId, simulateId, compilerFrontendVariant)) {
  2746. gg.MarkAsGCCOnWindows();
  2747. }
  2748. # endif
  2749. return gg.WriteDyndepFile(dir_top_src, dir_top_bld, dir_cur_src, dir_cur_bld,
  2750. arg_dd, arg_ddis, module_dir, linked_target_dirs,
  2751. forward_modules_from_target_dirs, arg_lang,
  2752. arg_modmapfmt, *export_info)
  2753. ? 0
  2754. : 1;
  2755. }
  2756. #endif
  2757. bool cmGlobalNinjaGenerator::EnableCrossConfigBuild() const
  2758. {
  2759. return !this->CrossConfigs.empty();
  2760. }
  2761. void cmGlobalNinjaGenerator::AppendDirectoryForConfig(
  2762. std::string const& prefix, std::string const& config,
  2763. std::string const& suffix, std::string& dir)
  2764. {
  2765. if (!config.empty() && this->IsMultiConfig()) {
  2766. dir += cmStrCat(prefix, config, suffix);
  2767. }
  2768. }
  2769. std::set<std::string> cmGlobalNinjaGenerator::GetCrossConfigs(
  2770. std::string const& fileConfig) const
  2771. {
  2772. auto result = this->CrossConfigs;
  2773. result.insert(fileConfig);
  2774. return result;
  2775. }
  2776. bool cmGlobalNinjaGenerator::IsSingleConfigUtility(
  2777. cmGeneratorTarget const* target) const
  2778. {
  2779. return target->GetType() == cmStateEnums::UTILITY &&
  2780. !this->PerConfigUtilityTargets.count(target->GetName());
  2781. }
  2782. std::string cmGlobalNinjaGenerator::ConvertToOutputPath(std::string path) const
  2783. {
  2784. return this->ConvertToNinjaPath(path);
  2785. }
  2786. char const* cmGlobalNinjaMultiGenerator::NINJA_COMMON_FILE =
  2787. "CMakeFiles/common.ninja";
  2788. char const* cmGlobalNinjaMultiGenerator::NINJA_FILE_EXTENSION = ".ninja";
  2789. cmGlobalNinjaMultiGenerator::cmGlobalNinjaMultiGenerator(cmake* cm)
  2790. : cmGlobalNinjaGenerator(cm)
  2791. {
  2792. cm->GetState()->SetIsGeneratorMultiConfig(true);
  2793. cm->GetState()->SetNinjaMulti(true);
  2794. }
  2795. cmDocumentationEntry cmGlobalNinjaMultiGenerator::GetDocumentation()
  2796. {
  2797. return { cmGlobalNinjaMultiGenerator::GetActualName(),
  2798. "Generates build-<Config>.ninja files." };
  2799. }
  2800. std::string cmGlobalNinjaMultiGenerator::ExpandCFGIntDir(
  2801. std::string const& str, std::string const& config) const
  2802. {
  2803. std::string result = str;
  2804. cmSystemTools::ReplaceString(result, this->GetCMakeCFGIntDir(), config);
  2805. return result;
  2806. }
  2807. bool cmGlobalNinjaMultiGenerator::OpenBuildFileStreams()
  2808. {
  2809. if (!this->OpenFileStream(this->CommonFileStream,
  2810. cmGlobalNinjaMultiGenerator::NINJA_COMMON_FILE)) {
  2811. return false;
  2812. }
  2813. if (!this->OpenFileStream(this->DefaultFileStream, NINJA_BUILD_FILE)) {
  2814. return false;
  2815. }
  2816. *this->DefaultFileStream << "# Build using rules for '"
  2817. << this->DefaultFileConfig << "'.\n\n"
  2818. << "include "
  2819. << this->NinjaOutputPath(
  2820. GetNinjaImplFilename(this->DefaultFileConfig))
  2821. << "\n\n";
  2822. // Write a comment about this file.
  2823. *this->CommonFileStream
  2824. << "# This file contains build statements common to all "
  2825. "configurations.\n\n";
  2826. std::vector<std::string> const& configs = this->GetConfigNames();
  2827. return std::all_of(
  2828. configs.begin(), configs.end(), [this](std::string const& config) -> bool {
  2829. // Open impl file.
  2830. if (!this->OpenFileStream(this->ImplFileStreams[config],
  2831. GetNinjaImplFilename(config))) {
  2832. return false;
  2833. }
  2834. // Write a comment about this file.
  2835. *this->ImplFileStreams[config]
  2836. << "# This file contains build statements specific to the \"" << config
  2837. << "\"\n# configuration.\n\n";
  2838. // Open config file.
  2839. if (!this->OpenFileStream(this->ConfigFileStreams[config],
  2840. GetNinjaConfigFilename(config))) {
  2841. return false;
  2842. }
  2843. // Write a comment about this file.
  2844. *this->ConfigFileStreams[config]
  2845. << "# This file contains aliases specific to the \"" << config
  2846. << "\"\n# configuration.\n\n"
  2847. << "include " << this->NinjaOutputPath(GetNinjaImplFilename(config))
  2848. << "\n\n";
  2849. return true;
  2850. });
  2851. }
  2852. void cmGlobalNinjaMultiGenerator::CloseBuildFileStreams()
  2853. {
  2854. if (this->CommonFileStream) {
  2855. this->CommonFileStream.reset();
  2856. } else {
  2857. cmSystemTools::Error("Common file stream was not open.");
  2858. }
  2859. if (this->DefaultFileStream) {
  2860. this->DefaultFileStream.reset();
  2861. } // No error if it wasn't open
  2862. for (std::string const& config : this->GetConfigNames()) {
  2863. if (this->ImplFileStreams[config]) {
  2864. this->ImplFileStreams[config].reset();
  2865. } else {
  2866. cmSystemTools::Error(
  2867. cmStrCat("Impl file stream for \"", config, "\" was not open."));
  2868. }
  2869. if (this->ConfigFileStreams[config]) {
  2870. this->ConfigFileStreams[config].reset();
  2871. } else {
  2872. cmSystemTools::Error(
  2873. cmStrCat("Config file stream for \"", config, "\" was not open."));
  2874. }
  2875. }
  2876. }
  2877. void cmGlobalNinjaMultiGenerator::AppendNinjaFileArgument(
  2878. GeneratedMakeCommand& command, std::string const& config) const
  2879. {
  2880. if (!config.empty()) {
  2881. command.Add("-f");
  2882. command.Add(GetNinjaConfigFilename(config));
  2883. }
  2884. }
  2885. std::string cmGlobalNinjaMultiGenerator::GetNinjaImplFilename(
  2886. std::string const& config)
  2887. {
  2888. return cmStrCat("CMakeFiles/impl-", config,
  2889. cmGlobalNinjaMultiGenerator::NINJA_FILE_EXTENSION);
  2890. }
  2891. std::string cmGlobalNinjaMultiGenerator::GetNinjaConfigFilename(
  2892. std::string const& config)
  2893. {
  2894. return cmStrCat("build-", config,
  2895. cmGlobalNinjaMultiGenerator::NINJA_FILE_EXTENSION);
  2896. }
  2897. void cmGlobalNinjaMultiGenerator::AddRebuildManifestOutputs(
  2898. cmNinjaDeps& outputs) const
  2899. {
  2900. for (std::string const& config : this->GetConfigNames()) {
  2901. outputs.push_back(this->NinjaOutputPath(GetNinjaImplFilename(config)));
  2902. outputs.push_back(this->NinjaOutputPath(GetNinjaConfigFilename(config)));
  2903. }
  2904. if (!this->DefaultFileConfig.empty()) {
  2905. outputs.push_back(this->NinjaOutputPath(NINJA_BUILD_FILE));
  2906. }
  2907. this->AddCMakeFilesToRebuild(outputs);
  2908. }
  2909. void cmGlobalNinjaMultiGenerator::GetQtAutoGenConfigs(
  2910. std::vector<std::string>& configs) const
  2911. {
  2912. std::vector<std::string> const& allConfigs = this->GetConfigNames();
  2913. configs.insert(configs.end(), cm::cbegin(allConfigs), cm::cend(allConfigs));
  2914. }
  2915. bool cmGlobalNinjaMultiGenerator::InspectConfigTypeVariables()
  2916. {
  2917. std::vector<std::string> configsList =
  2918. this->Makefiles.front()->GetGeneratorConfigs(
  2919. cmMakefile::IncludeEmptyConfig);
  2920. std::set<std::string> configs(configsList.cbegin(), configsList.cend());
  2921. this->DefaultFileConfig =
  2922. this->Makefiles.front()->GetSafeDefinition("CMAKE_DEFAULT_BUILD_TYPE");
  2923. if (this->DefaultFileConfig.empty()) {
  2924. this->DefaultFileConfig = configsList.front();
  2925. }
  2926. if (!configs.count(this->DefaultFileConfig)) {
  2927. std::ostringstream msg;
  2928. msg << "The configuration specified by "
  2929. << "CMAKE_DEFAULT_BUILD_TYPE (" << this->DefaultFileConfig
  2930. << ") is not present in CMAKE_CONFIGURATION_TYPES";
  2931. this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  2932. msg.str());
  2933. return false;
  2934. }
  2935. cmList crossConfigsList{ this->Makefiles.front()->GetSafeDefinition(
  2936. "CMAKE_CROSS_CONFIGS") };
  2937. auto crossConfigs = ListSubsetWithAll(configs, configs, crossConfigsList);
  2938. if (!crossConfigs) {
  2939. std::ostringstream msg;
  2940. msg << "CMAKE_CROSS_CONFIGS is not a subset of "
  2941. << "CMAKE_CONFIGURATION_TYPES";
  2942. this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  2943. msg.str());
  2944. return false;
  2945. }
  2946. this->CrossConfigs = *crossConfigs;
  2947. auto defaultConfigsString =
  2948. this->Makefiles.front()->GetSafeDefinition("CMAKE_DEFAULT_CONFIGS");
  2949. if (defaultConfigsString.empty()) {
  2950. defaultConfigsString = this->DefaultFileConfig;
  2951. }
  2952. if (!defaultConfigsString.empty() &&
  2953. defaultConfigsString != this->DefaultFileConfig &&
  2954. (this->DefaultFileConfig.empty() || this->CrossConfigs.empty())) {
  2955. std::ostringstream msg;
  2956. msg << "CMAKE_DEFAULT_CONFIGS cannot be used without "
  2957. << "CMAKE_DEFAULT_BUILD_TYPE or CMAKE_CROSS_CONFIGS";
  2958. this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  2959. msg.str());
  2960. return false;
  2961. }
  2962. cmList defaultConfigsList(defaultConfigsString);
  2963. if (!this->DefaultFileConfig.empty()) {
  2964. auto defaultConfigs =
  2965. ListSubsetWithAll(this->GetCrossConfigs(this->DefaultFileConfig),
  2966. this->CrossConfigs, defaultConfigsList);
  2967. if (!defaultConfigs) {
  2968. std::ostringstream msg;
  2969. msg << "CMAKE_DEFAULT_CONFIGS is not a subset of CMAKE_CROSS_CONFIGS";
  2970. this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  2971. msg.str());
  2972. return false;
  2973. }
  2974. this->DefaultConfigs = *defaultConfigs;
  2975. }
  2976. return true;
  2977. }
  2978. std::string cmGlobalNinjaMultiGenerator::GetDefaultBuildConfig() const
  2979. {
  2980. return "";
  2981. }
  2982. std::string cmGlobalNinjaMultiGenerator::OrderDependsTargetForTarget(
  2983. cmGeneratorTarget const* target, std::string const& config) const
  2984. {
  2985. return cmStrCat("cmake_object_order_depends_target_", target->GetName(), '_',
  2986. cmSystemTools::UpperCase(config));
  2987. }