cmGlobalNinjaGenerator.cxx 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2011 Peter Collingbourne <[email protected]>
  4. Copyright 2011 Nicolas Despres <[email protected]>
  5. Distributed under the OSI-approved BSD License (the "License");
  6. see accompanying file Copyright.txt for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the License for more information.
  10. ============================================================================*/
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGeneratorExpressionEvaluationFile.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmGlobalNinjaGenerator.h"
  15. #include "cmLocalNinjaGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmVersion.h"
  18. #include "cmAlgorithms.h"
  19. #include <algorithm>
  20. #include <assert.h>
  21. #include <ctype.h>
  22. const char* cmGlobalNinjaGenerator::NINJA_BUILD_FILE = "build.ninja";
  23. const char* cmGlobalNinjaGenerator::NINJA_RULES_FILE = "rules.ninja";
  24. const char* cmGlobalNinjaGenerator::INDENT = " ";
  25. void cmGlobalNinjaGenerator::Indent(std::ostream& os, int count)
  26. {
  27. for(int i = 0; i < count; ++i)
  28. os << cmGlobalNinjaGenerator::INDENT;
  29. }
  30. void cmGlobalNinjaGenerator::WriteDivider(std::ostream& os)
  31. {
  32. os
  33. << "# ======================================"
  34. << "=======================================\n";
  35. }
  36. void cmGlobalNinjaGenerator::WriteComment(std::ostream& os,
  37. const std::string& comment)
  38. {
  39. if (comment.empty())
  40. return;
  41. std::string replace = comment;
  42. std::string::size_type lpos = 0;
  43. std::string::size_type rpos;
  44. os << "\n#############################################\n";
  45. while((rpos = replace.find('\n', lpos)) != std::string::npos)
  46. {
  47. os << "# " << replace.substr(lpos, rpos - lpos) << "\n";
  48. lpos = rpos + 1;
  49. }
  50. os << "# " << replace.substr(lpos) << "\n\n";
  51. }
  52. std::string cmGlobalNinjaGenerator::EncodeRuleName(std::string const& name)
  53. {
  54. // Ninja rule names must match "[a-zA-Z0-9_.-]+". Use ".xx" to encode
  55. // "." and all invalid characters as hexadecimal.
  56. std::string encoded;
  57. for (std::string::const_iterator i = name.begin();
  58. i != name.end(); ++i)
  59. {
  60. if (isalnum(*i) || *i == '_' || *i == '-')
  61. {
  62. encoded += *i;
  63. }
  64. else
  65. {
  66. char buf[16];
  67. sprintf(buf, ".%02x", static_cast<unsigned int>(*i));
  68. encoded += buf;
  69. }
  70. }
  71. return encoded;
  72. }
  73. static bool IsIdentChar(char c)
  74. {
  75. return
  76. ('a' <= c && c <= 'z') ||
  77. ('+' <= c && c <= '9') || // +,-./ and numbers
  78. ('A' <= c && c <= 'Z') ||
  79. (c == '_') || (c == '$') || (c == '\\') ||
  80. (c == ' ') || (c == ':');
  81. }
  82. std::string cmGlobalNinjaGenerator::EncodeIdent(const std::string &ident,
  83. std::ostream &vars) {
  84. if (std::find_if(ident.begin(), ident.end(),
  85. std::not1(std::ptr_fun(IsIdentChar))) != ident.end()) {
  86. static unsigned VarNum = 0;
  87. std::ostringstream names;
  88. names << "ident" << VarNum++;
  89. vars << names.str() << " = " << ident << "\n";
  90. return "$" + names.str();
  91. } else {
  92. std::string result = ident;
  93. cmSystemTools::ReplaceString(result, " ", "$ ");
  94. cmSystemTools::ReplaceString(result, ":", "$:");
  95. return result;
  96. }
  97. }
  98. std::string cmGlobalNinjaGenerator::EncodeLiteral(const std::string &lit)
  99. {
  100. std::string result = lit;
  101. cmSystemTools::ReplaceString(result, "$", "$$");
  102. cmSystemTools::ReplaceString(result, "\n", "$\n");
  103. return result;
  104. }
  105. std::string cmGlobalNinjaGenerator::EncodePath(const std::string &path)
  106. {
  107. std::string result = path;
  108. #ifdef _WIN32
  109. if (this->IsGCCOnWindows())
  110. cmSystemTools::ReplaceString(result, "\\", "/");
  111. else
  112. cmSystemTools::ReplaceString(result, "/", "\\");
  113. #endif
  114. return EncodeLiteral(result);
  115. }
  116. std::string cmGlobalNinjaGenerator::EncodeDepfileSpace(const std::string &path)
  117. {
  118. std::string result = path;
  119. cmSystemTools::ReplaceString(result, " ", "\\ ");
  120. return result;
  121. }
  122. void cmGlobalNinjaGenerator::WriteBuild(std::ostream& os,
  123. const std::string& comment,
  124. const std::string& rule,
  125. const cmNinjaDeps& outputs,
  126. const cmNinjaDeps& explicitDeps,
  127. const cmNinjaDeps& implicitDeps,
  128. const cmNinjaDeps& orderOnlyDeps,
  129. const cmNinjaVars& variables,
  130. const std::string& rspfile,
  131. int cmdLineLimit,
  132. bool* usedResponseFile)
  133. {
  134. // Make sure there is a rule.
  135. if(rule.empty())
  136. {
  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. {
  145. cmSystemTools::Error("No output files for WriteBuildStatement! called "
  146. "with comment: ",
  147. comment.c_str());
  148. return;
  149. }
  150. cmGlobalNinjaGenerator::WriteComment(os, comment);
  151. std::string arguments;
  152. // TODO: Better formatting for when there are multiple input/output files.
  153. // Write explicit dependencies.
  154. for(cmNinjaDeps::const_iterator i = explicitDeps.begin();
  155. i != explicitDeps.end();
  156. ++i)
  157. {
  158. arguments += " " + EncodeIdent(EncodePath(*i), os);
  159. }
  160. // Write implicit dependencies.
  161. if(!implicitDeps.empty())
  162. {
  163. arguments += " |";
  164. for(cmNinjaDeps::const_iterator i = implicitDeps.begin();
  165. i != implicitDeps.end();
  166. ++i)
  167. arguments += " " + EncodeIdent(EncodePath(*i), os);
  168. }
  169. // Write order-only dependencies.
  170. if(!orderOnlyDeps.empty())
  171. {
  172. arguments += " ||";
  173. for(cmNinjaDeps::const_iterator i = orderOnlyDeps.begin();
  174. i != orderOnlyDeps.end();
  175. ++i)
  176. arguments += " " + EncodeIdent(EncodePath(*i), os);
  177. }
  178. arguments += "\n";
  179. std::string build;
  180. // Write outputs files.
  181. build += "build";
  182. for(cmNinjaDeps::const_iterator i = outputs.begin();
  183. i != outputs.end(); ++i)
  184. {
  185. build += " " + EncodeIdent(EncodePath(*i), os);
  186. if (this->ComputingUnknownDependencies)
  187. {
  188. this->CombinedBuildOutputs.insert( EncodePath(*i) );
  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();
  197. i != variables.end(); ++i)
  198. cmGlobalNinjaGenerator::WriteVariable(variable_assignments,
  199. i->first, i->second, "", 1);
  200. // check if a response file rule should be used
  201. std::string buildstr = build;
  202. std::string assignments = variable_assignments.str();
  203. const std::string& args = arguments;
  204. bool useResponseFile = false;
  205. if (cmdLineLimit > 0
  206. && args.size() + buildstr.size() + assignments.size()
  207. > (size_t) cmdLineLimit) {
  208. variable_assignments.str(std::string());
  209. cmGlobalNinjaGenerator::WriteVariable(variable_assignments,
  210. "RSP_FILE", rspfile, "", 1);
  211. assignments += variable_assignments.str();
  212. useResponseFile = true;
  213. }
  214. if (usedResponseFile)
  215. {
  216. *usedResponseFile = useResponseFile;
  217. }
  218. os << buildstr << args << assignments;
  219. }
  220. void cmGlobalNinjaGenerator::WritePhonyBuild(std::ostream& os,
  221. const std::string& comment,
  222. const cmNinjaDeps& outputs,
  223. const cmNinjaDeps& explicitDeps,
  224. const cmNinjaDeps& implicitDeps,
  225. const cmNinjaDeps& orderOnlyDeps,
  226. const cmNinjaVars& variables)
  227. {
  228. this->WriteBuild(os,
  229. comment,
  230. "phony",
  231. outputs,
  232. explicitDeps,
  233. implicitDeps,
  234. orderOnlyDeps,
  235. variables);
  236. }
  237. void cmGlobalNinjaGenerator::AddCustomCommandRule()
  238. {
  239. this->AddRule("CUSTOM_COMMAND",
  240. "$COMMAND",
  241. "$DESC",
  242. "Rule for running custom commands.",
  243. /*depfile*/ "",
  244. /*deptype*/ "",
  245. /*rspfile*/ "",
  246. /*rspcontent*/ "",
  247. /*restat*/ "", // bound on each build statement as needed
  248. /*generator*/ false);
  249. }
  250. void
  251. cmGlobalNinjaGenerator::WriteCustomCommandBuild(const std::string& command,
  252. const std::string& description,
  253. const std::string& comment,
  254. bool uses_terminal,
  255. bool restat,
  256. const cmNinjaDeps& outputs,
  257. const cmNinjaDeps& deps,
  258. const cmNinjaDeps& orderOnly)
  259. {
  260. std::string cmd = command;
  261. #ifdef _WIN32
  262. if (cmd.empty())
  263. // TODO Shouldn't an empty command be handled by ninja?
  264. cmd = "cmd.exe /c";
  265. #endif
  266. this->AddCustomCommandRule();
  267. cmNinjaVars vars;
  268. vars["COMMAND"] = cmd;
  269. vars["DESC"] = EncodeLiteral(description);
  270. if (restat)
  271. {
  272. vars["restat"] = "1";
  273. }
  274. if (uses_terminal && SupportsConsolePool())
  275. {
  276. vars["pool"] = "console";
  277. }
  278. this->WriteBuild(*this->BuildFileStream,
  279. comment,
  280. "CUSTOM_COMMAND",
  281. outputs,
  282. deps,
  283. cmNinjaDeps(),
  284. orderOnly,
  285. vars);
  286. if (this->ComputingUnknownDependencies)
  287. {
  288. //we need to track every dependency that comes in, since we are trying
  289. //to find dependencies that are side effects of build commands
  290. for(cmNinjaDeps::const_iterator i = deps.begin(); i != deps.end(); ++i)
  291. {
  292. this->CombinedCustomCommandExplicitDependencies.insert(EncodePath(*i));
  293. }
  294. }
  295. }
  296. void
  297. cmGlobalNinjaGenerator::AddMacOSXContentRule()
  298. {
  299. cmLocalGenerator *lg = this->LocalGenerators[0];
  300. std::ostringstream cmd;
  301. cmd << lg->ConvertToOutputFormat(cmSystemTools::GetCMakeCommand(),
  302. cmLocalGenerator::SHELL)
  303. << " -E copy $in $out";
  304. this->AddRule("COPY_OSX_CONTENT",
  305. cmd.str(),
  306. "Copying OS X Content $out",
  307. "Rule for copying OS X bundle content file.",
  308. /*depfile*/ "",
  309. /*deptype*/ "",
  310. /*rspfile*/ "",
  311. /*rspcontent*/ "",
  312. /*restat*/ "",
  313. /*generator*/ false);
  314. }
  315. void
  316. cmGlobalNinjaGenerator::WriteMacOSXContentBuild(const std::string& input,
  317. const std::string& output)
  318. {
  319. this->AddMacOSXContentRule();
  320. cmNinjaDeps outputs;
  321. outputs.push_back(output);
  322. cmNinjaDeps deps;
  323. deps.push_back(input);
  324. cmNinjaVars vars;
  325. this->WriteBuild(*this->BuildFileStream,
  326. "",
  327. "COPY_OSX_CONTENT",
  328. outputs,
  329. deps,
  330. cmNinjaDeps(),
  331. cmNinjaDeps(),
  332. cmNinjaVars());
  333. }
  334. void cmGlobalNinjaGenerator::WriteRule(std::ostream& os,
  335. const std::string& name,
  336. const std::string& command,
  337. const std::string& description,
  338. const std::string& comment,
  339. const std::string& depfile,
  340. const std::string& deptype,
  341. const std::string& rspfile,
  342. const std::string& rspcontent,
  343. const std::string& restat,
  344. bool generator)
  345. {
  346. // Make sure the rule has a name.
  347. if(name.empty())
  348. {
  349. cmSystemTools::Error("No name given for WriteRuleStatement! called "
  350. "with comment: ",
  351. comment.c_str());
  352. return;
  353. }
  354. // Make sure a command is given.
  355. if(command.empty())
  356. {
  357. cmSystemTools::Error("No command given for WriteRuleStatement! called "
  358. "with comment: ",
  359. comment.c_str());
  360. return;
  361. }
  362. cmGlobalNinjaGenerator::WriteComment(os, comment);
  363. // Write the rule.
  364. os << "rule " << name << "\n";
  365. // Write the depfile if any.
  366. if(!depfile.empty())
  367. {
  368. cmGlobalNinjaGenerator::Indent(os, 1);
  369. os << "depfile = " << depfile << "\n";
  370. }
  371. // Write the deptype if any.
  372. if (!deptype.empty())
  373. {
  374. cmGlobalNinjaGenerator::Indent(os, 1);
  375. os << "deps = " << deptype << "\n";
  376. }
  377. // Write the command.
  378. cmGlobalNinjaGenerator::Indent(os, 1);
  379. os << "command = " << command << "\n";
  380. // Write the description if any.
  381. if(!description.empty())
  382. {
  383. cmGlobalNinjaGenerator::Indent(os, 1);
  384. os << "description = " << description << "\n";
  385. }
  386. if(!rspfile.empty())
  387. {
  388. if (rspcontent.empty())
  389. {
  390. cmSystemTools::Error("No rspfile_content given!", comment.c_str());
  391. return;
  392. }
  393. cmGlobalNinjaGenerator::Indent(os, 1);
  394. os << "rspfile = " << rspfile << "\n";
  395. cmGlobalNinjaGenerator::Indent(os, 1);
  396. os << "rspfile_content = " << rspcontent << "\n";
  397. }
  398. if(!restat.empty())
  399. {
  400. cmGlobalNinjaGenerator::Indent(os, 1);
  401. os << "restat = " << restat << "\n";
  402. }
  403. if(generator)
  404. {
  405. cmGlobalNinjaGenerator::Indent(os, 1);
  406. os << "generator = 1\n";
  407. }
  408. os << "\n";
  409. }
  410. void cmGlobalNinjaGenerator::WriteVariable(std::ostream& os,
  411. const std::string& name,
  412. const std::string& value,
  413. const std::string& comment,
  414. int indent)
  415. {
  416. // Make sure we have a name.
  417. if(name.empty())
  418. {
  419. cmSystemTools::Error("No name given for WriteVariable! called "
  420. "with comment: ",
  421. comment.c_str());
  422. return;
  423. }
  424. // Do not add a variable if the value is empty.
  425. std::string val = cmSystemTools::TrimWhitespace(value);
  426. if(val.empty())
  427. {
  428. return;
  429. }
  430. cmGlobalNinjaGenerator::WriteComment(os, comment);
  431. cmGlobalNinjaGenerator::Indent(os, indent);
  432. os << name << " = " << val << "\n";
  433. }
  434. void cmGlobalNinjaGenerator::WriteInclude(std::ostream& os,
  435. const std::string& filename,
  436. const std::string& comment)
  437. {
  438. cmGlobalNinjaGenerator::WriteComment(os, comment);
  439. os << "include " << filename << "\n";
  440. }
  441. void cmGlobalNinjaGenerator::WriteDefault(std::ostream& os,
  442. const cmNinjaDeps& targets,
  443. const std::string& comment)
  444. {
  445. cmGlobalNinjaGenerator::WriteComment(os, comment);
  446. os << "default";
  447. for(cmNinjaDeps::const_iterator i = targets.begin(); i != targets.end(); ++i)
  448. os << " " << *i;
  449. os << "\n";
  450. }
  451. cmGlobalNinjaGenerator::cmGlobalNinjaGenerator(cmake* cm)
  452. : cmGlobalCommonGenerator(cm)
  453. , BuildFileStream(0)
  454. , RulesFileStream(0)
  455. , CompileCommandsStream(0)
  456. , Rules()
  457. , AllDependencies()
  458. , UsingGCCOnWindows(false)
  459. , ComputingUnknownDependencies(false)
  460. , PolicyCMP0058(cmPolicies::WARN)
  461. {
  462. #ifdef _WIN32
  463. cm->GetState()->SetWindowsShell(true);
  464. #endif
  465. // // Ninja is not ported to non-Unix OS yet.
  466. // this->ForceUnixPaths = true;
  467. this->FindMakeProgramFile = "CMakeNinjaFindMake.cmake";
  468. }
  469. //----------------------------------------------------------------------------
  470. // Virtual public methods.
  471. cmLocalGenerator*
  472. cmGlobalNinjaGenerator::CreateLocalGenerator(cmMakefile* mf)
  473. {
  474. return new cmLocalNinjaGenerator(this, mf);
  475. }
  476. void cmGlobalNinjaGenerator
  477. ::GetDocumentation(cmDocumentationEntry& entry)
  478. {
  479. entry.Name = cmGlobalNinjaGenerator::GetActualName();
  480. entry.Brief = "Generates build.ninja files.";
  481. }
  482. // Implemented in all cmGlobaleGenerator sub-classes.
  483. // Used in:
  484. // Source/cmLocalGenerator.cxx
  485. // Source/cmake.cxx
  486. void cmGlobalNinjaGenerator::Generate()
  487. {
  488. // Check minimum Ninja version.
  489. if (cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  490. this->NinjaVersion.c_str(),
  491. RequiredNinjaVersion().c_str()))
  492. {
  493. std::ostringstream msg;
  494. msg << "The detected version of Ninja (" << this->NinjaVersion;
  495. msg << ") is less than the version of Ninja required by CMake (";
  496. msg << this->RequiredNinjaVersion() << ").";
  497. this->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, msg.str());
  498. return;
  499. }
  500. this->OpenBuildFileStream();
  501. this->OpenRulesFileStream();
  502. this->PolicyCMP0058 =
  503. this->LocalGenerators[0]->GetMakefile()
  504. ->GetPolicyStatus(cmPolicies::CMP0058);
  505. this->ComputingUnknownDependencies =
  506. (this->PolicyCMP0058 == cmPolicies::OLD ||
  507. this->PolicyCMP0058 == cmPolicies::WARN);
  508. this->cmGlobalGenerator::Generate();
  509. this->WriteAssumedSourceDependencies();
  510. this->WriteTargetAliases(*this->BuildFileStream);
  511. this->WriteUnknownExplicitDependencies(*this->BuildFileStream);
  512. this->WriteBuiltinTargets(*this->BuildFileStream);
  513. if (cmSystemTools::GetErrorOccuredFlag()) {
  514. this->RulesFileStream->setstate(std::ios_base::failbit);
  515. this->BuildFileStream->setstate(std::ios_base::failbit);
  516. }
  517. this->CloseCompileCommandsStream();
  518. this->CloseRulesFileStream();
  519. this->CloseBuildFileStream();
  520. }
  521. void cmGlobalNinjaGenerator::FindMakeProgram(cmMakefile* mf)
  522. {
  523. this->cmGlobalGenerator::FindMakeProgram(mf);
  524. if (const char* ninjaCommand = mf->GetDefinition("CMAKE_MAKE_PROGRAM"))
  525. {
  526. this->NinjaCommand = ninjaCommand;
  527. std::vector<std::string> command;
  528. command.push_back(this->NinjaCommand);
  529. command.push_back("--version");
  530. std::string version;
  531. cmSystemTools::RunSingleCommand(command,
  532. &version, 0, 0, 0,
  533. cmSystemTools::OUTPUT_NONE);
  534. this->NinjaVersion = cmSystemTools::TrimWhitespace(version);
  535. }
  536. }
  537. void cmGlobalNinjaGenerator
  538. ::EnableLanguage(std::vector<std::string>const& langs,
  539. cmMakefile* mf,
  540. bool optional)
  541. {
  542. if (std::find(langs.begin(), langs.end(), "Fortran") != langs.end())
  543. {
  544. cmSystemTools::Error("The Ninja generator does not support Fortran yet.");
  545. }
  546. this->cmGlobalGenerator::EnableLanguage(langs, mf, optional);
  547. for(std::vector<std::string>::const_iterator l = langs.begin();
  548. l != langs.end(); ++l)
  549. {
  550. if(*l == "NONE")
  551. {
  552. continue;
  553. }
  554. this->ResolveLanguageCompiler(*l, mf, optional);
  555. }
  556. #ifdef _WIN32
  557. if (mf->IsOn("CMAKE_COMPILER_IS_MINGW") ||
  558. strcmp(mf->GetSafeDefinition("CMAKE_C_COMPILER_ID"), "GNU") == 0 ||
  559. strcmp(mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID"), "GNU") == 0 ||
  560. strcmp(mf->GetSafeDefinition("CMAKE_C_SIMULATE_ID"), "GNU") == 0 ||
  561. strcmp(mf->GetSafeDefinition("CMAKE_CXX_SIMULATE_ID"), "GNU") == 0)
  562. {
  563. this->UsingGCCOnWindows = true;
  564. }
  565. #endif
  566. }
  567. // Implemented by:
  568. // cmGlobalUnixMakefileGenerator3
  569. // cmGlobalGhsMultiGenerator
  570. // cmGlobalVisualStudio10Generator
  571. // cmGlobalVisualStudio6Generator
  572. // cmGlobalVisualStudio7Generator
  573. // cmGlobalXCodeGenerator
  574. // Called by:
  575. // cmGlobalGenerator::Build()
  576. void cmGlobalNinjaGenerator
  577. ::GenerateBuildCommand(std::vector<std::string>& makeCommand,
  578. const std::string& makeProgram,
  579. const std::string& /*projectName*/,
  580. const std::string& /*projectDir*/,
  581. const std::string& targetName,
  582. const std::string& /*config*/,
  583. bool /*fast*/,
  584. bool verbose,
  585. std::vector<std::string> const& makeOptions)
  586. {
  587. makeCommand.push_back(
  588. this->SelectMakeProgram(makeProgram)
  589. );
  590. if(verbose)
  591. {
  592. makeCommand.push_back("-v");
  593. }
  594. makeCommand.insert(makeCommand.end(),
  595. makeOptions.begin(), makeOptions.end());
  596. if(!targetName.empty())
  597. {
  598. if(targetName == "clean")
  599. {
  600. makeCommand.push_back("-t");
  601. makeCommand.push_back("clean");
  602. }
  603. else
  604. {
  605. makeCommand.push_back(targetName);
  606. }
  607. }
  608. }
  609. //----------------------------------------------------------------------------
  610. // Non-virtual public methods.
  611. void cmGlobalNinjaGenerator::AddRule(const std::string& name,
  612. const std::string& command,
  613. const std::string& description,
  614. const std::string& comment,
  615. const std::string& depfile,
  616. const std::string& deptype,
  617. const std::string& rspfile,
  618. const std::string& rspcontent,
  619. const std::string& restat,
  620. bool generator)
  621. {
  622. // Do not add the same rule twice.
  623. if (this->HasRule(name))
  624. {
  625. return;
  626. }
  627. this->Rules.insert(name);
  628. cmGlobalNinjaGenerator::WriteRule(*this->RulesFileStream,
  629. name,
  630. command,
  631. description,
  632. comment,
  633. depfile,
  634. deptype,
  635. rspfile,
  636. rspcontent,
  637. restat,
  638. generator);
  639. this->RuleCmdLength[name] = (int) command.size();
  640. }
  641. bool cmGlobalNinjaGenerator::HasRule(const std::string &name)
  642. {
  643. RulesSetType::const_iterator rule = this->Rules.find(name);
  644. return (rule != this->Rules.end());
  645. }
  646. //----------------------------------------------------------------------------
  647. // Private virtual overrides
  648. std::string cmGlobalNinjaGenerator::GetEditCacheCommand() const
  649. {
  650. // Ninja by design does not run interactive tools in the terminal,
  651. // so our only choice is cmake-gui.
  652. return cmSystemTools::GetCMakeGUICommand();
  653. }
  654. //----------------------------------------------------------------------------
  655. void cmGlobalNinjaGenerator
  656. ::ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const
  657. {
  658. // Compute full path to object file directory for this target.
  659. std::string dir;
  660. dir += gt->LocalGenerator->GetCurrentBinaryDirectory();
  661. dir += "/";
  662. dir += gt->LocalGenerator->GetTargetDirectory(gt);
  663. dir += "/";
  664. gt->ObjectDirectory = dir;
  665. }
  666. //----------------------------------------------------------------------------
  667. // Private methods
  668. void cmGlobalNinjaGenerator::OpenBuildFileStream()
  669. {
  670. // Compute Ninja's build file path.
  671. std::string buildFilePath =
  672. this->GetCMakeInstance()->GetHomeOutputDirectory();
  673. buildFilePath += "/";
  674. buildFilePath += cmGlobalNinjaGenerator::NINJA_BUILD_FILE;
  675. // Get a stream where to generate things.
  676. if (!this->BuildFileStream)
  677. {
  678. this->BuildFileStream = new cmGeneratedFileStream(buildFilePath.c_str());
  679. if (!this->BuildFileStream)
  680. {
  681. // An error message is generated by the constructor if it cannot
  682. // open the file.
  683. return;
  684. }
  685. }
  686. // Write the do not edit header.
  687. this->WriteDisclaimer(*this->BuildFileStream);
  688. // Write a comment about this file.
  689. *this->BuildFileStream
  690. << "# This file contains all the build statements describing the\n"
  691. << "# compilation DAG.\n\n"
  692. ;
  693. }
  694. void cmGlobalNinjaGenerator::CloseBuildFileStream()
  695. {
  696. if (this->BuildFileStream)
  697. {
  698. delete this->BuildFileStream;
  699. this->BuildFileStream = 0;
  700. }
  701. else
  702. {
  703. cmSystemTools::Error("Build file stream was not open.");
  704. }
  705. }
  706. void cmGlobalNinjaGenerator::OpenRulesFileStream()
  707. {
  708. // Compute Ninja's build file path.
  709. std::string rulesFilePath =
  710. this->GetCMakeInstance()->GetHomeOutputDirectory();
  711. rulesFilePath += "/";
  712. rulesFilePath += cmGlobalNinjaGenerator::NINJA_RULES_FILE;
  713. // Get a stream where to generate things.
  714. if (!this->RulesFileStream)
  715. {
  716. this->RulesFileStream = new cmGeneratedFileStream(rulesFilePath.c_str());
  717. if (!this->RulesFileStream)
  718. {
  719. // An error message is generated by the constructor if it cannot
  720. // open the file.
  721. return;
  722. }
  723. }
  724. // Write the do not edit header.
  725. this->WriteDisclaimer(*this->RulesFileStream);
  726. // Write comment about this file.
  727. *this->RulesFileStream
  728. << "# This file contains all the rules used to get the outputs files\n"
  729. << "# built from the input files.\n"
  730. << "# It is included in the main '" << NINJA_BUILD_FILE << "'.\n\n"
  731. ;
  732. }
  733. void cmGlobalNinjaGenerator::CloseRulesFileStream()
  734. {
  735. if (this->RulesFileStream)
  736. {
  737. delete this->RulesFileStream;
  738. this->RulesFileStream = 0;
  739. }
  740. else
  741. {
  742. cmSystemTools::Error("Rules file stream was not open.");
  743. }
  744. }
  745. std::string cmGlobalNinjaGenerator::ConvertToNinjaPath(const std::string& path)
  746. {
  747. cmLocalNinjaGenerator *ng =
  748. static_cast<cmLocalNinjaGenerator *>(this->LocalGenerators[0]);
  749. std::string convPath = ng->Convert(path, cmOutputConverter::HOME_OUTPUT);
  750. #ifdef _WIN32
  751. cmSystemTools::ReplaceString(convPath, "/", "\\");
  752. #endif
  753. return convPath;
  754. }
  755. void cmGlobalNinjaGenerator::AddCXXCompileCommand(
  756. const std::string &commandLine,
  757. const std::string &sourceFile)
  758. {
  759. // Compute Ninja's build file path.
  760. std::string buildFileDir =
  761. this->GetCMakeInstance()->GetHomeOutputDirectory();
  762. if (!this->CompileCommandsStream)
  763. {
  764. std::string buildFilePath = buildFileDir + "/compile_commands.json";
  765. // Get a stream where to generate things.
  766. this->CompileCommandsStream =
  767. new cmGeneratedFileStream(buildFilePath.c_str());
  768. *this->CompileCommandsStream << "[";
  769. } else {
  770. *this->CompileCommandsStream << "," << std::endl;
  771. }
  772. std::string sourceFileName = sourceFile;
  773. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str()))
  774. {
  775. sourceFileName = cmSystemTools::CollapseFullPath(
  776. sourceFileName,
  777. this->GetCMakeInstance()->GetHomeOutputDirectory());
  778. }
  779. *this->CompileCommandsStream << "\n{\n"
  780. << " \"directory\": \""
  781. << cmGlobalGenerator::EscapeJSON(buildFileDir) << "\",\n"
  782. << " \"command\": \""
  783. << cmGlobalGenerator::EscapeJSON(commandLine) << "\",\n"
  784. << " \"file\": \""
  785. << cmGlobalGenerator::EscapeJSON(sourceFileName) << "\"\n"
  786. << "}";
  787. }
  788. void cmGlobalNinjaGenerator::CloseCompileCommandsStream()
  789. {
  790. if (this->CompileCommandsStream)
  791. {
  792. *this->CompileCommandsStream << "\n]";
  793. delete this->CompileCommandsStream;
  794. this->CompileCommandsStream = 0;
  795. }
  796. }
  797. void cmGlobalNinjaGenerator::WriteDisclaimer(std::ostream& os)
  798. {
  799. os
  800. << "# CMAKE generated file: DO NOT EDIT!\n"
  801. << "# Generated by \"" << this->GetName() << "\""
  802. << " Generator, CMake Version "
  803. << cmVersion::GetMajorVersion() << "."
  804. << cmVersion::GetMinorVersion() << "\n\n";
  805. }
  806. void cmGlobalNinjaGenerator::AddDependencyToAll(cmGeneratorTarget* target)
  807. {
  808. this->AppendTargetOutputs(target, this->AllDependencies);
  809. }
  810. void cmGlobalNinjaGenerator::AddDependencyToAll(const std::string& input)
  811. {
  812. this->AllDependencies.push_back(input);
  813. }
  814. void cmGlobalNinjaGenerator::WriteAssumedSourceDependencies()
  815. {
  816. for (std::map<std::string, std::set<std::string> >::iterator
  817. i = this->AssumedSourceDependencies.begin();
  818. i != this->AssumedSourceDependencies.end(); ++i) {
  819. cmNinjaDeps deps;
  820. std::copy(i->second.begin(), i->second.end(), std::back_inserter(deps));
  821. WriteCustomCommandBuild(/*command=*/"", /*description=*/"",
  822. "Assume dependencies for generated source file.",
  823. /*uses_terminal*/false,
  824. /*restat*/true,
  825. cmNinjaDeps(1, i->first), deps);
  826. }
  827. }
  828. void
  829. cmGlobalNinjaGenerator
  830. ::AppendTargetOutputs(cmGeneratorTarget const* target, cmNinjaDeps& outputs)
  831. {
  832. std::string configName =
  833. target->Target->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE");
  834. // for frameworks, we want the real name, not smple name
  835. // frameworks always appear versioned, and the build.ninja
  836. // will always attempt to manage symbolic links instead
  837. // of letting cmOSXBundleGenerator do it.
  838. bool realname = target->IsFrameworkOnApple();
  839. switch (target->GetType()) {
  840. case cmState::EXECUTABLE:
  841. case cmState::SHARED_LIBRARY:
  842. case cmState::STATIC_LIBRARY:
  843. case cmState::MODULE_LIBRARY:
  844. {
  845. outputs.push_back(this->ConvertToNinjaPath(
  846. target->GetFullPath(configName, false, realname)));
  847. break;
  848. }
  849. case cmState::OBJECT_LIBRARY:
  850. case cmState::UTILITY: {
  851. std::string path = this->ConvertToNinjaPath(
  852. target->GetLocalGenerator()->GetCurrentBinaryDirectory());
  853. if (path.empty() || path == ".")
  854. outputs.push_back(target->GetName());
  855. else {
  856. path += "/";
  857. path += target->GetName();
  858. outputs.push_back(path);
  859. }
  860. break;
  861. }
  862. case cmState::GLOBAL_TARGET:
  863. // Always use the target in HOME instead of an unused duplicate in a
  864. // subdirectory.
  865. outputs.push_back(target->GetName());
  866. break;
  867. default:
  868. return;
  869. }
  870. }
  871. void
  872. cmGlobalNinjaGenerator
  873. ::AppendTargetDepends(cmGeneratorTarget const* target, cmNinjaDeps& outputs)
  874. {
  875. if (target->GetType() == cmState::GLOBAL_TARGET) {
  876. // Global targets only depend on other utilities, which may not appear in
  877. // the TargetDepends set (e.g. "all").
  878. std::set<std::string> const& utils = target->GetUtilities();
  879. std::copy(utils.begin(), utils.end(), std::back_inserter(outputs));
  880. } else {
  881. cmTargetDependSet const& targetDeps = this->GetTargetDirectDepends(target);
  882. for (cmTargetDependSet::const_iterator i = targetDeps.begin();
  883. i != targetDeps.end(); ++i)
  884. {
  885. if ((*i)->GetType() == cmState::INTERFACE_LIBRARY)
  886. {
  887. continue;
  888. }
  889. this->AppendTargetOutputs(*i, outputs);
  890. }
  891. }
  892. }
  893. void cmGlobalNinjaGenerator::AddTargetAlias(const std::string& alias,
  894. cmGeneratorTarget* target) {
  895. cmNinjaDeps outputs;
  896. this->AppendTargetOutputs(target, outputs);
  897. // Mark the target's outputs as ambiguous to ensure that no other target uses
  898. // the output as an alias.
  899. for (cmNinjaDeps::iterator i = outputs.begin(); i != outputs.end(); ++i)
  900. TargetAliases[*i] = 0;
  901. // Insert the alias into the map. If the alias was already present in the
  902. // map and referred to another target, mark it as ambiguous.
  903. std::pair<TargetAliasMap::iterator, bool> newAlias =
  904. TargetAliases.insert(std::make_pair(alias, target));
  905. if (newAlias.second && newAlias.first->second != target)
  906. newAlias.first->second = 0;
  907. }
  908. void cmGlobalNinjaGenerator::WriteTargetAliases(std::ostream& os)
  909. {
  910. cmGlobalNinjaGenerator::WriteDivider(os);
  911. os << "# Target aliases.\n\n";
  912. for (TargetAliasMap::const_iterator i = TargetAliases.begin();
  913. i != TargetAliases.end(); ++i) {
  914. // Don't write ambiguous aliases.
  915. if (!i->second)
  916. continue;
  917. cmNinjaDeps deps;
  918. this->AppendTargetOutputs(i->second, deps);
  919. this->WritePhonyBuild(os,
  920. "",
  921. cmNinjaDeps(1, i->first),
  922. deps);
  923. }
  924. }
  925. void cmGlobalNinjaGenerator::WriteUnknownExplicitDependencies(std::ostream& os)
  926. {
  927. if (!this->ComputingUnknownDependencies)
  928. {
  929. return;
  930. }
  931. // We need to collect the set of known build outputs.
  932. // Start with those generated by WriteBuild calls.
  933. // No other method needs this so we can take ownership
  934. // of the set locally and throw it out when we are done.
  935. std::set<std::string> knownDependencies;
  936. knownDependencies.swap(this->CombinedBuildOutputs);
  937. //now write out the unknown explicit dependencies.
  938. //union the configured files, evaluations files and the CombinedBuildOutputs,
  939. //and then difference with CombinedExplicitDependencies to find the explicit
  940. //dependencies that we have no rule for
  941. cmGlobalNinjaGenerator::WriteDivider(os);
  942. os << "# Unknown Build Time Dependencies.\n"
  943. << "# Tell Ninja that they may appear as side effects of build rules\n"
  944. << "# otherwise ordered by order-only dependencies.\n\n";
  945. //get the list of files that cmake itself has generated as a
  946. //product of configuration.
  947. for (std::vector<cmLocalGenerator *>::const_iterator i =
  948. this->LocalGenerators.begin(); i != this->LocalGenerators.end(); ++i)
  949. {
  950. //get the vector of files created by this makefile and convert them
  951. //to ninja paths, which are all relative in respect to the build directory
  952. const std::vector<std::string>& files =
  953. (*i)->GetMakefile()->GetOutputFiles();
  954. typedef std::vector<std::string>::const_iterator vect_it;
  955. for(vect_it j = files.begin(); j != files.end(); ++j)
  956. {
  957. knownDependencies.insert( this->ConvertToNinjaPath( *j ) );
  958. }
  959. //get list files which are implicit dependencies as well and will be phony
  960. //for rebuild manifest
  961. std::vector<std::string> const& lf = (*i)->GetMakefile()->GetListFiles();
  962. typedef std::vector<std::string>::const_iterator vect_it;
  963. for(vect_it j = lf.begin(); j != lf.end(); ++j)
  964. {
  965. knownDependencies.insert( this->ConvertToNinjaPath( *j ) );
  966. }
  967. std::vector<cmGeneratorExpressionEvaluationFile*> const& ef =
  968. (*i)->GetMakefile()->GetEvaluationFiles();
  969. for(std::vector<cmGeneratorExpressionEvaluationFile*>::const_iterator
  970. li = ef.begin(); li != ef.end(); ++li)
  971. {
  972. //get all the files created by generator expressions and convert them
  973. //to ninja paths
  974. std::vector<std::string> evaluationFiles = (*li)->GetFiles();
  975. for(vect_it j = evaluationFiles.begin(); j != evaluationFiles.end(); ++j)
  976. {
  977. knownDependencies.insert( this->ConvertToNinjaPath( *j ) );
  978. }
  979. }
  980. }
  981. knownDependencies.insert( "CMakeCache.txt" );
  982. for(TargetAliasMap::const_iterator i= this->TargetAliases.begin();
  983. i != this->TargetAliases.end();
  984. ++i)
  985. {
  986. knownDependencies.insert( this->ConvertToNinjaPath(i->first) );
  987. }
  988. //remove all source files we know will exist.
  989. typedef std::map<std::string, std::set<std::string> >::const_iterator map_it;
  990. for(map_it i = this->AssumedSourceDependencies.begin();
  991. i != this->AssumedSourceDependencies.end();
  992. ++i)
  993. {
  994. knownDependencies.insert( this->ConvertToNinjaPath(i->first) );
  995. }
  996. //now we difference with CombinedCustomCommandExplicitDependencies to find
  997. //the list of items we know nothing about.
  998. //We have encoded all the paths in CombinedCustomCommandExplicitDependencies
  999. //and knownDependencies so no matter if unix or windows paths they
  1000. //should all match now.
  1001. std::vector<std::string> unknownExplicitDepends;
  1002. this->CombinedCustomCommandExplicitDependencies.erase("all");
  1003. std::set_difference(this->CombinedCustomCommandExplicitDependencies.begin(),
  1004. this->CombinedCustomCommandExplicitDependencies.end(),
  1005. knownDependencies.begin(),
  1006. knownDependencies.end(),
  1007. std::back_inserter(unknownExplicitDepends));
  1008. std::string const rootBuildDirectory =
  1009. this->GetCMakeInstance()->GetHomeOutputDirectory();
  1010. bool const inSourceBuild =
  1011. (rootBuildDirectory == this->GetCMakeInstance()->GetHomeDirectory());
  1012. std::vector<std::string> warnExplicitDepends;
  1013. for (std::vector<std::string>::const_iterator
  1014. i = unknownExplicitDepends.begin();
  1015. i != unknownExplicitDepends.end();
  1016. ++i)
  1017. {
  1018. //verify the file is in the build directory
  1019. std::string const absDepPath = cmSystemTools::CollapseFullPath(
  1020. *i, rootBuildDirectory.c_str());
  1021. bool const inBuildDir = cmSystemTools::IsSubDirectory(absDepPath,
  1022. rootBuildDirectory);
  1023. if(inBuildDir)
  1024. {
  1025. cmNinjaDeps deps(1,*i);
  1026. this->WritePhonyBuild(os,
  1027. "",
  1028. deps,
  1029. cmNinjaDeps());
  1030. if (this->PolicyCMP0058 == cmPolicies::WARN &&
  1031. !inSourceBuild && warnExplicitDepends.size() < 10)
  1032. {
  1033. warnExplicitDepends.push_back(*i);
  1034. }
  1035. }
  1036. }
  1037. if (!warnExplicitDepends.empty())
  1038. {
  1039. std::ostringstream w;
  1040. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0058) << "\n"
  1041. "This project specifies custom command DEPENDS on files "
  1042. "in the build tree that are not specified as the OUTPUT or "
  1043. "BYPRODUCTS of any add_custom_command or add_custom_target:\n"
  1044. " " << cmJoin(warnExplicitDepends, "\n ") <<
  1045. "\n"
  1046. "For compatibility with versions of CMake that did not have "
  1047. "the BYPRODUCTS option, CMake is generating phony rules for "
  1048. "such files to convince 'ninja' to build."
  1049. "\n"
  1050. "Project authors should add the missing BYPRODUCTS or OUTPUT "
  1051. "options to the custom commands that produce these files."
  1052. ;
  1053. this->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  1054. }
  1055. }
  1056. void cmGlobalNinjaGenerator::WriteBuiltinTargets(std::ostream& os)
  1057. {
  1058. // Write headers.
  1059. cmGlobalNinjaGenerator::WriteDivider(os);
  1060. os << "# Built-in targets\n\n";
  1061. this->WriteTargetAll(os);
  1062. this->WriteTargetRebuildManifest(os);
  1063. this->WriteTargetClean(os);
  1064. this->WriteTargetHelp(os);
  1065. }
  1066. void cmGlobalNinjaGenerator::WriteTargetAll(std::ostream& os)
  1067. {
  1068. cmNinjaDeps outputs;
  1069. outputs.push_back("all");
  1070. this->WritePhonyBuild(os,
  1071. "The main all target.",
  1072. outputs,
  1073. this->AllDependencies);
  1074. cmGlobalNinjaGenerator::WriteDefault(os,
  1075. outputs,
  1076. "Make the all target the default.");
  1077. }
  1078. void cmGlobalNinjaGenerator::WriteTargetRebuildManifest(std::ostream& os)
  1079. {
  1080. cmLocalGenerator *lg = this->LocalGenerators[0];
  1081. std::ostringstream cmd;
  1082. cmd << lg->ConvertToOutputFormat(cmSystemTools::GetCMakeCommand(),
  1083. cmLocalGenerator::SHELL)
  1084. << " -H"
  1085. << lg->ConvertToOutputFormat(lg->GetSourceDirectory(),
  1086. cmLocalGenerator::SHELL)
  1087. << " -B"
  1088. << lg->ConvertToOutputFormat(lg->GetBinaryDirectory(),
  1089. cmLocalGenerator::SHELL);
  1090. WriteRule(*this->RulesFileStream,
  1091. "RERUN_CMAKE",
  1092. cmd.str(),
  1093. "Re-running CMake...",
  1094. "Rule for re-running cmake.",
  1095. /*depfile=*/ "",
  1096. /*deptype=*/ "",
  1097. /*rspfile=*/ "",
  1098. /*rspcontent*/ "",
  1099. /*restat=*/ "",
  1100. /*generator=*/ true);
  1101. cmNinjaDeps implicitDeps;
  1102. for(std::vector<cmLocalGenerator*>::const_iterator i =
  1103. this->LocalGenerators.begin(); i != this->LocalGenerators.end(); ++i)
  1104. {
  1105. std::vector<std::string> const& lf = (*i)->GetMakefile()->GetListFiles();
  1106. for(std::vector<std::string>::const_iterator fi = lf.begin();
  1107. fi != lf.end(); ++fi)
  1108. {
  1109. implicitDeps.push_back(this->ConvertToNinjaPath(*fi));
  1110. }
  1111. }
  1112. implicitDeps.push_back("CMakeCache.txt");
  1113. std::sort(implicitDeps.begin(), implicitDeps.end());
  1114. implicitDeps.erase(std::unique(implicitDeps.begin(), implicitDeps.end()),
  1115. implicitDeps.end());
  1116. cmNinjaVars variables;
  1117. // Use 'console' pool to get non buffered output of the CMake re-run call
  1118. // Available since Ninja 1.5
  1119. if(SupportsConsolePool())
  1120. {
  1121. variables["pool"] = "console";
  1122. }
  1123. this->WriteBuild(os,
  1124. "Re-run CMake if any of its inputs changed.",
  1125. "RERUN_CMAKE",
  1126. /*outputs=*/ cmNinjaDeps(1, NINJA_BUILD_FILE),
  1127. /*explicitDeps=*/ cmNinjaDeps(),
  1128. implicitDeps,
  1129. /*orderOnlyDeps=*/ cmNinjaDeps(),
  1130. variables);
  1131. this->WritePhonyBuild(os,
  1132. "A missing CMake input file is not an error.",
  1133. implicitDeps,
  1134. cmNinjaDeps());
  1135. }
  1136. std::string cmGlobalNinjaGenerator::ninjaCmd() const
  1137. {
  1138. cmLocalGenerator* lgen = this->LocalGenerators[0];
  1139. if (lgen) {
  1140. return lgen->ConvertToOutputFormat(this->NinjaCommand,
  1141. cmLocalGenerator::SHELL);
  1142. }
  1143. return "ninja";
  1144. }
  1145. bool cmGlobalNinjaGenerator::SupportsConsolePool() const
  1146. {
  1147. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  1148. this->NinjaVersion.c_str(),
  1149. RequiredNinjaVersionForConsolePool().c_str()) == false;
  1150. }
  1151. void cmGlobalNinjaGenerator::WriteTargetClean(std::ostream& os)
  1152. {
  1153. WriteRule(*this->RulesFileStream,
  1154. "CLEAN",
  1155. ninjaCmd() + " -t clean",
  1156. "Cleaning all built files...",
  1157. "Rule for cleaning all built files.",
  1158. /*depfile=*/ "",
  1159. /*deptype=*/ "",
  1160. /*rspfile=*/ "",
  1161. /*rspcontent*/ "",
  1162. /*restat=*/ "",
  1163. /*generator=*/ false);
  1164. WriteBuild(os,
  1165. "Clean all the built files.",
  1166. "CLEAN",
  1167. /*outputs=*/ cmNinjaDeps(1, "clean"),
  1168. /*explicitDeps=*/ cmNinjaDeps(),
  1169. /*implicitDeps=*/ cmNinjaDeps(),
  1170. /*orderOnlyDeps=*/ cmNinjaDeps(),
  1171. /*variables=*/ cmNinjaVars());
  1172. }
  1173. void cmGlobalNinjaGenerator::WriteTargetHelp(std::ostream& os)
  1174. {
  1175. WriteRule(*this->RulesFileStream,
  1176. "HELP",
  1177. ninjaCmd() + " -t targets",
  1178. "All primary targets available:",
  1179. "Rule for printing all primary targets available.",
  1180. /*depfile=*/ "",
  1181. /*deptype=*/ "",
  1182. /*rspfile=*/ "",
  1183. /*rspcontent*/ "",
  1184. /*restat=*/ "",
  1185. /*generator=*/ false);
  1186. WriteBuild(os,
  1187. "Print all primary targets available.",
  1188. "HELP",
  1189. /*outputs=*/ cmNinjaDeps(1, "help"),
  1190. /*explicitDeps=*/ cmNinjaDeps(),
  1191. /*implicitDeps=*/ cmNinjaDeps(),
  1192. /*orderOnlyDeps=*/ cmNinjaDeps(),
  1193. /*variables=*/ cmNinjaVars());
  1194. }