cmGlobalNinjaGenerator.cxx 42 KB

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