cmGlobalNinjaGenerator.cxx 63 KB

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