cmGlobalNinjaGenerator.cxx 116 KB

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