cmGlobalNinjaGenerator.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 "cmGlobalGenerator.h"
  14. # include "cmNinjaTypes.h"
  15. class cmLocalGenerator;
  16. class cmGeneratedFileStream;
  17. class cmGeneratorTarget;
  18. /**
  19. * \class cmGlobalNinjaGenerator
  20. * \brief Write a build.ninja file.
  21. *
  22. * The main differences between this generator and the UnixMakefile
  23. * generator family are:
  24. * - We don't care about VERBOSE variable or RULE_MESSAGES property since
  25. * it is handle by Ninja's -v option.
  26. * - We don't care about computing any progress status since Ninja manages
  27. * it itself.
  28. * - We don't care about generating a clean target since Ninja already have
  29. * a clean tool.
  30. * - We generate one build.ninja and one rules.ninja per project.
  31. * - We try to minimize the number of generated rules: one per target and
  32. * language.
  33. * - We use Ninja special variable $in and $out to produce nice output.
  34. * - We extensively use Ninja variable overloading system to minimize the
  35. * number of generated rules.
  36. */
  37. class cmGlobalNinjaGenerator : public cmGlobalGenerator
  38. {
  39. public:
  40. /// The default name of Ninja's build file. Typically: build.ninja.
  41. static const char* NINJA_BUILD_FILE;
  42. /// The default name of Ninja's rules file. Typically: rules.ninja.
  43. /// It is included in the main build.ninja file.
  44. static const char* NINJA_RULES_FILE;
  45. /// The indentation string used when generating Ninja's build file.
  46. static const char* INDENT;
  47. /// Write @a count times INDENT level to output stream @a os.
  48. static void Indent(std::ostream& os, int count);
  49. /// Write a divider in the given output stream @a os.
  50. static void WriteDivider(std::ostream& os);
  51. static std::string EncodeIdent(const std::string &ident, std::ostream &vars);
  52. static std::string EncodeLiteral(const std::string &lit);
  53. static std::string EncodePath(const std::string &path);
  54. /**
  55. * Write the given @a comment to the output stream @a os. It
  56. * handles new line character properly.
  57. */
  58. static void WriteComment(std::ostream& os, const std::string& comment);
  59. /**
  60. * Write a build statement to @a os with the @a comment using
  61. * the @a rule the list of @a outputs files and inputs.
  62. * It also writes the variables bound to this build statement.
  63. * @warning no escaping of any kind is done here.
  64. */
  65. static void WriteBuild(std::ostream& os,
  66. const std::string& comment,
  67. const std::string& rule,
  68. const cmNinjaDeps& outputs,
  69. const cmNinjaDeps& explicitDeps,
  70. const cmNinjaDeps& implicitDeps,
  71. const cmNinjaDeps& orderOnlyDeps,
  72. const cmNinjaVars& variables);
  73. /**
  74. * Helper to write a build statement with the special 'phony' rule.
  75. */
  76. static void WritePhonyBuild(std::ostream& os,
  77. const std::string& comment,
  78. const cmNinjaDeps& outputs,
  79. const cmNinjaDeps& explicitDeps,
  80. const cmNinjaDeps& implicitDeps = cmNinjaDeps(),
  81. const cmNinjaDeps& orderOnlyDeps = cmNinjaDeps(),
  82. const cmNinjaVars& variables = cmNinjaVars());
  83. void WriteCustomCommandBuild(const std::string& command,
  84. const std::string& description,
  85. const std::string& comment,
  86. const cmNinjaDeps& outputs,
  87. const cmNinjaDeps& deps = cmNinjaDeps(),
  88. const cmNinjaDeps& orderOnlyDeps = cmNinjaDeps());
  89. /**
  90. * Write a rule statement named @a name to @a os with the @a comment,
  91. * the mandatory @a command, the @a depfile and the @a description.
  92. * It also writes the variables bound to this rule statement.
  93. * @warning no escaping of any kind is done here.
  94. */
  95. static void WriteRule(std::ostream& os,
  96. const std::string& name,
  97. const std::string& command,
  98. const std::string& description,
  99. const std::string& comment = "",
  100. const std::string& depfile = "",
  101. bool restat = false,
  102. bool generator = false);
  103. /**
  104. * Write a variable named @a name to @a os with value @a value and an
  105. * optional @a comment. An @a indent level can be specified.
  106. * @warning no escaping of any kind is done here.
  107. */
  108. static void WriteVariable(std::ostream& os,
  109. const std::string& name,
  110. const std::string& value,
  111. const std::string& comment = "",
  112. int indent = 0);
  113. /**
  114. * Write an include statement including @a filename with an optional
  115. * @a comment to the @a os stream.
  116. */
  117. static void WriteInclude(std::ostream& os,
  118. const std::string& filename,
  119. const std::string& comment = "");
  120. /**
  121. * Write a default target statement specifying @a targets as
  122. * the default targets.
  123. */
  124. static void WriteDefault(std::ostream& os,
  125. const cmNinjaDeps& targets,
  126. const std::string& comment = "");
  127. public:
  128. /// Default constructor.
  129. cmGlobalNinjaGenerator();
  130. /// Convenience method for creating an instance of this class.
  131. static cmGlobalGenerator* New() {
  132. return new cmGlobalNinjaGenerator; }
  133. /// Destructor.
  134. virtual ~cmGlobalNinjaGenerator() { }
  135. /// Overloaded methods. @see cmGlobalGenerator::CreateLocalGenerator()
  136. virtual cmLocalGenerator* CreateLocalGenerator();
  137. /// Overloaded methods. @see cmGlobalGenerator::GetName().
  138. virtual const char* GetName() const {
  139. return cmGlobalNinjaGenerator::GetActualName(); }
  140. /// @return the name of this generator.
  141. static const char* GetActualName() { return "Ninja"; }
  142. /// Overloaded methods. @see cmGlobalGenerator::GetDocumentation()
  143. virtual void GetDocumentation(cmDocumentationEntry& entry) const;
  144. /// Overloaded methods. @see cmGlobalGenerator::Generate()
  145. virtual void Generate();
  146. /// Overloaded methods. @see cmGlobalGenerator::EnableLanguage()
  147. virtual void EnableLanguage(std::vector<std::string>const& languages,
  148. cmMakefile* mf,
  149. bool optional);
  150. /// Overloaded methods. @see cmGlobalGenerator::GenerateBuildCommand()
  151. virtual std::string GenerateBuildCommand(const char* makeProgram,
  152. const char* projectName,
  153. const char* additionalOptions,
  154. const char* targetName,
  155. const char* config,
  156. bool ignoreErrors,
  157. bool fast);
  158. // Setup target names
  159. virtual const char* GetAllTargetName() const { return "all"; }
  160. virtual const char* GetInstallTargetName() const { return "install"; }
  161. virtual const char* GetInstallLocalTargetName() const {
  162. return "install/local";
  163. }
  164. virtual const char* GetInstallStripTargetName() const {
  165. return "install/strip";
  166. }
  167. virtual const char* GetTestTargetName() const { return "test"; }
  168. virtual const char* GetPackageTargetName() const { return "package"; }
  169. virtual const char* GetPackageSourceTargetName() const {
  170. return "package_source";
  171. }
  172. virtual const char* GetEditCacheTargetName() const {
  173. return "edit_cache";
  174. }
  175. virtual const char* GetRebuildCacheTargetName() const {
  176. return "rebuild_cache";
  177. }
  178. virtual const char* GetCleanTargetName() const { return "clean"; }
  179. public:
  180. cmGeneratedFileStream* GetBuildFileStream() const
  181. { return this->BuildFileStream; }
  182. cmGeneratedFileStream* GetRulesFileStream() const
  183. { return this->RulesFileStream; }
  184. /**
  185. * Add a rule to the generated build system.
  186. * Call WriteRule() behind the scene but perform some check before like:
  187. * - Do not add twice the same rule.
  188. */
  189. void AddRule(const std::string& name,
  190. const std::string& command,
  191. const std::string& description,
  192. const std::string& comment = "",
  193. const std::string& depfile = "",
  194. bool restat = false,
  195. bool generator = false);
  196. bool HasRule(const std::string& name);
  197. void AddCustomCommandRule();
  198. protected:
  199. /// Overloaded methods.
  200. /// @see cmGlobalGenerator::CheckALLOW_DUPLICATE_CUSTOM_TARGETS()
  201. virtual bool CheckALLOW_DUPLICATE_CUSTOM_TARGETS() { return true; }
  202. private:
  203. /// @see cmGlobalGenerator::ComputeTargetObjects
  204. virtual void ComputeTargetObjects(cmGeneratorTarget* gt) const;
  205. private:
  206. // In order to access the AddDependencyToAll() functions and co.
  207. friend class cmLocalNinjaGenerator;
  208. // In order to access the SeenCustomCommand() function.
  209. friend class cmNinjaTargetGenerator;
  210. friend class cmNinjaNormalTargetGenerator;
  211. friend class cmNinjaUtilityTargetGenerator;
  212. private:
  213. void OpenBuildFileStream();
  214. void CloseBuildFileStream();
  215. void OpenRulesFileStream();
  216. void CloseRulesFileStream();
  217. /// Write the common disclaimer text at the top of each build file.
  218. void WriteDisclaimer(std::ostream& os);
  219. void AddDependencyToAll(cmTarget* target);
  220. void WriteAssumedSourceDependencies();
  221. void AppendTargetOutputs(cmTarget* target, cmNinjaDeps& outputs);
  222. void AppendTargetDepends(cmTarget* target, cmNinjaDeps& outputs);
  223. void AddTargetAlias(const std::string& alias, cmTarget* target);
  224. void WriteTargetAliases(std::ostream& os);
  225. void WriteBuiltinTargets(std::ostream& os);
  226. void WriteTargetAll(std::ostream& os);
  227. void WriteTargetRebuildManifest(std::ostream& os);
  228. /// Called when we have seen the given custom command. Returns true
  229. /// if we has seen it before.
  230. bool SeenCustomCommand(cmCustomCommand const *cc) {
  231. return !this->CustomCommands.insert(cc).second;
  232. }
  233. /// Called when we have seen the given custom command output.
  234. void SeenCustomCommandOutput(const std::string &output) {
  235. this->CustomCommandOutputs.insert(output);
  236. // We don't need the assumed dependencies anymore, because we have
  237. // an output.
  238. this->AssumedSourceDependencies.erase(output);
  239. }
  240. bool HasCustomCommandOutput(const std::string &output) {
  241. return this->CustomCommandOutputs.find(output) !=
  242. this->CustomCommandOutputs.end();
  243. }
  244. void AddAssumedSourceDependencies(const std::string &source,
  245. const cmNinjaDeps &deps) {
  246. std::set<std::string> &ASD = this->AssumedSourceDependencies[source];
  247. // Because we may see the same source file multiple times (same source
  248. // specified in multiple targets), compute the union of any assumed
  249. // dependencies.
  250. ASD.insert(deps.begin(), deps.end());
  251. }
  252. private:
  253. /// The file containing the build statement. (the relation ship of the
  254. /// compilation DAG).
  255. cmGeneratedFileStream* BuildFileStream;
  256. /// The file containing the rule statements. (The action attached to each
  257. /// edge of the compilation DAG).
  258. cmGeneratedFileStream* RulesFileStream;
  259. /// The type used to store the set of rules added to the generated build
  260. /// system.
  261. typedef std::set<std::string> RulesSetType;
  262. /// The set of rules added to the generated build system.
  263. RulesSetType Rules;
  264. /// The set of dependencies to add to the "all" target.
  265. cmNinjaDeps AllDependencies;
  266. /// The set of custom commands we have seen.
  267. std::set<cmCustomCommand const*> CustomCommands;
  268. /// The set of custom command outputs we have seen.
  269. std::set<std::string> CustomCommandOutputs;
  270. /// The mapping from source file to assumed dependencies.
  271. std::map<std::string, std::set<std::string> > AssumedSourceDependencies;
  272. typedef std::map<std::string, cmTarget*> TargetAliasMap;
  273. TargetAliasMap TargetAliases;
  274. static cmLocalGenerator* LocalGenerator;
  275. };
  276. #endif // ! cmGlobalNinjaGenerator_h