| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240 |
- /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
- file Copyright.txt or https://cmake.org/licensing for details. */
- #include "cmGlobalNinjaGenerator.h"
- #include <algorithm>
- #include <cassert>
- #include <cctype>
- #include <cstdio>
- #include <functional>
- #include <sstream>
- #include <type_traits>
- #include <utility>
- #include <cm/iterator>
- #include <cm/memory>
- #include <cm/optional>
- #include <cm/string_view>
- #include <cmext/algorithm>
- #include <cmext/memory>
- #include <cmext/string_view>
- #include <cm3p/json/reader.h>
- #include <cm3p/json/value.h>
- #include <cm3p/json/writer.h>
- #include "cmsys/FStream.hxx"
- #include "cmCustomCommand.h"
- #include "cmCxxModuleMapper.h"
- #include "cmDyndepCollation.h"
- #include "cmFortranParser.h"
- #include "cmGeneratedFileStream.h"
- #include "cmGeneratorTarget.h"
- #include "cmGlobalGenerator.h"
- #include "cmLinkLineComputer.h"
- #include "cmList.h"
- #include "cmListFileCache.h"
- #include "cmLocalGenerator.h"
- #include "cmLocalNinjaGenerator.h"
- #include "cmMakefile.h"
- #include "cmMessageType.h"
- #include "cmNinjaLinkLineComputer.h"
- #include "cmOutputConverter.h"
- #include "cmRange.h"
- #include "cmScanDepFormat.h"
- #include "cmSourceFile.h"
- #include "cmState.h"
- #include "cmStateDirectory.h"
- #include "cmStateSnapshot.h"
- #include "cmStateTypes.h"
- #include "cmStringAlgorithms.h"
- #include "cmSystemTools.h"
- #include "cmTarget.h"
- #include "cmTargetDepend.h"
- #include "cmValue.h"
- #include "cmVersion.h"
- #include "cmake.h"
- const char* cmGlobalNinjaGenerator::NINJA_BUILD_FILE = "build.ninja";
- const char* cmGlobalNinjaGenerator::NINJA_RULES_FILE =
- "CMakeFiles/rules.ninja";
- const char* cmGlobalNinjaGenerator::INDENT = " ";
- #ifdef _WIN32
- std::string const cmGlobalNinjaGenerator::SHELL_NOOP = "cd .";
- #else
- std::string const cmGlobalNinjaGenerator::SHELL_NOOP = ":";
- #endif
- namespace {
- #ifdef _WIN32
- bool DetectGCCOnWindows(cm::string_view compilerId, cm::string_view simulateId,
- cm::string_view compilerFrontendVariant)
- {
- return ((compilerId == "Clang"_s && compilerFrontendVariant == "GNU"_s) ||
- (simulateId != "MSVC"_s &&
- (compilerId == "GNU"_s || compilerId == "QCC"_s ||
- cmHasLiteralSuffix(compilerId, "Clang"))));
- }
- #endif
- }
- bool operator==(
- const cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey& lhs,
- const cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey& rhs)
- {
- return lhs.Target == rhs.Target && lhs.Config == rhs.Config &&
- lhs.GenexOutput == rhs.GenexOutput;
- }
- bool operator!=(
- const cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey& lhs,
- const cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey& rhs)
- {
- return !(lhs == rhs);
- }
- bool operator<(
- const cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey& lhs,
- const cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey& rhs)
- {
- return lhs.Target < rhs.Target ||
- (lhs.Target == rhs.Target &&
- (lhs.Config < rhs.Config ||
- (lhs.Config == rhs.Config && lhs.GenexOutput < rhs.GenexOutput)));
- }
- bool operator>(
- const cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey& lhs,
- const cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey& rhs)
- {
- return rhs < lhs;
- }
- bool operator<=(
- const cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey& lhs,
- const cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey& rhs)
- {
- return !(lhs > rhs);
- }
- bool operator>=(
- const cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey& lhs,
- const cmGlobalNinjaGenerator::ByConfig::TargetDependsClosureKey& rhs)
- {
- return rhs <= lhs;
- }
- void cmGlobalNinjaGenerator::Indent(std::ostream& os, int count)
- {
- for (int i = 0; i < count; ++i) {
- os << cmGlobalNinjaGenerator::INDENT;
- }
- }
- void cmGlobalNinjaGenerator::WriteDivider(std::ostream& os)
- {
- os << "# ======================================"
- "=======================================\n";
- }
- void cmGlobalNinjaGenerator::WriteComment(std::ostream& os,
- const std::string& comment)
- {
- if (comment.empty()) {
- return;
- }
- std::string::size_type lpos = 0;
- std::string::size_type rpos;
- os << "\n#############################################\n";
- while ((rpos = comment.find('\n', lpos)) != std::string::npos) {
- os << "# " << comment.substr(lpos, rpos - lpos) << "\n";
- lpos = rpos + 1;
- }
- os << "# " << comment.substr(lpos) << "\n\n";
- }
- std::unique_ptr<cmLinkLineComputer>
- cmGlobalNinjaGenerator::CreateLinkLineComputer(
- cmOutputConverter* outputConverter,
- cmStateDirectory const& /* stateDir */) const
- {
- return std::unique_ptr<cmLinkLineComputer>(
- cm::make_unique<cmNinjaLinkLineComputer>(
- outputConverter,
- this->LocalGenerators[0]->GetStateSnapshot().GetDirectory(), this));
- }
- std::string cmGlobalNinjaGenerator::EncodeRuleName(std::string const& name)
- {
- // Ninja rule names must match "[a-zA-Z0-9_.-]+". Use ".xx" to encode
- // "." and all invalid characters as hexadecimal.
- std::string encoded;
- for (char i : name) {
- if (isalnum(i) || i == '_' || i == '-') {
- encoded += i;
- } else {
- char buf[16];
- snprintf(buf, sizeof(buf), ".%02x", static_cast<unsigned int>(i));
- encoded += buf;
- }
- }
- return encoded;
- }
- std::string cmGlobalNinjaGenerator::GetEncodedLiteral(const std::string& lit)
- {
- std::string result = lit;
- return this->EncodeLiteral(result);
- }
- std::string& cmGlobalNinjaGenerator::EncodeLiteral(std::string& lit)
- {
- cmSystemTools::ReplaceString(lit, "$", "$$");
- cmSystemTools::ReplaceString(lit, "\n", "$\n");
- if (this->IsMultiConfig()) {
- cmSystemTools::ReplaceString(lit, cmStrCat('$', this->GetCMakeCFGIntDir()),
- this->GetCMakeCFGIntDir());
- }
- return lit;
- }
- std::string cmGlobalNinjaGenerator::EncodePath(const std::string& path)
- {
- std::string result = path;
- #ifdef _WIN32
- if (this->IsGCCOnWindows())
- std::replace(result.begin(), result.end(), '\\', '/');
- else
- std::replace(result.begin(), result.end(), '/', '\\');
- #endif
- this->EncodeLiteral(result);
- cmSystemTools::ReplaceString(result, " ", "$ ");
- cmSystemTools::ReplaceString(result, ":", "$:");
- return result;
- }
- void cmGlobalNinjaGenerator::WriteBuild(std::ostream& os,
- cmNinjaBuild const& build,
- int cmdLineLimit,
- bool* usedResponseFile)
- {
- // Make sure there is a rule.
- if (build.Rule.empty()) {
- cmSystemTools::Error(cmStrCat(
- "No rule for WriteBuild! called with comment: ", build.Comment));
- return;
- }
- // Make sure there is at least one output file.
- if (build.Outputs.empty()) {
- cmSystemTools::Error(cmStrCat(
- "No output files for WriteBuild! called with comment: ", build.Comment));
- return;
- }
- cmGlobalNinjaGenerator::WriteComment(os, build.Comment);
- // Write output files.
- std::string buildStr("build");
- {
- // Write explicit outputs
- for (std::string const& output : build.Outputs) {
- buildStr = cmStrCat(buildStr, ' ', this->EncodePath(output));
- }
- // Write implicit outputs
- if (!build.ImplicitOuts.empty()) {
- // Assume Ninja is new enough to support implicit outputs.
- // Callers should not populate this field otherwise.
- buildStr = cmStrCat(buildStr, " |");
- for (std::string const& implicitOut : build.ImplicitOuts) {
- buildStr = cmStrCat(buildStr, ' ', this->EncodePath(implicitOut));
- }
- }
- // Repeat some outputs, but expressed as absolute paths.
- // This helps Ninja handle absolute paths found in a depfile.
- // FIXME: Unfortunately this causes Ninja to stat the file twice.
- // We could avoid this if Ninja Issue 1251 were fixed.
- if (!build.WorkDirOuts.empty()) {
- if (this->SupportsImplicitOuts() && build.ImplicitOuts.empty()) {
- // Make them implicit outputs if supported by this version of Ninja.
- buildStr = cmStrCat(buildStr, " |");
- }
- for (std::string const& workdirOut : build.WorkDirOuts) {
- buildStr = cmStrCat(buildStr, " ${cmake_ninja_workdir}",
- this->EncodePath(workdirOut));
- }
- }
- // Write the rule.
- buildStr = cmStrCat(buildStr, ": ", build.Rule);
- }
- std::string arguments;
- {
- // TODO: Better formatting for when there are multiple input/output files.
- // Write explicit dependencies.
- for (std::string const& explicitDep : build.ExplicitDeps) {
- arguments += cmStrCat(' ', this->EncodePath(explicitDep));
- }
- // Write implicit dependencies.
- if (!build.ImplicitDeps.empty()) {
- arguments += " |";
- for (std::string const& implicitDep : build.ImplicitDeps) {
- arguments += cmStrCat(' ', this->EncodePath(implicitDep));
- }
- }
- // Write order-only dependencies.
- if (!build.OrderOnlyDeps.empty()) {
- arguments += " ||";
- for (std::string const& orderOnlyDep : build.OrderOnlyDeps) {
- arguments += cmStrCat(' ', this->EncodePath(orderOnlyDep));
- }
- }
- arguments += '\n';
- }
- // Write the variables bound to this build statement.
- std::string assignments;
- {
- std::ostringstream variable_assignments;
- for (auto const& variable : build.Variables) {
- cmGlobalNinjaGenerator::WriteVariable(
- variable_assignments, variable.first, variable.second, "", 1);
- }
- // check if a response file rule should be used
- assignments = variable_assignments.str();
- bool useResponseFile = false;
- if (cmdLineLimit < 0 ||
- (cmdLineLimit > 0 &&
- (arguments.size() + buildStr.size() + assignments.size() + 1000) >
- static_cast<size_t>(cmdLineLimit))) {
- variable_assignments.str(std::string());
- cmGlobalNinjaGenerator::WriteVariable(variable_assignments, "RSP_FILE",
- build.RspFile, "", 1);
- assignments += variable_assignments.str();
- useResponseFile = true;
- }
- if (usedResponseFile) {
- *usedResponseFile = useResponseFile;
- }
- }
- os << buildStr << arguments << assignments << "\n";
- }
- void cmGlobalNinjaGenerator::AddCustomCommandRule()
- {
- cmNinjaRule rule("CUSTOM_COMMAND");
- rule.Command = "$COMMAND";
- rule.Description = "$DESC";
- rule.Comment = "Rule for running custom commands.";
- this->AddRule(rule);
- }
- void cmGlobalNinjaGenerator::CCOutputs::Add(
- std::vector<std::string> const& paths)
- {
- for (std::string const& path : paths) {
- std::string out = this->GG->ConvertToNinjaPath(path);
- if (!cmSystemTools::FileIsFullPath(out)) {
- // This output is expressed as a relative path. Repeat it,
- // but expressed as an absolute path for Ninja Issue 1251.
- this->WorkDirOuts.emplace_back(out);
- this->GG->SeenCustomCommandOutput(this->GG->ConvertToNinjaAbsPath(path));
- }
- this->GG->SeenCustomCommandOutput(out);
- this->ExplicitOuts.emplace_back(std::move(out));
- }
- }
- void cmGlobalNinjaGenerator::WriteCustomCommandBuild(
- std::string const& command, std::string const& description,
- std::string const& comment, std::string const& depfile,
- std::string const& job_pool, bool uses_terminal, bool restat,
- std::string const& config, CCOutputs outputs, cmNinjaDeps explicitDeps,
- cmNinjaDeps orderOnlyDeps)
- {
- this->AddCustomCommandRule();
- {
- std::string ninjaDepfilePath;
- bool depfileIsOutput = false;
- if (!depfile.empty()) {
- ninjaDepfilePath = this->ConvertToNinjaPath(depfile);
- depfileIsOutput =
- std::find(outputs.ExplicitOuts.begin(), outputs.ExplicitOuts.end(),
- ninjaDepfilePath) != outputs.ExplicitOuts.end();
- }
- cmNinjaBuild build("CUSTOM_COMMAND");
- build.Comment = comment;
- build.Outputs = std::move(outputs.ExplicitOuts);
- build.WorkDirOuts = std::move(outputs.WorkDirOuts);
- build.ExplicitDeps = std::move(explicitDeps);
- build.OrderOnlyDeps = std::move(orderOnlyDeps);
- cmNinjaVars& vars = build.Variables;
- {
- std::string cmd = command; // NOLINT(*)
- #ifdef _WIN32
- if (cmd.empty())
- // TODO Shouldn't an empty command be handled by ninja?
- cmd = "cmd.exe /c";
- #endif
- vars["COMMAND"] = std::move(cmd);
- }
- vars["DESC"] = this->GetEncodedLiteral(description);
- if (restat) {
- vars["restat"] = "1";
- }
- if (uses_terminal && this->SupportsDirectConsole()) {
- vars["pool"] = "console";
- } else if (!job_pool.empty()) {
- vars["pool"] = job_pool;
- }
- if (!depfile.empty()) {
- vars["depfile"] = ninjaDepfilePath;
- // Add the depfile to the `.ninja_deps` database. Since this (generally)
- // removes the file, it cannot be declared as an output or byproduct of
- // the command.
- if (!depfileIsOutput) {
- vars["deps"] = "gcc";
- }
- }
- if (config.empty()) {
- this->WriteBuild(*this->GetCommonFileStream(), build);
- } else {
- this->WriteBuild(*this->GetImplFileStream(config), build);
- }
- }
- }
- void cmGlobalNinjaGenerator::AddMacOSXContentRule()
- {
- cmNinjaRule rule("COPY_OSX_CONTENT");
- rule.Command = cmStrCat(this->CMakeCmd(), " -E copy $in $out");
- rule.Description = "Copying OS X Content $out";
- rule.Comment = "Rule for copying OS X bundle content file.";
- this->AddRule(rule);
- }
- void cmGlobalNinjaGenerator::WriteMacOSXContentBuild(std::string input,
- std::string output,
- const std::string& config)
- {
- this->AddMacOSXContentRule();
- {
- cmNinjaBuild build("COPY_OSX_CONTENT");
- build.Outputs.push_back(std::move(output));
- build.ExplicitDeps.push_back(std::move(input));
- this->WriteBuild(*this->GetImplFileStream(config), build);
- }
- }
- void cmGlobalNinjaGenerator::WriteRule(std::ostream& os,
- cmNinjaRule const& rule)
- {
- // -- Parameter checks
- // Make sure the rule has a name.
- if (rule.Name.empty()) {
- cmSystemTools::Error(cmStrCat(
- "No name given for WriteRule! called with comment: ", rule.Comment));
- return;
- }
- // Make sure a command is given.
- if (rule.Command.empty()) {
- cmSystemTools::Error(cmStrCat(
- "No command given for WriteRule! called with comment: ", rule.Comment));
- return;
- }
- // Make sure response file content is given
- if (!rule.RspFile.empty() && rule.RspContent.empty()) {
- cmSystemTools::Error(
- cmStrCat("rspfile but no rspfile_content given for WriteRule! "
- "called with comment: ",
- rule.Comment));
- return;
- }
- // -- Write rule
- // Write rule intro
- cmGlobalNinjaGenerator::WriteComment(os, rule.Comment);
- os << "rule " << rule.Name << '\n';
- // Write rule key/value pairs
- auto writeKV = [&os](const char* key, std::string const& value) {
- if (!value.empty()) {
- cmGlobalNinjaGenerator::Indent(os, 1);
- os << key << " = " << value << '\n';
- }
- };
- writeKV("depfile", rule.DepFile);
- writeKV("deps", rule.DepType);
- writeKV("command", rule.Command);
- writeKV("description", rule.Description);
- if (!rule.RspFile.empty()) {
- writeKV("rspfile", rule.RspFile);
- writeKV("rspfile_content", rule.RspContent);
- }
- writeKV("restat", rule.Restat);
- if (rule.Generator) {
- writeKV("generator", "1");
- }
- // Finish rule
- os << '\n';
- }
- void cmGlobalNinjaGenerator::WriteVariable(std::ostream& os,
- const std::string& name,
- const std::string& value,
- const std::string& comment,
- int indent)
- {
- // Make sure we have a name.
- if (name.empty()) {
- cmSystemTools::Error(cmStrCat("No name given for WriteVariable! called "
- "with comment: ",
- comment));
- return;
- }
- std::string val;
- static std::unordered_set<std::string> const variablesShouldNotBeTrimmed = {
- "CODE_CHECK", "LAUNCHER"
- };
- if (variablesShouldNotBeTrimmed.find(name) ==
- variablesShouldNotBeTrimmed.end()) {
- val = cmTrimWhitespace(value);
- } else {
- val = value;
- }
- // Do not add a variable if the value is empty.
- if (val.empty()) {
- return;
- }
- cmGlobalNinjaGenerator::WriteComment(os, comment);
- cmGlobalNinjaGenerator::Indent(os, indent);
- os << name << " = " << val << "\n";
- }
- void cmGlobalNinjaGenerator::WriteInclude(std::ostream& os,
- const std::string& filename,
- const std::string& comment)
- {
- cmGlobalNinjaGenerator::WriteComment(os, comment);
- os << "include " << filename << "\n";
- }
- void cmGlobalNinjaGenerator::WriteDefault(std::ostream& os,
- const cmNinjaDeps& targets,
- const std::string& comment)
- {
- cmGlobalNinjaGenerator::WriteComment(os, comment);
- os << "default";
- for (std::string const& target : targets) {
- os << " " << target;
- }
- os << "\n";
- }
- cmGlobalNinjaGenerator::cmGlobalNinjaGenerator(cmake* cm)
- : cmGlobalCommonGenerator(cm)
- {
- #ifdef _WIN32
- cm->GetState()->SetWindowsShell(true);
- // Attempt to use full path to COMSPEC, default "cmd.exe"
- this->Comspec = cmSystemTools::GetComspec();
- #endif
- cm->GetState()->SetNinja(true);
- this->FindMakeProgramFile = "CMakeNinjaFindMake.cmake";
- }
- // Virtual public methods.
- std::unique_ptr<cmLocalGenerator> cmGlobalNinjaGenerator::CreateLocalGenerator(
- cmMakefile* mf)
- {
- return std::unique_ptr<cmLocalGenerator>(
- cm::make_unique<cmLocalNinjaGenerator>(this, mf));
- }
- codecvt_Encoding cmGlobalNinjaGenerator::GetMakefileEncoding() const
- {
- return this->NinjaExpectedEncoding;
- }
- cmDocumentationEntry cmGlobalNinjaGenerator::GetDocumentation()
- {
- return { cmGlobalNinjaGenerator::GetActualName(),
- "Generates build.ninja files." };
- }
- std::vector<std::string> const& cmGlobalNinjaGenerator::GetConfigNames() const
- {
- return static_cast<cmLocalNinjaGenerator const*>(
- this->LocalGenerators.front().get())
- ->GetConfigNames();
- }
- // Implemented in all cmGlobaleGenerator sub-classes.
- // Used in:
- // Source/cmLocalGenerator.cxx
- // Source/cmake.cxx
- void cmGlobalNinjaGenerator::Generate()
- {
- // Check minimum Ninja version.
- if (cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
- RequiredNinjaVersion())) {
- std::ostringstream msg;
- msg << "The detected version of Ninja (" << this->NinjaVersion;
- msg << ") is less than the version of Ninja required by CMake (";
- msg << cmGlobalNinjaGenerator::RequiredNinjaVersion() << ").";
- this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
- msg.str());
- return;
- }
- this->InitOutputPathPrefix();
- if (!this->OpenBuildFileStreams()) {
- return;
- }
- if (!this->OpenRulesFileStream()) {
- return;
- }
- for (auto& it : this->Configs) {
- it.second.TargetDependsClosures.clear();
- }
- this->TargetAll = this->NinjaOutputPath("all");
- this->CMakeCacheFile = this->NinjaOutputPath("CMakeCache.txt");
- this->DiagnosedCxxModuleNinjaSupport = false;
- this->ClangTidyExportFixesDirs.clear();
- this->ClangTidyExportFixesFiles.clear();
- this->cmGlobalGenerator::Generate();
- this->WriteAssumedSourceDependencies();
- this->WriteTargetAliases(*this->GetCommonFileStream());
- this->WriteFolderTargets(*this->GetCommonFileStream());
- this->WriteBuiltinTargets(*this->GetCommonFileStream());
- if (cmSystemTools::GetErrorOccurredFlag()) {
- this->RulesFileStream->setstate(std::ios::failbit);
- for (std::string const& config : this->GetConfigNames()) {
- this->GetImplFileStream(config)->setstate(std::ios::failbit);
- this->GetConfigFileStream(config)->setstate(std::ios::failbit);
- }
- this->GetCommonFileStream()->setstate(std::ios::failbit);
- }
- this->CloseCompileCommandsStream();
- this->CloseRulesFileStream();
- this->CloseBuildFileStreams();
- #ifdef _WIN32
- // Older ninja tools will not be able to update metadata on Windows
- // when we are re-generating inside an existing 'ninja' invocation
- // because the outer tool has the files open for write.
- if (this->NinjaSupportsMetadataOnRegeneration ||
- !this->GetCMakeInstance()->GetRegenerateDuringBuild())
- #endif
- {
- this->CleanMetaData();
- }
- this->RemoveUnknownClangTidyExportFixesFiles();
- }
- void cmGlobalNinjaGenerator::CleanMetaData()
- {
- auto run_ninja_tool = [this](std::vector<char const*> const& args) {
- std::vector<std::string> command;
- command.push_back(this->NinjaCommand);
- command.emplace_back("-C");
- command.emplace_back(this->GetCMakeInstance()->GetHomeOutputDirectory());
- command.emplace_back("-t");
- for (auto const& arg : args) {
- command.emplace_back(arg);
- }
- std::string error;
- if (!cmSystemTools::RunSingleCommand(command, nullptr, &error, nullptr,
- nullptr,
- cmSystemTools::OUTPUT_NONE)) {
- this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
- cmStrCat("Running\n '",
- cmJoin(command, "' '"),
- "'\n"
- "failed with:\n ",
- error));
- cmSystemTools::SetFatalErrorOccurred();
- }
- };
- // Can the tools below expect 'build.ninja' to be loadable?
- bool const expectBuildManifest =
- !this->IsMultiConfig() && this->OutputPathPrefix.empty();
- // Skip some ninja tools if they need 'build.ninja' but it is missing.
- bool const missingBuildManifest = expectBuildManifest &&
- this->NinjaSupportsUnconditionalRecompactTool &&
- !cmSystemTools::FileExists("build.ninja");
- // The `recompact` tool loads the manifest. As above, we don't have a single
- // `build.ninja` to load for this in Ninja-Multi. This may be relaxed in the
- // future pending further investigation into how Ninja works upstream
- // (ninja#1721).
- if (this->NinjaSupportsUnconditionalRecompactTool &&
- !this->GetCMakeInstance()->GetRegenerateDuringBuild() &&
- expectBuildManifest && !missingBuildManifest) {
- run_ninja_tool({ "recompact" });
- }
- if (this->NinjaSupportsRestatTool && this->OutputPathPrefix.empty()) {
- // XXX(ninja): We only list `build.ninja` entry files here because CMake
- // *always* rewrites these files on a reconfigure. If CMake ever gets
- // smarter about this, all CMake-time created/edited files listed as
- // outputs for the reconfigure build statement will need to be listed here.
- cmNinjaDeps outputs;
- this->AddRebuildManifestOutputs(outputs);
- std::vector<const char*> args;
- args.reserve(outputs.size() + 1);
- args.push_back("restat");
- for (auto const& output : outputs) {
- args.push_back(output.c_str());
- }
- run_ninja_tool(args);
- }
- }
- bool cmGlobalNinjaGenerator::FindMakeProgram(cmMakefile* mf)
- {
- if (!this->cmGlobalGenerator::FindMakeProgram(mf)) {
- return false;
- }
- if (cmValue ninjaCommand = mf->GetDefinition("CMAKE_MAKE_PROGRAM")) {
- this->NinjaCommand = *ninjaCommand;
- std::vector<std::string> command;
- command.push_back(this->NinjaCommand);
- command.emplace_back("--version");
- std::string version;
- std::string error;
- if (!cmSystemTools::RunSingleCommand(command, &version, &error, nullptr,
- nullptr,
- cmSystemTools::OUTPUT_NONE)) {
- mf->IssueMessage(MessageType::FATAL_ERROR,
- cmStrCat("Running\n '", cmJoin(command, "' '"),
- "'\n"
- "failed with:\n ",
- error));
- cmSystemTools::SetFatalErrorOccurred();
- return false;
- }
- this->NinjaVersion = cmTrimWhitespace(version);
- this->CheckNinjaFeatures();
- }
- return true;
- }
- void cmGlobalNinjaGenerator::CheckNinjaFeatures()
- {
- this->NinjaSupportsConsolePool =
- !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
- RequiredNinjaVersionForConsolePool());
- this->NinjaSupportsImplicitOuts = !cmSystemTools::VersionCompare(
- cmSystemTools::OP_LESS, this->NinjaVersion,
- cmGlobalNinjaGenerator::RequiredNinjaVersionForImplicitOuts());
- this->NinjaSupportsManifestRestat =
- !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
- RequiredNinjaVersionForManifestRestat());
- this->NinjaSupportsMultilineDepfile =
- !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
- RequiredNinjaVersionForMultilineDepfile());
- this->NinjaSupportsDyndepsCxx =
- !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
- RequiredNinjaVersionForDyndepsCxx());
- this->NinjaSupportsDyndepsFortran =
- !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
- RequiredNinjaVersionForDyndepsFortran());
- if (!this->NinjaSupportsDyndepsFortran) {
- // The ninja version number is not new enough to have upstream support.
- // Our ninja branch adds ".dyndep-#" to its version number,
- // where '#' is a feature-specific version number. Extract it.
- static std::string const k_DYNDEP_ = ".dyndep-";
- std::string::size_type pos = this->NinjaVersion.find(k_DYNDEP_);
- if (pos != std::string::npos) {
- const char* fv = &this->NinjaVersion[pos + k_DYNDEP_.size()];
- unsigned long dyndep = 0;
- cmStrToULong(fv, &dyndep);
- if (dyndep == 1) {
- this->NinjaSupportsDyndepsFortran = true;
- }
- }
- }
- this->NinjaSupportsUnconditionalRecompactTool =
- !cmSystemTools::VersionCompare(
- cmSystemTools::OP_LESS, this->NinjaVersion,
- RequiredNinjaVersionForUnconditionalRecompactTool());
- this->NinjaSupportsRestatTool =
- !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
- RequiredNinjaVersionForRestatTool());
- this->NinjaSupportsMultipleOutputs =
- !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
- RequiredNinjaVersionForMultipleOutputs());
- this->NinjaSupportsMetadataOnRegeneration = !cmSystemTools::VersionCompare(
- cmSystemTools::OP_LESS, this->NinjaVersion,
- RequiredNinjaVersionForMetadataOnRegeneration());
- #ifdef _WIN32
- this->NinjaSupportsCodePage =
- !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
- RequiredNinjaVersionForCodePage());
- if (this->NinjaSupportsCodePage) {
- this->CheckNinjaCodePage();
- } else {
- this->NinjaExpectedEncoding = codecvt_Encoding::ANSI;
- }
- #endif
- this->NinjaSupportsCWDDepend =
- !cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, this->NinjaVersion,
- RequiredNinjaVersionForCWDDepend());
- }
- void cmGlobalNinjaGenerator::CheckNinjaCodePage()
- {
- std::vector<std::string> command{ this->NinjaCommand, "-t", "wincodepage" };
- std::string output;
- std::string error;
- int result;
- if (!cmSystemTools::RunSingleCommand(command, &output, &error, &result,
- nullptr, cmSystemTools::OUTPUT_NONE)) {
- this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
- cmStrCat("Running\n '",
- cmJoin(command, "' '"),
- "'\n"
- "failed with:\n ",
- error));
- cmSystemTools::SetFatalErrorOccurred();
- } else if (result == 0) {
- std::istringstream outputStream(output);
- std::string line;
- bool found = false;
- while (cmSystemTools::GetLineFromStream(outputStream, line)) {
- if (cmHasLiteralPrefix(line, "Build file encoding: ")) {
- cm::string_view lineView(line);
- cm::string_view encoding =
- lineView.substr(cmStrLen("Build file encoding: "));
- if (encoding == "UTF-8") {
- // Ninja expects UTF-8. We use that internally. No conversion needed.
- this->NinjaExpectedEncoding = codecvt_Encoding::None;
- } else {
- this->NinjaExpectedEncoding = codecvt_Encoding::ANSI;
- }
- found = true;
- break;
- }
- }
- if (!found) {
- this->GetCMakeInstance()->IssueMessage(
- MessageType::WARNING,
- "Could not determine Ninja's code page, defaulting to UTF-8");
- this->NinjaExpectedEncoding = codecvt_Encoding::None;
- }
- } else {
- this->NinjaExpectedEncoding = codecvt_Encoding::ANSI;
- }
- }
- bool cmGlobalNinjaGenerator::CheckLanguages(
- std::vector<std::string> const& languages, cmMakefile* mf) const
- {
- if (cm::contains(languages, "Fortran")) {
- return this->CheckFortran(mf);
- }
- if (cm::contains(languages, "ISPC")) {
- return this->CheckISPC(mf);
- }
- if (cm::contains(languages, "Swift")) {
- const std::string architectures =
- mf->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES");
- if (architectures.find_first_of(';') != std::string::npos) {
- mf->IssueMessage(MessageType::FATAL_ERROR,
- "multiple values for CMAKE_OSX_ARCHITECTURES not "
- "supported with Swift");
- cmSystemTools::SetFatalErrorOccurred();
- return false;
- }
- }
- return true;
- }
- bool cmGlobalNinjaGenerator::CheckCxxModuleSupport(CxxModuleSupportQuery query)
- {
- if (this->NinjaSupportsDyndepsCxx) {
- return true;
- }
- bool const diagnose = !this->DiagnosedCxxModuleNinjaSupport &&
- !this->CMakeInstance->GetIsInTryCompile() &&
- query == CxxModuleSupportQuery::Expected;
- if (diagnose) {
- std::ostringstream e;
- /* clang-format off */
- e <<
- "The Ninja generator does not support C++20 modules "
- "using Ninja version \n"
- " " << this->NinjaVersion << "\n"
- "due to lack of required features. "
- "Ninja " << RequiredNinjaVersionForDyndepsCxx() <<
- " or higher is required."
- ;
- /* clang-format on */
- this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR, e.str());
- cmSystemTools::SetFatalErrorOccurred();
- }
- return false;
- }
- bool cmGlobalNinjaGenerator::CheckFortran(cmMakefile* mf) const
- {
- if (this->NinjaSupportsDyndepsFortran) {
- return true;
- }
- std::ostringstream e;
- /* clang-format off */
- e <<
- "The Ninja generator does not support Fortran using Ninja version\n"
- " " << this->NinjaVersion << "\n"
- "due to lack of required features. "
- "Ninja " << RequiredNinjaVersionForDyndepsFortran() <<
- " or higher is required."
- ;
- /* clang-format on */
- mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
- cmSystemTools::SetFatalErrorOccurred();
- return false;
- }
- bool cmGlobalNinjaGenerator::CheckISPC(cmMakefile* mf) const
- {
- if (this->NinjaSupportsMultipleOutputs) {
- return true;
- }
- std::ostringstream e;
- /* clang-format off */
- e <<
- "The Ninja generator does not support ISPC using Ninja version\n"
- " " << this->NinjaVersion << "\n"
- "due to lack of required features. "
- "Ninja " << RequiredNinjaVersionForMultipleOutputs() <<
- " or higher is required."
- ;
- /* clang-format on */
- mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
- cmSystemTools::SetFatalErrorOccurred();
- return false;
- }
- void cmGlobalNinjaGenerator::EnableLanguage(
- std::vector<std::string> const& langs, cmMakefile* mf, bool optional)
- {
- if (this->IsMultiConfig()) {
- mf->InitCMAKE_CONFIGURATION_TYPES("Debug;Release;RelWithDebInfo");
- }
- this->cmGlobalGenerator::EnableLanguage(langs, mf, optional);
- for (std::string const& l : langs) {
- if (l == "NONE") {
- continue;
- }
- this->ResolveLanguageCompiler(l, mf, optional);
- #ifdef _WIN32
- std::string const& compilerId =
- mf->GetSafeDefinition(cmStrCat("CMAKE_", l, "_COMPILER_ID"));
- std::string const& simulateId =
- mf->GetSafeDefinition(cmStrCat("CMAKE_", l, "_SIMULATE_ID"));
- std::string const& compilerFrontendVariant = mf->GetSafeDefinition(
- cmStrCat("CMAKE_", l, "_COMPILER_FRONTEND_VARIANT"));
- if (DetectGCCOnWindows(compilerId, simulateId, compilerFrontendVariant)) {
- this->MarkAsGCCOnWindows();
- }
- #endif
- }
- }
- // Implemented by:
- // cmGlobalUnixMakefileGenerator3
- // cmGlobalGhsMultiGenerator
- // cmGlobalVisualStudio10Generator
- // cmGlobalVisualStudio7Generator
- // cmGlobalXCodeGenerator
- // Called by:
- // cmGlobalGenerator::Build()
- std::vector<cmGlobalGenerator::GeneratedMakeCommand>
- cmGlobalNinjaGenerator::GenerateBuildCommand(
- const std::string& makeProgram, const std::string& /*projectName*/,
- const std::string& /*projectDir*/,
- std::vector<std::string> const& targetNames, const std::string& config,
- int jobs, bool verbose, const cmBuildOptions& /*buildOptions*/,
- std::vector<std::string> const& makeOptions)
- {
- GeneratedMakeCommand makeCommand;
- makeCommand.Add(this->SelectMakeProgram(makeProgram));
- if (verbose) {
- makeCommand.Add("-v");
- }
- if ((jobs != cmake::NO_BUILD_PARALLEL_LEVEL) &&
- (jobs != cmake::DEFAULT_BUILD_PARALLEL_LEVEL)) {
- makeCommand.Add("-j", std::to_string(jobs));
- }
- this->AppendNinjaFileArgument(makeCommand, config);
- makeCommand.Add(makeOptions.begin(), makeOptions.end());
- for (const auto& tname : targetNames) {
- if (!tname.empty()) {
- makeCommand.Add(tname);
- }
- }
- return { std::move(makeCommand) };
- }
- // Non-virtual public methods.
- void cmGlobalNinjaGenerator::AddRule(cmNinjaRule const& rule)
- {
- // Do not add the same rule twice.
- if (!this->Rules.insert(rule.Name).second) {
- return;
- }
- // Store command length
- this->RuleCmdLength[rule.Name] = static_cast<int>(rule.Command.size());
- // Write rule
- cmGlobalNinjaGenerator::WriteRule(*this->RulesFileStream, rule);
- }
- bool cmGlobalNinjaGenerator::HasRule(const std::string& name)
- {
- return (this->Rules.find(name) != this->Rules.end());
- }
- // Private virtual overrides
- void cmGlobalNinjaGenerator::ComputeTargetObjectDirectory(
- cmGeneratorTarget* gt) const
- {
- // Compute full path to object file directory for this target.
- std::string dir = cmStrCat(gt->LocalGenerator->GetCurrentBinaryDirectory(),
- '/', gt->LocalGenerator->GetTargetDirectory(gt),
- '/', this->GetCMakeCFGIntDir(), '/');
- gt->ObjectDirectory = dir;
- }
- // Private methods
- bool cmGlobalNinjaGenerator::OpenBuildFileStreams()
- {
- if (!this->OpenFileStream(this->BuildFileStream,
- cmGlobalNinjaGenerator::NINJA_BUILD_FILE)) {
- return false;
- }
- // Write a comment about this file.
- *this->BuildFileStream
- << "# This file contains all the build statements describing the\n"
- << "# compilation DAG.\n\n";
- return true;
- }
- bool cmGlobalNinjaGenerator::OpenFileStream(
- std::unique_ptr<cmGeneratedFileStream>& stream, const std::string& name)
- {
- // Get a stream where to generate things.
- if (!stream) {
- // Compute Ninja's build file path.
- std::string path =
- cmStrCat(this->GetCMakeInstance()->GetHomeOutputDirectory(), '/', name);
- stream = cm::make_unique<cmGeneratedFileStream>(
- path, false, this->GetMakefileEncoding());
- if (!(*stream)) {
- // An error message is generated by the constructor if it cannot
- // open the file.
- return false;
- }
- // Write the do not edit header.
- this->WriteDisclaimer(*stream);
- }
- return true;
- }
- cm::optional<std::set<std::string>> cmGlobalNinjaGenerator::ListSubsetWithAll(
- const std::set<std::string>& all, const std::set<std::string>& defaults,
- const std::vector<std::string>& items)
- {
- std::set<std::string> result;
- for (auto const& item : items) {
- if (item == "all") {
- if (items.size() == 1) {
- result = defaults;
- } else {
- return cm::nullopt;
- }
- } else if (all.count(item)) {
- result.insert(item);
- } else {
- return cm::nullopt;
- }
- }
- return cm::make_optional(result);
- }
- void cmGlobalNinjaGenerator::CloseBuildFileStreams()
- {
- if (this->BuildFileStream) {
- this->BuildFileStream.reset();
- } else {
- cmSystemTools::Error("Build file stream was not open.");
- }
- }
- bool cmGlobalNinjaGenerator::OpenRulesFileStream()
- {
- if (!this->OpenFileStream(this->RulesFileStream,
- cmGlobalNinjaGenerator::NINJA_RULES_FILE)) {
- return false;
- }
- // Write comment about this file.
- /* clang-format off */
- *this->RulesFileStream
- << "# This file contains all the rules used to get the outputs files\n"
- << "# built from the input files.\n"
- << "# It is included in the main '" << NINJA_BUILD_FILE << "'.\n\n"
- ;
- /* clang-format on */
- return true;
- }
- void cmGlobalNinjaGenerator::CloseRulesFileStream()
- {
- if (this->RulesFileStream) {
- this->RulesFileStream.reset();
- } else {
- cmSystemTools::Error("Rules file stream was not open.");
- }
- }
- static void EnsureTrailingSlash(std::string& path)
- {
- if (path.empty()) {
- return;
- }
- std::string::value_type last = path.back();
- #ifdef _WIN32
- if (last != '\\') {
- path += '\\';
- }
- #else
- if (last != '/') {
- path += '/';
- }
- #endif
- }
- std::string const& cmGlobalNinjaGenerator::ConvertToNinjaPath(
- const std::string& path) const
- {
- auto const f = this->ConvertToNinjaPathCache.find(path);
- if (f != this->ConvertToNinjaPathCache.end()) {
- return f->second;
- }
- std::string convPath =
- this->LocalGenerators[0]->MaybeRelativeToTopBinDir(path);
- convPath = this->NinjaOutputPath(convPath);
- #ifdef _WIN32
- std::replace(convPath.begin(), convPath.end(), '/', '\\');
- #endif
- return this->ConvertToNinjaPathCache.emplace(path, std::move(convPath))
- .first->second;
- }
- std::string cmGlobalNinjaGenerator::ConvertToNinjaAbsPath(
- std::string path) const
- {
- #ifdef _WIN32
- std::replace(path.begin(), path.end(), '/', '\\');
- #endif
- return path;
- }
- void cmGlobalNinjaGenerator::AddAdditionalCleanFile(std::string fileName,
- const std::string& config)
- {
- this->Configs[config].AdditionalCleanFiles.emplace(std::move(fileName));
- }
- void cmGlobalNinjaGenerator::AddCXXCompileCommand(
- const std::string& commandLine, const std::string& sourceFile,
- const std::string& objPath)
- {
- // Compute Ninja's build file path.
- std::string buildFileDir =
- this->GetCMakeInstance()->GetHomeOutputDirectory();
- if (!this->CompileCommandsStream) {
- std::string buildFilePath =
- cmStrCat(buildFileDir, "/compile_commands.json");
- // Get a stream where to generate things.
- this->CompileCommandsStream =
- cm::make_unique<cmGeneratedFileStream>(buildFilePath);
- *this->CompileCommandsStream << "[\n";
- } else {
- *this->CompileCommandsStream << ",\n";
- }
- std::string sourceFileName = sourceFile;
- if (!cmSystemTools::FileIsFullPath(sourceFileName)) {
- sourceFileName = cmSystemTools::CollapseFullPath(
- sourceFileName, this->GetCMakeInstance()->GetHomeOutputDirectory());
- }
- /* clang-format off */
- *this->CompileCommandsStream << "{\n"
- << R"( "directory": ")"
- << cmGlobalGenerator::EscapeJSON(buildFileDir) << "\",\n"
- << R"( "command": ")"
- << cmGlobalGenerator::EscapeJSON(commandLine) << "\",\n"
- << R"( "file": ")"
- << cmGlobalGenerator::EscapeJSON(sourceFileName) << "\",\n"
- << R"( "output": ")"
- << cmGlobalGenerator::EscapeJSON(objPath) << "\"\n"
- << "}";
- /* clang-format on */
- }
- void cmGlobalNinjaGenerator::CloseCompileCommandsStream()
- {
- if (this->CompileCommandsStream) {
- *this->CompileCommandsStream << "\n]";
- this->CompileCommandsStream.reset();
- }
- }
- void cmGlobalNinjaGenerator::WriteDisclaimer(std::ostream& os) const
- {
- os << "# CMAKE generated file: DO NOT EDIT!\n"
- << "# Generated by \"" << this->GetName() << "\""
- << " Generator, CMake Version " << cmVersion::GetMajorVersion() << "."
- << cmVersion::GetMinorVersion() << "\n\n";
- }
- void cmGlobalNinjaGenerator::WriteAssumedSourceDependencies()
- {
- for (auto const& asd : this->AssumedSourceDependencies) {
- CCOutputs outputs(this);
- outputs.ExplicitOuts.emplace_back(asd.first);
- cmNinjaDeps orderOnlyDeps;
- std::copy(asd.second.begin(), asd.second.end(),
- std::back_inserter(orderOnlyDeps));
- this->WriteCustomCommandBuild(
- /*command=*/"", /*description=*/"",
- "Assume dependencies for generated source file.",
- /*depfile*/ "", /*job_pool*/ "",
- /*uses_terminal*/ false,
- /*restat*/ true, std::string(), outputs, cmNinjaDeps(),
- std::move(orderOnlyDeps));
- }
- }
- std::string cmGlobalNinjaGenerator::OrderDependsTargetForTarget(
- cmGeneratorTarget const* target, const std::string& /*config*/) const
- {
- return cmStrCat("cmake_object_order_depends_target_", target->GetName());
- }
- std::string cmGlobalNinjaGenerator::OrderDependsTargetForTargetPrivate(
- cmGeneratorTarget const* target, const std::string& config) const
- {
- return cmStrCat(this->OrderDependsTargetForTarget(target, config),
- "_private");
- }
- void cmGlobalNinjaGenerator::AppendTargetOutputs(
- cmGeneratorTarget const* target, cmNinjaDeps& outputs,
- const std::string& config, cmNinjaTargetDepends depends) const
- {
- // for frameworks, we want the real name, not sample name
- // frameworks always appear versioned, and the build.ninja
- // will always attempt to manage symbolic links instead
- // of letting cmOSXBundleGenerator do it.
- bool realname = target->IsFrameworkOnApple();
- switch (target->GetType()) {
- case cmStateEnums::SHARED_LIBRARY:
- case cmStateEnums::STATIC_LIBRARY:
- case cmStateEnums::MODULE_LIBRARY: {
- if (depends == DependOnTargetOrdering) {
- outputs.push_back(this->OrderDependsTargetForTarget(target, config));
- break;
- }
- }
- CM_FALLTHROUGH;
- case cmStateEnums::EXECUTABLE: {
- if (target->IsApple() && target->HasImportLibrary(config)) {
- outputs.push_back(this->ConvertToNinjaPath(target->GetFullPath(
- config, cmStateEnums::ImportLibraryArtifact, realname)));
- }
- outputs.push_back(this->ConvertToNinjaPath(target->GetFullPath(
- config, cmStateEnums::RuntimeBinaryArtifact, realname)));
- break;
- }
- case cmStateEnums::OBJECT_LIBRARY: {
- if (depends == DependOnTargetOrdering) {
- outputs.push_back(this->OrderDependsTargetForTarget(target, config));
- break;
- }
- }
- CM_FALLTHROUGH;
- case cmStateEnums::GLOBAL_TARGET:
- case cmStateEnums::INTERFACE_LIBRARY:
- case cmStateEnums::UTILITY: {
- std::string path =
- cmStrCat(target->GetLocalGenerator()->GetCurrentBinaryDirectory(), '/',
- target->GetName());
- std::string output = this->ConvertToNinjaPath(path);
- if (target->Target->IsPerConfig()) {
- output = this->BuildAlias(output, config);
- }
- outputs.push_back(output);
- break;
- }
- case cmStateEnums::UNKNOWN_LIBRARY:
- break;
- }
- }
- void cmGlobalNinjaGenerator::AppendTargetDepends(
- cmGeneratorTarget const* target, cmNinjaDeps& outputs,
- const std::string& config, const std::string& fileConfig,
- cmNinjaTargetDepends depends)
- {
- if (target->GetType() == cmStateEnums::GLOBAL_TARGET) {
- // These depend only on other CMake-provided targets, e.g. "all".
- for (BT<std::pair<std::string, bool>> const& util :
- target->GetUtilities()) {
- std::string d =
- cmStrCat(target->GetLocalGenerator()->GetCurrentBinaryDirectory(), '/',
- util.Value.first);
- outputs.push_back(this->BuildAlias(this->ConvertToNinjaPath(d), config));
- }
- } else {
- cmNinjaDeps outs;
- auto computeISPCOutputs = [](cmGlobalNinjaGenerator* gg,
- cmGeneratorTarget const* depTarget,
- cmNinjaDeps& outputDeps,
- const std::string& targetConfig) {
- if (depTarget->CanCompileSources()) {
- auto headers = depTarget->GetGeneratedISPCHeaders(targetConfig);
- if (!headers.empty()) {
- std::transform(headers.begin(), headers.end(), headers.begin(),
- gg->MapToNinjaPath());
- outputDeps.insert(outputDeps.end(), headers.begin(), headers.end());
- }
- auto objs = depTarget->GetGeneratedISPCObjects(targetConfig);
- if (!objs.empty()) {
- std::transform(objs.begin(), objs.end(), objs.begin(),
- gg->MapToNinjaPath());
- outputDeps.insert(outputDeps.end(), objs.begin(), objs.end());
- }
- }
- };
- for (cmTargetDepend const& targetDep :
- this->GetTargetDirectDepends(target)) {
- if (!targetDep->IsInBuildSystem()) {
- continue;
- }
- if (targetDep.IsCross()) {
- this->AppendTargetOutputs(targetDep, outs, fileConfig, depends);
- computeISPCOutputs(this, targetDep, outs, fileConfig);
- } else {
- this->AppendTargetOutputs(targetDep, outs, config, depends);
- computeISPCOutputs(this, targetDep, outs, config);
- }
- }
- std::sort(outs.begin(), outs.end());
- cm::append(outputs, outs);
- }
- }
- void cmGlobalNinjaGenerator::AppendTargetDependsClosure(
- cmGeneratorTarget const* target, std::unordered_set<std::string>& outputs,
- const std::string& config, const std::string& fileConfig, bool genexOutput,
- bool omit_self)
- {
- // try to locate the target in the cache
- ByConfig::TargetDependsClosureKey key{
- target,
- config,
- genexOutput,
- };
- auto find = this->Configs[fileConfig].TargetDependsClosures.lower_bound(key);
- if (find == this->Configs[fileConfig].TargetDependsClosures.end() ||
- find->first != key) {
- // We now calculate the closure outputs by inspecting the dependent
- // targets recursively.
- // For that we have to distinguish between a local result set that is only
- // relevant for filling the cache entries properly isolated and a global
- // result set that is relevant for the result of the top level call to
- // AppendTargetDependsClosure.
- std::unordered_set<std::string>
- this_outs; // this will be the new cache entry
- for (auto const& dep_target : this->GetTargetDirectDepends(target)) {
- if (!dep_target->IsInBuildSystem()) {
- continue;
- }
- if (!this->IsSingleConfigUtility(target) &&
- !this->IsSingleConfigUtility(dep_target) &&
- this->EnableCrossConfigBuild() && !dep_target.IsCross() &&
- !genexOutput) {
- continue;
- }
- if (dep_target.IsCross()) {
- this->AppendTargetDependsClosure(dep_target, this_outs, fileConfig,
- fileConfig, genexOutput, false);
- } else {
- this->AppendTargetDependsClosure(dep_target, this_outs, config,
- fileConfig, genexOutput, false);
- }
- }
- find = this->Configs[fileConfig].TargetDependsClosures.emplace_hint(
- find, key, std::move(this_outs));
- }
- // now fill the outputs of the final result from the newly generated cache
- // entry
- outputs.insert(find->second.begin(), find->second.end());
- // finally generate the outputs of the target itself, if applicable
- cmNinjaDeps outs;
- if (!omit_self) {
- this->AppendTargetOutputs(target, outs, config, DependOnTargetArtifact);
- }
- outputs.insert(outs.begin(), outs.end());
- }
- void cmGlobalNinjaGenerator::AddTargetAlias(const std::string& alias,
- cmGeneratorTarget* target,
- const std::string& config)
- {
- std::string outputPath = this->NinjaOutputPath(alias);
- std::string buildAlias = this->BuildAlias(outputPath, config);
- cmNinjaDeps outputs;
- if (config != "all") {
- this->AppendTargetOutputs(target, outputs, config, DependOnTargetArtifact);
- }
- // Mark the target's outputs as ambiguous to ensure that no other target
- // uses the output as an alias.
- for (std::string const& output : outputs) {
- this->TargetAliases[output].GeneratorTarget = nullptr;
- this->DefaultTargetAliases[output].GeneratorTarget = nullptr;
- for (std::string const& config2 : this->GetConfigNames()) {
- this->Configs[config2].TargetAliases[output].GeneratorTarget = nullptr;
- }
- }
- // Insert the alias into the map. If the alias was already present in the
- // map and referred to another target, mark it as ambiguous.
- TargetAlias ta;
- ta.GeneratorTarget = target;
- ta.Config = config;
- auto newAliasGlobal =
- this->TargetAliases.insert(std::make_pair(buildAlias, ta));
- if (newAliasGlobal.second &&
- newAliasGlobal.first->second.GeneratorTarget != target) {
- newAliasGlobal.first->second.GeneratorTarget = nullptr;
- }
- auto newAliasConfig =
- this->Configs[config].TargetAliases.insert(std::make_pair(outputPath, ta));
- if (newAliasConfig.second &&
- newAliasConfig.first->second.GeneratorTarget != target) {
- newAliasConfig.first->second.GeneratorTarget = nullptr;
- }
- if (this->DefaultConfigs.count(config)) {
- auto newAliasDefaultGlobal =
- this->DefaultTargetAliases.insert(std::make_pair(outputPath, ta));
- if (newAliasDefaultGlobal.second &&
- newAliasDefaultGlobal.first->second.GeneratorTarget != target) {
- newAliasDefaultGlobal.first->second.GeneratorTarget = nullptr;
- }
- }
- }
- void cmGlobalNinjaGenerator::WriteTargetAliases(std::ostream& os)
- {
- cmGlobalNinjaGenerator::WriteDivider(os);
- os << "# Target aliases.\n\n";
- cmNinjaBuild build("phony");
- build.Outputs.emplace_back();
- for (auto const& ta : this->TargetAliases) {
- // Don't write ambiguous aliases.
- if (!ta.second.GeneratorTarget) {
- continue;
- }
- // Don't write alias if there is a already a custom command with
- // matching output
- if (this->HasCustomCommandOutput(ta.first)) {
- continue;
- }
- build.Outputs.front() = ta.first;
- build.ExplicitDeps.clear();
- if (ta.second.Config == "all") {
- for (auto const& config : this->CrossConfigs) {
- this->AppendTargetOutputs(ta.second.GeneratorTarget,
- build.ExplicitDeps, config,
- DependOnTargetArtifact);
- }
- } else {
- this->AppendTargetOutputs(ta.second.GeneratorTarget, build.ExplicitDeps,
- ta.second.Config, DependOnTargetArtifact);
- }
- this->WriteBuild(this->EnableCrossConfigBuild() &&
- (ta.second.Config == "all" ||
- this->CrossConfigs.count(ta.second.Config))
- ? os
- : *this->GetImplFileStream(ta.second.Config),
- build);
- }
- if (this->IsMultiConfig()) {
- for (std::string const& config : this->GetConfigNames()) {
- for (auto const& ta : this->Configs[config].TargetAliases) {
- // Don't write ambiguous aliases.
- if (!ta.second.GeneratorTarget) {
- continue;
- }
- // Don't write alias if there is a already a custom command with
- // matching output
- if (this->HasCustomCommandOutput(ta.first)) {
- continue;
- }
- build.Outputs.front() = ta.first;
- build.ExplicitDeps.clear();
- this->AppendTargetOutputs(ta.second.GeneratorTarget,
- build.ExplicitDeps, config,
- DependOnTargetArtifact);
- this->WriteBuild(*this->GetConfigFileStream(config), build);
- }
- }
- if (!this->DefaultConfigs.empty()) {
- for (auto const& ta : this->DefaultTargetAliases) {
- // Don't write ambiguous aliases.
- if (!ta.second.GeneratorTarget) {
- continue;
- }
- // Don't write alias if there is a already a custom command with
- // matching output
- if (this->HasCustomCommandOutput(ta.first)) {
- continue;
- }
- build.Outputs.front() = ta.first;
- build.ExplicitDeps.clear();
- for (auto const& config : this->DefaultConfigs) {
- this->AppendTargetOutputs(ta.second.GeneratorTarget,
- build.ExplicitDeps, config,
- DependOnTargetArtifact);
- }
- this->WriteBuild(*this->GetDefaultFileStream(), build);
- }
- }
- }
- }
- void cmGlobalNinjaGenerator::WriteFolderTargets(std::ostream& os)
- {
- cmGlobalNinjaGenerator::WriteDivider(os);
- os << "# Folder targets.\n\n";
- std::map<std::string, DirectoryTarget> dirTargets =
- this->ComputeDirectoryTargets();
- // Codegen target
- if (this->CheckCMP0171()) {
- for (auto const& it : dirTargets) {
- cmNinjaBuild build("phony");
- cmGlobalNinjaGenerator::WriteDivider(os);
- std::string const& currentBinaryDir = it.first;
- DirectoryTarget const& dt = it.second;
- std::vector<std::string> configs =
- static_cast<cmLocalNinjaGenerator const*>(dt.LG)->GetConfigNames();
- // Setup target
- build.Comment = cmStrCat("Folder: ", currentBinaryDir);
- build.Outputs.emplace_back();
- std::string const buildDirCodegenTarget =
- this->ConvertToNinjaPath(cmStrCat(currentBinaryDir, "/codegen"));
- for (auto const& config : configs) {
- build.ExplicitDeps.clear();
- build.Outputs.front() =
- this->BuildAlias(buildDirCodegenTarget, config);
- for (DirectoryTarget::Target const& t : dt.Targets) {
- if (this->IsExcludedFromAllInConfig(t, config)) {
- continue;
- }
- std::vector<cmSourceFile const*> customCommandSources;
- t.GT->GetCustomCommands(customCommandSources, config);
- for (cmSourceFile const* sf : customCommandSources) {
- cmCustomCommand const* cc = sf->GetCustomCommand();
- if (cc->GetCodegen()) {
- auto const& outputs = cc->GetOutputs();
- std::transform(outputs.begin(), outputs.end(),
- std::back_inserter(build.ExplicitDeps),
- this->MapToNinjaPath());
- }
- }
- }
- for (DirectoryTarget::Dir const& d : dt.Children) {
- if (!d.ExcludeFromAll) {
- build.ExplicitDeps.emplace_back(this->BuildAlias(
- this->ConvertToNinjaPath(cmStrCat(d.Path, "/codegen")), config));
- }
- }
- // Write target
- this->WriteBuild(this->EnableCrossConfigBuild() &&
- this->CrossConfigs.count(config)
- ? os
- : *this->GetImplFileStream(config),
- build);
- }
- // Add shortcut target
- if (this->IsMultiConfig()) {
- for (auto const& config : configs) {
- build.ExplicitDeps = { this->BuildAlias(buildDirCodegenTarget,
- config) };
- build.Outputs.front() = buildDirCodegenTarget;
- this->WriteBuild(*this->GetConfigFileStream(config), build);
- }
- if (!this->DefaultFileConfig.empty()) {
- build.ExplicitDeps.clear();
- for (auto const& config : this->DefaultConfigs) {
- build.ExplicitDeps.push_back(
- this->BuildAlias(buildDirCodegenTarget, config));
- }
- build.Outputs.front() = buildDirCodegenTarget;
- this->WriteBuild(*this->GetDefaultFileStream(), build);
- }
- }
- // Add target for all configs
- if (this->EnableCrossConfigBuild()) {
- build.ExplicitDeps.clear();
- for (auto const& config : this->CrossConfigs) {
- build.ExplicitDeps.push_back(
- this->BuildAlias(buildDirCodegenTarget, config));
- }
- build.Outputs.front() =
- this->BuildAlias(buildDirCodegenTarget, "codegen");
- this->WriteBuild(os, build);
- }
- }
- }
- // All target
- for (auto const& it : dirTargets) {
- cmNinjaBuild build("phony");
- cmGlobalNinjaGenerator::WriteDivider(os);
- std::string const& currentBinaryDir = it.first;
- DirectoryTarget const& dt = it.second;
- std::vector<std::string> configs =
- static_cast<cmLocalNinjaGenerator const*>(dt.LG)->GetConfigNames();
- // Setup target
- build.Comment = cmStrCat("Folder: ", currentBinaryDir);
- build.Outputs.emplace_back();
- std::string const buildDirAllTarget =
- this->ConvertToNinjaPath(cmStrCat(currentBinaryDir, "/all"));
- for (auto const& config : configs) {
- build.ExplicitDeps.clear();
- build.Outputs.front() = this->BuildAlias(buildDirAllTarget, config);
- for (DirectoryTarget::Target const& t : dt.Targets) {
- if (!this->IsExcludedFromAllInConfig(t, config)) {
- this->AppendTargetOutputs(t.GT, build.ExplicitDeps, config,
- DependOnTargetArtifact);
- }
- }
- for (DirectoryTarget::Dir const& d : dt.Children) {
- if (!d.ExcludeFromAll) {
- build.ExplicitDeps.emplace_back(this->BuildAlias(
- this->ConvertToNinjaPath(cmStrCat(d.Path, "/all")), config));
- }
- }
- // Write target
- this->WriteBuild(this->EnableCrossConfigBuild() &&
- this->CrossConfigs.count(config)
- ? os
- : *this->GetImplFileStream(config),
- build);
- }
- // Add shortcut target
- if (this->IsMultiConfig()) {
- for (auto const& config : configs) {
- build.ExplicitDeps = { this->BuildAlias(buildDirAllTarget, config) };
- build.Outputs.front() = buildDirAllTarget;
- this->WriteBuild(*this->GetConfigFileStream(config), build);
- }
- if (!this->DefaultFileConfig.empty()) {
- build.ExplicitDeps.clear();
- for (auto const& config : this->DefaultConfigs) {
- build.ExplicitDeps.push_back(
- this->BuildAlias(buildDirAllTarget, config));
- }
- build.Outputs.front() = buildDirAllTarget;
- this->WriteBuild(*this->GetDefaultFileStream(), build);
- }
- }
- // Add target for all configs
- if (this->EnableCrossConfigBuild()) {
- build.ExplicitDeps.clear();
- for (auto const& config : this->CrossConfigs) {
- build.ExplicitDeps.push_back(
- this->BuildAlias(buildDirAllTarget, config));
- }
- build.Outputs.front() = this->BuildAlias(buildDirAllTarget, "all");
- this->WriteBuild(os, build);
- }
- }
- }
- void cmGlobalNinjaGenerator::WriteBuiltinTargets(std::ostream& os)
- {
- // Write headers.
- cmGlobalNinjaGenerator::WriteDivider(os);
- os << "# Built-in targets\n\n";
- this->WriteTargetRebuildManifest(os);
- this->WriteTargetClean(os);
- this->WriteTargetHelp(os);
- for (std::string const& config : this->GetConfigNames()) {
- this->WriteTargetDefault(*this->GetConfigFileStream(config));
- }
- if (!this->DefaultFileConfig.empty()) {
- this->WriteTargetDefault(*this->GetDefaultFileStream());
- }
- if (this->InstallTargetEnabled &&
- this->GetCMakeInstance()->GetState()->GetGlobalPropertyAsBool(
- "INSTALL_PARALLEL") &&
- !this->Makefiles[0]->IsOn("CMAKE_SKIP_INSTALL_RULES")) {
- cmNinjaBuild build("phony");
- build.Comment = "Install every subdirectory in parallel";
- build.Outputs.emplace_back(this->GetInstallParallelTargetName());
- for (auto const& mf : this->Makefiles) {
- build.ExplicitDeps.emplace_back(
- this->ConvertToNinjaPath(cmStrCat(mf->GetCurrentBinaryDirectory(), "/",
- this->GetInstallLocalTargetName())));
- }
- WriteBuild(os, build);
- }
- }
- void cmGlobalNinjaGenerator::WriteTargetDefault(std::ostream& os)
- {
- if (!this->HasOutputPathPrefix()) {
- cmNinjaDeps all;
- all.push_back(this->TargetAll);
- cmGlobalNinjaGenerator::WriteDefault(os, all,
- "Make the all target the default.");
- }
- }
- void cmGlobalNinjaGenerator::WriteTargetRebuildManifest(std::ostream& os)
- {
- if (this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
- return;
- }
- cmake* cm = this->GetCMakeInstance();
- const auto& lg = this->LocalGenerators[0];
- {
- cmNinjaRule rule("RERUN_CMAKE");
- rule.Command = cmStrCat(
- this->CMakeCmd(), " --regenerate-during-build",
- cm->GetIgnoreCompileWarningAsError() ? " --compile-no-warning-as-error"
- : "",
- cm->GetIgnoreLinkWarningAsError() ? " --link-no-warning-as-error" : "",
- " -S",
- lg->ConvertToOutputFormat(lg->GetSourceDirectory(),
- cmOutputConverter::SHELL),
- " -B",
- lg->ConvertToOutputFormat(lg->GetBinaryDirectory(),
- cmOutputConverter::SHELL));
- rule.Description = "Re-running CMake...";
- rule.Comment = "Rule for re-running cmake.";
- rule.Generator = true;
- WriteRule(*this->RulesFileStream, rule);
- }
- cmNinjaBuild reBuild("RERUN_CMAKE");
- reBuild.Comment = "Re-run CMake if any of its inputs changed.";
- this->AddRebuildManifestOutputs(reBuild.Outputs);
- for (const auto& localGen : this->LocalGenerators) {
- for (std::string const& fi : localGen->GetMakefile()->GetListFiles()) {
- reBuild.ImplicitDeps.push_back(this->ConvertToNinjaPath(fi));
- }
- }
- reBuild.ImplicitDeps.push_back(this->CMakeCacheFile);
- // Use 'console' pool to get non buffered output of the CMake re-run call
- // Available since Ninja 1.5
- if (this->SupportsDirectConsole()) {
- reBuild.Variables["pool"] = "console";
- }
- if (this->SupportsManifestRestat() && cm->DoWriteGlobVerifyTarget()) {
- {
- cmNinjaRule rule("VERIFY_GLOBS");
- rule.Command =
- cmStrCat(this->CMakeCmd(), " -P ",
- lg->ConvertToOutputFormat(cm->GetGlobVerifyScript(),
- cmOutputConverter::SHELL));
- rule.Description = "Re-checking globbed directories...";
- rule.Comment = "Rule for re-checking globbed directories.";
- rule.Generator = true;
- this->WriteRule(*this->RulesFileStream, rule);
- }
- cmNinjaBuild phonyBuild("phony");
- phonyBuild.Comment = "Phony target to force glob verification run.";
- phonyBuild.Outputs.push_back(
- cmStrCat(cm->GetGlobVerifyScript(), "_force"));
- this->WriteBuild(os, phonyBuild);
- reBuild.Variables["restat"] = "1";
- std::string const verifyScriptFile =
- this->NinjaOutputPath(cm->GetGlobVerifyScript());
- std::string const verifyStampFile =
- this->NinjaOutputPath(cm->GetGlobVerifyStamp());
- {
- cmNinjaBuild vgBuild("VERIFY_GLOBS");
- vgBuild.Comment =
- "Re-run CMake to check if globbed directories changed.";
- vgBuild.Outputs.push_back(verifyStampFile);
- vgBuild.ImplicitDeps = phonyBuild.Outputs;
- vgBuild.Variables = reBuild.Variables;
- this->WriteBuild(os, vgBuild);
- }
- reBuild.Variables.erase("restat");
- reBuild.ImplicitDeps.push_back(verifyScriptFile);
- reBuild.ExplicitDeps.push_back(verifyStampFile);
- } else if (!this->SupportsManifestRestat() &&
- cm->DoWriteGlobVerifyTarget()) {
- std::ostringstream msg;
- msg << "The detected version of Ninja:\n"
- << " " << this->NinjaVersion << "\n"
- << "is less than the version of Ninja required by CMake for adding "
- "restat dependencies to the build.ninja manifest regeneration "
- "target:\n"
- << " "
- << cmGlobalNinjaGenerator::RequiredNinjaVersionForManifestRestat()
- << "\n";
- msg << "Any pre-check scripts, such as those generated for file(GLOB "
- "CONFIGURE_DEPENDS), will not be run by Ninja.";
- this->GetCMakeInstance()->IssueMessage(MessageType::AUTHOR_WARNING,
- msg.str());
- }
- std::sort(reBuild.ImplicitDeps.begin(), reBuild.ImplicitDeps.end());
- reBuild.ImplicitDeps.erase(
- std::unique(reBuild.ImplicitDeps.begin(), reBuild.ImplicitDeps.end()),
- reBuild.ImplicitDeps.end());
- this->WriteBuild(os, reBuild);
- {
- cmNinjaBuild build("phony");
- build.Comment = "A missing CMake input file is not an error.";
- std::set_difference(std::make_move_iterator(reBuild.ImplicitDeps.begin()),
- std::make_move_iterator(reBuild.ImplicitDeps.end()),
- this->CustomCommandOutputs.begin(),
- this->CustomCommandOutputs.end(),
- std::back_inserter(build.Outputs));
- this->WriteBuild(os, build);
- }
- }
- std::string cmGlobalNinjaGenerator::CMakeCmd() const
- {
- const auto& lgen = this->LocalGenerators.at(0);
- return lgen->ConvertToOutputFormat(cmSystemTools::GetCMakeCommand(),
- cmOutputConverter::SHELL);
- }
- std::string cmGlobalNinjaGenerator::NinjaCmd() const
- {
- const auto& lgen = this->LocalGenerators[0];
- if (lgen) {
- return lgen->ConvertToOutputFormat(this->NinjaCommand,
- cmOutputConverter::SHELL);
- }
- return "ninja";
- }
- bool cmGlobalNinjaGenerator::SupportsDirectConsole() const
- {
- return this->NinjaSupportsConsolePool;
- }
- bool cmGlobalNinjaGenerator::SupportsImplicitOuts() const
- {
- return this->NinjaSupportsImplicitOuts;
- }
- bool cmGlobalNinjaGenerator::SupportsManifestRestat() const
- {
- return this->NinjaSupportsManifestRestat;
- }
- bool cmGlobalNinjaGenerator::SupportsMultilineDepfile() const
- {
- return this->NinjaSupportsMultilineDepfile;
- }
- bool cmGlobalNinjaGenerator::SupportsCWDDepend() const
- {
- return this->NinjaSupportsCWDDepend;
- }
- bool cmGlobalNinjaGenerator::WriteTargetCleanAdditional(std::ostream& os)
- {
- const auto& lgr = this->LocalGenerators.at(0);
- std::string cleanScriptRel = "CMakeFiles/clean_additional.cmake";
- std::string cleanScriptAbs =
- cmStrCat(lgr->GetBinaryDirectory(), '/', cleanScriptRel);
- std::vector<std::string> const& configs = this->GetConfigNames();
- // Check if there are additional files to clean
- bool empty = true;
- for (auto const& config : configs) {
- auto const it = this->Configs.find(config);
- if (it != this->Configs.end() &&
- !it->second.AdditionalCleanFiles.empty()) {
- empty = false;
- break;
- }
- }
- if (empty) {
- // Remove cmake clean script file if it exists
- cmSystemTools::RemoveFile(cleanScriptAbs);
- return false;
- }
- // Write cmake clean script file
- {
- cmGeneratedFileStream fout(cleanScriptAbs);
- if (!fout) {
- return false;
- }
- fout << "# Additional clean files\ncmake_minimum_required(VERSION 3.16)\n";
- for (auto const& config : configs) {
- auto const it = this->Configs.find(config);
- if (it != this->Configs.end() &&
- !it->second.AdditionalCleanFiles.empty()) {
- fout << "\nif(\"${CONFIG}\" STREQUAL \"\" OR \"${CONFIG}\" STREQUAL \""
- << config << "\")\n";
- fout << " file(REMOVE_RECURSE\n";
- for (std::string const& acf : it->second.AdditionalCleanFiles) {
- fout << " "
- << cmOutputConverter::EscapeForCMake(
- this->ConvertToNinjaPath(acf))
- << '\n';
- }
- fout << " )\n";
- fout << "endif()\n";
- }
- }
- }
- // Register clean script file
- lgr->GetMakefile()->AddCMakeOutputFile(cleanScriptAbs);
- // Write rule
- {
- cmNinjaRule rule("CLEAN_ADDITIONAL");
- rule.Command = cmStrCat(
- this->CMakeCmd(), " -DCONFIG=$CONFIG -P ",
- lgr->ConvertToOutputFormat(this->NinjaOutputPath(cleanScriptRel),
- cmOutputConverter::SHELL));
- rule.Description = "Cleaning additional files...";
- rule.Comment = "Rule for cleaning additional files.";
- WriteRule(*this->RulesFileStream, rule);
- }
- // Write build
- {
- cmNinjaBuild build("CLEAN_ADDITIONAL");
- build.Comment = "Clean additional files.";
- build.Outputs.emplace_back();
- for (auto const& config : configs) {
- build.Outputs.front() = this->BuildAlias(
- this->NinjaOutputPath(this->GetAdditionalCleanTargetName()), config);
- build.Variables["CONFIG"] = config;
- this->WriteBuild(os, build);
- }
- if (this->IsMultiConfig()) {
- build.Outputs.front() =
- this->NinjaOutputPath(this->GetAdditionalCleanTargetName());
- build.Variables["CONFIG"] = "";
- this->WriteBuild(os, build);
- }
- }
- // Return success
- return true;
- }
- void cmGlobalNinjaGenerator::WriteTargetClean(std::ostream& os)
- {
- // -- Additional clean target
- bool additionalFiles = this->WriteTargetCleanAdditional(os);
- // -- Default clean target
- // Write rule
- {
- cmNinjaRule rule("CLEAN");
- rule.Command = cmStrCat(this->NinjaCmd(), " $FILE_ARG -t clean $TARGETS");
- rule.Description = "Cleaning all built files...";
- rule.Comment = "Rule for cleaning all built files.";
- WriteRule(*this->RulesFileStream, rule);
- }
- // Write build
- {
- cmNinjaBuild build("CLEAN");
- build.Comment = "Clean all the built files.";
- build.Outputs.emplace_back();
- for (std::string const& config : this->GetConfigNames()) {
- build.Outputs.front() = this->BuildAlias(
- this->NinjaOutputPath(this->GetCleanTargetName()), config);
- if (this->IsMultiConfig()) {
- build.Variables["TARGETS"] = cmStrCat(
- this->BuildAlias(
- this->NinjaOutputPath(GetByproductsForCleanTargetName()), config),
- " ", this->NinjaOutputPath(GetByproductsForCleanTargetName()));
- }
- build.ExplicitDeps.clear();
- if (additionalFiles) {
- build.ExplicitDeps.push_back(this->BuildAlias(
- this->NinjaOutputPath(this->GetAdditionalCleanTargetName()),
- config));
- }
- for (std::string const& fileConfig : this->GetConfigNames()) {
- if (fileConfig != config && !this->EnableCrossConfigBuild()) {
- continue;
- }
- if (this->IsMultiConfig()) {
- build.Variables["FILE_ARG"] = cmStrCat(
- "-f ",
- this->NinjaOutputPath(
- cmGlobalNinjaMultiGenerator::GetNinjaImplFilename(fileConfig)));
- }
- this->WriteBuild(*this->GetImplFileStream(fileConfig), build);
- }
- }
- if (this->EnableCrossConfigBuild()) {
- build.Outputs.front() = this->BuildAlias(
- this->NinjaOutputPath(this->GetCleanTargetName()), "all");
- build.ExplicitDeps.clear();
- if (additionalFiles) {
- for (auto const& config : this->CrossConfigs) {
- build.ExplicitDeps.push_back(this->BuildAlias(
- this->NinjaOutputPath(this->GetAdditionalCleanTargetName()),
- config));
- }
- }
- std::vector<std::string> byproducts;
- byproducts.reserve(this->CrossConfigs.size());
- for (auto const& config : this->CrossConfigs) {
- byproducts.push_back(this->BuildAlias(
- this->NinjaOutputPath(GetByproductsForCleanTargetName()), config));
- }
- byproducts.emplace_back(GetByproductsForCleanTargetName());
- build.Variables["TARGETS"] = cmJoin(byproducts, " ");
- for (std::string const& fileConfig : this->GetConfigNames()) {
- build.Variables["FILE_ARG"] = cmStrCat(
- "-f ",
- this->NinjaOutputPath(
- cmGlobalNinjaMultiGenerator::GetNinjaImplFilename(fileConfig)));
- this->WriteBuild(*this->GetImplFileStream(fileConfig), build);
- }
- }
- }
- if (this->IsMultiConfig()) {
- cmNinjaBuild build("phony");
- build.Outputs.emplace_back(
- this->NinjaOutputPath(this->GetCleanTargetName()));
- build.ExplicitDeps.emplace_back();
- for (std::string const& config : this->GetConfigNames()) {
- build.ExplicitDeps.front() = this->BuildAlias(
- this->NinjaOutputPath(this->GetCleanTargetName()), config);
- this->WriteBuild(*this->GetConfigFileStream(config), build);
- }
- if (!this->DefaultConfigs.empty()) {
- build.ExplicitDeps.clear();
- for (auto const& config : this->DefaultConfigs) {
- build.ExplicitDeps.push_back(this->BuildAlias(
- this->NinjaOutputPath(this->GetCleanTargetName()), config));
- }
- this->WriteBuild(*this->GetDefaultFileStream(), build);
- }
- }
- // Write byproducts
- if (this->IsMultiConfig()) {
- cmNinjaBuild build("phony");
- build.Comment = "Clean byproducts.";
- build.Outputs.emplace_back(
- this->ConvertToNinjaPath(GetByproductsForCleanTargetName()));
- build.ExplicitDeps = this->ByproductsForCleanTarget;
- this->WriteBuild(os, build);
- for (std::string const& config : this->GetConfigNames()) {
- build.Outputs.front() = this->BuildAlias(
- this->ConvertToNinjaPath(GetByproductsForCleanTargetName()), config);
- build.ExplicitDeps = this->Configs[config].ByproductsForCleanTarget;
- this->WriteBuild(os, build);
- }
- }
- }
- void cmGlobalNinjaGenerator::WriteTargetHelp(std::ostream& os)
- {
- {
- cmNinjaRule rule("HELP");
- rule.Command = cmStrCat(this->NinjaCmd(), " -t targets");
- rule.Description = "All primary targets available:";
- rule.Comment = "Rule for printing all primary targets available.";
- WriteRule(*this->RulesFileStream, rule);
- }
- {
- cmNinjaBuild build("HELP");
- build.Comment = "Print all primary targets available.";
- build.Outputs.push_back(this->NinjaOutputPath("help"));
- this->WriteBuild(os, build);
- }
- }
- void cmGlobalNinjaGenerator::InitOutputPathPrefix()
- {
- this->OutputPathPrefix =
- this->LocalGenerators[0]->GetMakefile()->GetSafeDefinition(
- "CMAKE_NINJA_OUTPUT_PATH_PREFIX");
- EnsureTrailingSlash(this->OutputPathPrefix);
- }
- std::string cmGlobalNinjaGenerator::NinjaOutputPath(
- std::string const& path) const
- {
- if (!this->HasOutputPathPrefix() || cmSystemTools::FileIsFullPath(path)) {
- return path;
- }
- return cmStrCat(this->OutputPathPrefix, path);
- }
- void cmGlobalNinjaGenerator::StripNinjaOutputPathPrefixAsSuffix(
- std::string& path)
- {
- if (path.empty()) {
- return;
- }
- EnsureTrailingSlash(path);
- cmStripSuffixIfExists(path, this->OutputPathPrefix);
- }
- #if !defined(CMAKE_BOOTSTRAP)
- /*
- We use the following approach to support Fortran. Each target already
- has a <target>.dir/ directory used to hold intermediate files for CMake.
- For each target, a FortranDependInfo.json file is generated by CMake with
- information about include directories, module directories, and the locations
- the per-target directories for target dependencies.
- Compilation of source files within a target is split into the following steps:
- 1. Preprocess all sources, scan preprocessed output for module dependencies.
- This step is done with independent build statements for each source,
- and can therefore be done in parallel.
- rule Fortran_PREPROCESS
- depfile = $DEP_FILE
- command = gfortran -cpp $DEFINES $INCLUDES $FLAGS -E $in -o $out &&
- cmake -E cmake_ninja_depends \
- --tdi=FortranDependInfo.json --lang=Fortran \
- --src=$out --out=$out --dep=$DEP_FILE --obj=$OBJ_FILE \
- --ddi=$DYNDEP_INTERMEDIATE_FILE
- build src.f90-pp.f90 | src.f90.o.ddi: Fortran_PREPROCESS src.f90
- OBJ_FILE = src.f90.o
- DEP_FILE = src.f90.o.d
- DYNDEP_INTERMEDIATE_FILE = src.f90.o.ddi
- The ``cmake -E cmake_ninja_depends`` tool reads the preprocessed output
- and generates the ninja depfile for preprocessor dependencies. It also
- generates a "ddi" file (in a format private to CMake) that lists the
- object file that compilation will produce along with the module names
- it provides and/or requires. The "ddi" file is an implicit output
- because it should not appear in "$out" but is generated by the rule.
- 2. Consolidate the per-source module dependencies saved in the "ddi"
- files from all sources to produce a ninja "dyndep" file, ``Fortran.dd``.
- rule Fortran_DYNDEP
- command = cmake -E cmake_ninja_dyndep \
- --tdi=FortranDependInfo.json --lang=Fortran --dd=$out $in
- build Fortran.dd: Fortran_DYNDEP src1.f90.o.ddi src2.f90.o.ddi
- The ``cmake -E cmake_ninja_dyndep`` tool reads the "ddi" files from all
- sources in the target and the ``FortranModules.json`` files from targets
- on which the target depends. It computes dependency edges on compilations
- that require modules to those that provide the modules. This information
- is placed in the ``Fortran.dd`` file for ninja to load later. It also
- writes the expected location of modules provided by this target into
- ``FortranModules.json`` for use by dependent targets.
- 3. Compile all sources after loading dynamically discovered dependencies
- of the compilation build statements from their ``dyndep`` bindings.
- rule Fortran_COMPILE
- command = gfortran $INCLUDES $FLAGS -c $in -o $out
- build src1.f90.o: Fortran_COMPILE src1.f90-pp.f90 || Fortran.dd
- dyndep = Fortran.dd
- The "dyndep" binding tells ninja to load dynamically discovered
- dependency information from ``Fortran.dd``. This adds information
- such as:
- build src1.f90.o | mod1.mod: dyndep
- restat = 1
- This tells ninja that ``mod1.mod`` is an implicit output of compiling
- the object file ``src1.f90.o``. The ``restat`` binding tells it that
- the timestamp of the output may not always change. Additionally:
- build src2.f90.o: dyndep | mod1.mod
- This tells ninja that ``mod1.mod`` is a dependency of compiling the
- object file ``src2.f90.o``. This ensures that ``src1.f90.o`` and
- ``mod1.mod`` will always be up to date before ``src2.f90.o`` is built
- (because the latter consumes the module).
- */
- namespace {
- struct cmSourceInfo
- {
- cmScanDepInfo ScanDep;
- std::vector<std::string> Includes;
- };
- cm::optional<cmSourceInfo> cmcmd_cmake_ninja_depends_fortran(
- std::string const& arg_tdi, std::string const& arg_src,
- std::string const& arg_src_orig);
- }
- int cmcmd_cmake_ninja_depends(std::vector<std::string>::const_iterator argBeg,
- std::vector<std::string>::const_iterator argEnd)
- {
- std::string arg_tdi;
- std::string arg_src;
- std::string arg_src_orig;
- std::string arg_out;
- std::string arg_dep;
- std::string arg_obj;
- std::string arg_ddi;
- std::string arg_lang;
- for (std::string const& arg : cmMakeRange(argBeg, argEnd)) {
- if (cmHasLiteralPrefix(arg, "--tdi=")) {
- arg_tdi = arg.substr(6);
- } else if (cmHasLiteralPrefix(arg, "--src=")) {
- arg_src = arg.substr(6);
- } else if (cmHasLiteralPrefix(arg, "--src-orig=")) {
- arg_src_orig = arg.substr(11);
- } else if (cmHasLiteralPrefix(arg, "--out=")) {
- arg_out = arg.substr(6);
- } else if (cmHasLiteralPrefix(arg, "--dep=")) {
- arg_dep = arg.substr(6);
- } else if (cmHasLiteralPrefix(arg, "--obj=")) {
- arg_obj = arg.substr(6);
- } else if (cmHasLiteralPrefix(arg, "--ddi=")) {
- arg_ddi = arg.substr(6);
- } else if (cmHasLiteralPrefix(arg, "--lang=")) {
- arg_lang = arg.substr(7);
- } else if (cmHasLiteralPrefix(arg, "--pp=")) {
- // CMake 3.26 and below used '--pp=' instead of '--src=' and '--out='.
- arg_src = arg.substr(5);
- arg_out = arg_src;
- } else {
- cmSystemTools::Error(
- cmStrCat("-E cmake_ninja_depends unknown argument: ", arg));
- return 1;
- }
- }
- if (arg_tdi.empty()) {
- cmSystemTools::Error("-E cmake_ninja_depends requires value for --tdi=");
- return 1;
- }
- if (arg_src.empty()) {
- cmSystemTools::Error("-E cmake_ninja_depends requires value for --src=");
- return 1;
- }
- if (arg_out.empty()) {
- cmSystemTools::Error("-E cmake_ninja_depends requires value for --out=");
- return 1;
- }
- if (arg_dep.empty()) {
- cmSystemTools::Error("-E cmake_ninja_depends requires value for --dep=");
- return 1;
- }
- if (arg_obj.empty()) {
- cmSystemTools::Error("-E cmake_ninja_depends requires value for --obj=");
- return 1;
- }
- if (arg_ddi.empty()) {
- cmSystemTools::Error("-E cmake_ninja_depends requires value for --ddi=");
- return 1;
- }
- if (arg_lang.empty()) {
- cmSystemTools::Error("-E cmake_ninja_depends requires value for --lang=");
- return 1;
- }
- cm::optional<cmSourceInfo> info;
- if (arg_lang == "Fortran") {
- info = cmcmd_cmake_ninja_depends_fortran(arg_tdi, arg_src, arg_src_orig);
- } else {
- cmSystemTools::Error(
- cmStrCat("-E cmake_ninja_depends does not understand the ", arg_lang,
- " language"));
- return 1;
- }
- if (!info) {
- // The error message is already expected to have been output.
- return 1;
- }
- info->ScanDep.PrimaryOutput = arg_obj;
- {
- cmGeneratedFileStream depfile(arg_dep);
- depfile << cmSystemTools::ConvertToUnixOutputPath(arg_out) << ":";
- for (std::string const& include : info->Includes) {
- depfile << " \\\n " << cmSystemTools::ConvertToUnixOutputPath(include);
- }
- depfile << "\n";
- }
- if (!cmScanDepFormat_P1689_Write(arg_ddi, info->ScanDep)) {
- cmSystemTools::Error(
- cmStrCat("-E cmake_ninja_depends failed to write ", arg_ddi));
- return 1;
- }
- return 0;
- }
- namespace {
- cm::optional<cmSourceInfo> cmcmd_cmake_ninja_depends_fortran(
- std::string const& arg_tdi, std::string const& arg_src,
- std::string const& arg_src_orig)
- {
- cm::optional<cmSourceInfo> info;
- cmFortranCompiler fc;
- std::vector<std::string> includes;
- std::string dir_top_bld;
- std::string module_dir;
- if (!arg_src_orig.empty()) {
- // Prepend the original source file's directory as an include directory
- // so Fortran INCLUDE statements can look for files in it.
- std::string src_orig_dir = cmSystemTools::GetParentDirectory(arg_src_orig);
- if (!src_orig_dir.empty()) {
- includes.push_back(src_orig_dir);
- }
- }
- {
- Json::Value tdio;
- Json::Value const& tdi = tdio;
- {
- cmsys::ifstream tdif(arg_tdi.c_str(), std::ios::in | std::ios::binary);
- Json::Reader reader;
- if (!reader.parse(tdif, tdio, false)) {
- cmSystemTools::Error(
- cmStrCat("-E cmake_ninja_depends failed to parse ", arg_tdi,
- reader.getFormattedErrorMessages()));
- return info;
- }
- }
- dir_top_bld = tdi["dir-top-bld"].asString();
- if (!dir_top_bld.empty() && !cmHasLiteralSuffix(dir_top_bld, "/")) {
- dir_top_bld += '/';
- }
- Json::Value const& tdi_include_dirs = tdi["include-dirs"];
- if (tdi_include_dirs.isArray()) {
- for (auto const& tdi_include_dir : tdi_include_dirs) {
- includes.push_back(tdi_include_dir.asString());
- }
- }
- Json::Value const& tdi_module_dir = tdi["module-dir"];
- module_dir = tdi_module_dir.asString();
- if (!module_dir.empty() && !cmHasLiteralSuffix(module_dir, "/")) {
- module_dir += '/';
- }
- Json::Value const& tdi_compiler_id = tdi["compiler-id"];
- fc.Id = tdi_compiler_id.asString();
- Json::Value const& tdi_submodule_sep = tdi["submodule-sep"];
- fc.SModSep = tdi_submodule_sep.asString();
- Json::Value const& tdi_submodule_ext = tdi["submodule-ext"];
- fc.SModExt = tdi_submodule_ext.asString();
- }
- cmFortranSourceInfo finfo;
- std::set<std::string> defines;
- cmFortranParser parser(fc, includes, defines, finfo);
- if (!cmFortranParser_FilePush(&parser, arg_src.c_str())) {
- cmSystemTools::Error(
- cmStrCat("-E cmake_ninja_depends failed to open ", arg_src));
- return info;
- }
- if (cmFortran_yyparse(parser.Scanner) != 0) {
- // Failed to parse the file.
- return info;
- }
- info = cmSourceInfo();
- for (std::string const& provide : finfo.Provides) {
- cmSourceReqInfo src_info;
- src_info.LogicalName = provide;
- if (!module_dir.empty()) {
- std::string mod = cmStrCat(module_dir, provide);
- if (!dir_top_bld.empty() && cmHasPrefix(mod, dir_top_bld)) {
- mod = mod.substr(dir_top_bld.size());
- }
- src_info.CompiledModulePath = std::move(mod);
- }
- info->ScanDep.Provides.emplace_back(src_info);
- }
- for (std::string const& require : finfo.Requires) {
- // Require modules not provided in the same source.
- if (finfo.Provides.count(require)) {
- continue;
- }
- cmSourceReqInfo src_info;
- src_info.LogicalName = require;
- info->ScanDep.Requires.emplace_back(src_info);
- }
- for (std::string const& include : finfo.Includes) {
- info->Includes.push_back(include);
- }
- return info;
- }
- }
- bool cmGlobalNinjaGenerator::WriteDyndepFile(
- std::string const& dir_top_src, std::string const& dir_top_bld,
- std::string const& dir_cur_src, std::string const& dir_cur_bld,
- std::string const& arg_dd, std::vector<std::string> const& arg_ddis,
- std::string const& module_dir,
- std::vector<std::string> const& linked_target_dirs,
- std::vector<std::string> const& forward_modules_from_target_dirs,
- std::string const& arg_lang, std::string const& arg_modmapfmt,
- cmCxxModuleExportInfo const& export_info)
- {
- // Setup path conversions.
- {
- cmStateSnapshot snapshot = this->GetCMakeInstance()->GetCurrentSnapshot();
- snapshot.GetDirectory().SetCurrentSource(dir_cur_src);
- snapshot.GetDirectory().SetCurrentBinary(dir_cur_bld);
- auto mfd = cm::make_unique<cmMakefile>(this, snapshot);
- auto lgd = this->CreateLocalGenerator(mfd.get());
- lgd->SetRelativePathTop(dir_top_src, dir_top_bld);
- this->Makefiles.push_back(std::move(mfd));
- this->LocalGenerators.push_back(std::move(lgd));
- }
- std::vector<cmScanDepInfo> objects;
- for (std::string const& arg_ddi : arg_ddis) {
- cmScanDepInfo info;
- if (!cmScanDepFormat_P1689_Parse(arg_ddi, &info)) {
- cmSystemTools::Error(
- cmStrCat("-E cmake_ninja_dyndep failed to parse ddi file ", arg_ddi));
- return false;
- }
- objects.push_back(std::move(info));
- }
- CxxModuleUsage usages;
- // Map from module name to module file path, if known.
- struct AvailableModuleInfo
- {
- std::string BmiPath;
- bool IsPrivate;
- };
- std::map<std::string, AvailableModuleInfo> mod_files;
- // Populate the module map with those provided by linked targets first.
- for (std::string const& linked_target_dir : linked_target_dirs) {
- std::string const ltmn =
- cmStrCat(linked_target_dir, '/', arg_lang, "Modules.json");
- Json::Value ltm;
- cmsys::ifstream ltmf(ltmn.c_str(), std::ios::in | std::ios::binary);
- if (!ltmf) {
- cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to open ",
- ltmn, " for module information"));
- return false;
- }
- Json::Reader reader;
- if (!reader.parse(ltmf, ltm, false)) {
- cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
- linked_target_dir,
- reader.getFormattedErrorMessages()));
- return false;
- }
- if (ltm.isObject()) {
- Json::Value const& target_modules = ltm["modules"];
- if (target_modules.isObject()) {
- for (auto i = target_modules.begin(); i != target_modules.end(); ++i) {
- Json::Value const& visible_module = *i;
- if (visible_module.isObject()) {
- Json::Value const& bmi_path = visible_module["bmi"];
- Json::Value const& is_private = visible_module["is-private"];
- mod_files[i.key().asString()] = AvailableModuleInfo{
- bmi_path.asString(),
- is_private.asBool(),
- };
- }
- }
- }
- Json::Value const& target_modules_references = ltm["references"];
- if (target_modules_references.isObject()) {
- for (auto i = target_modules_references.begin();
- i != target_modules_references.end(); ++i) {
- if (i->isObject()) {
- Json::Value const& reference_path = (*i)["path"];
- CxxModuleReference module_reference;
- if (reference_path.isString()) {
- module_reference.Path = reference_path.asString();
- }
- Json::Value const& reference_method = (*i)["lookup-method"];
- if (reference_method.isString()) {
- std::string reference = reference_method.asString();
- if (reference == "by-name") {
- module_reference.Method = LookupMethod::ByName;
- } else if (reference == "include-angle") {
- module_reference.Method = LookupMethod::IncludeAngle;
- } else if (reference == "include-quote") {
- module_reference.Method = LookupMethod::IncludeQuote;
- }
- }
- usages.Reference[i.key().asString()] = module_reference;
- }
- }
- }
- Json::Value const& target_modules_usage = ltm["usages"];
- if (target_modules_usage.isObject()) {
- for (auto i = target_modules_usage.begin();
- i != target_modules_usage.end(); ++i) {
- if (i->isArray()) {
- for (auto j = i->begin(); j != i->end(); ++j) {
- usages.Usage[i.key().asString()].insert(j->asString());
- }
- }
- }
- }
- }
- }
- cm::optional<CxxModuleMapFormat> modmap_fmt;
- if (arg_modmapfmt.empty()) {
- // nothing to do.
- } else if (arg_modmapfmt == "clang") {
- modmap_fmt = CxxModuleMapFormat::Clang;
- } else if (arg_modmapfmt == "gcc") {
- modmap_fmt = CxxModuleMapFormat::Gcc;
- } else if (arg_modmapfmt == "msvc") {
- modmap_fmt = CxxModuleMapFormat::Msvc;
- } else {
- cmSystemTools::Error(
- cmStrCat("-E cmake_ninja_dyndep does not understand the ", arg_modmapfmt,
- " module map format"));
- return false;
- }
- auto module_ext = CxxModuleMapExtension(modmap_fmt);
- // Extend the module map with those provided by this target.
- // We do this after loading the modules provided by linked targets
- // in case we have one of the same name that must be preferred.
- Json::Value target_modules = Json::objectValue;
- for (cmScanDepInfo const& object : objects) {
- for (auto const& p : object.Provides) {
- std::string mod;
- if (cmDyndepCollation::IsBmiOnly(export_info, object.PrimaryOutput)) {
- mod = object.PrimaryOutput;
- } else if (!p.CompiledModulePath.empty()) {
- // The scanner provided the path to the module file.
- mod = p.CompiledModulePath;
- if (!cmSystemTools::FileIsFullPath(mod)) {
- // Treat relative to work directory (top of build tree).
- mod = cmSystemTools::CollapseFullPath(mod, dir_top_bld);
- }
- } else {
- // Assume the module file path matches the logical module name.
- std::string safe_logical_name =
- p.LogicalName; // TODO: needs fixing for header units
- cmSystemTools::ReplaceString(safe_logical_name, ":", "-");
- mod = cmStrCat(module_dir, safe_logical_name, module_ext);
- }
- mod_files[p.LogicalName] = AvailableModuleInfo{
- mod,
- false, // Always visible within our own target.
- };
- Json::Value& module_info = target_modules[p.LogicalName] =
- Json::objectValue;
- module_info["bmi"] = mod;
- module_info["is-private"] =
- cmDyndepCollation::IsObjectPrivate(object.PrimaryOutput, export_info);
- }
- }
- cmGeneratedFileStream ddf(arg_dd);
- ddf << "ninja_dyndep_version = 1.0\n";
- {
- CxxModuleLocations locs;
- locs.RootDirectory = ".";
- locs.PathForGenerator = [this](std::string path) -> std::string {
- path = this->ConvertToNinjaPath(path);
- # ifdef _WIN32
- if (this->IsGCCOnWindows()) {
- std::replace(path.begin(), path.end(), '\\', '/');
- }
- # endif
- return path;
- };
- locs.BmiLocationForModule =
- [&mod_files](std::string const& logical) -> CxxBmiLocation {
- auto m = mod_files.find(logical);
- if (m != mod_files.end()) {
- if (m->second.IsPrivate) {
- return CxxBmiLocation::Private();
- }
- return CxxBmiLocation::Known(m->second.BmiPath);
- }
- return CxxBmiLocation::Unknown();
- };
- // Insert information about the current target's modules.
- if (modmap_fmt) {
- bool private_usage_found = false;
- auto cycle_modules =
- CxxModuleUsageSeed(locs, objects, usages, private_usage_found);
- if (!cycle_modules.empty()) {
- cmSystemTools::Error(
- cmStrCat("Circular dependency detected in the C++ module import "
- "graph. See modules named: \"",
- cmJoin(cycle_modules, R"(", ")"_s), '"'));
- return false;
- }
- if (private_usage_found) {
- // Already errored in the function.
- return false;
- }
- }
- cmNinjaBuild build("dyndep");
- build.Outputs.emplace_back("");
- for (cmScanDepInfo const& object : objects) {
- build.Outputs[0] = this->ConvertToNinjaPath(object.PrimaryOutput);
- build.ImplicitOuts.clear();
- for (auto const& p : object.Provides) {
- auto const implicitOut =
- this->ConvertToNinjaPath(mod_files[p.LogicalName].BmiPath);
- // Ignore the `provides` when the BMI is the output.
- if (implicitOut != build.Outputs[0]) {
- build.ImplicitOuts.emplace_back(implicitOut);
- }
- }
- build.ImplicitDeps.clear();
- for (auto const& r : object.Requires) {
- auto mit = mod_files.find(r.LogicalName);
- if (mit != mod_files.end()) {
- build.ImplicitDeps.push_back(
- this->ConvertToNinjaPath(mit->second.BmiPath));
- }
- }
- build.Variables.clear();
- if (!object.Provides.empty()) {
- build.Variables.emplace("restat", "1");
- }
- if (modmap_fmt) {
- auto mm = CxxModuleMapContent(*modmap_fmt, locs, object, usages);
- // XXX(modmap): If changing this path construction, change
- // `cmNinjaTargetGenerator::WriteObjectBuildStatements` and
- // `cmNinjaTargetGenerator::ExportObjectCompileCommand` to generate the
- // corresponding file path.
- cmGeneratedFileStream mmf;
- mmf.Open(cmStrCat(object.PrimaryOutput, ".modmap"), false,
- CxxModuleMapOpenMode(*modmap_fmt) ==
- CxxModuleMapMode::Binary);
- mmf.SetCopyIfDifferent(true);
- mmf << mm;
- }
- this->WriteBuild(ddf, build);
- }
- }
- Json::Value target_module_info = Json::objectValue;
- target_module_info["modules"] = target_modules;
- auto& target_usages = target_module_info["usages"] = Json::objectValue;
- for (auto const& u : usages.Usage) {
- auto& mod_usage = target_usages[u.first] = Json::arrayValue;
- for (auto const& v : u.second) {
- mod_usage.append(v);
- }
- }
- auto name_for_method = [](LookupMethod method) -> cm::static_string_view {
- switch (method) {
- case LookupMethod::ByName:
- return "by-name"_s;
- case LookupMethod::IncludeAngle:
- return "include-angle"_s;
- case LookupMethod::IncludeQuote:
- return "include-quote"_s;
- }
- assert(false && "unsupported lookup method");
- return ""_s;
- };
- auto& target_references = target_module_info["references"] =
- Json::objectValue;
- for (auto const& r : usages.Reference) {
- auto& mod_ref = target_references[r.first] = Json::objectValue;
- mod_ref["path"] = r.second.Path;
- mod_ref["lookup-method"] = std::string(name_for_method(r.second.Method));
- }
- // Store the map of modules provided by this target in a file for
- // use by dependents that reference this target in linked-target-dirs.
- std::string const target_mods_file = cmStrCat(
- cmSystemTools::GetFilenamePath(arg_dd), '/', arg_lang, "Modules.json");
- // Populate the module map with those provided by linked targets first.
- for (std::string const& forward_modules_from_target_dir :
- forward_modules_from_target_dirs) {
- std::string const fmftn =
- cmStrCat(forward_modules_from_target_dir, '/', arg_lang, "Modules.json");
- Json::Value fmft;
- cmsys::ifstream fmftf(fmftn.c_str(), std::ios::in | std::ios::binary);
- if (!fmftf) {
- cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to open ",
- fmftn, " for module information"));
- return false;
- }
- Json::Reader reader;
- if (!reader.parse(fmftf, fmft, false)) {
- cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
- forward_modules_from_target_dir,
- reader.getFormattedErrorMessages()));
- return false;
- }
- if (!fmft.isObject()) {
- continue;
- }
- auto forward_info = [](Json::Value& target, Json::Value const& source) {
- if (!source.isObject()) {
- return;
- }
- for (auto i = source.begin(); i != source.end(); ++i) {
- std::string const key = i.key().asString();
- if (target.isMember(key)) {
- continue;
- }
- target[key] = *i;
- }
- };
- // Forward info from forwarding targets into our collation.
- Json::Value& tmi_target_modules = target_module_info["modules"];
- forward_info(tmi_target_modules, fmft["modules"]);
- forward_info(target_references, fmft["references"]);
- forward_info(target_usages, fmft["usages"]);
- }
- cmGeneratedFileStream tmf(target_mods_file);
- tmf.SetCopyIfDifferent(true);
- tmf << target_module_info;
- cmDyndepMetadataCallbacks cb;
- cb.ModuleFile =
- [mod_files](std::string const& name) -> cm::optional<std::string> {
- auto m = mod_files.find(name);
- if (m != mod_files.end()) {
- return m->second.BmiPath;
- }
- return {};
- };
- return cmDyndepCollation::WriteDyndepMetadata(arg_lang, objects, export_info,
- cb);
- }
- int cmcmd_cmake_ninja_dyndep(std::vector<std::string>::const_iterator argBeg,
- std::vector<std::string>::const_iterator argEnd)
- {
- std::vector<std::string> arg_full =
- cmSystemTools::HandleResponseFile(argBeg, argEnd);
- std::string arg_dd;
- std::string arg_lang;
- std::string arg_tdi;
- std::string arg_modmapfmt;
- std::vector<std::string> arg_ddis;
- for (std::string const& arg : arg_full) {
- if (cmHasLiteralPrefix(arg, "--tdi=")) {
- arg_tdi = arg.substr(6);
- } else if (cmHasLiteralPrefix(arg, "--lang=")) {
- arg_lang = arg.substr(7);
- } else if (cmHasLiteralPrefix(arg, "--dd=")) {
- arg_dd = arg.substr(5);
- } else if (cmHasLiteralPrefix(arg, "--modmapfmt=")) {
- arg_modmapfmt = arg.substr(12);
- } else if (!cmHasLiteralPrefix(arg, "--") &&
- cmHasLiteralSuffix(arg, ".ddi")) {
- arg_ddis.push_back(arg);
- } else {
- cmSystemTools::Error(
- cmStrCat("-E cmake_ninja_dyndep unknown argument: ", arg));
- return 1;
- }
- }
- if (arg_tdi.empty()) {
- cmSystemTools::Error("-E cmake_ninja_dyndep requires value for --tdi=");
- return 1;
- }
- if (arg_lang.empty()) {
- cmSystemTools::Error("-E cmake_ninja_dyndep requires value for --lang=");
- return 1;
- }
- if (arg_dd.empty()) {
- cmSystemTools::Error("-E cmake_ninja_dyndep requires value for --dd=");
- return 1;
- }
- Json::Value tdio;
- Json::Value const& tdi = tdio;
- {
- cmsys::ifstream tdif(arg_tdi.c_str(), std::ios::in | std::ios::binary);
- Json::Reader reader;
- if (!reader.parse(tdif, tdio, false)) {
- cmSystemTools::Error(cmStrCat("-E cmake_ninja_dyndep failed to parse ",
- arg_tdi,
- reader.getFormattedErrorMessages()));
- return 1;
- }
- }
- std::string const dir_cur_bld = tdi["dir-cur-bld"].asString();
- std::string const dir_cur_src = tdi["dir-cur-src"].asString();
- std::string const dir_top_bld = tdi["dir-top-bld"].asString();
- std::string const dir_top_src = tdi["dir-top-src"].asString();
- std::string module_dir = tdi["module-dir"].asString();
- if (!module_dir.empty() && !cmHasLiteralSuffix(module_dir, "/")) {
- module_dir += '/';
- }
- std::vector<std::string> linked_target_dirs;
- Json::Value const& tdi_linked_target_dirs = tdi["linked-target-dirs"];
- if (tdi_linked_target_dirs.isArray()) {
- for (auto const& tdi_linked_target_dir : tdi_linked_target_dirs) {
- linked_target_dirs.push_back(tdi_linked_target_dir.asString());
- }
- }
- std::vector<std::string> forward_modules_from_target_dirs;
- Json::Value const& tdi_forward_modules_from_target_dirs =
- tdi["forward-modules-from-target-dirs"];
- if (tdi_forward_modules_from_target_dirs.isArray()) {
- for (auto const& tdi_forward_modules_from_target_dir :
- tdi_forward_modules_from_target_dirs) {
- forward_modules_from_target_dirs.push_back(
- tdi_forward_modules_from_target_dir.asString());
- }
- }
- std::string const compilerId = tdi["compiler-id"].asString();
- std::string const simulateId = tdi["compiler-simulate-id"].asString();
- std::string const compilerFrontendVariant =
- tdi["compiler-frontend-variant"].asString();
- auto export_info = cmDyndepCollation::ParseExportInfo(tdi);
- cmake cm(cmake::RoleInternal, cmState::Unknown);
- cm.SetHomeDirectory(dir_top_src);
- cm.SetHomeOutputDirectory(dir_top_bld);
- auto ggd = cm.CreateGlobalGenerator("Ninja");
- if (!ggd) {
- return 1;
- }
- cmGlobalNinjaGenerator& gg =
- cm::static_reference_cast<cmGlobalNinjaGenerator>(ggd);
- # ifdef _WIN32
- if (DetectGCCOnWindows(compilerId, simulateId, compilerFrontendVariant)) {
- gg.MarkAsGCCOnWindows();
- }
- # endif
- return gg.WriteDyndepFile(dir_top_src, dir_top_bld, dir_cur_src, dir_cur_bld,
- arg_dd, arg_ddis, module_dir, linked_target_dirs,
- forward_modules_from_target_dirs, arg_lang,
- arg_modmapfmt, *export_info)
- ? 0
- : 1;
- }
- #endif
- bool cmGlobalNinjaGenerator::EnableCrossConfigBuild() const
- {
- return !this->CrossConfigs.empty();
- }
- void cmGlobalNinjaGenerator::AppendDirectoryForConfig(
- const std::string& prefix, const std::string& config,
- const std::string& suffix, std::string& dir)
- {
- if (!config.empty() && this->IsMultiConfig()) {
- dir += cmStrCat(prefix, config, suffix);
- }
- }
- std::set<std::string> cmGlobalNinjaGenerator::GetCrossConfigs(
- const std::string& fileConfig) const
- {
- auto result = this->CrossConfigs;
- result.insert(fileConfig);
- return result;
- }
- bool cmGlobalNinjaGenerator::IsSingleConfigUtility(
- cmGeneratorTarget const* target) const
- {
- return target->GetType() == cmStateEnums::UTILITY &&
- !this->PerConfigUtilityTargets.count(target->GetName());
- }
- std::string cmGlobalNinjaGenerator::ConvertToOutputPath(std::string path) const
- {
- return this->ConvertToNinjaPath(path);
- }
- const char* cmGlobalNinjaMultiGenerator::NINJA_COMMON_FILE =
- "CMakeFiles/common.ninja";
- const char* cmGlobalNinjaMultiGenerator::NINJA_FILE_EXTENSION = ".ninja";
- cmGlobalNinjaMultiGenerator::cmGlobalNinjaMultiGenerator(cmake* cm)
- : cmGlobalNinjaGenerator(cm)
- {
- cm->GetState()->SetIsGeneratorMultiConfig(true);
- cm->GetState()->SetNinjaMulti(true);
- }
- cmDocumentationEntry cmGlobalNinjaMultiGenerator::GetDocumentation()
- {
- return { cmGlobalNinjaMultiGenerator::GetActualName(),
- "Generates build-<Config>.ninja files." };
- }
- std::string cmGlobalNinjaMultiGenerator::ExpandCFGIntDir(
- const std::string& str, const std::string& config) const
- {
- std::string result = str;
- cmSystemTools::ReplaceString(result, this->GetCMakeCFGIntDir(), config);
- return result;
- }
- bool cmGlobalNinjaMultiGenerator::OpenBuildFileStreams()
- {
- if (!this->OpenFileStream(this->CommonFileStream,
- cmGlobalNinjaMultiGenerator::NINJA_COMMON_FILE)) {
- return false;
- }
- if (!this->OpenFileStream(this->DefaultFileStream, NINJA_BUILD_FILE)) {
- return false;
- }
- *this->DefaultFileStream << "# Build using rules for '"
- << this->DefaultFileConfig << "'.\n\n"
- << "include "
- << this->NinjaOutputPath(
- GetNinjaImplFilename(this->DefaultFileConfig))
- << "\n\n";
- // Write a comment about this file.
- *this->CommonFileStream
- << "# This file contains build statements common to all "
- "configurations.\n\n";
- std::vector<std::string> const& configs = this->GetConfigNames();
- return std::all_of(
- configs.begin(), configs.end(), [this](std::string const& config) -> bool {
- // Open impl file.
- if (!this->OpenFileStream(this->ImplFileStreams[config],
- GetNinjaImplFilename(config))) {
- return false;
- }
- // Write a comment about this file.
- *this->ImplFileStreams[config]
- << "# This file contains build statements specific to the \"" << config
- << "\"\n# configuration.\n\n";
- // Open config file.
- if (!this->OpenFileStream(this->ConfigFileStreams[config],
- GetNinjaConfigFilename(config))) {
- return false;
- }
- // Write a comment about this file.
- *this->ConfigFileStreams[config]
- << "# This file contains aliases specific to the \"" << config
- << "\"\n# configuration.\n\n"
- << "include " << this->NinjaOutputPath(GetNinjaImplFilename(config))
- << "\n\n";
- return true;
- });
- }
- void cmGlobalNinjaMultiGenerator::CloseBuildFileStreams()
- {
- if (this->CommonFileStream) {
- this->CommonFileStream.reset();
- } else {
- cmSystemTools::Error("Common file stream was not open.");
- }
- if (this->DefaultFileStream) {
- this->DefaultFileStream.reset();
- } // No error if it wasn't open
- for (std::string const& config : this->GetConfigNames()) {
- if (this->ImplFileStreams[config]) {
- this->ImplFileStreams[config].reset();
- } else {
- cmSystemTools::Error(
- cmStrCat("Impl file stream for \"", config, "\" was not open."));
- }
- if (this->ConfigFileStreams[config]) {
- this->ConfigFileStreams[config].reset();
- } else {
- cmSystemTools::Error(
- cmStrCat("Config file stream for \"", config, "\" was not open."));
- }
- }
- }
- void cmGlobalNinjaMultiGenerator::AppendNinjaFileArgument(
- GeneratedMakeCommand& command, const std::string& config) const
- {
- if (!config.empty()) {
- command.Add("-f");
- command.Add(GetNinjaConfigFilename(config));
- }
- }
- std::string cmGlobalNinjaMultiGenerator::GetNinjaImplFilename(
- const std::string& config)
- {
- return cmStrCat("CMakeFiles/impl-", config,
- cmGlobalNinjaMultiGenerator::NINJA_FILE_EXTENSION);
- }
- std::string cmGlobalNinjaMultiGenerator::GetNinjaConfigFilename(
- const std::string& config)
- {
- return cmStrCat("build-", config,
- cmGlobalNinjaMultiGenerator::NINJA_FILE_EXTENSION);
- }
- void cmGlobalNinjaMultiGenerator::AddRebuildManifestOutputs(
- cmNinjaDeps& outputs) const
- {
- for (std::string const& config : this->GetConfigNames()) {
- outputs.push_back(this->NinjaOutputPath(GetNinjaImplFilename(config)));
- outputs.push_back(this->NinjaOutputPath(GetNinjaConfigFilename(config)));
- }
- if (!this->DefaultFileConfig.empty()) {
- outputs.push_back(this->NinjaOutputPath(NINJA_BUILD_FILE));
- }
- this->AddCMakeFilesToRebuild(outputs);
- }
- void cmGlobalNinjaMultiGenerator::GetQtAutoGenConfigs(
- std::vector<std::string>& configs) const
- {
- std::vector<std::string> const& allConfigs = this->GetConfigNames();
- configs.insert(configs.end(), cm::cbegin(allConfigs), cm::cend(allConfigs));
- }
- bool cmGlobalNinjaMultiGenerator::InspectConfigTypeVariables()
- {
- std::vector<std::string> configsList =
- this->Makefiles.front()->GetGeneratorConfigs(
- cmMakefile::IncludeEmptyConfig);
- std::set<std::string> configs(configsList.cbegin(), configsList.cend());
- this->DefaultFileConfig =
- this->Makefiles.front()->GetSafeDefinition("CMAKE_DEFAULT_BUILD_TYPE");
- if (this->DefaultFileConfig.empty()) {
- this->DefaultFileConfig = configsList.front();
- }
- if (!configs.count(this->DefaultFileConfig)) {
- std::ostringstream msg;
- msg << "The configuration specified by "
- << "CMAKE_DEFAULT_BUILD_TYPE (" << this->DefaultFileConfig
- << ") is not present in CMAKE_CONFIGURATION_TYPES";
- this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
- msg.str());
- return false;
- }
- cmList crossConfigsList{ this->Makefiles.front()->GetSafeDefinition(
- "CMAKE_CROSS_CONFIGS") };
- auto crossConfigs = ListSubsetWithAll(configs, configs, crossConfigsList);
- if (!crossConfigs) {
- std::ostringstream msg;
- msg << "CMAKE_CROSS_CONFIGS is not a subset of "
- << "CMAKE_CONFIGURATION_TYPES";
- this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
- msg.str());
- return false;
- }
- this->CrossConfigs = *crossConfigs;
- auto defaultConfigsString =
- this->Makefiles.front()->GetSafeDefinition("CMAKE_DEFAULT_CONFIGS");
- if (defaultConfigsString.empty()) {
- defaultConfigsString = this->DefaultFileConfig;
- }
- if (!defaultConfigsString.empty() &&
- defaultConfigsString != this->DefaultFileConfig &&
- (this->DefaultFileConfig.empty() || this->CrossConfigs.empty())) {
- std::ostringstream msg;
- msg << "CMAKE_DEFAULT_CONFIGS cannot be used without "
- << "CMAKE_DEFAULT_BUILD_TYPE or CMAKE_CROSS_CONFIGS";
- this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
- msg.str());
- return false;
- }
- cmList defaultConfigsList(defaultConfigsString);
- if (!this->DefaultFileConfig.empty()) {
- auto defaultConfigs =
- ListSubsetWithAll(this->GetCrossConfigs(this->DefaultFileConfig),
- this->CrossConfigs, defaultConfigsList);
- if (!defaultConfigs) {
- std::ostringstream msg;
- msg << "CMAKE_DEFAULT_CONFIGS is not a subset of CMAKE_CROSS_CONFIGS";
- this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
- msg.str());
- return false;
- }
- this->DefaultConfigs = *defaultConfigs;
- }
- return true;
- }
- std::string cmGlobalNinjaMultiGenerator::GetDefaultBuildConfig() const
- {
- return "";
- }
- std::string cmGlobalNinjaMultiGenerator::OrderDependsTargetForTarget(
- cmGeneratorTarget const* target, const std::string& config) const
- {
- return cmStrCat("cmake_object_order_depends_target_", target->GetName(), '_',
- cmSystemTools::UpperCase(config));
- }
|