cmGlobalNinjaGenerator.cxx 108 KB

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