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