cmGlobalNinjaGenerator.cxx 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  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 "cm_jsoncpp_reader.h"
  5. #include "cm_jsoncpp_value.h"
  6. #include "cm_jsoncpp_writer.h"
  7. #include "cmsys/FStream.hxx"
  8. #include <algorithm>
  9. #include <ctype.h>
  10. #include <iterator>
  11. #include <memory> // IWYU pragma: keep
  12. #include <sstream>
  13. #include <stdio.h>
  14. #include "cmAlgorithms.h"
  15. #include "cmDocumentationEntry.h"
  16. #include "cmFortranParser.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmGeneratorExpressionEvaluationFile.h"
  19. #include "cmGeneratorTarget.h"
  20. #include "cmListFileCache.h"
  21. #include "cmLocalGenerator.h"
  22. #include "cmLocalNinjaGenerator.h"
  23. #include "cmMakefile.h"
  24. #include "cmMessageType.h"
  25. #include "cmNinjaLinkLineComputer.h"
  26. #include "cmOutputConverter.h"
  27. #include "cmRange.h"
  28. #include "cmState.h"
  29. #include "cmStateDirectory.h"
  30. #include "cmStateSnapshot.h"
  31. #include "cmStateTypes.h"
  32. #include "cmSystemTools.h"
  33. #include "cmTarget.h"
  34. #include "cmTargetDepend.h"
  35. #include "cmVersion.h"
  36. #include "cmake.h"
  37. class cmLinkLineComputer;
  38. const char* cmGlobalNinjaGenerator::NINJA_BUILD_FILE = "build.ninja";
  39. const char* cmGlobalNinjaGenerator::NINJA_RULES_FILE = "rules.ninja";
  40. const char* cmGlobalNinjaGenerator::INDENT = " ";
  41. #ifdef _WIN32
  42. std::string const cmGlobalNinjaGenerator::SHELL_NOOP = "cd .";
  43. #else
  44. std::string const cmGlobalNinjaGenerator::SHELL_NOOP = ":";
  45. #endif
  46. void cmGlobalNinjaGenerator::Indent(std::ostream& os, int count)
  47. {
  48. for (int i = 0; i < count; ++i) {
  49. os << cmGlobalNinjaGenerator::INDENT;
  50. }
  51. }
  52. void cmGlobalNinjaGenerator::WriteDivider(std::ostream& os)
  53. {
  54. os << "# ======================================"
  55. "=======================================\n";
  56. }
  57. void cmGlobalNinjaGenerator::WriteComment(std::ostream& os,
  58. const std::string& comment)
  59. {
  60. if (comment.empty()) {
  61. return;
  62. }
  63. std::string::size_type lpos = 0;
  64. std::string::size_type rpos;
  65. os << "\n#############################################\n";
  66. while ((rpos = comment.find('\n', lpos)) != std::string::npos) {
  67. os << "# " << comment.substr(lpos, rpos - lpos) << "\n";
  68. lpos = rpos + 1;
  69. }
  70. os << "# " << comment.substr(lpos) << "\n\n";
  71. }
  72. cmLinkLineComputer* cmGlobalNinjaGenerator::CreateLinkLineComputer(
  73. cmOutputConverter* outputConverter,
  74. cmStateDirectory const& /* stateDir */) const
  75. {
  76. return new cmNinjaLinkLineComputer(
  77. outputConverter,
  78. this->LocalGenerators[0]->GetStateSnapshot().GetDirectory(), this);
  79. }
  80. std::string cmGlobalNinjaGenerator::EncodeRuleName(std::string const& name)
  81. {
  82. // Ninja rule names must match "[a-zA-Z0-9_.-]+". Use ".xx" to encode
  83. // "." and all invalid characters as hexadecimal.
  84. std::string encoded;
  85. for (char i : name) {
  86. if (isalnum(i) || i == '_' || i == '-') {
  87. encoded += i;
  88. } else {
  89. char buf[16];
  90. sprintf(buf, ".%02x", static_cast<unsigned int>(i));
  91. encoded += buf;
  92. }
  93. }
  94. return encoded;
  95. }
  96. std::string cmGlobalNinjaGenerator::EncodeLiteral(const std::string& lit)
  97. {
  98. std::string result = lit;
  99. cmSystemTools::ReplaceString(result, "$", "$$");
  100. cmSystemTools::ReplaceString(result, "\n", "$\n");
  101. return result;
  102. }
  103. std::string cmGlobalNinjaGenerator::EncodePath(const std::string& path)
  104. {
  105. std::string result = path;
  106. #ifdef _WIN32
  107. if (this->IsGCCOnWindows())
  108. std::replace(result.begin(), result.end(), '\\', '/');
  109. else
  110. std::replace(result.begin(), result.end(), '/', '\\');
  111. #endif
  112. result = EncodeLiteral(result);
  113. cmSystemTools::ReplaceString(result, " ", "$ ");
  114. cmSystemTools::ReplaceString(result, ":", "$:");
  115. return result;
  116. }
  117. void cmGlobalNinjaGenerator::WriteBuild(
  118. std::ostream& os, const std::string& comment, const std::string& rule,
  119. const cmNinjaDeps& outputs, const cmNinjaDeps& implicitOuts,
  120. const cmNinjaDeps& explicitDeps, const cmNinjaDeps& implicitDeps,
  121. const cmNinjaDeps& orderOnlyDeps, const cmNinjaVars& variables,
  122. const std::string& rspfile, int cmdLineLimit, bool* usedResponseFile)
  123. {
  124. // Make sure there is a rule.
  125. if (rule.empty()) {
  126. cmSystemTools::Error("No rule for WriteBuild! called with comment: " +
  127. comment);
  128. return;
  129. }
  130. // Make sure there is at least one output file.
  131. if (outputs.empty()) {
  132. cmSystemTools::Error(
  133. "No output files for WriteBuild! called with comment: " + comment);
  134. return;
  135. }
  136. cmGlobalNinjaGenerator::WriteComment(os, comment);
  137. std::string arguments;
  138. // TODO: Better formatting for when there are multiple input/output files.
  139. // Write explicit dependencies.
  140. for (std::string const& explicitDep : explicitDeps) {
  141. arguments += " " + EncodePath(explicitDep);
  142. }
  143. // Write implicit dependencies.
  144. if (!implicitDeps.empty()) {
  145. arguments += " |";
  146. for (std::string const& implicitDep : implicitDeps) {
  147. arguments += " " + EncodePath(implicitDep);
  148. }
  149. }
  150. // Write order-only dependencies.
  151. if (!orderOnlyDeps.empty()) {
  152. arguments += " ||";
  153. for (std::string const& orderOnlyDep : orderOnlyDeps) {
  154. arguments += " " + EncodePath(orderOnlyDep);
  155. }
  156. }
  157. arguments += "\n";
  158. std::string build;
  159. // Write outputs files.
  160. build += "build";
  161. for (std::string const& output : outputs) {
  162. build += " " + EncodePath(output);
  163. if (this->ComputingUnknownDependencies) {
  164. this->CombinedBuildOutputs.insert(output);
  165. }
  166. }
  167. if (!implicitOuts.empty()) {
  168. build += " |";
  169. for (std::string const& implicitOut : implicitOuts) {
  170. build += " " + EncodePath(implicitOut);
  171. }
  172. }
  173. build += ":";
  174. // Write the rule.
  175. build += " " + rule;
  176. // Write the variables bound to this build statement.
  177. std::ostringstream variable_assignments;
  178. for (auto const& variable : variables) {
  179. cmGlobalNinjaGenerator::WriteVariable(variable_assignments, variable.first,
  180. variable.second, "", 1);
  181. }
  182. // check if a response file rule should be used
  183. std::string buildstr = build;
  184. std::string assignments = variable_assignments.str();
  185. bool useResponseFile = false;
  186. if (cmdLineLimit < 0 ||
  187. (cmdLineLimit > 0 &&
  188. (arguments.size() + buildstr.size() + assignments.size() + 1000) >
  189. static_cast<size_t>(cmdLineLimit))) {
  190. variable_assignments.str(std::string());
  191. cmGlobalNinjaGenerator::WriteVariable(variable_assignments, "RSP_FILE",
  192. rspfile, "", 1);
  193. assignments += variable_assignments.str();
  194. useResponseFile = true;
  195. }
  196. if (usedResponseFile) {
  197. *usedResponseFile = useResponseFile;
  198. }
  199. os << buildstr << arguments << assignments;
  200. }
  201. void cmGlobalNinjaGenerator::WritePhonyBuild(
  202. std::ostream& os, const std::string& comment, const cmNinjaDeps& outputs,
  203. const cmNinjaDeps& explicitDeps, const cmNinjaDeps& implicitDeps,
  204. const cmNinjaDeps& orderOnlyDeps, const cmNinjaVars& variables)
  205. {
  206. this->WriteBuild(os, comment, "phony", outputs,
  207. /*implicitOuts=*/cmNinjaDeps(), explicitDeps, implicitDeps,
  208. orderOnlyDeps, variables);
  209. }
  210. void cmGlobalNinjaGenerator::AddCustomCommandRule()
  211. {
  212. this->AddRule("CUSTOM_COMMAND", "$COMMAND", "$DESC",
  213. "Rule for running custom commands.",
  214. /*depfile*/ "",
  215. /*deptype*/ "",
  216. /*rspfile*/ "",
  217. /*rspcontent*/ "",
  218. /*restat*/ "", // bound on each build statement as needed
  219. /*generator*/ false);
  220. }
  221. void cmGlobalNinjaGenerator::WriteCustomCommandBuild(
  222. const std::string& command, const std::string& description,
  223. const std::string& comment, const std::string& depfile, bool uses_terminal,
  224. bool restat, const cmNinjaDeps& outputs, const cmNinjaDeps& deps,
  225. const cmNinjaDeps& orderOnly)
  226. {
  227. std::string cmd = command; // NOLINT(*)
  228. #ifdef _WIN32
  229. if (cmd.empty())
  230. // TODO Shouldn't an empty command be handled by ninja?
  231. cmd = "cmd.exe /c";
  232. #endif
  233. this->AddCustomCommandRule();
  234. cmNinjaVars vars;
  235. vars["COMMAND"] = cmd;
  236. vars["DESC"] = EncodeLiteral(description);
  237. if (restat) {
  238. vars["restat"] = "1";
  239. }
  240. if (uses_terminal && SupportsConsolePool()) {
  241. vars["pool"] = "console";
  242. }
  243. if (!depfile.empty()) {
  244. vars["depfile"] = depfile;
  245. }
  246. this->WriteBuild(*this->BuildFileStream, comment, "CUSTOM_COMMAND", outputs,
  247. /*implicitOuts=*/cmNinjaDeps(), deps, cmNinjaDeps(),
  248. orderOnly, vars);
  249. if (this->ComputingUnknownDependencies) {
  250. // we need to track every dependency that comes in, since we are trying
  251. // to find dependencies that are side effects of build commands
  252. for (std::string const& dep : deps) {
  253. this->CombinedCustomCommandExplicitDependencies.insert(dep);
  254. }
  255. }
  256. }
  257. void cmGlobalNinjaGenerator::AddMacOSXContentRule()
  258. {
  259. this->AddRule("COPY_OSX_CONTENT", CMakeCmd() + " -E copy $in $out",
  260. "Copying OS X Content $out",
  261. "Rule for copying OS X bundle content file.",
  262. /*depfile*/ "",
  263. /*deptype*/ "",
  264. /*rspfile*/ "",
  265. /*rspcontent*/ "",
  266. /*restat*/ "",
  267. /*generator*/ false);
  268. }
  269. void cmGlobalNinjaGenerator::WriteMacOSXContentBuild(const std::string& input,
  270. const std::string& output)
  271. {
  272. this->AddMacOSXContentRule();
  273. cmNinjaDeps outputs;
  274. outputs.push_back(output);
  275. cmNinjaDeps deps;
  276. deps.push_back(input);
  277. cmNinjaVars vars;
  278. this->WriteBuild(*this->BuildFileStream, "", "COPY_OSX_CONTENT", outputs,
  279. /*implicitOuts=*/cmNinjaDeps(), deps, cmNinjaDeps(),
  280. cmNinjaDeps(), cmNinjaVars());
  281. }
  282. void cmGlobalNinjaGenerator::WriteRule(
  283. std::ostream& os, const std::string& name, const std::string& command,
  284. const std::string& description, const std::string& comment,
  285. const std::string& depfile, const std::string& deptype,
  286. const std::string& rspfile, const std::string& rspcontent,
  287. const std::string& restat, bool generator)
  288. {
  289. // -- Parameter checks
  290. // Make sure the rule has a name.
  291. if (name.empty()) {
  292. cmSystemTools::Error("No name given for WriteRule! called with comment: " +
  293. comment);
  294. return;
  295. }
  296. // Make sure a command is given.
  297. if (command.empty()) {
  298. cmSystemTools::Error(
  299. "No command given for WriteRule! called with comment: " + comment);
  300. return;
  301. }
  302. // Make sure response file content is given
  303. if (!rspfile.empty() && rspcontent.empty()) {
  304. cmSystemTools::Error("rspfile but no rspfile_content given for WriteRule! "
  305. "called with comment: " +
  306. comment);
  307. return;
  308. }
  309. // -- Write rule
  310. // Write rule intro
  311. cmGlobalNinjaGenerator::WriteComment(os, comment);
  312. os << "rule " << name << '\n';
  313. // Write rule key/value pairs
  314. auto writeKV = [&os](const char* key, std::string const& value) {
  315. if (!value.empty()) {
  316. cmGlobalNinjaGenerator::Indent(os, 1);
  317. os << key << " = " << value << '\n';
  318. }
  319. };
  320. writeKV("depfile", depfile);
  321. writeKV("deps", deptype);
  322. writeKV("command", command);
  323. writeKV("description", description);
  324. if (!rspfile.empty()) {
  325. writeKV("rspfile", rspfile);
  326. writeKV("rspfile_content", rspcontent);
  327. }
  328. writeKV("restat", restat);
  329. if (generator) {
  330. writeKV("generator", "1");
  331. }
  332. // Finish rule
  333. os << '\n';
  334. }
  335. void cmGlobalNinjaGenerator::WriteVariable(std::ostream& os,
  336. const std::string& name,
  337. const std::string& value,
  338. const std::string& comment,
  339. int indent)
  340. {
  341. // Make sure we have a name.
  342. if (name.empty()) {
  343. cmSystemTools::Error("No name given for WriteVariable! called "
  344. "with comment: " +
  345. comment);
  346. return;
  347. }
  348. // Do not add a variable if the value is empty.
  349. std::string val = cmSystemTools::TrimWhitespace(value);
  350. if (val.empty()) {
  351. return;
  352. }
  353. cmGlobalNinjaGenerator::WriteComment(os, comment);
  354. cmGlobalNinjaGenerator::Indent(os, indent);
  355. os << name << " = " << val << "\n";
  356. }
  357. void cmGlobalNinjaGenerator::WriteInclude(std::ostream& os,
  358. const std::string& filename,
  359. const std::string& comment)
  360. {
  361. cmGlobalNinjaGenerator::WriteComment(os, comment);
  362. os << "include " << filename << "\n";
  363. }
  364. void cmGlobalNinjaGenerator::WriteDefault(std::ostream& os,
  365. const cmNinjaDeps& targets,
  366. const std::string& comment)
  367. {
  368. cmGlobalNinjaGenerator::WriteComment(os, comment);
  369. os << "default";
  370. for (std::string const& target : targets) {
  371. os << " " << target;
  372. }
  373. os << "\n";
  374. }
  375. cmGlobalNinjaGenerator::cmGlobalNinjaGenerator(cmake* cm)
  376. : cmGlobalCommonGenerator(cm)
  377. , UsingGCCOnWindows(false)
  378. , ComputingUnknownDependencies(false)
  379. , PolicyCMP0058(cmPolicies::WARN)
  380. , NinjaSupportsConsolePool(false)
  381. , NinjaSupportsImplicitOuts(false)
  382. , NinjaSupportsManifestRestat(false)
  383. , NinjaSupportsMultilineDepfile(false)
  384. , NinjaSupportsDyndeps(0)
  385. {
  386. #ifdef _WIN32
  387. cm->GetState()->SetWindowsShell(true);
  388. #endif
  389. // // Ninja is not ported to non-Unix OS yet.
  390. // this->ForceUnixPaths = true;
  391. this->FindMakeProgramFile = "CMakeNinjaFindMake.cmake";
  392. }
  393. // Virtual public methods.
  394. cmLocalGenerator* cmGlobalNinjaGenerator::CreateLocalGenerator(cmMakefile* mf)
  395. {
  396. return new cmLocalNinjaGenerator(this, mf);
  397. }
  398. codecvt::Encoding cmGlobalNinjaGenerator::GetMakefileEncoding() const
  399. {
  400. #ifdef _WIN32
  401. // Ninja on Windows does not support non-ANSI characters.
  402. // https://github.com/ninja-build/ninja/issues/1195
  403. return codecvt::ANSI;
  404. #else
  405. // No encoding conversion needed on other platforms.
  406. return codecvt::None;
  407. #endif
  408. }
  409. void cmGlobalNinjaGenerator::GetDocumentation(cmDocumentationEntry& entry)
  410. {
  411. entry.Name = cmGlobalNinjaGenerator::GetActualName();
  412. entry.Brief = "Generates build.ninja files.";
  413. }
  414. // Implemented in all cmGlobaleGenerator sub-classes.
  415. // Used in:
  416. // Source/cmLocalGenerator.cxx
  417. // Source/cmake.cxx
  418. void cmGlobalNinjaGenerator::Generate()
  419. {
  420. // Check minimum Ninja version.
  421. if (cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  422. this->NinjaVersion.c_str(),
  423. RequiredNinjaVersion().c_str())) {
  424. std::ostringstream msg;
  425. msg << "The detected version of Ninja (" << this->NinjaVersion;
  426. msg << ") is less than the version of Ninja required by CMake (";
  427. msg << cmGlobalNinjaGenerator::RequiredNinjaVersion() << ").";
  428. this->GetCMakeInstance()->IssueMessage(MessageType::FATAL_ERROR,
  429. msg.str());
  430. return;
  431. }
  432. if (!this->OpenBuildFileStream()) {
  433. return;
  434. }
  435. if (!this->OpenRulesFileStream()) {
  436. return;
  437. }
  438. this->TargetDependsClosures.clear();
  439. this->InitOutputPathPrefix();
  440. this->TargetAll = this->NinjaOutputPath("all");
  441. this->CMakeCacheFile = this->NinjaOutputPath("CMakeCache.txt");
  442. this->PolicyCMP0058 =
  443. this->LocalGenerators[0]->GetMakefile()->GetPolicyStatus(
  444. cmPolicies::CMP0058);
  445. this->ComputingUnknownDependencies =
  446. (this->PolicyCMP0058 == cmPolicies::OLD ||
  447. this->PolicyCMP0058 == cmPolicies::WARN);
  448. this->cmGlobalGenerator::Generate();
  449. this->WriteAssumedSourceDependencies();
  450. this->WriteTargetAliases(*this->BuildFileStream);
  451. this->WriteFolderTargets(*this->BuildFileStream);
  452. this->WriteUnknownExplicitDependencies(*this->BuildFileStream);
  453. this->WriteBuiltinTargets(*this->BuildFileStream);
  454. if (cmSystemTools::GetErrorOccuredFlag()) {
  455. this->RulesFileStream->setstate(std::ios::failbit);
  456. this->BuildFileStream->setstate(std::ios::failbit);
  457. }
  458. this->CloseCompileCommandsStream();
  459. this->CloseRulesFileStream();
  460. this->CloseBuildFileStream();
  461. }
  462. bool cmGlobalNinjaGenerator::FindMakeProgram(cmMakefile* mf)
  463. {
  464. if (!this->cmGlobalGenerator::FindMakeProgram(mf)) {
  465. return false;
  466. }
  467. if (const char* ninjaCommand = mf->GetDefinition("CMAKE_MAKE_PROGRAM")) {
  468. this->NinjaCommand = ninjaCommand;
  469. std::vector<std::string> command;
  470. command.push_back(this->NinjaCommand);
  471. command.emplace_back("--version");
  472. std::string version;
  473. std::string error;
  474. if (!cmSystemTools::RunSingleCommand(command, &version, &error, nullptr,
  475. nullptr,
  476. cmSystemTools::OUTPUT_NONE)) {
  477. mf->IssueMessage(MessageType::FATAL_ERROR,
  478. "Running\n '" + cmJoin(command, "' '") +
  479. "'\n"
  480. "failed with:\n " +
  481. error);
  482. cmSystemTools::SetFatalErrorOccured();
  483. return false;
  484. }
  485. this->NinjaVersion = cmSystemTools::TrimWhitespace(version);
  486. this->CheckNinjaFeatures();
  487. }
  488. return true;
  489. }
  490. void cmGlobalNinjaGenerator::CheckNinjaFeatures()
  491. {
  492. this->NinjaSupportsConsolePool = !cmSystemTools::VersionCompare(
  493. cmSystemTools::OP_LESS, this->NinjaVersion.c_str(),
  494. RequiredNinjaVersionForConsolePool().c_str());
  495. this->NinjaSupportsImplicitOuts = !cmSystemTools::VersionCompare(
  496. cmSystemTools::OP_LESS, this->NinjaVersion.c_str(),
  497. cmGlobalNinjaGenerator::RequiredNinjaVersionForImplicitOuts().c_str());
  498. this->NinjaSupportsManifestRestat = !cmSystemTools::VersionCompare(
  499. cmSystemTools::OP_LESS, this->NinjaVersion.c_str(),
  500. RequiredNinjaVersionForManifestRestat().c_str());
  501. this->NinjaSupportsMultilineDepfile = !cmSystemTools::VersionCompare(
  502. cmSystemTools::OP_LESS, this->NinjaVersion.c_str(),
  503. RequiredNinjaVersionForMultilineDepfile().c_str());
  504. {
  505. // Our ninja branch adds ".dyndep-#" to its version number,
  506. // where '#' is a feature-specific version number. Extract it.
  507. static std::string const k_DYNDEP_ = ".dyndep-";
  508. std::string::size_type pos = this->NinjaVersion.find(k_DYNDEP_);
  509. if (pos != std::string::npos) {
  510. const char* fv = this->NinjaVersion.c_str() + pos + k_DYNDEP_.size();
  511. cmSystemTools::StringToULong(fv, &this->NinjaSupportsDyndeps);
  512. }
  513. }
  514. }
  515. bool cmGlobalNinjaGenerator::CheckLanguages(
  516. std::vector<std::string> const& languages, cmMakefile* mf) const
  517. {
  518. if (std::find(languages.begin(), languages.end(), "Fortran") !=
  519. languages.end()) {
  520. return this->CheckFortran(mf);
  521. }
  522. return true;
  523. }
  524. bool cmGlobalNinjaGenerator::CheckFortran(cmMakefile* mf) const
  525. {
  526. if (this->NinjaSupportsDyndeps == 1) {
  527. return true;
  528. }
  529. std::ostringstream e;
  530. if (this->NinjaSupportsDyndeps == 0) {
  531. /* clang-format off */
  532. e <<
  533. "The Ninja generator does not support Fortran using Ninja version\n"
  534. " " + this->NinjaVersion + "\n"
  535. "due to lack of required features. "
  536. "Kitware has implemented the required features but as of this version "
  537. "of CMake they have not been integrated to upstream ninja. "
  538. "Pending integration, Kitware maintains a branch at:\n"
  539. " https://github.com/Kitware/ninja/tree/features-for-fortran#readme\n"
  540. "with the required features. "
  541. "One may build ninja from that branch to get support for Fortran."
  542. ;
  543. /* clang-format on */
  544. } else {
  545. /* clang-format off */
  546. e <<
  547. "The Ninja generator in this version of CMake does not support Fortran "
  548. "using Ninja version\n"
  549. " " + this->NinjaVersion + "\n"
  550. "because its 'dyndep' feature version is " <<
  551. this->NinjaSupportsDyndeps << ". "
  552. "This version of CMake is aware only of 'dyndep' feature version 1."
  553. ;
  554. /* clang-format on */
  555. }
  556. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  557. cmSystemTools::SetFatalErrorOccured();
  558. return false;
  559. }
  560. void cmGlobalNinjaGenerator::EnableLanguage(
  561. std::vector<std::string> const& langs, cmMakefile* mf, bool optional)
  562. {
  563. this->cmGlobalGenerator::EnableLanguage(langs, mf, optional);
  564. for (std::string const& l : langs) {
  565. if (l == "NONE") {
  566. continue;
  567. }
  568. this->ResolveLanguageCompiler(l, mf, optional);
  569. }
  570. #ifdef _WIN32
  571. if ((mf->GetSafeDefinition("CMAKE_C_SIMULATE_ID") != "MSVC") &&
  572. (mf->GetSafeDefinition("CMAKE_CXX_SIMULATE_ID") != "MSVC") &&
  573. (mf->IsOn("CMAKE_COMPILER_IS_MINGW") ||
  574. (mf->GetSafeDefinition("CMAKE_C_COMPILER_ID") == "GNU") ||
  575. (mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID") == "GNU") ||
  576. (mf->GetSafeDefinition("CMAKE_C_COMPILER_ID") == "Clang") ||
  577. (mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID") == "Clang"))) {
  578. this->UsingGCCOnWindows = true;
  579. }
  580. #endif
  581. }
  582. // Implemented by:
  583. // cmGlobalUnixMakefileGenerator3
  584. // cmGlobalGhsMultiGenerator
  585. // cmGlobalVisualStudio10Generator
  586. // cmGlobalVisualStudio7Generator
  587. // cmGlobalXCodeGenerator
  588. // Called by:
  589. // cmGlobalGenerator::Build()
  590. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  591. cmGlobalNinjaGenerator::GenerateBuildCommand(
  592. const std::string& makeProgram, const std::string& /*projectName*/,
  593. const std::string& /*projectDir*/,
  594. std::vector<std::string> const& targetNames, const std::string& /*config*/,
  595. bool /*fast*/, int jobs, bool verbose,
  596. std::vector<std::string> const& makeOptions)
  597. {
  598. GeneratedMakeCommand makeCommand;
  599. makeCommand.Add(this->SelectMakeProgram(makeProgram));
  600. if (verbose) {
  601. makeCommand.Add("-v");
  602. }
  603. if ((jobs != cmake::NO_BUILD_PARALLEL_LEVEL) &&
  604. (jobs != cmake::DEFAULT_BUILD_PARALLEL_LEVEL)) {
  605. makeCommand.Add("-j", std::to_string(jobs));
  606. }
  607. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  608. for (const auto& tname : targetNames) {
  609. if (!tname.empty()) {
  610. makeCommand.Add(tname);
  611. }
  612. }
  613. return { std::move(makeCommand) };
  614. }
  615. // Non-virtual public methods.
  616. void cmGlobalNinjaGenerator::AddRule(
  617. const std::string& name, const std::string& command,
  618. const std::string& description, const std::string& comment,
  619. const std::string& depfile, const std::string& deptype,
  620. const std::string& rspfile, const std::string& rspcontent,
  621. const std::string& restat, bool generator)
  622. {
  623. // Do not add the same rule twice.
  624. if (!this->Rules.insert(name).second) {
  625. return;
  626. }
  627. // Store command length
  628. this->RuleCmdLength[name] = static_cast<int>(command.size());
  629. // Write rule
  630. cmGlobalNinjaGenerator::WriteRule(*this->RulesFileStream, name, command,
  631. description, comment, depfile, deptype,
  632. rspfile, rspcontent, restat, generator);
  633. }
  634. bool cmGlobalNinjaGenerator::HasRule(const std::string& name)
  635. {
  636. return (this->Rules.find(name) != this->Rules.end());
  637. }
  638. // Private virtual overrides
  639. std::string cmGlobalNinjaGenerator::GetEditCacheCommand() const
  640. {
  641. // Ninja by design does not run interactive tools in the terminal,
  642. // so our only choice is cmake-gui.
  643. return cmSystemTools::GetCMakeGUICommand();
  644. }
  645. void cmGlobalNinjaGenerator::ComputeTargetObjectDirectory(
  646. cmGeneratorTarget* gt) const
  647. {
  648. // Compute full path to object file directory for this target.
  649. std::string dir;
  650. dir += gt->LocalGenerator->GetCurrentBinaryDirectory();
  651. dir += "/";
  652. dir += gt->LocalGenerator->GetTargetDirectory(gt);
  653. dir += "/";
  654. gt->ObjectDirectory = dir;
  655. }
  656. // Private methods
  657. bool cmGlobalNinjaGenerator::OpenBuildFileStream()
  658. {
  659. // Compute Ninja's build file path.
  660. std::string buildFilePath =
  661. this->GetCMakeInstance()->GetHomeOutputDirectory();
  662. buildFilePath += "/";
  663. buildFilePath += cmGlobalNinjaGenerator::NINJA_BUILD_FILE;
  664. // Get a stream where to generate things.
  665. if (!this->BuildFileStream) {
  666. this->BuildFileStream = cm::make_unique<cmGeneratedFileStream>(
  667. buildFilePath, false, this->GetMakefileEncoding());
  668. if (!(*this->BuildFileStream)) {
  669. // An error message is generated by the constructor if it cannot
  670. // open the file.
  671. return false;
  672. }
  673. }
  674. // Write the do not edit header.
  675. this->WriteDisclaimer(*this->BuildFileStream);
  676. // Write a comment about this file.
  677. *this->BuildFileStream
  678. << "# This file contains all the build statements describing the\n"
  679. << "# compilation DAG.\n\n";
  680. return true;
  681. }
  682. void cmGlobalNinjaGenerator::CloseBuildFileStream()
  683. {
  684. if (this->BuildFileStream) {
  685. this->BuildFileStream.reset();
  686. } else {
  687. cmSystemTools::Error("Build file stream was not open.");
  688. }
  689. }
  690. bool cmGlobalNinjaGenerator::OpenRulesFileStream()
  691. {
  692. // Compute Ninja's build file path.
  693. std::string rulesFilePath =
  694. this->GetCMakeInstance()->GetHomeOutputDirectory();
  695. rulesFilePath += "/";
  696. rulesFilePath += cmGlobalNinjaGenerator::NINJA_RULES_FILE;
  697. // Get a stream where to generate things.
  698. if (!this->RulesFileStream) {
  699. this->RulesFileStream = cm::make_unique<cmGeneratedFileStream>(
  700. rulesFilePath, false, this->GetMakefileEncoding());
  701. if (!(*this->RulesFileStream)) {
  702. // An error message is generated by the constructor if it cannot
  703. // open the file.
  704. return false;
  705. }
  706. }
  707. // Write the do not edit header.
  708. this->WriteDisclaimer(*this->RulesFileStream);
  709. // Write comment about this file.
  710. /* clang-format off */
  711. *this->RulesFileStream
  712. << "# This file contains all the rules used to get the outputs files\n"
  713. << "# built from the input files.\n"
  714. << "# It is included in the main '" << NINJA_BUILD_FILE << "'.\n\n"
  715. ;
  716. /* clang-format on */
  717. return true;
  718. }
  719. void cmGlobalNinjaGenerator::CloseRulesFileStream()
  720. {
  721. if (this->RulesFileStream) {
  722. this->RulesFileStream.reset();
  723. } else {
  724. cmSystemTools::Error("Rules file stream was not open.");
  725. }
  726. }
  727. static void EnsureTrailingSlash(std::string& path)
  728. {
  729. if (path.empty()) {
  730. return;
  731. }
  732. std::string::value_type last = path.back();
  733. #ifdef _WIN32
  734. if (last != '\\') {
  735. path += '\\';
  736. }
  737. #else
  738. if (last != '/') {
  739. path += '/';
  740. }
  741. #endif
  742. }
  743. std::string const& cmGlobalNinjaGenerator::ConvertToNinjaPath(
  744. const std::string& path) const
  745. {
  746. auto const f = ConvertToNinjaPathCache.find(path);
  747. if (f != ConvertToNinjaPathCache.end()) {
  748. return f->second;
  749. }
  750. cmLocalNinjaGenerator* ng =
  751. static_cast<cmLocalNinjaGenerator*>(this->LocalGenerators[0]);
  752. std::string const& bin_dir = ng->GetState()->GetBinaryDirectory();
  753. std::string convPath = ng->MaybeConvertToRelativePath(bin_dir, path);
  754. convPath = this->NinjaOutputPath(convPath);
  755. #ifdef _WIN32
  756. std::replace(convPath.begin(), convPath.end(), '/', '\\');
  757. #endif
  758. return ConvertToNinjaPathCache.emplace(path, std::move(convPath))
  759. .first->second;
  760. }
  761. void cmGlobalNinjaGenerator::AddAdditionalCleanFile(std::string fileName)
  762. {
  763. this->AdditionalCleanFiles.emplace(std::move(fileName));
  764. }
  765. void cmGlobalNinjaGenerator::AddCXXCompileCommand(
  766. const std::string& commandLine, const std::string& sourceFile)
  767. {
  768. // Compute Ninja's build file path.
  769. std::string buildFileDir =
  770. this->GetCMakeInstance()->GetHomeOutputDirectory();
  771. if (!this->CompileCommandsStream) {
  772. std::string buildFilePath = buildFileDir + "/compile_commands.json";
  773. if (this->ComputingUnknownDependencies) {
  774. this->CombinedBuildOutputs.insert(
  775. this->NinjaOutputPath("compile_commands.json"));
  776. }
  777. // Get a stream where to generate things.
  778. this->CompileCommandsStream =
  779. cm::make_unique<cmGeneratedFileStream>(buildFilePath);
  780. *this->CompileCommandsStream << "[";
  781. } else {
  782. *this->CompileCommandsStream << "," << std::endl;
  783. }
  784. std::string sourceFileName = sourceFile;
  785. if (!cmSystemTools::FileIsFullPath(sourceFileName)) {
  786. sourceFileName = cmSystemTools::CollapseFullPath(
  787. sourceFileName, this->GetCMakeInstance()->GetHomeOutputDirectory());
  788. }
  789. /* clang-format off */
  790. *this->CompileCommandsStream << "\n{\n"
  791. << R"( "directory": ")"
  792. << cmGlobalGenerator::EscapeJSON(buildFileDir) << "\",\n"
  793. << R"( "command": ")"
  794. << cmGlobalGenerator::EscapeJSON(commandLine) << "\",\n"
  795. << R"( "file": ")"
  796. << cmGlobalGenerator::EscapeJSON(sourceFileName) << "\"\n"
  797. << "}";
  798. /* clang-format on */
  799. }
  800. void cmGlobalNinjaGenerator::CloseCompileCommandsStream()
  801. {
  802. if (this->CompileCommandsStream) {
  803. *this->CompileCommandsStream << "\n]";
  804. this->CompileCommandsStream.reset();
  805. }
  806. }
  807. void cmGlobalNinjaGenerator::WriteDisclaimer(std::ostream& os)
  808. {
  809. os << "# CMAKE generated file: DO NOT EDIT!\n"
  810. << "# Generated by \"" << this->GetName() << "\""
  811. << " Generator, CMake Version " << cmVersion::GetMajorVersion() << "."
  812. << cmVersion::GetMinorVersion() << "\n\n";
  813. }
  814. void cmGlobalNinjaGenerator::AddDependencyToAll(cmGeneratorTarget* target)
  815. {
  816. this->AppendTargetOutputs(target, this->AllDependencies);
  817. }
  818. void cmGlobalNinjaGenerator::AddDependencyToAll(const std::string& input)
  819. {
  820. this->AllDependencies.push_back(input);
  821. }
  822. void cmGlobalNinjaGenerator::WriteAssumedSourceDependencies()
  823. {
  824. for (auto const& asd : this->AssumedSourceDependencies) {
  825. cmNinjaDeps orderOnlyDeps;
  826. std::copy(asd.second.begin(), asd.second.end(),
  827. std::back_inserter(orderOnlyDeps));
  828. WriteCustomCommandBuild(/*command=*/"", /*description=*/"",
  829. "Assume dependencies for generated source file.",
  830. /*depfile*/ "", /*uses_terminal*/ false,
  831. /*restat*/ true, cmNinjaDeps(1, asd.first),
  832. cmNinjaDeps(), orderOnlyDeps);
  833. }
  834. }
  835. std::string OrderDependsTargetForTarget(cmGeneratorTarget const* target)
  836. {
  837. return "cmake_object_order_depends_target_" + target->GetName();
  838. }
  839. void cmGlobalNinjaGenerator::AppendTargetOutputs(
  840. cmGeneratorTarget const* target, cmNinjaDeps& outputs,
  841. cmNinjaTargetDepends depends)
  842. {
  843. std::string configName =
  844. target->Target->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE");
  845. // for frameworks, we want the real name, not smple name
  846. // frameworks always appear versioned, and the build.ninja
  847. // will always attempt to manage symbolic links instead
  848. // of letting cmOSXBundleGenerator do it.
  849. bool realname = target->IsFrameworkOnApple();
  850. switch (target->GetType()) {
  851. case cmStateEnums::SHARED_LIBRARY:
  852. case cmStateEnums::STATIC_LIBRARY:
  853. case cmStateEnums::MODULE_LIBRARY: {
  854. if (depends == DependOnTargetOrdering) {
  855. outputs.push_back(OrderDependsTargetForTarget(target));
  856. break;
  857. }
  858. }
  859. // FALLTHROUGH
  860. case cmStateEnums::EXECUTABLE: {
  861. outputs.push_back(this->ConvertToNinjaPath(target->GetFullPath(
  862. configName, cmStateEnums::RuntimeBinaryArtifact, realname)));
  863. break;
  864. }
  865. case cmStateEnums::OBJECT_LIBRARY: {
  866. if (depends == DependOnTargetOrdering) {
  867. outputs.push_back(OrderDependsTargetForTarget(target));
  868. break;
  869. }
  870. }
  871. // FALLTHROUGH
  872. case cmStateEnums::GLOBAL_TARGET:
  873. case cmStateEnums::UTILITY: {
  874. std::string path =
  875. target->GetLocalGenerator()->GetCurrentBinaryDirectory() +
  876. std::string("/") + target->GetName();
  877. outputs.push_back(this->ConvertToNinjaPath(path));
  878. break;
  879. }
  880. default:
  881. return;
  882. }
  883. }
  884. void cmGlobalNinjaGenerator::AppendTargetDepends(
  885. cmGeneratorTarget const* target, cmNinjaDeps& outputs,
  886. cmNinjaTargetDepends depends)
  887. {
  888. if (target->GetType() == cmStateEnums::GLOBAL_TARGET) {
  889. // These depend only on other CMake-provided targets, e.g. "all".
  890. for (BT<std::string> const& util : target->GetUtilities()) {
  891. std::string d =
  892. target->GetLocalGenerator()->GetCurrentBinaryDirectory() + "/" +
  893. util.Value;
  894. outputs.push_back(this->ConvertToNinjaPath(d));
  895. }
  896. } else {
  897. cmNinjaDeps outs;
  898. for (cmTargetDepend const& targetDep :
  899. this->GetTargetDirectDepends(target)) {
  900. if (targetDep->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  901. continue;
  902. }
  903. this->AppendTargetOutputs(targetDep, outs, depends);
  904. }
  905. std::sort(outs.begin(), outs.end());
  906. outputs.insert(outputs.end(), outs.begin(), outs.end());
  907. }
  908. }
  909. void cmGlobalNinjaGenerator::AppendTargetDependsClosure(
  910. cmGeneratorTarget const* target, cmNinjaDeps& outputs)
  911. {
  912. cmNinjaOuts outs;
  913. this->AppendTargetDependsClosure(target, outs, true);
  914. outputs.insert(outputs.end(), outs.begin(), outs.end());
  915. }
  916. void cmGlobalNinjaGenerator::AppendTargetDependsClosure(
  917. cmGeneratorTarget const* target, cmNinjaOuts& outputs, bool omit_self)
  918. {
  919. // try to locate the target in the cache
  920. auto find = this->TargetDependsClosures.lower_bound(target);
  921. if (find == this->TargetDependsClosures.end() || find->first != target) {
  922. // We now calculate the closure outputs by inspecting the dependent
  923. // targets recursively.
  924. // For that we have to distinguish between a local result set that is only
  925. // relevant for filling the cache entries properly isolated and a global
  926. // result set that is relevant for the result of the top level call to
  927. // AppendTargetDependsClosure.
  928. cmNinjaOuts this_outs; // this will be the new cache entry
  929. for (auto const& dep_target : this->GetTargetDirectDepends(target)) {
  930. if (dep_target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  931. continue;
  932. }
  933. // Collect the dependent targets for _this_ target
  934. this->AppendTargetDependsClosure(dep_target, this_outs, false);
  935. }
  936. find = this->TargetDependsClosures.emplace_hint(find, target,
  937. std::move(this_outs));
  938. }
  939. // now fill the outputs of the final result from the newly generated cache
  940. // entry
  941. outputs.insert(find->second.begin(), find->second.end());
  942. // finally generate the outputs of the target itself, if applicable
  943. cmNinjaDeps outs;
  944. if (!omit_self) {
  945. this->AppendTargetOutputs(target, outs);
  946. }
  947. outputs.insert(outs.begin(), outs.end());
  948. }
  949. void cmGlobalNinjaGenerator::AddTargetAlias(const std::string& alias,
  950. cmGeneratorTarget* target)
  951. {
  952. std::string buildAlias = this->NinjaOutputPath(alias);
  953. cmNinjaDeps outputs;
  954. this->AppendTargetOutputs(target, outputs);
  955. // Mark the target's outputs as ambiguous to ensure that no other target uses
  956. // the output as an alias.
  957. for (std::string const& output : outputs) {
  958. TargetAliases[output] = nullptr;
  959. }
  960. // Insert the alias into the map. If the alias was already present in the
  961. // map and referred to another target, mark it as ambiguous.
  962. std::pair<TargetAliasMap::iterator, bool> newAlias =
  963. TargetAliases.insert(std::make_pair(buildAlias, target));
  964. if (newAlias.second && newAlias.first->second != target) {
  965. newAlias.first->second = nullptr;
  966. }
  967. }
  968. void cmGlobalNinjaGenerator::WriteTargetAliases(std::ostream& os)
  969. {
  970. cmGlobalNinjaGenerator::WriteDivider(os);
  971. os << "# Target aliases.\n\n";
  972. for (auto const& ta : TargetAliases) {
  973. // Don't write ambiguous aliases.
  974. if (!ta.second) {
  975. continue;
  976. }
  977. // Don't write alias if there is a already a custom command with
  978. // matching output
  979. if (this->HasCustomCommandOutput(ta.first)) {
  980. continue;
  981. }
  982. cmNinjaDeps deps;
  983. this->AppendTargetOutputs(ta.second, deps);
  984. this->WritePhonyBuild(os, "", cmNinjaDeps(1, ta.first), deps);
  985. }
  986. }
  987. void cmGlobalNinjaGenerator::WriteFolderTargets(std::ostream& os)
  988. {
  989. cmGlobalNinjaGenerator::WriteDivider(os);
  990. os << "# Folder targets.\n\n";
  991. std::map<std::string, cmNinjaDeps> targetsPerFolder;
  992. for (cmLocalGenerator const* lg : this->LocalGenerators) {
  993. const std::string currentBinaryFolder(
  994. lg->GetStateSnapshot().GetDirectory().GetCurrentBinary());
  995. // The directory-level rule should depend on the target-level rules
  996. // for all targets in the directory.
  997. targetsPerFolder[currentBinaryFolder] = cmNinjaDeps();
  998. for (auto gt : lg->GetGeneratorTargets()) {
  999. cmStateEnums::TargetType const type = gt->GetType();
  1000. if ((type == cmStateEnums::EXECUTABLE ||
  1001. type == cmStateEnums::STATIC_LIBRARY ||
  1002. type == cmStateEnums::SHARED_LIBRARY ||
  1003. type == cmStateEnums::MODULE_LIBRARY ||
  1004. type == cmStateEnums::OBJECT_LIBRARY ||
  1005. type == cmStateEnums::UTILITY) &&
  1006. !gt->GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  1007. targetsPerFolder[currentBinaryFolder].push_back(gt->GetName());
  1008. }
  1009. }
  1010. // The directory-level rule should depend on the directory-level
  1011. // rules of the subdirectories.
  1012. for (cmStateSnapshot const& state : lg->GetStateSnapshot().GetChildren()) {
  1013. std::string const currentBinaryDir =
  1014. state.GetDirectory().GetCurrentBinary();
  1015. targetsPerFolder[currentBinaryFolder].push_back(
  1016. this->ConvertToNinjaPath(currentBinaryDir + "/all"));
  1017. }
  1018. }
  1019. std::string const rootBinaryDir =
  1020. this->LocalGenerators[0]->GetBinaryDirectory();
  1021. for (auto const& it : targetsPerFolder) {
  1022. cmGlobalNinjaGenerator::WriteDivider(os);
  1023. std::string const& currentBinaryDir = it.first;
  1024. // Do not generate a rule for the root binary dir.
  1025. if (rootBinaryDir.length() >= currentBinaryDir.length()) {
  1026. continue;
  1027. }
  1028. std::string const comment = "Folder: " + currentBinaryDir;
  1029. cmNinjaDeps output(1);
  1030. output.push_back(this->ConvertToNinjaPath(currentBinaryDir + "/all"));
  1031. this->WritePhonyBuild(os, comment, output, it.second);
  1032. }
  1033. }
  1034. void cmGlobalNinjaGenerator::WriteUnknownExplicitDependencies(std::ostream& os)
  1035. {
  1036. if (!this->ComputingUnknownDependencies) {
  1037. return;
  1038. }
  1039. // We need to collect the set of known build outputs.
  1040. // Start with those generated by WriteBuild calls.
  1041. // No other method needs this so we can take ownership
  1042. // of the set locally and throw it out when we are done.
  1043. std::set<std::string> knownDependencies;
  1044. knownDependencies.swap(this->CombinedBuildOutputs);
  1045. // now write out the unknown explicit dependencies.
  1046. // union the configured files, evaluations files and the
  1047. // CombinedBuildOutputs,
  1048. // and then difference with CombinedExplicitDependencies to find the explicit
  1049. // dependencies that we have no rule for
  1050. cmGlobalNinjaGenerator::WriteDivider(os);
  1051. /* clang-format off */
  1052. os << "# Unknown Build Time Dependencies.\n"
  1053. << "# Tell Ninja that they may appear as side effects of build rules\n"
  1054. << "# otherwise ordered by order-only dependencies.\n\n";
  1055. /* clang-format on */
  1056. // get the list of files that cmake itself has generated as a
  1057. // product of configuration.
  1058. for (cmLocalGenerator* lg : this->LocalGenerators) {
  1059. // get the vector of files created by this makefile and convert them
  1060. // to ninja paths, which are all relative in respect to the build directory
  1061. for (std::string const& file : lg->GetMakefile()->GetOutputFiles()) {
  1062. knownDependencies.insert(this->ConvertToNinjaPath(file));
  1063. }
  1064. if (!this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  1065. // get list files which are implicit dependencies as well and will be
  1066. // phony for rebuild manifest
  1067. for (std::string const& j : lg->GetMakefile()->GetListFiles()) {
  1068. knownDependencies.insert(this->ConvertToNinjaPath(j));
  1069. }
  1070. }
  1071. for (cmGeneratorExpressionEvaluationFile* li :
  1072. lg->GetMakefile()->GetEvaluationFiles()) {
  1073. // get all the files created by generator expressions and convert them
  1074. // to ninja paths
  1075. for (std::string const& evaluationFile : li->GetFiles()) {
  1076. knownDependencies.insert(this->ConvertToNinjaPath(evaluationFile));
  1077. }
  1078. }
  1079. }
  1080. knownDependencies.insert(this->CMakeCacheFile);
  1081. for (auto const& ta : this->TargetAliases) {
  1082. knownDependencies.insert(this->ConvertToNinjaPath(ta.first));
  1083. }
  1084. // remove all source files we know will exist.
  1085. for (auto const& i : this->AssumedSourceDependencies) {
  1086. knownDependencies.insert(this->ConvertToNinjaPath(i.first));
  1087. }
  1088. // now we difference with CombinedCustomCommandExplicitDependencies to find
  1089. // the list of items we know nothing about.
  1090. // We have encoded all the paths in CombinedCustomCommandExplicitDependencies
  1091. // and knownDependencies so no matter if unix or windows paths they
  1092. // should all match now.
  1093. std::vector<std::string> unknownExplicitDepends;
  1094. this->CombinedCustomCommandExplicitDependencies.erase(this->TargetAll);
  1095. std::set_difference(this->CombinedCustomCommandExplicitDependencies.begin(),
  1096. this->CombinedCustomCommandExplicitDependencies.end(),
  1097. knownDependencies.begin(), knownDependencies.end(),
  1098. std::back_inserter(unknownExplicitDepends));
  1099. std::string const rootBuildDirectory =
  1100. this->GetCMakeInstance()->GetHomeOutputDirectory();
  1101. bool const inSourceBuild =
  1102. (rootBuildDirectory == this->GetCMakeInstance()->GetHomeDirectory());
  1103. std::vector<std::string> warnExplicitDepends;
  1104. for (std::string const& i : unknownExplicitDepends) {
  1105. // verify the file is in the build directory
  1106. std::string const absDepPath =
  1107. cmSystemTools::CollapseFullPath(i, rootBuildDirectory.c_str());
  1108. bool const inBuildDir =
  1109. cmSystemTools::IsSubDirectory(absDepPath, rootBuildDirectory);
  1110. if (inBuildDir) {
  1111. cmNinjaDeps deps(1, i);
  1112. this->WritePhonyBuild(os, "", deps, cmNinjaDeps());
  1113. if (this->PolicyCMP0058 == cmPolicies::WARN && !inSourceBuild &&
  1114. warnExplicitDepends.size() < 10) {
  1115. warnExplicitDepends.push_back(i);
  1116. }
  1117. }
  1118. }
  1119. if (!warnExplicitDepends.empty()) {
  1120. std::ostringstream w;
  1121. /* clang-format off */
  1122. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0058) << "\n"
  1123. "This project specifies custom command DEPENDS on files "
  1124. "in the build tree that are not specified as the OUTPUT or "
  1125. "BYPRODUCTS of any add_custom_command or add_custom_target:\n"
  1126. " " << cmJoin(warnExplicitDepends, "\n ") <<
  1127. "\n"
  1128. "For compatibility with versions of CMake that did not have "
  1129. "the BYPRODUCTS option, CMake is generating phony rules for "
  1130. "such files to convince 'ninja' to build."
  1131. "\n"
  1132. "Project authors should add the missing BYPRODUCTS or OUTPUT "
  1133. "options to the custom commands that produce these files."
  1134. ;
  1135. /* clang-format on */
  1136. this->GetCMakeInstance()->IssueMessage(MessageType::AUTHOR_WARNING,
  1137. w.str());
  1138. }
  1139. }
  1140. void cmGlobalNinjaGenerator::WriteBuiltinTargets(std::ostream& os)
  1141. {
  1142. // Write headers.
  1143. cmGlobalNinjaGenerator::WriteDivider(os);
  1144. os << "# Built-in targets\n\n";
  1145. this->WriteTargetAll(os);
  1146. this->WriteTargetRebuildManifest(os);
  1147. this->WriteTargetClean(os);
  1148. this->WriteTargetHelp(os);
  1149. }
  1150. void cmGlobalNinjaGenerator::WriteTargetAll(std::ostream& os)
  1151. {
  1152. cmNinjaDeps outputs;
  1153. outputs.push_back(this->TargetAll);
  1154. this->WritePhonyBuild(os, "The main all target.", outputs,
  1155. this->AllDependencies);
  1156. if (!this->HasOutputPathPrefix()) {
  1157. cmGlobalNinjaGenerator::WriteDefault(os, outputs,
  1158. "Make the all target the default.");
  1159. }
  1160. }
  1161. void cmGlobalNinjaGenerator::WriteTargetRebuildManifest(std::ostream& os)
  1162. {
  1163. if (this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  1164. return;
  1165. }
  1166. cmLocalGenerator* lg = this->LocalGenerators[0];
  1167. {
  1168. std::string cmd = CMakeCmd();
  1169. cmd += " -S";
  1170. cmd += lg->ConvertToOutputFormat(lg->GetSourceDirectory(),
  1171. cmOutputConverter::SHELL);
  1172. cmd += " -B";
  1173. cmd += lg->ConvertToOutputFormat(lg->GetBinaryDirectory(),
  1174. cmOutputConverter::SHELL);
  1175. WriteRule(*this->RulesFileStream, "RERUN_CMAKE", cmd,
  1176. "Re-running CMake...", "Rule for re-running cmake.",
  1177. /*depfile=*/"",
  1178. /*deptype=*/"",
  1179. /*rspfile=*/"",
  1180. /*rspcontent*/ "",
  1181. /*restat=*/"",
  1182. /*generator=*/true);
  1183. }
  1184. cmNinjaDeps implicitDeps;
  1185. cmNinjaDeps explicitDeps;
  1186. for (cmLocalGenerator* localGen : this->LocalGenerators) {
  1187. for (std::string const& fi : localGen->GetMakefile()->GetListFiles()) {
  1188. implicitDeps.push_back(this->ConvertToNinjaPath(fi));
  1189. }
  1190. }
  1191. implicitDeps.push_back(this->CMakeCacheFile);
  1192. cmNinjaVars variables;
  1193. // Use 'console' pool to get non buffered output of the CMake re-run call
  1194. // Available since Ninja 1.5
  1195. if (SupportsConsolePool()) {
  1196. variables["pool"] = "console";
  1197. }
  1198. cmake* cm = this->GetCMakeInstance();
  1199. if (this->SupportsManifestRestat() && cm->DoWriteGlobVerifyTarget()) {
  1200. {
  1201. std::string cmd = CMakeCmd();
  1202. cmd += " -P ";
  1203. cmd += lg->ConvertToOutputFormat(cm->GetGlobVerifyScript(),
  1204. cmOutputConverter::SHELL);
  1205. WriteRule(*this->RulesFileStream, "VERIFY_GLOBS", cmd,
  1206. "Re-checking globbed directories...",
  1207. "Rule for re-checking globbed directories.",
  1208. /*depfile=*/"",
  1209. /*deptype=*/"",
  1210. /*rspfile=*/"",
  1211. /*rspcontent*/ "",
  1212. /*restat=*/"",
  1213. /*generator=*/true);
  1214. }
  1215. std::string verifyForce = cm->GetGlobVerifyScript() + "_force";
  1216. cmNinjaDeps verifyForceDeps(1, this->NinjaOutputPath(verifyForce));
  1217. this->WritePhonyBuild(os, "Phony target to force glob verification run.",
  1218. verifyForceDeps, cmNinjaDeps());
  1219. variables["restat"] = "1";
  1220. std::string const verifyScriptFile =
  1221. this->NinjaOutputPath(cm->GetGlobVerifyScript());
  1222. std::string const verifyStampFile =
  1223. this->NinjaOutputPath(cm->GetGlobVerifyStamp());
  1224. this->WriteBuild(os,
  1225. "Re-run CMake to check if globbed directories changed.",
  1226. "VERIFY_GLOBS",
  1227. /*outputs=*/cmNinjaDeps(1, verifyStampFile),
  1228. /*implicitOuts=*/cmNinjaDeps(),
  1229. /*explicitDeps=*/cmNinjaDeps(),
  1230. /*implicitDeps=*/verifyForceDeps,
  1231. /*orderOnlyDeps=*/cmNinjaDeps(), variables);
  1232. variables.erase("restat");
  1233. implicitDeps.push_back(verifyScriptFile);
  1234. explicitDeps.push_back(verifyStampFile);
  1235. } else if (!this->SupportsManifestRestat() &&
  1236. cm->DoWriteGlobVerifyTarget()) {
  1237. std::ostringstream msg;
  1238. msg << "The detected version of Ninja:\n"
  1239. << " " << this->NinjaVersion << "\n"
  1240. << "is less than the version of Ninja required by CMake for adding "
  1241. "restat dependencies to the build.ninja manifest regeneration "
  1242. "target:\n"
  1243. << " "
  1244. << cmGlobalNinjaGenerator::RequiredNinjaVersionForManifestRestat()
  1245. << "\n";
  1246. msg << "Any pre-check scripts, such as those generated for file(GLOB "
  1247. "CONFIGURE_DEPENDS), will not be run by Ninja.";
  1248. this->GetCMakeInstance()->IssueMessage(MessageType::AUTHOR_WARNING,
  1249. msg.str());
  1250. }
  1251. std::sort(implicitDeps.begin(), implicitDeps.end());
  1252. implicitDeps.erase(std::unique(implicitDeps.begin(), implicitDeps.end()),
  1253. implicitDeps.end());
  1254. std::string const ninjaBuildFile = this->NinjaOutputPath(NINJA_BUILD_FILE);
  1255. this->WriteBuild(os, "Re-run CMake if any of its inputs changed.",
  1256. "RERUN_CMAKE",
  1257. /*outputs=*/cmNinjaDeps(1, ninjaBuildFile),
  1258. /*implicitOuts=*/cmNinjaDeps(), explicitDeps, implicitDeps,
  1259. /*orderOnlyDeps=*/cmNinjaDeps(), variables);
  1260. cmNinjaDeps missingInputs;
  1261. std::set_difference(std::make_move_iterator(implicitDeps.begin()),
  1262. std::make_move_iterator(implicitDeps.end()),
  1263. CustomCommandOutputs.begin(), CustomCommandOutputs.end(),
  1264. std::back_inserter(missingInputs));
  1265. this->WritePhonyBuild(os, "A missing CMake input file is not an error.",
  1266. missingInputs, cmNinjaDeps());
  1267. }
  1268. std::string cmGlobalNinjaGenerator::CMakeCmd() const
  1269. {
  1270. cmLocalGenerator* lgen = this->LocalGenerators.at(0);
  1271. return lgen->ConvertToOutputFormat(cmSystemTools::GetCMakeCommand(),
  1272. cmOutputConverter::SHELL);
  1273. }
  1274. std::string cmGlobalNinjaGenerator::NinjaCmd() const
  1275. {
  1276. cmLocalGenerator* lgen = this->LocalGenerators[0];
  1277. if (lgen != nullptr) {
  1278. return lgen->ConvertToOutputFormat(this->NinjaCommand,
  1279. cmOutputConverter::SHELL);
  1280. }
  1281. return "ninja";
  1282. }
  1283. bool cmGlobalNinjaGenerator::SupportsConsolePool() const
  1284. {
  1285. return this->NinjaSupportsConsolePool;
  1286. }
  1287. bool cmGlobalNinjaGenerator::SupportsImplicitOuts() const
  1288. {
  1289. return this->NinjaSupportsImplicitOuts;
  1290. }
  1291. bool cmGlobalNinjaGenerator::SupportsManifestRestat() const
  1292. {
  1293. return this->NinjaSupportsManifestRestat;
  1294. }
  1295. bool cmGlobalNinjaGenerator::SupportsMultilineDepfile() const
  1296. {
  1297. return this->NinjaSupportsMultilineDepfile;
  1298. }
  1299. bool cmGlobalNinjaGenerator::WriteTargetCleanAdditional(std::ostream& os)
  1300. {
  1301. cmLocalGenerator* lgr = this->LocalGenerators.at(0);
  1302. std::string cleanScriptRel = "CMakeFiles/clean_additional.cmake";
  1303. std::string cleanScriptAbs = lgr->GetBinaryDirectory();
  1304. cleanScriptAbs += '/';
  1305. cleanScriptAbs += cleanScriptRel;
  1306. // Check if there are additional files to clean
  1307. if (this->AdditionalCleanFiles.empty()) {
  1308. // Remove cmake clean script file if it exists
  1309. cmSystemTools::RemoveFile(cleanScriptAbs);
  1310. return false;
  1311. }
  1312. // Write cmake clean script file
  1313. {
  1314. cmGeneratedFileStream fout(cleanScriptAbs);
  1315. if (!fout) {
  1316. return false;
  1317. }
  1318. fout << "# Additional clean files\n\n";
  1319. fout << "file(REMOVE_RECURSE\n";
  1320. for (std::string const& acf : this->AdditionalCleanFiles) {
  1321. fout << " "
  1322. << cmOutputConverter::EscapeForCMake(ConvertToNinjaPath(acf))
  1323. << '\n';
  1324. }
  1325. fout << ")\n";
  1326. }
  1327. // Register clean script file
  1328. lgr->GetMakefile()->AddCMakeOutputFile(cleanScriptAbs);
  1329. // Write rule
  1330. {
  1331. std::string cmd = CMakeCmd();
  1332. cmd += " -P ";
  1333. cmd += lgr->ConvertToOutputFormat(this->NinjaOutputPath(cleanScriptRel),
  1334. cmOutputConverter::SHELL);
  1335. WriteRule(*this->RulesFileStream, "CLEAN_ADDITIONAL", cmd,
  1336. "Cleaning additional files...",
  1337. "Rule for cleaning additional files.",
  1338. /*depfile=*/"",
  1339. /*deptype=*/"",
  1340. /*rspfile=*/"",
  1341. /*rspcontent*/ "",
  1342. /*restat=*/"",
  1343. /*generator=*/false);
  1344. }
  1345. // Write build
  1346. {
  1347. cmNinjaDeps outputs;
  1348. outputs.emplace_back(
  1349. this->NinjaOutputPath(this->GetAdditionalCleanTargetName()));
  1350. WriteBuild(os, "Clean additional files.", "CLEAN_ADDITIONAL",
  1351. /*outputs=*/outputs,
  1352. /*implicitOuts=*/cmNinjaDeps(),
  1353. /*explicitDeps=*/cmNinjaDeps(),
  1354. /*implicitDeps=*/cmNinjaDeps(),
  1355. /*orderOnlyDeps=*/cmNinjaDeps(),
  1356. /*variables=*/cmNinjaVars());
  1357. }
  1358. // Return success
  1359. return true;
  1360. }
  1361. void cmGlobalNinjaGenerator::WriteTargetClean(std::ostream& os)
  1362. {
  1363. // -- Additional clean target
  1364. bool additionalFiles = WriteTargetCleanAdditional(os);
  1365. // -- Default clean target
  1366. // Write rule
  1367. WriteRule(*this->RulesFileStream, "CLEAN", NinjaCmd() + " -t clean",
  1368. "Cleaning all built files...",
  1369. "Rule for cleaning all built files.",
  1370. /*depfile=*/"",
  1371. /*deptype=*/"",
  1372. /*rspfile=*/"",
  1373. /*rspcontent*/ "",
  1374. /*restat=*/"",
  1375. /*generator=*/false);
  1376. // Write build
  1377. {
  1378. cmNinjaDeps explicitDeps;
  1379. if (additionalFiles) {
  1380. explicitDeps.emplace_back(
  1381. this->NinjaOutputPath(this->GetAdditionalCleanTargetName()));
  1382. }
  1383. cmNinjaDeps outputs;
  1384. outputs.emplace_back(this->NinjaOutputPath(this->GetCleanTargetName()));
  1385. WriteBuild(os, "Clean all the built files.", "CLEAN",
  1386. /*outputs=*/outputs,
  1387. /*implicitOuts=*/cmNinjaDeps(),
  1388. /*explicitDeps=*/explicitDeps,
  1389. /*implicitDeps=*/cmNinjaDeps(),
  1390. /*orderOnlyDeps=*/cmNinjaDeps(),
  1391. /*variables=*/cmNinjaVars());
  1392. }
  1393. }
  1394. void cmGlobalNinjaGenerator::WriteTargetHelp(std::ostream& os)
  1395. {
  1396. WriteRule(*this->RulesFileStream, "HELP", NinjaCmd() + " -t targets",
  1397. "All primary targets available:",
  1398. "Rule for printing all primary targets available.",
  1399. /*depfile=*/"",
  1400. /*deptype=*/"",
  1401. /*rspfile=*/"",
  1402. /*rspcontent*/ "",
  1403. /*restat=*/"",
  1404. /*generator=*/false);
  1405. WriteBuild(os, "Print all primary targets available.", "HELP",
  1406. /*outputs=*/cmNinjaDeps(1, this->NinjaOutputPath("help")),
  1407. /*implicitOuts=*/cmNinjaDeps(),
  1408. /*explicitDeps=*/cmNinjaDeps(),
  1409. /*implicitDeps=*/cmNinjaDeps(),
  1410. /*orderOnlyDeps=*/cmNinjaDeps(),
  1411. /*variables=*/cmNinjaVars());
  1412. }
  1413. void cmGlobalNinjaGenerator::InitOutputPathPrefix()
  1414. {
  1415. this->OutputPathPrefix =
  1416. this->LocalGenerators[0]->GetMakefile()->GetSafeDefinition(
  1417. "CMAKE_NINJA_OUTPUT_PATH_PREFIX");
  1418. EnsureTrailingSlash(this->OutputPathPrefix);
  1419. }
  1420. std::string cmGlobalNinjaGenerator::NinjaOutputPath(
  1421. std::string const& path) const
  1422. {
  1423. if (!this->HasOutputPathPrefix() || cmSystemTools::FileIsFullPath(path)) {
  1424. return path;
  1425. }
  1426. return this->OutputPathPrefix + path;
  1427. }
  1428. void cmGlobalNinjaGenerator::StripNinjaOutputPathPrefixAsSuffix(
  1429. std::string& path)
  1430. {
  1431. if (path.empty()) {
  1432. return;
  1433. }
  1434. EnsureTrailingSlash(path);
  1435. cmStripSuffixIfExists(path, this->OutputPathPrefix);
  1436. }
  1437. /*
  1438. We use the following approach to support Fortran. Each target already
  1439. has a <target>.dir/ directory used to hold intermediate files for CMake.
  1440. For each target, a FortranDependInfo.json file is generated by CMake with
  1441. information about include directories, module directories, and the locations
  1442. the per-target directories for target dependencies.
  1443. Compilation of source files within a target is split into the following steps:
  1444. 1. Preprocess all sources, scan preprocessed output for module dependencies.
  1445. This step is done with independent build statements for each source,
  1446. and can therefore be done in parallel.
  1447. rule Fortran_PREPROCESS
  1448. depfile = $DEP_FILE
  1449. command = gfortran -cpp $DEFINES $INCLUDES $FLAGS -E $in -o $out &&
  1450. cmake -E cmake_ninja_depends \
  1451. --tdi=FortranDependInfo.json --pp=$out --dep=$DEP_FILE \
  1452. --obj=$OBJ_FILE --ddi=$DYNDEP_INTERMEDIATE_FILE \
  1453. --lang=Fortran
  1454. build src.f90-pp.f90 | src.f90.o.ddi: Fortran_PREPROCESS src.f90
  1455. OBJ_FILE = src.f90.o
  1456. DEP_FILE = src.f90.o.d
  1457. DYNDEP_INTERMEDIATE_FILE = src.f90.o.ddi
  1458. The ``cmake -E cmake_ninja_depends`` tool reads the preprocessed output
  1459. and generates the ninja depfile for preprocessor dependencies. It also
  1460. generates a "ddi" file (in a format private to CMake) that lists the
  1461. object file that compilation will produce along with the module names
  1462. it provides and/or requires. The "ddi" file is an implicit output
  1463. because it should not appear in "$out" but is generated by the rule.
  1464. 2. Consolidate the per-source module dependencies saved in the "ddi"
  1465. files from all sources to produce a ninja "dyndep" file, ``Fortran.dd``.
  1466. rule Fortran_DYNDEP
  1467. command = cmake -E cmake_ninja_dyndep \
  1468. --tdi=FortranDependInfo.json --lang=Fortran --dd=$out $in
  1469. build Fortran.dd: Fortran_DYNDEP src1.f90.o.ddi src2.f90.o.ddi
  1470. The ``cmake -E cmake_ninja_dyndep`` tool reads the "ddi" files from all
  1471. sources in the target and the ``FortranModules.json`` files from targets
  1472. on which the target depends. It computes dependency edges on compilations
  1473. that require modules to those that provide the modules. This information
  1474. is placed in the ``Fortran.dd`` file for ninja to load later. It also
  1475. writes the expected location of modules provided by this target into
  1476. ``FortranModules.json`` for use by dependent targets.
  1477. 3. Compile all sources after loading dynamically discovered dependencies
  1478. of the compilation build statements from their ``dyndep`` bindings.
  1479. rule Fortran_COMPILE
  1480. command = gfortran $INCLUDES $FLAGS -c $in -o $out
  1481. build src1.f90.o: Fortran_COMPILE src1.f90-pp.f90 || Fortran.dd
  1482. dyndep = Fortran.dd
  1483. The "dyndep" binding tells ninja to load dynamically discovered
  1484. dependency information from ``Fortran.dd``. This adds information
  1485. such as:
  1486. build src1.f90.o | mod1.mod: dyndep
  1487. restat = 1
  1488. This tells ninja that ``mod1.mod`` is an implicit output of compiling
  1489. the object file ``src1.f90.o``. The ``restat`` binding tells it that
  1490. the timestamp of the output may not always change. Additionally:
  1491. build src2.f90.o: dyndep | mod1.mod
  1492. This tells ninja that ``mod1.mod`` is a dependency of compiling the
  1493. object file ``src2.f90.o``. This ensures that ``src1.f90.o`` and
  1494. ``mod1.mod`` will always be up to date before ``src2.f90.o`` is built
  1495. (because the latter consumes the module).
  1496. */
  1497. struct cmSourceInfo
  1498. {
  1499. // Set of provided and required modules.
  1500. std::set<std::string> Provides;
  1501. std::set<std::string> Requires;
  1502. // Set of files included in the translation unit.
  1503. std::set<std::string> Includes;
  1504. };
  1505. static std::unique_ptr<cmSourceInfo> cmcmd_cmake_ninja_depends_fortran(
  1506. std::string const& arg_tdi, std::string const& arg_pp);
  1507. int cmcmd_cmake_ninja_depends(std::vector<std::string>::const_iterator argBeg,
  1508. std::vector<std::string>::const_iterator argEnd)
  1509. {
  1510. std::string arg_tdi;
  1511. std::string arg_pp;
  1512. std::string arg_dep;
  1513. std::string arg_obj;
  1514. std::string arg_ddi;
  1515. std::string arg_lang;
  1516. for (std::string const& arg : cmMakeRange(argBeg, argEnd)) {
  1517. if (cmHasLiteralPrefix(arg, "--tdi=")) {
  1518. arg_tdi = arg.substr(6);
  1519. } else if (cmHasLiteralPrefix(arg, "--pp=")) {
  1520. arg_pp = arg.substr(5);
  1521. } else if (cmHasLiteralPrefix(arg, "--dep=")) {
  1522. arg_dep = arg.substr(6);
  1523. } else if (cmHasLiteralPrefix(arg, "--obj=")) {
  1524. arg_obj = arg.substr(6);
  1525. } else if (cmHasLiteralPrefix(arg, "--ddi=")) {
  1526. arg_ddi = arg.substr(6);
  1527. } else if (cmHasLiteralPrefix(arg, "--lang=")) {
  1528. arg_lang = arg.substr(7);
  1529. } else {
  1530. cmSystemTools::Error("-E cmake_ninja_depends unknown argument: " + arg);
  1531. return 1;
  1532. }
  1533. }
  1534. if (arg_tdi.empty()) {
  1535. cmSystemTools::Error("-E cmake_ninja_depends requires value for --tdi=");
  1536. return 1;
  1537. }
  1538. if (arg_pp.empty()) {
  1539. cmSystemTools::Error("-E cmake_ninja_depends requires value for --pp=");
  1540. return 1;
  1541. }
  1542. if (arg_dep.empty()) {
  1543. cmSystemTools::Error("-E cmake_ninja_depends requires value for --dep=");
  1544. return 1;
  1545. }
  1546. if (arg_obj.empty()) {
  1547. cmSystemTools::Error("-E cmake_ninja_depends requires value for --obj=");
  1548. return 1;
  1549. }
  1550. if (arg_ddi.empty()) {
  1551. cmSystemTools::Error("-E cmake_ninja_depends requires value for --ddi=");
  1552. return 1;
  1553. }
  1554. if (arg_lang.empty()) {
  1555. cmSystemTools::Error("-E cmake_ninja_depends requires value for --lang=");
  1556. return 1;
  1557. }
  1558. std::unique_ptr<cmSourceInfo> info;
  1559. if (arg_lang == "Fortran") {
  1560. info = cmcmd_cmake_ninja_depends_fortran(arg_tdi, arg_pp);
  1561. } else {
  1562. cmSystemTools::Error("-E cmake_ninja_depends does not understand the " +
  1563. arg_lang + " language");
  1564. return 1;
  1565. }
  1566. if (!info) {
  1567. // The error message is already expected to have been output.
  1568. return 1;
  1569. }
  1570. {
  1571. cmGeneratedFileStream depfile(arg_dep);
  1572. depfile << cmSystemTools::ConvertToUnixOutputPath(arg_pp) << ":";
  1573. for (std::string const& include : info->Includes) {
  1574. depfile << " \\\n " << cmSystemTools::ConvertToUnixOutputPath(include);
  1575. }
  1576. depfile << "\n";
  1577. }
  1578. Json::Value ddi(Json::objectValue);
  1579. ddi["object"] = arg_obj;
  1580. Json::Value& ddi_provides = ddi["provides"] = Json::arrayValue;
  1581. for (std::string const& provide : info->Provides) {
  1582. ddi_provides.append(provide);
  1583. }
  1584. Json::Value& ddi_requires = ddi["requires"] = Json::arrayValue;
  1585. for (std::string const& r : info->Requires) {
  1586. // Require modules not provided in the same source.
  1587. if (!info->Provides.count(r)) {
  1588. ddi_requires.append(r);
  1589. }
  1590. }
  1591. cmGeneratedFileStream ddif(arg_ddi);
  1592. ddif << ddi;
  1593. if (!ddif) {
  1594. cmSystemTools::Error("-E cmake_ninja_depends failed to write " + arg_ddi);
  1595. return 1;
  1596. }
  1597. return 0;
  1598. }
  1599. std::unique_ptr<cmSourceInfo> cmcmd_cmake_ninja_depends_fortran(
  1600. std::string const& arg_tdi, std::string const& arg_pp)
  1601. {
  1602. cmFortranCompiler fc;
  1603. std::vector<std::string> includes;
  1604. {
  1605. Json::Value tdio;
  1606. Json::Value const& tdi = tdio;
  1607. {
  1608. cmsys::ifstream tdif(arg_tdi.c_str(), std::ios::in | std::ios::binary);
  1609. Json::Reader reader;
  1610. if (!reader.parse(tdif, tdio, false)) {
  1611. cmSystemTools::Error("-E cmake_ninja_depends failed to parse " +
  1612. arg_tdi + reader.getFormattedErrorMessages());
  1613. return nullptr;
  1614. }
  1615. }
  1616. Json::Value const& tdi_include_dirs = tdi["include-dirs"];
  1617. if (tdi_include_dirs.isArray()) {
  1618. for (auto const& tdi_include_dir : tdi_include_dirs) {
  1619. includes.push_back(tdi_include_dir.asString());
  1620. }
  1621. }
  1622. Json::Value const& tdi_compiler_id = tdi["compiler-id"];
  1623. fc.Id = tdi_compiler_id.asString();
  1624. Json::Value const& tdi_submodule_sep = tdi["submodule-sep"];
  1625. fc.SModSep = tdi_submodule_sep.asString();
  1626. Json::Value const& tdi_submodule_ext = tdi["submodule-ext"];
  1627. fc.SModExt = tdi_submodule_ext.asString();
  1628. }
  1629. cmFortranSourceInfo finfo;
  1630. std::set<std::string> defines;
  1631. cmFortranParser parser(fc, includes, defines, finfo);
  1632. if (!cmFortranParser_FilePush(&parser, arg_pp.c_str())) {
  1633. cmSystemTools::Error("-E cmake_ninja_depends failed to open " + arg_pp);
  1634. return nullptr;
  1635. }
  1636. if (cmFortran_yyparse(parser.Scanner) != 0) {
  1637. // Failed to parse the file.
  1638. return nullptr;
  1639. }
  1640. auto info = cm::make_unique<cmSourceInfo>();
  1641. info->Provides = finfo.Provides;
  1642. info->Requires = finfo.Requires;
  1643. info->Includes = finfo.Includes;
  1644. return info;
  1645. }
  1646. struct cmDyndepObjectInfo
  1647. {
  1648. std::string Object;
  1649. std::vector<std::string> Provides;
  1650. std::vector<std::string> Requires;
  1651. };
  1652. bool cmGlobalNinjaGenerator::WriteDyndepFile(
  1653. std::string const& dir_top_src, std::string const& dir_top_bld,
  1654. std::string const& dir_cur_src, std::string const& dir_cur_bld,
  1655. std::string const& arg_dd, std::vector<std::string> const& arg_ddis,
  1656. std::string const& module_dir,
  1657. std::vector<std::string> const& linked_target_dirs,
  1658. std::string const& arg_lang)
  1659. {
  1660. // Setup path conversions.
  1661. {
  1662. cmStateSnapshot snapshot = this->GetCMakeInstance()->GetCurrentSnapshot();
  1663. snapshot.GetDirectory().SetCurrentSource(dir_cur_src);
  1664. snapshot.GetDirectory().SetCurrentBinary(dir_cur_bld);
  1665. snapshot.GetDirectory().SetRelativePathTopSource(dir_top_src.c_str());
  1666. snapshot.GetDirectory().SetRelativePathTopBinary(dir_top_bld.c_str());
  1667. auto mfd = cm::make_unique<cmMakefile>(this, snapshot);
  1668. std::unique_ptr<cmLocalNinjaGenerator> lgd(
  1669. static_cast<cmLocalNinjaGenerator*>(
  1670. this->CreateLocalGenerator(mfd.get())));
  1671. this->Makefiles.push_back(mfd.release());
  1672. this->LocalGenerators.push_back(lgd.release());
  1673. }
  1674. std::vector<cmDyndepObjectInfo> objects;
  1675. for (std::string const& arg_ddi : arg_ddis) {
  1676. // Load the ddi file and compute the module file paths it provides.
  1677. Json::Value ddio;
  1678. Json::Value const& ddi = ddio;
  1679. cmsys::ifstream ddif(arg_ddi.c_str(), std::ios::in | std::ios::binary);
  1680. Json::Reader reader;
  1681. if (!reader.parse(ddif, ddio, false)) {
  1682. cmSystemTools::Error("-E cmake_ninja_dyndep failed to parse " + arg_ddi +
  1683. reader.getFormattedErrorMessages());
  1684. return false;
  1685. }
  1686. cmDyndepObjectInfo info;
  1687. info.Object = ddi["object"].asString();
  1688. Json::Value const& ddi_provides = ddi["provides"];
  1689. if (ddi_provides.isArray()) {
  1690. for (auto const& ddi_provide : ddi_provides) {
  1691. info.Provides.push_back(ddi_provide.asString());
  1692. }
  1693. }
  1694. Json::Value const& ddi_requires = ddi["requires"];
  1695. if (ddi_requires.isArray()) {
  1696. for (auto const& ddi_require : ddi_requires) {
  1697. info.Requires.push_back(ddi_require.asString());
  1698. }
  1699. }
  1700. objects.push_back(std::move(info));
  1701. }
  1702. // Map from module name to module file path, if known.
  1703. std::map<std::string, std::string> mod_files;
  1704. // Populate the module map with those provided by linked targets first.
  1705. for (std::string const& linked_target_dir : linked_target_dirs) {
  1706. std::string const ltmn =
  1707. linked_target_dir + "/" + arg_lang + "Modules.json";
  1708. Json::Value ltm;
  1709. cmsys::ifstream ltmf(ltmn.c_str(), std::ios::in | std::ios::binary);
  1710. Json::Reader reader;
  1711. if (ltmf && !reader.parse(ltmf, ltm, false)) {
  1712. cmSystemTools::Error("-E cmake_ninja_dyndep failed to parse " +
  1713. linked_target_dir +
  1714. reader.getFormattedErrorMessages());
  1715. return false;
  1716. }
  1717. if (ltm.isObject()) {
  1718. for (Json::Value::iterator i = ltm.begin(); i != ltm.end(); ++i) {
  1719. mod_files[i.key().asString()] = i->asString();
  1720. }
  1721. }
  1722. }
  1723. // Extend the module map with those provided by this target.
  1724. // We do this after loading the modules provided by linked targets
  1725. // in case we have one of the same name that must be preferred.
  1726. Json::Value tm = Json::objectValue;
  1727. for (cmDyndepObjectInfo const& object : objects) {
  1728. for (std::string const& p : object.Provides) {
  1729. std::string const mod = module_dir + p;
  1730. mod_files[p] = mod;
  1731. tm[p] = mod;
  1732. }
  1733. }
  1734. cmGeneratedFileStream ddf(arg_dd);
  1735. ddf << "ninja_dyndep_version = 1.0\n";
  1736. for (cmDyndepObjectInfo const& object : objects) {
  1737. std::string const ddComment;
  1738. std::string const ddRule = "dyndep";
  1739. cmNinjaDeps ddOutputs;
  1740. cmNinjaDeps ddImplicitOuts;
  1741. cmNinjaDeps ddExplicitDeps;
  1742. cmNinjaDeps ddImplicitDeps;
  1743. cmNinjaDeps ddOrderOnlyDeps;
  1744. cmNinjaVars ddVars;
  1745. ddOutputs.push_back(object.Object);
  1746. for (std::string const& p : object.Provides) {
  1747. ddImplicitOuts.push_back(this->ConvertToNinjaPath(mod_files[p]));
  1748. }
  1749. for (std::string const& r : object.Requires) {
  1750. std::map<std::string, std::string>::iterator m = mod_files.find(r);
  1751. if (m != mod_files.end()) {
  1752. ddImplicitDeps.push_back(this->ConvertToNinjaPath(m->second));
  1753. }
  1754. }
  1755. if (!object.Provides.empty()) {
  1756. ddVars["restat"] = "1";
  1757. }
  1758. this->WriteBuild(ddf, ddComment, ddRule, ddOutputs, ddImplicitOuts,
  1759. ddExplicitDeps, ddImplicitDeps, ddOrderOnlyDeps, ddVars);
  1760. }
  1761. // Store the map of modules provided by this target in a file for
  1762. // use by dependents that reference this target in linked-target-dirs.
  1763. std::string const target_mods_file =
  1764. cmSystemTools::GetFilenamePath(arg_dd) + "/" + arg_lang + "Modules.json";
  1765. cmGeneratedFileStream tmf(target_mods_file);
  1766. tmf << tm;
  1767. return true;
  1768. }
  1769. int cmcmd_cmake_ninja_dyndep(std::vector<std::string>::const_iterator argBeg,
  1770. std::vector<std::string>::const_iterator argEnd)
  1771. {
  1772. std::vector<std::string> arg_full =
  1773. cmSystemTools::HandleResponseFile(argBeg, argEnd);
  1774. std::string arg_dd;
  1775. std::string arg_lang;
  1776. std::string arg_tdi;
  1777. std::vector<std::string> arg_ddis;
  1778. for (std::string const& arg : arg_full) {
  1779. if (cmHasLiteralPrefix(arg, "--tdi=")) {
  1780. arg_tdi = arg.substr(6);
  1781. } else if (cmHasLiteralPrefix(arg, "--lang=")) {
  1782. arg_lang = arg.substr(7);
  1783. } else if (cmHasLiteralPrefix(arg, "--dd=")) {
  1784. arg_dd = arg.substr(5);
  1785. } else if (!cmHasLiteralPrefix(arg, "--") &&
  1786. cmHasLiteralSuffix(arg, ".ddi")) {
  1787. arg_ddis.push_back(arg);
  1788. } else {
  1789. cmSystemTools::Error("-E cmake_ninja_dyndep unknown argument: " + arg);
  1790. return 1;
  1791. }
  1792. }
  1793. if (arg_tdi.empty()) {
  1794. cmSystemTools::Error("-E cmake_ninja_dyndep requires value for --tdi=");
  1795. return 1;
  1796. }
  1797. if (arg_lang.empty()) {
  1798. cmSystemTools::Error("-E cmake_ninja_dyndep requires value for --lang=");
  1799. return 1;
  1800. }
  1801. if (arg_dd.empty()) {
  1802. cmSystemTools::Error("-E cmake_ninja_dyndep requires value for --dd=");
  1803. return 1;
  1804. }
  1805. Json::Value tdio;
  1806. Json::Value const& tdi = tdio;
  1807. {
  1808. cmsys::ifstream tdif(arg_tdi.c_str(), std::ios::in | std::ios::binary);
  1809. Json::Reader reader;
  1810. if (!reader.parse(tdif, tdio, false)) {
  1811. cmSystemTools::Error("-E cmake_ninja_dyndep failed to parse " + arg_tdi +
  1812. reader.getFormattedErrorMessages());
  1813. return 1;
  1814. }
  1815. }
  1816. std::string const dir_cur_bld = tdi["dir-cur-bld"].asString();
  1817. std::string const dir_cur_src = tdi["dir-cur-src"].asString();
  1818. std::string const dir_top_bld = tdi["dir-top-bld"].asString();
  1819. std::string const dir_top_src = tdi["dir-top-src"].asString();
  1820. std::string module_dir = tdi["module-dir"].asString();
  1821. if (!module_dir.empty() && !cmHasLiteralSuffix(module_dir, "/")) {
  1822. module_dir += "/";
  1823. }
  1824. std::vector<std::string> linked_target_dirs;
  1825. Json::Value const& tdi_linked_target_dirs = tdi["linked-target-dirs"];
  1826. if (tdi_linked_target_dirs.isArray()) {
  1827. for (auto const& tdi_linked_target_dir : tdi_linked_target_dirs) {
  1828. linked_target_dirs.push_back(tdi_linked_target_dir.asString());
  1829. }
  1830. }
  1831. cmake cm(cmake::RoleInternal, cmState::Unknown);
  1832. cm.SetHomeDirectory(dir_top_src);
  1833. cm.SetHomeOutputDirectory(dir_top_bld);
  1834. std::unique_ptr<cmGlobalNinjaGenerator> ggd(
  1835. static_cast<cmGlobalNinjaGenerator*>(cm.CreateGlobalGenerator("Ninja")));
  1836. if (!ggd ||
  1837. !ggd->WriteDyndepFile(dir_top_src, dir_top_bld, dir_cur_src, dir_cur_bld,
  1838. arg_dd, arg_ddis, module_dir, linked_target_dirs,
  1839. arg_lang)) {
  1840. return 1;
  1841. }
  1842. return 0;
  1843. }