cmGlobalNinjaGenerator.cxx 42 KB

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