cmGlobalNinjaGenerator.cxx 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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. void cmGlobalNinjaGenerator::AddCXXCompileCommand(
  726. const std::string &commandLine,
  727. const std::string &sourceFile)
  728. {
  729. // Compute Ninja's build file path.
  730. std::string buildFileDir =
  731. this->GetCMakeInstance()->GetHomeOutputDirectory();
  732. if (!this->CompileCommandsStream)
  733. {
  734. std::string buildFilePath = buildFileDir + "/compile_commands.json";
  735. // Get a stream where to generate things.
  736. this->CompileCommandsStream =
  737. new cmGeneratedFileStream(buildFilePath.c_str());
  738. *this->CompileCommandsStream << "[";
  739. } else {
  740. *this->CompileCommandsStream << "," << std::endl;
  741. }
  742. std::string sourceFileName = sourceFile;
  743. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str()))
  744. {
  745. sourceFileName = cmSystemTools::CollapseFullPath(
  746. sourceFileName,
  747. this->GetCMakeInstance()->GetHomeOutputDirectory());
  748. }
  749. *this->CompileCommandsStream << "\n{\n"
  750. << " \"directory\": \""
  751. << cmGlobalGenerator::EscapeJSON(buildFileDir) << "\",\n"
  752. << " \"command\": \""
  753. << cmGlobalGenerator::EscapeJSON(commandLine) << "\",\n"
  754. << " \"file\": \""
  755. << cmGlobalGenerator::EscapeJSON(sourceFileName) << "\"\n"
  756. << "}";
  757. }
  758. void cmGlobalNinjaGenerator::CloseCompileCommandsStream()
  759. {
  760. if (this->CompileCommandsStream)
  761. {
  762. *this->CompileCommandsStream << "\n]";
  763. delete this->CompileCommandsStream;
  764. this->CompileCommandsStream = 0;
  765. }
  766. }
  767. void cmGlobalNinjaGenerator::WriteDisclaimer(std::ostream& os)
  768. {
  769. os
  770. << "# CMAKE generated file: DO NOT EDIT!\n"
  771. << "# Generated by \"" << this->GetName() << "\""
  772. << " Generator, CMake Version "
  773. << cmVersion::GetMajorVersion() << "."
  774. << cmVersion::GetMinorVersion() << "\n\n";
  775. }
  776. void cmGlobalNinjaGenerator::AddDependencyToAll(cmTarget* target)
  777. {
  778. this->AppendTargetOutputs(target, this->AllDependencies);
  779. }
  780. void cmGlobalNinjaGenerator::AddDependencyToAll(const std::string& input)
  781. {
  782. this->AllDependencies.push_back(input);
  783. }
  784. void cmGlobalNinjaGenerator::WriteAssumedSourceDependencies()
  785. {
  786. for (std::map<std::string, std::set<std::string> >::iterator
  787. i = this->AssumedSourceDependencies.begin();
  788. i != this->AssumedSourceDependencies.end(); ++i) {
  789. cmNinjaDeps deps;
  790. std::copy(i->second.begin(), i->second.end(), std::back_inserter(deps));
  791. WriteCustomCommandBuild(/*command=*/"", /*description=*/"",
  792. "Assume dependencies for generated source file.",
  793. /*uses_terminal*/false,
  794. cmNinjaDeps(1, i->first), deps);
  795. }
  796. }
  797. void
  798. cmGlobalNinjaGenerator
  799. ::AppendTargetOutputs(cmTarget const* target, cmNinjaDeps& outputs)
  800. {
  801. std::string configName =
  802. target->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE");
  803. cmLocalNinjaGenerator *ng =
  804. static_cast<cmLocalNinjaGenerator *>(this->LocalGenerators[0]);
  805. // for frameworks, we want the real name, not smple name
  806. // frameworks always appear versioned, and the build.ninja
  807. // will always attempt to manage symbolic links instead
  808. // of letting cmOSXBundleGenerator do it.
  809. bool realname = target->IsFrameworkOnApple();
  810. switch (target->GetType()) {
  811. case cmTarget::EXECUTABLE:
  812. case cmTarget::SHARED_LIBRARY:
  813. case cmTarget::STATIC_LIBRARY:
  814. case cmTarget::MODULE_LIBRARY:
  815. {
  816. cmGeneratorTarget *gtgt = this->GetGeneratorTarget(target);
  817. outputs.push_back(ng->ConvertToNinjaPath(
  818. gtgt->GetFullPath(configName, false, realname)));
  819. break;
  820. }
  821. case cmTarget::OBJECT_LIBRARY:
  822. case cmTarget::UTILITY: {
  823. std::string path = ng->ConvertToNinjaPath(
  824. target->GetMakefile()->GetCurrentBinaryDirectory());
  825. if (path.empty() || path == ".")
  826. outputs.push_back(target->GetName());
  827. else {
  828. path += "/";
  829. path += target->GetName();
  830. outputs.push_back(path);
  831. }
  832. break;
  833. }
  834. case cmTarget::GLOBAL_TARGET:
  835. // Always use the target in HOME instead of an unused duplicate in a
  836. // subdirectory.
  837. outputs.push_back(target->GetName());
  838. break;
  839. default:
  840. return;
  841. }
  842. }
  843. void
  844. cmGlobalNinjaGenerator
  845. ::AppendTargetDepends(cmTarget const* target, cmNinjaDeps& outputs)
  846. {
  847. if (target->GetType() == cmTarget::GLOBAL_TARGET) {
  848. // Global targets only depend on other utilities, which may not appear in
  849. // the TargetDepends set (e.g. "all").
  850. std::set<std::string> const& utils = target->GetUtilities();
  851. std::copy(utils.begin(), utils.end(), std::back_inserter(outputs));
  852. } else {
  853. cmGeneratorTarget* gt = this->GetGeneratorTarget(target);
  854. cmTargetDependSet const& targetDeps = this->GetTargetDirectDepends(gt);
  855. for (cmTargetDependSet::const_iterator i = targetDeps.begin();
  856. i != targetDeps.end(); ++i)
  857. {
  858. if ((*i)->GetType() == cmTarget::INTERFACE_LIBRARY)
  859. {
  860. continue;
  861. }
  862. this->AppendTargetOutputs((*i)->Target, outputs);
  863. }
  864. }
  865. }
  866. void cmGlobalNinjaGenerator::AddTargetAlias(const std::string& alias,
  867. cmTarget* target) {
  868. cmNinjaDeps outputs;
  869. this->AppendTargetOutputs(target, outputs);
  870. // Mark the target's outputs as ambiguous to ensure that no other target uses
  871. // the output as an alias.
  872. for (cmNinjaDeps::iterator i = outputs.begin(); i != outputs.end(); ++i)
  873. TargetAliases[*i] = 0;
  874. // Insert the alias into the map. If the alias was already present in the
  875. // map and referred to another target, mark it as ambiguous.
  876. std::pair<TargetAliasMap::iterator, bool> newAlias =
  877. TargetAliases.insert(std::make_pair(alias, target));
  878. if (newAlias.second && newAlias.first->second != target)
  879. newAlias.first->second = 0;
  880. }
  881. void cmGlobalNinjaGenerator::WriteTargetAliases(std::ostream& os)
  882. {
  883. cmGlobalNinjaGenerator::WriteDivider(os);
  884. os << "# Target aliases.\n\n";
  885. for (TargetAliasMap::const_iterator i = TargetAliases.begin();
  886. i != TargetAliases.end(); ++i) {
  887. // Don't write ambiguous aliases.
  888. if (!i->second)
  889. continue;
  890. cmNinjaDeps deps;
  891. this->AppendTargetOutputs(i->second, deps);
  892. this->WritePhonyBuild(os,
  893. "",
  894. cmNinjaDeps(1, i->first),
  895. deps);
  896. }
  897. }
  898. void cmGlobalNinjaGenerator::WriteUnknownExplicitDependencies(std::ostream& os)
  899. {
  900. if (!this->ComputingUnknownDependencies)
  901. {
  902. return;
  903. }
  904. // We need to collect the set of known build outputs.
  905. // Start with those generated by WriteBuild calls.
  906. // No other method needs this so we can take ownership
  907. // of the set locally and throw it out when we are done.
  908. std::set<std::string> knownDependencies;
  909. knownDependencies.swap(this->CombinedBuildOutputs);
  910. //now write out the unknown explicit dependencies.
  911. //union the configured files, evaluations files and the CombinedBuildOutputs,
  912. //and then difference with CombinedExplicitDependencies to find the explicit
  913. //dependencies that we have no rule for
  914. cmGlobalNinjaGenerator::WriteDivider(os);
  915. os << "# Unknown Build Time Dependencies.\n"
  916. << "# Tell Ninja that they may appear as side effects of build rules\n"
  917. << "# otherwise ordered by order-only dependencies.\n\n";
  918. //get the list of files that cmake itself has generated as a
  919. //product of configuration.
  920. cmLocalNinjaGenerator *ng =
  921. static_cast<cmLocalNinjaGenerator *>(this->LocalGenerators[0]);
  922. for (std::vector<cmLocalGenerator *>::const_iterator i =
  923. this->LocalGenerators.begin(); i != this->LocalGenerators.end(); ++i)
  924. {
  925. //get the vector of files created by this makefile and convert them
  926. //to ninja paths, which are all relative in respect to the build directory
  927. const std::vector<std::string>& files =
  928. (*i)->GetMakefile()->GetOutputFiles();
  929. typedef std::vector<std::string>::const_iterator vect_it;
  930. for(vect_it j = files.begin(); j != files.end(); ++j)
  931. {
  932. knownDependencies.insert( ng->ConvertToNinjaPath( *j ) );
  933. }
  934. //get list files which are implicit dependencies as well and will be phony
  935. //for rebuild manifest
  936. std::vector<std::string> const& lf = (*i)->GetMakefile()->GetListFiles();
  937. typedef std::vector<std::string>::const_iterator vect_it;
  938. for(vect_it j = lf.begin(); j != lf.end(); ++j)
  939. {
  940. knownDependencies.insert( ng->ConvertToNinjaPath( *j ) );
  941. }
  942. std::vector<cmGeneratorExpressionEvaluationFile*> const& ef =
  943. (*i)->GetMakefile()->GetEvaluationFiles();
  944. for(std::vector<cmGeneratorExpressionEvaluationFile*>::const_iterator
  945. li = ef.begin(); li != ef.end(); ++li)
  946. {
  947. //get all the files created by generator expressions and convert them
  948. //to ninja paths
  949. std::vector<std::string> evaluationFiles = (*li)->GetFiles();
  950. for(vect_it j = evaluationFiles.begin(); j != evaluationFiles.end(); ++j)
  951. {
  952. knownDependencies.insert( ng->ConvertToNinjaPath( *j ) );
  953. }
  954. }
  955. }
  956. knownDependencies.insert( "CMakeCache.txt" );
  957. for(TargetAliasMap::const_iterator i= this->TargetAliases.begin();
  958. i != this->TargetAliases.end();
  959. ++i)
  960. {
  961. knownDependencies.insert( ng->ConvertToNinjaPath(i->first) );
  962. }
  963. //remove all source files we know will exist.
  964. typedef std::map<std::string, std::set<std::string> >::const_iterator map_it;
  965. for(map_it i = this->AssumedSourceDependencies.begin();
  966. i != this->AssumedSourceDependencies.end();
  967. ++i)
  968. {
  969. knownDependencies.insert( ng->ConvertToNinjaPath(i->first) );
  970. }
  971. //now we difference with CombinedCustomCommandExplicitDependencies to find
  972. //the list of items we know nothing about.
  973. //We have encoded all the paths in CombinedCustomCommandExplicitDependencies
  974. //and knownDependencies so no matter if unix or windows paths they
  975. //should all match now.
  976. std::vector<std::string> unknownExplicitDepends;
  977. this->CombinedCustomCommandExplicitDependencies.erase("all");
  978. std::set_difference(this->CombinedCustomCommandExplicitDependencies.begin(),
  979. this->CombinedCustomCommandExplicitDependencies.end(),
  980. knownDependencies.begin(),
  981. knownDependencies.end(),
  982. std::back_inserter(unknownExplicitDepends));
  983. std::string const rootBuildDirectory =
  984. this->GetCMakeInstance()->GetHomeOutputDirectory();
  985. bool const inSourceBuild =
  986. (rootBuildDirectory == this->GetCMakeInstance()->GetHomeDirectory());
  987. std::vector<std::string> warnExplicitDepends;
  988. for (std::vector<std::string>::const_iterator
  989. i = unknownExplicitDepends.begin();
  990. i != unknownExplicitDepends.end();
  991. ++i)
  992. {
  993. //verify the file is in the build directory
  994. std::string const absDepPath = cmSystemTools::CollapseFullPath(
  995. *i, rootBuildDirectory.c_str());
  996. bool const inBuildDir = cmSystemTools::IsSubDirectory(absDepPath,
  997. rootBuildDirectory);
  998. if(inBuildDir)
  999. {
  1000. cmNinjaDeps deps(1,*i);
  1001. this->WritePhonyBuild(os,
  1002. "",
  1003. deps,
  1004. cmNinjaDeps());
  1005. if (this->PolicyCMP0058 == cmPolicies::WARN &&
  1006. !inSourceBuild && warnExplicitDepends.size() < 10)
  1007. {
  1008. warnExplicitDepends.push_back(*i);
  1009. }
  1010. }
  1011. }
  1012. if (!warnExplicitDepends.empty())
  1013. {
  1014. std::ostringstream w;
  1015. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0058) << "\n"
  1016. "This project specifies custom command DEPENDS on files "
  1017. "in the build tree that are not specified as the OUTPUT or "
  1018. "BYPRODUCTS of any add_custom_command or add_custom_target:\n"
  1019. " " << cmJoin(warnExplicitDepends, "\n ") <<
  1020. "\n"
  1021. "For compatibility with versions of CMake that did not have "
  1022. "the BYPRODUCTS option, CMake is generating phony rules for "
  1023. "such files to convince 'ninja' to build."
  1024. "\n"
  1025. "Project authors should add the missing BYPRODUCTS or OUTPUT "
  1026. "options to the custom commands that produce these files."
  1027. ;
  1028. this->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  1029. }
  1030. }
  1031. void cmGlobalNinjaGenerator::WriteBuiltinTargets(std::ostream& os)
  1032. {
  1033. // Write headers.
  1034. cmGlobalNinjaGenerator::WriteDivider(os);
  1035. os << "# Built-in targets\n\n";
  1036. this->WriteTargetAll(os);
  1037. this->WriteTargetRebuildManifest(os);
  1038. this->WriteTargetClean(os);
  1039. this->WriteTargetHelp(os);
  1040. }
  1041. void cmGlobalNinjaGenerator::WriteTargetAll(std::ostream& os)
  1042. {
  1043. cmNinjaDeps outputs;
  1044. outputs.push_back("all");
  1045. this->WritePhonyBuild(os,
  1046. "The main all target.",
  1047. outputs,
  1048. this->AllDependencies);
  1049. cmGlobalNinjaGenerator::WriteDefault(os,
  1050. outputs,
  1051. "Make the all target the default.");
  1052. }
  1053. void cmGlobalNinjaGenerator::WriteTargetRebuildManifest(std::ostream& os)
  1054. {
  1055. cmLocalGenerator *lg = this->LocalGenerators[0];
  1056. cmMakefile* mfRoot = lg->GetMakefile();
  1057. std::ostringstream cmd;
  1058. cmd << lg->ConvertToOutputFormat(cmSystemTools::GetCMakeCommand(),
  1059. cmLocalGenerator::SHELL)
  1060. << " -H"
  1061. << lg->ConvertToOutputFormat(mfRoot->GetHomeDirectory(),
  1062. cmLocalGenerator::SHELL)
  1063. << " -B"
  1064. << lg->ConvertToOutputFormat(mfRoot->GetHomeOutputDirectory(),
  1065. cmLocalGenerator::SHELL);
  1066. WriteRule(*this->RulesFileStream,
  1067. "RERUN_CMAKE",
  1068. cmd.str(),
  1069. "Re-running CMake...",
  1070. "Rule for re-running cmake.",
  1071. /*depfile=*/ "",
  1072. /*deptype=*/ "",
  1073. /*rspfile=*/ "",
  1074. /*rspcontent*/ "",
  1075. /*restat=*/ "",
  1076. /*generator=*/ true);
  1077. cmLocalNinjaGenerator *ng = static_cast<cmLocalNinjaGenerator *>(lg);
  1078. cmNinjaDeps implicitDeps;
  1079. for(std::vector<cmLocalGenerator*>::const_iterator i =
  1080. this->LocalGenerators.begin(); i != this->LocalGenerators.end(); ++i)
  1081. {
  1082. std::vector<std::string> const& lf = (*i)->GetMakefile()->GetListFiles();
  1083. for(std::vector<std::string>::const_iterator fi = lf.begin();
  1084. fi != lf.end(); ++fi)
  1085. {
  1086. implicitDeps.push_back(ng->ConvertToNinjaPath(*fi));
  1087. }
  1088. }
  1089. implicitDeps.push_back("CMakeCache.txt");
  1090. std::sort(implicitDeps.begin(), implicitDeps.end());
  1091. implicitDeps.erase(std::unique(implicitDeps.begin(), implicitDeps.end()),
  1092. implicitDeps.end());
  1093. cmNinjaVars variables;
  1094. // Use 'console' pool to get non buffered output of the CMake re-run call
  1095. // Available since Ninja 1.5
  1096. if(SupportsConsolePool())
  1097. {
  1098. variables["pool"] = "console";
  1099. }
  1100. this->WriteBuild(os,
  1101. "Re-run CMake if any of its inputs changed.",
  1102. "RERUN_CMAKE",
  1103. /*outputs=*/ cmNinjaDeps(1, NINJA_BUILD_FILE),
  1104. /*explicitDeps=*/ cmNinjaDeps(),
  1105. implicitDeps,
  1106. /*orderOnlyDeps=*/ cmNinjaDeps(),
  1107. variables);
  1108. this->WritePhonyBuild(os,
  1109. "A missing CMake input file is not an error.",
  1110. implicitDeps,
  1111. cmNinjaDeps());
  1112. }
  1113. std::string cmGlobalNinjaGenerator::ninjaCmd() const
  1114. {
  1115. cmLocalGenerator* lgen = this->LocalGenerators[0];
  1116. if (lgen) {
  1117. return lgen->ConvertToOutputFormat(
  1118. lgen->GetMakefile()->GetRequiredDefinition("CMAKE_MAKE_PROGRAM"),
  1119. cmLocalGenerator::SHELL);
  1120. }
  1121. return "ninja";
  1122. }
  1123. std::string cmGlobalNinjaGenerator::CurrentNinjaVersion() const
  1124. {
  1125. std::string version;
  1126. std::string command = ninjaCmd() + " --version";
  1127. cmSystemTools::RunSingleCommand(command.c_str(),
  1128. &version, 0, 0, 0,
  1129. cmSystemTools::OUTPUT_NONE);
  1130. return cmSystemTools::TrimWhitespace(version);
  1131. }
  1132. bool cmGlobalNinjaGenerator::SupportsConsolePool() const
  1133. {
  1134. return cmSystemTools::VersionCompare(cmSystemTools::OP_LESS,
  1135. CurrentNinjaVersion().c_str(),
  1136. RequiredNinjaVersionForConsolePool().c_str()) == false;
  1137. }
  1138. void cmGlobalNinjaGenerator::WriteTargetClean(std::ostream& os)
  1139. {
  1140. WriteRule(*this->RulesFileStream,
  1141. "CLEAN",
  1142. ninjaCmd() + " -t clean",
  1143. "Cleaning all built files...",
  1144. "Rule for cleaning all built files.",
  1145. /*depfile=*/ "",
  1146. /*deptype=*/ "",
  1147. /*rspfile=*/ "",
  1148. /*rspcontent*/ "",
  1149. /*restat=*/ "",
  1150. /*generator=*/ false);
  1151. WriteBuild(os,
  1152. "Clean all the built files.",
  1153. "CLEAN",
  1154. /*outputs=*/ cmNinjaDeps(1, "clean"),
  1155. /*explicitDeps=*/ cmNinjaDeps(),
  1156. /*implicitDeps=*/ cmNinjaDeps(),
  1157. /*orderOnlyDeps=*/ cmNinjaDeps(),
  1158. /*variables=*/ cmNinjaVars());
  1159. }
  1160. void cmGlobalNinjaGenerator::WriteTargetHelp(std::ostream& os)
  1161. {
  1162. WriteRule(*this->RulesFileStream,
  1163. "HELP",
  1164. ninjaCmd() + " -t targets",
  1165. "All primary targets available:",
  1166. "Rule for printing all primary targets available.",
  1167. /*depfile=*/ "",
  1168. /*deptype=*/ "",
  1169. /*rspfile=*/ "",
  1170. /*rspcontent*/ "",
  1171. /*restat=*/ "",
  1172. /*generator=*/ false);
  1173. WriteBuild(os,
  1174. "Print all primary targets available.",
  1175. "HELP",
  1176. /*outputs=*/ cmNinjaDeps(1, "help"),
  1177. /*explicitDeps=*/ cmNinjaDeps(),
  1178. /*implicitDeps=*/ cmNinjaDeps(),
  1179. /*orderOnlyDeps=*/ cmNinjaDeps(),
  1180. /*variables=*/ cmNinjaVars());
  1181. }