cmGlobalNinjaGenerator.cxx 89 KB

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