cmGlobalNinjaGenerator.h 13 KB

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