cmGlobalNinjaGenerator.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. #ifndef cmGlobalNinjaGenerator_h
  12. # define cmGlobalNinjaGenerator_h
  13. # include "cmGlobalCommonGenerator.h"
  14. # include "cmGlobalGeneratorFactory.h"
  15. # include "cmNinjaTypes.h"
  16. //#define NINJA_GEN_VERBOSE_FILES
  17. class cmLocalGenerator;
  18. class cmGeneratedFileStream;
  19. class cmGeneratorTarget;
  20. /**
  21. * \class cmGlobalNinjaGenerator
  22. * \brief Write a build.ninja file.
  23. *
  24. * The main differences between this generator and the UnixMakefile
  25. * generator family are:
  26. * - We don't care about VERBOSE variable or RULE_MESSAGES property since
  27. * it is handle by Ninja's -v option.
  28. * - We don't care about computing any progress status since Ninja manages
  29. * it itself.
  30. * - We don't care about generating a clean target since Ninja already have
  31. * a clean tool.
  32. * - We generate one build.ninja and one rules.ninja per project.
  33. * - We try to minimize the number of generated rules: one per target and
  34. * language.
  35. * - We use Ninja special variable $in and $out to produce nice output.
  36. * - We extensively use Ninja variable overloading system to minimize the
  37. * number of generated rules.
  38. */
  39. class cmGlobalNinjaGenerator : public cmGlobalCommonGenerator
  40. {
  41. public:
  42. /// The default name of Ninja's build file. Typically: build.ninja.
  43. static const char* NINJA_BUILD_FILE;
  44. /// The default name of Ninja's rules file. Typically: rules.ninja.
  45. /// It is included in the main build.ninja file.
  46. static const char* NINJA_RULES_FILE;
  47. /// The indentation string used when generating Ninja's build file.
  48. static const char* INDENT;
  49. /// Write @a count times INDENT level to output stream @a os.
  50. static void Indent(std::ostream& os, int count);
  51. /// Write a divider in the given output stream @a os.
  52. static void WriteDivider(std::ostream& os);
  53. static std::string EncodeRuleName(std::string const& name);
  54. static std::string EncodeIdent(const std::string &ident, std::ostream &vars);
  55. static std::string EncodeLiteral(const std::string &lit);
  56. std::string EncodePath(const std::string &path);
  57. static std::string EncodeDepfileSpace(const std::string &path);
  58. /**
  59. * Write the given @a comment to the output stream @a os. It
  60. * handles new line character properly.
  61. */
  62. static void WriteComment(std::ostream& os, const std::string& comment);
  63. /**
  64. * Utilized by the generator factory to determine if this generator
  65. * supports toolsets.
  66. */
  67. static bool SupportsToolset() { return false; }
  68. /**
  69. * Write a build statement to @a os with the @a comment using
  70. * the @a rule the list of @a outputs files and inputs.
  71. * It also writes the variables bound to this build statement.
  72. * @warning no escaping of any kind is done here.
  73. */
  74. void WriteBuild(std::ostream& os,
  75. const std::string& comment,
  76. const std::string& rule,
  77. const cmNinjaDeps& outputs,
  78. const cmNinjaDeps& explicitDeps,
  79. const cmNinjaDeps& implicitDeps,
  80. const cmNinjaDeps& orderOnlyDeps,
  81. const cmNinjaVars& variables,
  82. const std::string& rspfile = std::string(),
  83. int cmdLineLimit = -1,
  84. bool* usedResponseFile = 0);
  85. /**
  86. * Helper to write a build statement with the special 'phony' rule.
  87. */
  88. void WritePhonyBuild(std::ostream& os,
  89. const std::string& comment,
  90. const cmNinjaDeps& outputs,
  91. const cmNinjaDeps& explicitDeps,
  92. const cmNinjaDeps& implicitDeps = cmNinjaDeps(),
  93. const cmNinjaDeps& orderOnlyDeps = cmNinjaDeps(),
  94. const cmNinjaVars& variables = cmNinjaVars());
  95. void WriteCustomCommandBuild(const std::string& command,
  96. const std::string& description,
  97. const std::string& comment,
  98. bool uses_terminal,
  99. bool restat,
  100. const cmNinjaDeps& outputs,
  101. const cmNinjaDeps& deps = cmNinjaDeps(),
  102. const cmNinjaDeps& orderOnly = cmNinjaDeps());
  103. void WriteMacOSXContentBuild(const std::string& input,
  104. const std::string& output);
  105. /**
  106. * Write a rule statement named @a name to @a os with the @a comment,
  107. * the mandatory @a command, the @a depfile and the @a description.
  108. * It also writes the variables bound to this rule statement.
  109. * @warning no escaping of any kind is done here.
  110. */
  111. static void WriteRule(std::ostream& os,
  112. const std::string& name,
  113. const std::string& command,
  114. const std::string& description,
  115. const std::string& comment,
  116. const std::string& depfile,
  117. const std::string& deptype,
  118. const std::string& rspfile,
  119. const std::string& rspcontent,
  120. const std::string& restat,
  121. bool generator);
  122. /**
  123. * Write a variable named @a name to @a os with value @a value and an
  124. * optional @a comment. An @a indent level can be specified.
  125. * @warning no escaping of any kind is done here.
  126. */
  127. static void WriteVariable(std::ostream& os,
  128. const std::string& name,
  129. const std::string& value,
  130. const std::string& comment = "",
  131. int indent = 0);
  132. /**
  133. * Write an include statement including @a filename with an optional
  134. * @a comment to the @a os stream.
  135. */
  136. static void WriteInclude(std::ostream& os,
  137. const std::string& filename,
  138. const std::string& comment = "");
  139. /**
  140. * Write a default target statement specifying @a targets as
  141. * the default targets.
  142. */
  143. static void WriteDefault(std::ostream& os,
  144. const cmNinjaDeps& targets,
  145. const std::string& comment = "");
  146. bool IsGCCOnWindows() const { return UsingGCCOnWindows; }
  147. public:
  148. cmGlobalNinjaGenerator(cmake* cm);
  149. static cmGlobalGeneratorFactory* NewFactory() {
  150. return new cmGlobalGeneratorSimpleFactory<cmGlobalNinjaGenerator>(); }
  151. virtual ~cmGlobalNinjaGenerator() { }
  152. virtual cmLocalGenerator* CreateLocalGenerator(cmMakefile* mf);
  153. virtual std::string GetName() const {
  154. return cmGlobalNinjaGenerator::GetActualName(); }
  155. static std::string GetActualName() { return "Ninja"; }
  156. static void GetDocumentation(cmDocumentationEntry& entry);
  157. virtual void EnableLanguage(std::vector<std::string>const& languages,
  158. cmMakefile* mf,
  159. bool optional);
  160. virtual void GenerateBuildCommand(
  161. std::vector<std::string>& makeCommand,
  162. const std::string& makeProgram,
  163. const std::string& projectName,
  164. const std::string& projectDir,
  165. const std::string& targetName,
  166. const std::string& config,
  167. bool fast, bool verbose,
  168. std::vector<std::string> const& makeOptions = std::vector<std::string>()
  169. );
  170. // Setup target names
  171. virtual const char* GetAllTargetName() const { return "all"; }
  172. virtual const char* GetInstallTargetName() const { return "install"; }
  173. virtual const char* GetInstallLocalTargetName() const {
  174. return "install/local";
  175. }
  176. virtual const char* GetInstallStripTargetName() const {
  177. return "install/strip";
  178. }
  179. virtual const char* GetTestTargetName() const { return "test"; }
  180. virtual const char* GetPackageTargetName() const { return "package"; }
  181. virtual const char* GetPackageSourceTargetName() const {
  182. return "package_source";
  183. }
  184. virtual const char* GetEditCacheTargetName() const {
  185. return "edit_cache";
  186. }
  187. virtual const char* GetRebuildCacheTargetName() const {
  188. return "rebuild_cache";
  189. }
  190. virtual const char* GetCleanTargetName() const { return "clean"; }
  191. cmGeneratedFileStream* GetBuildFileStream() const {
  192. return this->BuildFileStream; }
  193. cmGeneratedFileStream* GetRulesFileStream() const {
  194. return this->RulesFileStream; }
  195. std::string ConvertToNinjaPath(const std::string& path);
  196. struct MapToNinjaPathImpl {
  197. cmGlobalNinjaGenerator* GG;
  198. MapToNinjaPathImpl(cmGlobalNinjaGenerator* gg): GG(gg) {}
  199. std::string operator()(std::string const& path) {
  200. return this->GG->ConvertToNinjaPath(path);
  201. }
  202. };
  203. MapToNinjaPathImpl MapToNinjaPath() {
  204. return MapToNinjaPathImpl(this);
  205. }
  206. void AddCXXCompileCommand(const std::string &commandLine,
  207. const std::string &sourceFile);
  208. /**
  209. * Add a rule to the generated build system.
  210. * Call WriteRule() behind the scene but perform some check before like:
  211. * - Do not add twice the same rule.
  212. */
  213. void AddRule(const std::string& name,
  214. const std::string& command,
  215. const std::string& description,
  216. const std::string& comment,
  217. const std::string& depfile,
  218. const std::string& deptype,
  219. const std::string& rspfile,
  220. const std::string& rspcontent,
  221. const std::string& restat,
  222. bool generator);
  223. bool HasRule(const std::string& name);
  224. void AddCustomCommandRule();
  225. void AddMacOSXContentRule();
  226. bool HasCustomCommandOutput(const std::string &output) {
  227. return this->CustomCommandOutputs.find(output) !=
  228. this->CustomCommandOutputs.end();
  229. }
  230. /// Called when we have seen the given custom command. Returns true
  231. /// if we has seen it before.
  232. bool SeenCustomCommand(cmCustomCommand const *cc) {
  233. return !this->CustomCommands.insert(cc).second;
  234. }
  235. /// Called when we have seen the given custom command output.
  236. void SeenCustomCommandOutput(const std::string &output) {
  237. this->CustomCommandOutputs.insert(output);
  238. // We don't need the assumed dependencies anymore, because we have
  239. // an output.
  240. this->AssumedSourceDependencies.erase(output);
  241. }
  242. void AddAssumedSourceDependencies(const std::string &source,
  243. const cmNinjaDeps &deps) {
  244. std::set<std::string> &ASD = this->AssumedSourceDependencies[source];
  245. // Because we may see the same source file multiple times (same source
  246. // specified in multiple targets), compute the union of any assumed
  247. // dependencies.
  248. ASD.insert(deps.begin(), deps.end());
  249. }
  250. void AppendTargetOutputs(cmGeneratorTarget const* target,
  251. cmNinjaDeps& outputs);
  252. void AppendTargetDepends(cmGeneratorTarget const* target,
  253. cmNinjaDeps& outputs);
  254. void AddDependencyToAll(cmGeneratorTarget* target);
  255. void AddDependencyToAll(const std::string& input);
  256. const std::vector<cmLocalGenerator*>& GetLocalGenerators() const {
  257. return LocalGenerators; }
  258. bool IsExcluded(cmLocalGenerator* root, cmGeneratorTarget* target) {
  259. return cmGlobalGenerator::IsExcluded(root, target); }
  260. int GetRuleCmdLength(const std::string& name) {
  261. return RuleCmdLength[name]; }
  262. void AddTargetAlias(const std::string& alias, cmGeneratorTarget* target);
  263. virtual void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const;
  264. // Ninja generator uses 'deps' and 'msvc_deps_prefix' introduced in 1.3
  265. static std::string RequiredNinjaVersion() { return "1.3"; }
  266. static std::string RequiredNinjaVersionForConsolePool() { return "1.5"; }
  267. bool SupportsConsolePool() const;
  268. protected:
  269. virtual void Generate();
  270. virtual bool CheckALLOW_DUPLICATE_CUSTOM_TARGETS() const { return true; }
  271. private:
  272. virtual std::string GetEditCacheCommand() const;
  273. virtual void FindMakeProgram(cmMakefile* mf);
  274. void OpenBuildFileStream();
  275. void CloseBuildFileStream();
  276. void CloseCompileCommandsStream();
  277. void OpenRulesFileStream();
  278. void CloseRulesFileStream();
  279. /// Write the common disclaimer text at the top of each build file.
  280. void WriteDisclaimer(std::ostream& os);
  281. void WriteAssumedSourceDependencies();
  282. void WriteTargetAliases(std::ostream& os);
  283. void WriteUnknownExplicitDependencies(std::ostream& os);
  284. void WriteBuiltinTargets(std::ostream& os);
  285. void WriteTargetAll(std::ostream& os);
  286. void WriteTargetRebuildManifest(std::ostream& os);
  287. void WriteTargetClean(std::ostream& os);
  288. void WriteTargetHelp(std::ostream& os);
  289. std::string ninjaCmd() const;
  290. /// The file containing the build statement. (the relationship of the
  291. /// compilation DAG).
  292. cmGeneratedFileStream* BuildFileStream;
  293. /// The file containing the rule statements. (The action attached to each
  294. /// edge of the compilation DAG).
  295. cmGeneratedFileStream* RulesFileStream;
  296. cmGeneratedFileStream* CompileCommandsStream;
  297. /// The type used to store the set of rules added to the generated build
  298. /// system.
  299. typedef std::set<std::string> RulesSetType;
  300. /// The set of rules added to the generated build system.
  301. RulesSetType Rules;
  302. /// Length of rule command, used by rsp file evaluation
  303. std::map<std::string, int> RuleCmdLength;
  304. /// The set of dependencies to add to the "all" target.
  305. cmNinjaDeps AllDependencies;
  306. bool UsingGCCOnWindows;
  307. /// The set of custom commands we have seen.
  308. std::set<cmCustomCommand const*> CustomCommands;
  309. /// The set of custom command outputs we have seen.
  310. std::set<std::string> CustomCommandOutputs;
  311. /// Whether we are collecting known build outputs and needed
  312. /// dependencies to determine unknown dependencies.
  313. bool ComputingUnknownDependencies;
  314. cmPolicies::PolicyStatus PolicyCMP0058;
  315. /// The combined explicit dependencies of custom build commands
  316. std::set<std::string> CombinedCustomCommandExplicitDependencies;
  317. /// When combined with CombinedCustomCommandExplicitDependencies it allows
  318. /// us to detect the set of explicit dependencies that have
  319. std::set<std::string> CombinedBuildOutputs;
  320. /// The mapping from source file to assumed dependencies.
  321. std::map<std::string, std::set<std::string> > AssumedSourceDependencies;
  322. typedef std::map<std::string, cmGeneratorTarget*> TargetAliasMap;
  323. TargetAliasMap TargetAliases;
  324. std::string NinjaCommand;
  325. std::string NinjaVersion;
  326. };
  327. #endif // ! cmGlobalNinjaGenerator_h