cmGlobalNinjaGenerator.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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 "cmGlobalNinjaGenerator.h"
  12. #include "cmLocalNinjaGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGeneratorTarget.h"
  16. #include "cmVersion.h"
  17. #include <algorithm>
  18. const char* cmGlobalNinjaGenerator::NINJA_BUILD_FILE = "build.ninja";
  19. const char* cmGlobalNinjaGenerator::NINJA_RULES_FILE = "rules.ninja";
  20. const char* cmGlobalNinjaGenerator::INDENT = " ";
  21. void cmGlobalNinjaGenerator::Indent(std::ostream& os, int count)
  22. {
  23. for(int i = 0; i < count; ++i)
  24. os << cmGlobalNinjaGenerator::INDENT;
  25. }
  26. void cmGlobalNinjaGenerator::WriteDivider(std::ostream& os)
  27. {
  28. os
  29. << "# ======================================"
  30. << "=======================================\n";
  31. }
  32. void cmGlobalNinjaGenerator::WriteComment(std::ostream& os,
  33. const std::string& comment)
  34. {
  35. if (comment.empty())
  36. return;
  37. std::string replace = comment;
  38. std::string::size_type lpos = 0;
  39. std::string::size_type rpos;
  40. os << "\n#############################################\n";
  41. while((rpos = replace.find('\n', lpos)) != std::string::npos)
  42. {
  43. os << "# " << replace.substr(lpos, rpos - lpos) << "\n";
  44. lpos = rpos + 1;
  45. }
  46. os << "# " << replace.substr(lpos) << "\n\n";
  47. }
  48. static bool IsIdentChar(char c)
  49. {
  50. return
  51. ('a' <= c && c <= 'z') ||
  52. ('+' <= c && c <= '9') || // +,-./ and numbers
  53. ('A' <= c && c <= 'Z') ||
  54. (c == '_') || (c == '$') || (c == '\\') ||
  55. (c == ' ') || (c == ':');
  56. }
  57. std::string cmGlobalNinjaGenerator::EncodeIdent(const std::string &ident,
  58. std::ostream &vars) {
  59. if (std::find_if(ident.begin(), ident.end(),
  60. std::not1(std::ptr_fun(IsIdentChar))) != ident.end()) {
  61. static unsigned VarNum = 0;
  62. cmOStringStream names;
  63. names << "ident" << VarNum++;
  64. vars << names.str() << " = " << ident << "\n";
  65. return "$" + names.str();
  66. } else {
  67. std::string result = ident;
  68. cmSystemTools::ReplaceString(result, " ", "$ ");
  69. cmSystemTools::ReplaceString(result, ":", "$:");
  70. return result;
  71. }
  72. }
  73. std::string cmGlobalNinjaGenerator::EncodeLiteral(const std::string &lit)
  74. {
  75. std::string result = lit;
  76. cmSystemTools::ReplaceString(result, "$", "$$");
  77. cmSystemTools::ReplaceString(result, "\n", "$\n");
  78. return result;
  79. }
  80. std::string cmGlobalNinjaGenerator::EncodePath(const std::string &path)
  81. {
  82. std::string result = path;
  83. #ifdef _WIN32
  84. if(UsingMinGW)
  85. cmSystemTools::ReplaceString(result, "\\", "/");
  86. else
  87. cmSystemTools::ReplaceString(result, "/", "\\");
  88. #endif
  89. return EncodeLiteral(result);
  90. }
  91. void cmGlobalNinjaGenerator::WriteBuild(std::ostream& os,
  92. const std::string& comment,
  93. const std::string& rule,
  94. const cmNinjaDeps& outputs,
  95. const cmNinjaDeps& explicitDeps,
  96. const cmNinjaDeps& implicitDeps,
  97. const cmNinjaDeps& orderOnlyDeps,
  98. const cmNinjaVars& variables,
  99. const std::string& rspfile,
  100. int cmdLineLimit)
  101. {
  102. // Make sure there is a rule.
  103. if(rule.empty())
  104. {
  105. cmSystemTools::Error("No rule for WriteBuildStatement! called "
  106. "with comment: ",
  107. comment.c_str());
  108. return;
  109. }
  110. // Make sure there is at least one output file.
  111. if(outputs.empty())
  112. {
  113. cmSystemTools::Error("No output files for WriteBuildStatement! called "
  114. "with comment: ",
  115. comment.c_str());
  116. return;
  117. }
  118. cmGlobalNinjaGenerator::WriteComment(os, comment);
  119. cmOStringStream arguments;
  120. // TODO: Better formatting for when there are multiple input/output files.
  121. // Write explicit dependencies.
  122. for(cmNinjaDeps::const_iterator i = explicitDeps.begin();
  123. i != explicitDeps.end();
  124. ++i)
  125. arguments << " " << EncodeIdent(EncodePath(*i), os);
  126. // Write implicit dependencies.
  127. if(!implicitDeps.empty())
  128. {
  129. arguments << " |";
  130. for(cmNinjaDeps::const_iterator i = implicitDeps.begin();
  131. i != implicitDeps.end();
  132. ++i)
  133. arguments << " " << EncodeIdent(EncodePath(*i), os);
  134. }
  135. // Write order-only dependencies.
  136. if(!orderOnlyDeps.empty())
  137. {
  138. arguments << " ||";
  139. for(cmNinjaDeps::const_iterator i = orderOnlyDeps.begin();
  140. i != orderOnlyDeps.end();
  141. ++i)
  142. arguments << " " << EncodeIdent(EncodePath(*i), os);
  143. }
  144. arguments << "\n";
  145. cmOStringStream build;
  146. // Write outputs files.
  147. build << "build";
  148. for(cmNinjaDeps::const_iterator i = outputs.begin();
  149. i != outputs.end(); ++i)
  150. build << " " << EncodeIdent(EncodePath(*i), os);
  151. build << ":";
  152. // Write the rule.
  153. build << " " << rule;
  154. // Write the variables bound to this build statement.
  155. cmOStringStream variable_assignments;
  156. for(cmNinjaVars::const_iterator i = variables.begin();
  157. i != variables.end(); ++i)
  158. cmGlobalNinjaGenerator::WriteVariable(variable_assignments,
  159. i->first, i->second, "", 1);
  160. // check if a response file rule should be used
  161. std::string buildstr = build.str();
  162. std::string assignments = variable_assignments.str();
  163. const std::string args = arguments.str();
  164. if (cmdLineLimit > 0
  165. && args.size() + buildstr.size() + assignments.size()
  166. > (size_t) cmdLineLimit) {
  167. buildstr += "_RSP_FILE";
  168. variable_assignments.clear();
  169. cmGlobalNinjaGenerator::WriteVariable(variable_assignments,
  170. "RSP_FILE", rspfile, "", 1);
  171. assignments += variable_assignments.str();
  172. }
  173. os << buildstr << args << assignments;
  174. }
  175. void cmGlobalNinjaGenerator::WritePhonyBuild(std::ostream& os,
  176. const std::string& comment,
  177. const cmNinjaDeps& outputs,
  178. const cmNinjaDeps& explicitDeps,
  179. const cmNinjaDeps& implicitDeps,
  180. const cmNinjaDeps& orderOnlyDeps,
  181. const cmNinjaVars& variables)
  182. {
  183. cmGlobalNinjaGenerator::WriteBuild(os,
  184. comment,
  185. "phony",
  186. outputs,
  187. explicitDeps,
  188. implicitDeps,
  189. orderOnlyDeps,
  190. variables);
  191. }
  192. void cmGlobalNinjaGenerator::AddCustomCommandRule()
  193. {
  194. this->AddRule("CUSTOM_COMMAND",
  195. "$COMMAND",
  196. "$DESC",
  197. "Rule for running custom commands.",
  198. /*depfile*/ "",
  199. /*rspfile*/ "",
  200. /*rspcontent*/ "",
  201. /*restat*/ true);
  202. }
  203. void
  204. cmGlobalNinjaGenerator::WriteCustomCommandBuild(const std::string& command,
  205. const std::string& description,
  206. const std::string& comment,
  207. const cmNinjaDeps& outputs,
  208. const cmNinjaDeps& deps,
  209. const cmNinjaDeps& orderOnlyDeps)
  210. {
  211. std::string cmd = command;
  212. #ifdef _WIN32
  213. if (cmd.empty())
  214. // TODO Shouldn't an empty command be handled by ninja?
  215. cmd = "cmd.exe /c";
  216. #endif
  217. this->AddCustomCommandRule();
  218. cmNinjaVars vars;
  219. vars["COMMAND"] = cmd;
  220. vars["DESC"] = EncodeLiteral(description);
  221. cmGlobalNinjaGenerator::WriteBuild(*this->BuildFileStream,
  222. comment,
  223. "CUSTOM_COMMAND",
  224. outputs,
  225. deps,
  226. cmNinjaDeps(),
  227. orderOnlyDeps,
  228. vars);
  229. }
  230. void
  231. cmGlobalNinjaGenerator::AddMacOSXContentRule()
  232. {
  233. cmLocalGenerator *lg = this->LocalGenerators[0];
  234. cmMakefile* mfRoot = lg->GetMakefile();
  235. cmOStringStream cmd;
  236. cmd << lg->ConvertToOutputFormat(
  237. mfRoot->GetRequiredDefinition("CMAKE_COMMAND"),
  238. cmLocalGenerator::SHELL)
  239. << " -E copy $in $out";
  240. this->AddRule("COPY_OSX_CONTENT",
  241. cmd.str(),
  242. "Copying OS X Content $out",
  243. "Rule for copying OS X bundle content file."
  244. /*depfile*/ "",
  245. /*rspfile*/ "");
  246. }
  247. void
  248. cmGlobalNinjaGenerator::WriteMacOSXContentBuild(const std::string& input,
  249. const std::string& output)
  250. {
  251. this->AddMacOSXContentRule();
  252. cmNinjaDeps outputs;
  253. outputs.push_back(output);
  254. cmNinjaDeps deps;
  255. deps.push_back(input);
  256. cmNinjaVars vars;
  257. cmGlobalNinjaGenerator::WriteBuild(*this->BuildFileStream,
  258. "",
  259. "COPY_OSX_CONTENT",
  260. outputs,
  261. deps,
  262. cmNinjaDeps(),
  263. cmNinjaDeps(),
  264. cmNinjaVars());
  265. }
  266. void cmGlobalNinjaGenerator::WriteRule(std::ostream& os,
  267. const std::string& name,
  268. const std::string& command,
  269. const std::string& description,
  270. const std::string& comment,
  271. const std::string& depfile,
  272. const std::string& rspfile,
  273. const std::string& rspcontent,
  274. bool restat,
  275. bool generator)
  276. {
  277. // Make sure the rule has a name.
  278. if(name.empty())
  279. {
  280. cmSystemTools::Error("No name given for WriteRuleStatement! called "
  281. "with comment: ",
  282. comment.c_str());
  283. return;
  284. }
  285. // Make sure a command is given.
  286. if(command.empty())
  287. {
  288. cmSystemTools::Error("No command given for WriteRuleStatement! called "
  289. "with comment: ",
  290. comment.c_str());
  291. return;
  292. }
  293. cmGlobalNinjaGenerator::WriteComment(os, comment);
  294. // Write the rule.
  295. os << "rule " << name << "\n";
  296. // Write the depfile if any.
  297. if(!depfile.empty())
  298. {
  299. cmGlobalNinjaGenerator::Indent(os, 1);
  300. os << "depfile = " << depfile << "\n";
  301. }
  302. // Write the command.
  303. cmGlobalNinjaGenerator::Indent(os, 1);
  304. os << "command = " << command << "\n";
  305. // Write the description if any.
  306. if(!description.empty())
  307. {
  308. cmGlobalNinjaGenerator::Indent(os, 1);
  309. os << "description = " << description << "\n";
  310. }
  311. if(!rspfile.empty())
  312. {
  313. if (rspcontent.empty())
  314. {
  315. cmSystemTools::Error("No rspfile_content given!", comment.c_str());
  316. return;
  317. }
  318. cmGlobalNinjaGenerator::Indent(os, 1);
  319. os << "rspfile = " << rspfile << "\n";
  320. cmGlobalNinjaGenerator::Indent(os, 1);
  321. os << "rspfile_content = " << rspcontent << "\n";
  322. }
  323. if(restat)
  324. {
  325. cmGlobalNinjaGenerator::Indent(os, 1);
  326. os << "restat = 1\n";
  327. }
  328. if(generator)
  329. {
  330. cmGlobalNinjaGenerator::Indent(os, 1);
  331. os << "generator = 1\n";
  332. }
  333. os << "\n";
  334. }
  335. void cmGlobalNinjaGenerator::WriteVariable(std::ostream& os,
  336. const std::string& name,
  337. const std::string& value,
  338. const std::string& comment,
  339. int indent)
  340. {
  341. // Make sure we have a name.
  342. if(name.empty())
  343. {
  344. cmSystemTools::Error("No name given for WriteVariable! called "
  345. "with comment: ",
  346. comment.c_str());
  347. return;
  348. }
  349. // Do not add a variable if the value is empty.
  350. std::string val = cmSystemTools::TrimWhitespace(value);
  351. if(val.empty())
  352. {
  353. return;
  354. }
  355. cmGlobalNinjaGenerator::WriteComment(os, comment);
  356. cmGlobalNinjaGenerator::Indent(os, indent);
  357. os << name << " = " << val << "\n";
  358. }
  359. void cmGlobalNinjaGenerator::WriteInclude(std::ostream& os,
  360. const std::string& filename,
  361. const std::string& comment)
  362. {
  363. cmGlobalNinjaGenerator::WriteComment(os, comment);
  364. os << "include " << filename << "\n";
  365. }
  366. void cmGlobalNinjaGenerator::WriteDefault(std::ostream& os,
  367. const cmNinjaDeps& targets,
  368. const std::string& comment)
  369. {
  370. cmGlobalNinjaGenerator::WriteComment(os, comment);
  371. os << "default";
  372. for(cmNinjaDeps::const_iterator i = targets.begin(); i != targets.end(); ++i)
  373. os << " " << *i;
  374. os << "\n";
  375. }
  376. cmGlobalNinjaGenerator::cmGlobalNinjaGenerator()
  377. : cmGlobalGenerator()
  378. , BuildFileStream(0)
  379. , RulesFileStream(0)
  380. , CompileCommandsStream(0)
  381. , Rules()
  382. , AllDependencies()
  383. {
  384. // // Ninja is not ported to non-Unix OS yet.
  385. // this->ForceUnixPaths = true;
  386. this->FindMakeProgramFile = "CMakeNinjaFindMake.cmake";
  387. }
  388. //----------------------------------------------------------------------------
  389. // Virtual public methods.
  390. cmLocalGenerator* cmGlobalNinjaGenerator::CreateLocalGenerator()
  391. {
  392. cmLocalGenerator* lg = new cmLocalNinjaGenerator;
  393. lg->SetGlobalGenerator(this);
  394. return lg;
  395. }
  396. void cmGlobalNinjaGenerator
  397. ::GetDocumentation(cmDocumentationEntry& entry)
  398. {
  399. entry.Name = cmGlobalNinjaGenerator::GetActualName();
  400. entry.Brief = "Generates build.ninja files (experimental).";
  401. entry.Full =
  402. "A build.ninja file is generated into the build tree. Recent "
  403. "versions of the ninja program can build the project through the "
  404. "\"all\" target. An \"install\" target is also provided.";
  405. }
  406. // Implemented in all cmGlobaleGenerator sub-classes.
  407. // Used in:
  408. // Source/cmLocalGenerator.cxx
  409. // Source/cmake.cxx
  410. void cmGlobalNinjaGenerator::Generate()
  411. {
  412. this->OpenBuildFileStream();
  413. this->OpenRulesFileStream();
  414. this->cmGlobalGenerator::Generate();
  415. this->WriteAssumedSourceDependencies();
  416. this->WriteTargetAliases(*this->BuildFileStream);
  417. this->WriteBuiltinTargets(*this->BuildFileStream);
  418. if (cmSystemTools::GetErrorOccuredFlag()) {
  419. this->RulesFileStream->setstate(std::ios_base::failbit);
  420. this->BuildFileStream->setstate(std::ios_base::failbit);
  421. }
  422. this->CloseCompileCommandsStream();
  423. this->CloseRulesFileStream();
  424. this->CloseBuildFileStream();
  425. }
  426. // Implemented in all cmGlobaleGenerator sub-classes.
  427. // Used in:
  428. // Source/cmMakefile.cxx:
  429. void cmGlobalNinjaGenerator
  430. ::EnableLanguage(std::vector<std::string>const& langs,
  431. cmMakefile* makefile,
  432. bool optional)
  433. {
  434. if (makefile->IsOn("CMAKE_COMPILER_IS_MINGW"))
  435. {
  436. UsingMinGW = true;
  437. this->EnableMinGWLanguage(makefile);
  438. }
  439. if (std::find(langs.begin(), langs.end(), "Fortran") != langs.end())
  440. {
  441. cmSystemTools::Error("The Ninja generator does not support Fortran yet.");
  442. }
  443. this->cmGlobalGenerator::EnableLanguage(langs, makefile, optional);
  444. }
  445. bool cmGlobalNinjaGenerator::UsingMinGW = false;
  446. // Implemented by:
  447. // cmGlobalUnixMakefileGenerator3
  448. // cmGlobalVisualStudio10Generator
  449. // cmGlobalVisualStudio6Generator
  450. // cmGlobalVisualStudio7Generator
  451. // cmGlobalXCodeGenerator
  452. // Called by:
  453. // cmGlobalGenerator::Build()
  454. std::string cmGlobalNinjaGenerator
  455. ::GenerateBuildCommand(const char* makeProgram,
  456. const char* projectName,
  457. const char* projectDir,
  458. const char* additionalOptions,
  459. const char* targetName,
  460. const char* config,
  461. bool ignoreErrors,
  462. bool fast)
  463. {
  464. // Project name & dir and config are not used yet.
  465. (void)projectName;
  466. (void)projectDir;
  467. (void)config;
  468. // Ninja does not have -i equivalent option yet.
  469. (void)ignoreErrors;
  470. // We do not handle fast build yet.
  471. (void)fast;
  472. std::string makeCommand =
  473. cmSystemTools::ConvertToUnixOutputPath(makeProgram);
  474. if(additionalOptions)
  475. {
  476. makeCommand += " ";
  477. makeCommand += additionalOptions;
  478. }
  479. if(targetName)
  480. {
  481. if(strcmp(targetName, "clean") == 0)
  482. {
  483. makeCommand += " -t clean";
  484. }
  485. else
  486. {
  487. makeCommand += " ";
  488. makeCommand += targetName;
  489. }
  490. }
  491. return makeCommand;
  492. }
  493. //----------------------------------------------------------------------------
  494. // Non-virtual public methods.
  495. void cmGlobalNinjaGenerator::AddRule(const std::string& name,
  496. const std::string& command,
  497. const std::string& description,
  498. const std::string& comment,
  499. const std::string& depfile,
  500. const std::string& rspfile,
  501. const std::string& rspcontent,
  502. bool restat,
  503. bool generator)
  504. {
  505. // Do not add the same rule twice.
  506. if (this->HasRule(name))
  507. {
  508. return;
  509. }
  510. this->Rules.insert(name);
  511. cmGlobalNinjaGenerator::WriteRule(*this->RulesFileStream,
  512. name,
  513. command,
  514. description,
  515. comment,
  516. depfile,
  517. rspfile,
  518. rspcontent,
  519. restat,
  520. generator);
  521. this->RuleCmdLength[name] = (int) command.size();
  522. }
  523. bool cmGlobalNinjaGenerator::HasRule(const std::string &name)
  524. {
  525. RulesSetType::const_iterator rule = this->Rules.find(name);
  526. return (rule != this->Rules.end());
  527. }
  528. //----------------------------------------------------------------------------
  529. // Private virtual overrides
  530. // TODO: Refactor to combine with cmGlobalUnixMakefileGenerator3 impl.
  531. void cmGlobalNinjaGenerator::ComputeTargetObjects(cmGeneratorTarget* gt) const
  532. {
  533. cmTarget* target = gt->Target;
  534. // Compute full path to object file directory for this target.
  535. std::string dir_max;
  536. dir_max += gt->Makefile->GetCurrentOutputDirectory();
  537. dir_max += "/";
  538. dir_max += gt->LocalGenerator->GetTargetDirectory(*target);
  539. dir_max += "/";
  540. gt->ObjectDirectory = dir_max;
  541. // Compute the name of each object file.
  542. for(std::vector<cmSourceFile*>::iterator
  543. si = gt->ObjectSources.begin();
  544. si != gt->ObjectSources.end(); ++si)
  545. {
  546. cmSourceFile* sf = *si;
  547. std::string objectName = gt->LocalGenerator
  548. ->GetObjectFileNameWithoutTarget(*sf, dir_max);
  549. gt->Objects[sf] = objectName;
  550. }
  551. }
  552. //----------------------------------------------------------------------------
  553. // Private methods
  554. void cmGlobalNinjaGenerator::OpenBuildFileStream()
  555. {
  556. // Compute Ninja's build file path.
  557. std::string buildFilePath =
  558. this->GetCMakeInstance()->GetHomeOutputDirectory();
  559. buildFilePath += "/";
  560. buildFilePath += cmGlobalNinjaGenerator::NINJA_BUILD_FILE;
  561. // Get a stream where to generate things.
  562. if (!this->BuildFileStream)
  563. {
  564. this->BuildFileStream = new cmGeneratedFileStream(buildFilePath.c_str());
  565. if (!this->BuildFileStream)
  566. {
  567. // An error message is generated by the constructor if it cannot
  568. // open the file.
  569. return;
  570. }
  571. }
  572. // Write the do not edit header.
  573. this->WriteDisclaimer(*this->BuildFileStream);
  574. // Write a comment about this file.
  575. *this->BuildFileStream
  576. << "# This file contains all the build statements describing the\n"
  577. << "# compilation DAG.\n\n"
  578. ;
  579. }
  580. void cmGlobalNinjaGenerator::CloseBuildFileStream()
  581. {
  582. if (this->BuildFileStream)
  583. {
  584. delete this->BuildFileStream;
  585. this->BuildFileStream = 0;
  586. }
  587. else
  588. {
  589. cmSystemTools::Error("Build file stream was not open.");
  590. }
  591. }
  592. void cmGlobalNinjaGenerator::OpenRulesFileStream()
  593. {
  594. // Compute Ninja's build file path.
  595. std::string rulesFilePath =
  596. this->GetCMakeInstance()->GetHomeOutputDirectory();
  597. rulesFilePath += "/";
  598. rulesFilePath += cmGlobalNinjaGenerator::NINJA_RULES_FILE;
  599. // Get a stream where to generate things.
  600. if (!this->RulesFileStream)
  601. {
  602. this->RulesFileStream = new cmGeneratedFileStream(rulesFilePath.c_str());
  603. if (!this->RulesFileStream)
  604. {
  605. // An error message is generated by the constructor if it cannot
  606. // open the file.
  607. return;
  608. }
  609. }
  610. // Write the do not edit header.
  611. this->WriteDisclaimer(*this->RulesFileStream);
  612. // Write comment about this file.
  613. *this->RulesFileStream
  614. << "# This file contains all the rules used to get the outputs files\n"
  615. << "# built from the input files.\n"
  616. << "# It is included in the main '" << NINJA_BUILD_FILE << "'.\n\n"
  617. ;
  618. }
  619. void cmGlobalNinjaGenerator::CloseRulesFileStream()
  620. {
  621. if (this->RulesFileStream)
  622. {
  623. delete this->RulesFileStream;
  624. this->RulesFileStream = 0;
  625. }
  626. else
  627. {
  628. cmSystemTools::Error("Rules file stream was not open.");
  629. }
  630. }
  631. void cmGlobalNinjaGenerator::AddCXXCompileCommand(
  632. const std::string &commandLine,
  633. const std::string &sourceFile)
  634. {
  635. // Compute Ninja's build file path.
  636. std::string buildFileDir =
  637. this->GetCMakeInstance()->GetHomeOutputDirectory();
  638. if (!this->CompileCommandsStream)
  639. {
  640. std::string buildFilePath = buildFileDir + "/compile_commands.json";
  641. // Get a stream where to generate things.
  642. this->CompileCommandsStream =
  643. new cmGeneratedFileStream(buildFilePath.c_str());
  644. *this->CompileCommandsStream << "[";
  645. } else {
  646. *this->CompileCommandsStream << "," << std::endl;
  647. }
  648. std::string sourceFileName = sourceFile;
  649. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str()))
  650. {
  651. sourceFileName = cmSystemTools::CollapseFullPath(
  652. sourceFileName.c_str(),
  653. this->GetCMakeInstance()->GetHomeOutputDirectory());
  654. }
  655. *this->CompileCommandsStream << "\n{\n"
  656. << " \"directory\": \""
  657. << cmGlobalGenerator::EscapeJSON(buildFileDir) << "\",\n"
  658. << " \"command\": \""
  659. << cmGlobalGenerator::EscapeJSON(commandLine) << "\",\n"
  660. << " \"file\": \""
  661. << cmGlobalGenerator::EscapeJSON(sourceFileName) << "\"\n"
  662. << "}";
  663. }
  664. void cmGlobalNinjaGenerator::CloseCompileCommandsStream()
  665. {
  666. if (this->CompileCommandsStream)
  667. {
  668. *this->CompileCommandsStream << "\n]";
  669. delete this->CompileCommandsStream;
  670. this->CompileCommandsStream = 0;
  671. }
  672. }
  673. void cmGlobalNinjaGenerator::WriteDisclaimer(std::ostream& os)
  674. {
  675. os
  676. << "# CMAKE generated file: DO NOT EDIT!\n"
  677. << "# Generated by \"" << this->GetName() << "\""
  678. << " Generator, CMake Version "
  679. << cmVersion::GetMajorVersion() << "."
  680. << cmVersion::GetMinorVersion() << "\n\n";
  681. }
  682. void cmGlobalNinjaGenerator::AddDependencyToAll(cmTarget* target)
  683. {
  684. this->AppendTargetOutputs(target, this->AllDependencies);
  685. }
  686. void cmGlobalNinjaGenerator::AddDependencyToAll(const std::string& input)
  687. {
  688. this->AllDependencies.push_back(input);
  689. }
  690. void cmGlobalNinjaGenerator::WriteAssumedSourceDependencies()
  691. {
  692. for (std::map<std::string, std::set<std::string> >::iterator
  693. i = this->AssumedSourceDependencies.begin();
  694. i != this->AssumedSourceDependencies.end(); ++i) {
  695. cmNinjaDeps deps;
  696. std::copy(i->second.begin(), i->second.end(), std::back_inserter(deps));
  697. WriteCustomCommandBuild(/*command=*/"", /*description=*/"",
  698. "Assume dependencies for generated source file.",
  699. cmNinjaDeps(1, i->first), deps);
  700. }
  701. }
  702. void
  703. cmGlobalNinjaGenerator
  704. ::AppendTargetOutputs(cmTarget* target, cmNinjaDeps& outputs)
  705. {
  706. const char* configName =
  707. target->GetMakefile()->GetDefinition("CMAKE_BUILD_TYPE");
  708. cmLocalNinjaGenerator *ng =
  709. static_cast<cmLocalNinjaGenerator *>(this->LocalGenerators[0]);
  710. switch (target->GetType()) {
  711. case cmTarget::EXECUTABLE:
  712. case cmTarget::SHARED_LIBRARY:
  713. case cmTarget::STATIC_LIBRARY:
  714. case cmTarget::MODULE_LIBRARY:
  715. outputs.push_back(ng->ConvertToNinjaPath(
  716. target->GetFullPath(configName).c_str()));
  717. break;
  718. case cmTarget::OBJECT_LIBRARY:
  719. case cmTarget::UTILITY: {
  720. std::string path = ng->ConvertToNinjaPath(
  721. target->GetMakefile()->GetStartOutputDirectory());
  722. if (path.empty() || path == ".")
  723. outputs.push_back(target->GetName());
  724. else {
  725. path += "/";
  726. path += target->GetName();
  727. outputs.push_back(path);
  728. }
  729. break;
  730. }
  731. case cmTarget::GLOBAL_TARGET:
  732. // Always use the target in HOME instead of an unused duplicate in a
  733. // subdirectory.
  734. outputs.push_back(target->GetName());
  735. break;
  736. default:
  737. return;
  738. }
  739. }
  740. void
  741. cmGlobalNinjaGenerator
  742. ::AppendTargetDepends(cmTarget* target, cmNinjaDeps& outputs)
  743. {
  744. if (target->GetType() == cmTarget::GLOBAL_TARGET) {
  745. // Global targets only depend on other utilities, which may not appear in
  746. // the TargetDepends set (e.g. "all").
  747. std::set<cmStdString> const& utils = target->GetUtilities();
  748. std::copy(utils.begin(), utils.end(), std::back_inserter(outputs));
  749. } else {
  750. cmTargetDependSet const& targetDeps =
  751. this->GetTargetDirectDepends(*target);
  752. for (cmTargetDependSet::const_iterator i = targetDeps.begin();
  753. i != targetDeps.end(); ++i) {
  754. this->AppendTargetOutputs(*i, outputs);
  755. }
  756. }
  757. }
  758. void cmGlobalNinjaGenerator::AddTargetAlias(const std::string& alias,
  759. cmTarget* target) {
  760. cmNinjaDeps outputs;
  761. this->AppendTargetOutputs(target, outputs);
  762. // Mark the target's outputs as ambiguous to ensure that no other target uses
  763. // the output as an alias.
  764. for (cmNinjaDeps::iterator i = outputs.begin(); i != outputs.end(); ++i)
  765. TargetAliases[*i] = 0;
  766. // Insert the alias into the map. If the alias was already present in the
  767. // map and referred to another target, mark it as ambiguous.
  768. std::pair<TargetAliasMap::iterator, bool> newAlias =
  769. TargetAliases.insert(std::make_pair(alias, target));
  770. if (newAlias.second && newAlias.first->second != target)
  771. newAlias.first->second = 0;
  772. }
  773. void cmGlobalNinjaGenerator::WriteTargetAliases(std::ostream& os)
  774. {
  775. cmGlobalNinjaGenerator::WriteDivider(os);
  776. os << "# Target aliases.\n\n";
  777. for (TargetAliasMap::iterator i = TargetAliases.begin();
  778. i != TargetAliases.end(); ++i) {
  779. // Don't write ambiguous aliases.
  780. if (!i->second)
  781. continue;
  782. cmNinjaDeps deps;
  783. this->AppendTargetOutputs(i->second, deps);
  784. cmGlobalNinjaGenerator::WritePhonyBuild(os,
  785. "",
  786. cmNinjaDeps(1, i->first),
  787. deps);
  788. }
  789. }
  790. void cmGlobalNinjaGenerator::WriteBuiltinTargets(std::ostream& os)
  791. {
  792. // Write headers.
  793. cmGlobalNinjaGenerator::WriteDivider(os);
  794. os << "# Built-in targets\n\n";
  795. this->WriteTargetAll(os);
  796. this->WriteTargetRebuildManifest(os);
  797. this->WriteTargetClean(os);
  798. this->WriteTargetHelp(os);
  799. }
  800. void cmGlobalNinjaGenerator::WriteTargetAll(std::ostream& os)
  801. {
  802. cmNinjaDeps outputs;
  803. outputs.push_back("all");
  804. cmGlobalNinjaGenerator::WritePhonyBuild(os,
  805. "The main all target.",
  806. outputs,
  807. this->AllDependencies);
  808. cmGlobalNinjaGenerator::WriteDefault(os,
  809. outputs,
  810. "Make the all target the default.");
  811. }
  812. void cmGlobalNinjaGenerator::WriteTargetRebuildManifest(std::ostream& os)
  813. {
  814. cmLocalGenerator *lg = this->LocalGenerators[0];
  815. cmMakefile* mfRoot = lg->GetMakefile();
  816. cmOStringStream cmd;
  817. cmd << lg->ConvertToOutputFormat(
  818. mfRoot->GetRequiredDefinition("CMAKE_COMMAND"),
  819. cmLocalGenerator::SHELL)
  820. << " -H"
  821. << lg->ConvertToOutputFormat(mfRoot->GetHomeDirectory(),
  822. cmLocalGenerator::SHELL)
  823. << " -B"
  824. << lg->ConvertToOutputFormat(mfRoot->GetHomeOutputDirectory(),
  825. cmLocalGenerator::SHELL);
  826. WriteRule(*this->RulesFileStream,
  827. "RERUN_CMAKE",
  828. cmd.str(),
  829. "Re-running CMake...",
  830. "Rule for re-running cmake.",
  831. /*depfile=*/ "",
  832. /*rspfile=*/ "",
  833. /*rspcontent*/ "",
  834. /*restat=*/ false,
  835. /*generator=*/ true);
  836. cmNinjaDeps implicitDeps;
  837. for (std::vector<cmLocalGenerator *>::const_iterator i =
  838. this->LocalGenerators.begin(); i != this->LocalGenerators.end(); ++i) {
  839. const std::vector<std::string>& lf = (*i)->GetMakefile()->GetListFiles();
  840. implicitDeps.insert(implicitDeps.end(), lf.begin(), lf.end());
  841. const std::vector<std::string>& of = (*i)->GetMakefile()->GetOutputFiles();
  842. implicitDeps.insert(implicitDeps.end(), of.begin(), of.end());
  843. }
  844. std::sort(implicitDeps.begin(), implicitDeps.end());
  845. implicitDeps.erase(std::unique(implicitDeps.begin(), implicitDeps.end()),
  846. implicitDeps.end());
  847. implicitDeps.push_back("CMakeCache.txt");
  848. WriteBuild(os,
  849. "Re-run CMake if any of its inputs changed.",
  850. "RERUN_CMAKE",
  851. /*outputs=*/ cmNinjaDeps(1, NINJA_BUILD_FILE),
  852. /*explicitDeps=*/ cmNinjaDeps(),
  853. implicitDeps,
  854. /*orderOnlyDeps=*/ cmNinjaDeps(),
  855. /*variables=*/ cmNinjaVars());
  856. WritePhonyBuild(os,
  857. "A missing CMake input file is not an error.",
  858. implicitDeps,
  859. cmNinjaDeps());
  860. }
  861. std::string cmGlobalNinjaGenerator::ninjaCmd() const
  862. {
  863. cmLocalGenerator* lgen = this->LocalGenerators[0];
  864. if (lgen) {
  865. return lgen->ConvertToOutputFormat(
  866. lgen->GetMakefile()->GetRequiredDefinition("CMAKE_MAKE_PROGRAM"),
  867. cmLocalGenerator::SHELL);
  868. }
  869. return "ninja";
  870. }
  871. void cmGlobalNinjaGenerator::WriteTargetClean(std::ostream& os)
  872. {
  873. WriteRule(*this->RulesFileStream,
  874. "CLEAN",
  875. (ninjaCmd() + " -t clean").c_str(),
  876. "Cleaning all built files...",
  877. "Rule for cleaning all built files.",
  878. /*depfile=*/ "",
  879. /*rspfile=*/ "",
  880. /*rspcontent*/ "",
  881. /*restat=*/ false,
  882. /*generator=*/ false);
  883. WriteBuild(os,
  884. "Clean all the built files.",
  885. "CLEAN",
  886. /*outputs=*/ cmNinjaDeps(1, "clean"),
  887. /*explicitDeps=*/ cmNinjaDeps(),
  888. /*implicitDeps=*/ cmNinjaDeps(),
  889. /*orderOnlyDeps=*/ cmNinjaDeps(),
  890. /*variables=*/ cmNinjaVars());
  891. }
  892. void cmGlobalNinjaGenerator::WriteTargetHelp(std::ostream& os)
  893. {
  894. WriteRule(*this->RulesFileStream,
  895. "HELP",
  896. (ninjaCmd() + " -t targets").c_str(),
  897. "All primary targets available:",
  898. "Rule for printing all primary targets available.",
  899. /*depfile=*/ "",
  900. /*rspfile=*/ "",
  901. /*rspcontent*/ "",
  902. /*restat=*/ false,
  903. /*generator=*/ false);
  904. WriteBuild(os,
  905. "Print all primary targets available.",
  906. "HELP",
  907. /*outputs=*/ cmNinjaDeps(1, "help"),
  908. /*explicitDeps=*/ cmNinjaDeps(),
  909. /*implicitDeps=*/ cmNinjaDeps(),
  910. /*orderOnlyDeps=*/ cmNinjaDeps(),
  911. /*variables=*/ cmNinjaVars());
  912. }