cmGlobalNinjaGenerator.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. void AddCXXCompileCommand(const std::string &commandLine,
  185. const std::string &sourceFile);
  186. /**
  187. * Add a rule to the generated build system.
  188. * Call WriteRule() behind the scene but perform some check before like:
  189. * - Do not add twice the same rule.
  190. */
  191. void AddRule(const std::string& name,
  192. const std::string& command,
  193. const std::string& description,
  194. const std::string& comment = "",
  195. const std::string& depfile = "",
  196. bool restat = false,
  197. bool generator = false);
  198. bool HasRule(const std::string& name);
  199. void AddCustomCommandRule();
  200. protected:
  201. /// Overloaded methods.
  202. /// @see cmGlobalGenerator::CheckALLOW_DUPLICATE_CUSTOM_TARGETS()
  203. virtual bool CheckALLOW_DUPLICATE_CUSTOM_TARGETS() { return true; }
  204. private:
  205. /// @see cmGlobalGenerator::ComputeTargetObjects
  206. virtual void ComputeTargetObjects(cmGeneratorTarget* gt) const;
  207. private:
  208. // In order to access the AddDependencyToAll() functions and co.
  209. friend class cmLocalNinjaGenerator;
  210. // In order to access the SeenCustomCommand() function.
  211. friend class cmNinjaTargetGenerator;
  212. friend class cmNinjaNormalTargetGenerator;
  213. friend class cmNinjaUtilityTargetGenerator;
  214. private:
  215. void OpenBuildFileStream();
  216. void CloseBuildFileStream();
  217. void CloseCompileCommandsStream();
  218. void OpenRulesFileStream();
  219. void CloseRulesFileStream();
  220. /// Write the common disclaimer text at the top of each build file.
  221. void WriteDisclaimer(std::ostream& os);
  222. void AddDependencyToAll(cmTarget* target);
  223. void WriteAssumedSourceDependencies();
  224. void AppendTargetOutputs(cmTarget* target, cmNinjaDeps& outputs);
  225. void AppendTargetDepends(cmTarget* target, cmNinjaDeps& outputs);
  226. void AddTargetAlias(const std::string& alias, cmTarget* target);
  227. void WriteTargetAliases(std::ostream& os);
  228. void WriteBuiltinTargets(std::ostream& os);
  229. void WriteTargetAll(std::ostream& os);
  230. void WriteTargetRebuildManifest(std::ostream& os);
  231. void WriteTargetClean(std::ostream& os);
  232. void WriteTargetHelp(std::ostream& os);
  233. /// Called when we have seen the given custom command. Returns true
  234. /// if we has seen it before.
  235. bool SeenCustomCommand(cmCustomCommand const *cc) {
  236. return !this->CustomCommands.insert(cc).second;
  237. }
  238. /// Called when we have seen the given custom command output.
  239. void SeenCustomCommandOutput(const std::string &output) {
  240. this->CustomCommandOutputs.insert(output);
  241. // We don't need the assumed dependencies anymore, because we have
  242. // an output.
  243. this->AssumedSourceDependencies.erase(output);
  244. }
  245. bool HasCustomCommandOutput(const std::string &output) {
  246. return this->CustomCommandOutputs.find(output) !=
  247. this->CustomCommandOutputs.end();
  248. }
  249. void AddAssumedSourceDependencies(const std::string &source,
  250. const cmNinjaDeps &deps) {
  251. std::set<std::string> &ASD = this->AssumedSourceDependencies[source];
  252. // Because we may see the same source file multiple times (same source
  253. // specified in multiple targets), compute the union of any assumed
  254. // dependencies.
  255. ASD.insert(deps.begin(), deps.end());
  256. }
  257. private:
  258. /// The file containing the build statement. (the relation ship of the
  259. /// compilation DAG).
  260. cmGeneratedFileStream* BuildFileStream;
  261. /// The file containing the rule statements. (The action attached to each
  262. /// edge of the compilation DAG).
  263. cmGeneratedFileStream* RulesFileStream;
  264. cmGeneratedFileStream* CompileCommandsStream;
  265. /// The type used to store the set of rules added to the generated build
  266. /// system.
  267. typedef std::set<std::string> RulesSetType;
  268. /// The set of rules added to the generated build system.
  269. RulesSetType Rules;
  270. /// The set of dependencies to add to the "all" target.
  271. cmNinjaDeps AllDependencies;
  272. /// The set of custom commands we have seen.
  273. std::set<cmCustomCommand const*> CustomCommands;
  274. /// The set of custom command outputs we have seen.
  275. std::set<std::string> CustomCommandOutputs;
  276. /// The mapping from source file to assumed dependencies.
  277. std::map<std::string, std::set<std::string> > AssumedSourceDependencies;
  278. typedef std::map<std::string, cmTarget*> TargetAliasMap;
  279. TargetAliasMap TargetAliases;
  280. static cmLocalGenerator* LocalGenerator;
  281. };
  282. #endif // ! cmGlobalNinjaGenerator_h