cmGlobalNinjaGenerator.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 "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 cmGlobalGenerator
  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 EncodeIdent(const std::string &ident, std::ostream &vars);
  54. static std::string EncodeLiteral(const std::string &lit);
  55. static std::string EncodePath(const std::string &path);
  56. static std::string EncodeDepfileSpace(const std::string &path);
  57. /**
  58. * Write the given @a comment to the output stream @a os. It
  59. * handles new line character properly.
  60. */
  61. static void WriteComment(std::ostream& os, const std::string& comment);
  62. /**
  63. * Write a build statement to @a os with the @a comment using
  64. * the @a rule the list of @a outputs files and inputs.
  65. * It also writes the variables bound to this build statement.
  66. * @warning no escaping of any kind is done here.
  67. */
  68. void WriteBuild(std::ostream& os,
  69. const std::string& comment,
  70. const std::string& rule,
  71. const cmNinjaDeps& outputs,
  72. const cmNinjaDeps& explicitDeps,
  73. const cmNinjaDeps& implicitDeps,
  74. const cmNinjaDeps& orderOnlyDeps,
  75. const cmNinjaVars& variables,
  76. const std::string& rspfile = std::string(),
  77. int cmdLineLimit = -1);
  78. /**
  79. * Helper to write a build statement with the special 'phony' rule.
  80. */
  81. void WritePhonyBuild(std::ostream& os,
  82. const std::string& comment,
  83. const cmNinjaDeps& outputs,
  84. const cmNinjaDeps& explicitDeps,
  85. const cmNinjaDeps& implicitDeps = cmNinjaDeps(),
  86. const cmNinjaDeps& orderOnlyDeps = cmNinjaDeps(),
  87. const cmNinjaVars& variables = cmNinjaVars());
  88. void WriteCustomCommandBuild(const std::string& command,
  89. const std::string& description,
  90. const std::string& comment,
  91. bool uses_terminal,
  92. const cmNinjaDeps& outputs,
  93. const cmNinjaDeps& deps = cmNinjaDeps(),
  94. const cmNinjaDeps& orderOnly = cmNinjaDeps());
  95. void WriteMacOSXContentBuild(const std::string& input,
  96. const std::string& output);
  97. /**
  98. * Write a rule statement named @a name to @a os with the @a comment,
  99. * the mandatory @a command, the @a depfile and the @a description.
  100. * It also writes the variables bound to this rule statement.
  101. * @warning no escaping of any kind is done here.
  102. */
  103. static void WriteRule(std::ostream& os,
  104. const std::string& name,
  105. const std::string& command,
  106. const std::string& description,
  107. const std::string& comment,
  108. const std::string& depfile,
  109. const std::string& deptype,
  110. const std::string& rspfile,
  111. const std::string& rspcontent,
  112. bool restat,
  113. bool generator);
  114. /**
  115. * Write a variable named @a name to @a os with value @a value and an
  116. * optional @a comment. An @a indent level can be specified.
  117. * @warning no escaping of any kind is done here.
  118. */
  119. static void WriteVariable(std::ostream& os,
  120. const std::string& name,
  121. const std::string& value,
  122. const std::string& comment = "",
  123. int indent = 0);
  124. /**
  125. * Write an include statement including @a filename with an optional
  126. * @a comment to the @a os stream.
  127. */
  128. static void WriteInclude(std::ostream& os,
  129. const std::string& filename,
  130. const std::string& comment = "");
  131. /**
  132. * Write a default target statement specifying @a targets as
  133. * the default targets.
  134. */
  135. static void WriteDefault(std::ostream& os,
  136. const cmNinjaDeps& targets,
  137. const std::string& comment = "");
  138. static bool IsMinGW() { return UsingMinGW; }
  139. public:
  140. /// Default constructor.
  141. cmGlobalNinjaGenerator();
  142. /// Convenience method for creating an instance of this class.
  143. static cmGlobalGeneratorFactory* NewFactory() {
  144. return new cmGlobalGeneratorSimpleFactory<cmGlobalNinjaGenerator>(); }
  145. /// Destructor.
  146. virtual ~cmGlobalNinjaGenerator() { }
  147. /// Overloaded methods. @see cmGlobalGenerator::CreateLocalGenerator()
  148. virtual cmLocalGenerator* CreateLocalGenerator();
  149. /// Overloaded methods. @see cmGlobalGenerator::GetName().
  150. virtual std::string GetName() const {
  151. return cmGlobalNinjaGenerator::GetActualName(); }
  152. /// @return the name of this generator.
  153. static std::string GetActualName() { return "Ninja"; }
  154. /// Overloaded methods. @see cmGlobalGenerator::GetDocumentation()
  155. static void GetDocumentation(cmDocumentationEntry& entry);
  156. /// Overloaded methods. @see cmGlobalGenerator::EnableLanguage()
  157. virtual void EnableLanguage(std::vector<std::string>const& languages,
  158. cmMakefile* mf,
  159. bool optional);
  160. /// Overloaded methods. @see cmGlobalGenerator::GenerateBuildCommand()
  161. virtual void GenerateBuildCommand(
  162. std::vector<std::string>& makeCommand,
  163. const std::string& makeProgram,
  164. const std::string& projectName,
  165. const std::string& projectDir,
  166. const std::string& targetName,
  167. const std::string& config,
  168. bool fast,
  169. std::vector<std::string> const& makeOptions = std::vector<std::string>()
  170. );
  171. // Setup target names
  172. virtual const char* GetAllTargetName() const { return "all"; }
  173. virtual const char* GetInstallTargetName() const { return "install"; }
  174. virtual const char* GetInstallLocalTargetName() const {
  175. return "install/local";
  176. }
  177. virtual const char* GetInstallStripTargetName() const {
  178. return "install/strip";
  179. }
  180. virtual const char* GetTestTargetName() const { return "test"; }
  181. virtual const char* GetPackageTargetName() const { return "package"; }
  182. virtual const char* GetPackageSourceTargetName() const {
  183. return "package_source";
  184. }
  185. virtual const char* GetEditCacheTargetName() const {
  186. return "edit_cache";
  187. }
  188. virtual const char* GetRebuildCacheTargetName() const {
  189. return "rebuild_cache";
  190. }
  191. virtual const char* GetCleanTargetName() const { return "clean"; }
  192. cmGeneratedFileStream* GetBuildFileStream() const {
  193. return this->BuildFileStream; }
  194. cmGeneratedFileStream* GetRulesFileStream() const {
  195. return this->RulesFileStream; }
  196. void AddCXXCompileCommand(const std::string &commandLine,
  197. const std::string &sourceFile);
  198. /**
  199. * Add a rule to the generated build system.
  200. * Call WriteRule() behind the scene but perform some check before like:
  201. * - Do not add twice the same rule.
  202. */
  203. void AddRule(const std::string& name,
  204. const std::string& command,
  205. const std::string& description,
  206. const std::string& comment,
  207. const std::string& depfile,
  208. const std::string& deptype,
  209. const std::string& rspfile,
  210. const std::string& rspcontent,
  211. bool restat,
  212. bool generator);
  213. bool HasRule(const std::string& name);
  214. void AddCustomCommandRule();
  215. void AddMacOSXContentRule();
  216. bool HasCustomCommandOutput(const std::string &output) {
  217. return this->CustomCommandOutputs.find(output) !=
  218. this->CustomCommandOutputs.end();
  219. }
  220. /// Called when we have seen the given custom command. Returns true
  221. /// if we has seen it before.
  222. bool SeenCustomCommand(cmCustomCommand const *cc) {
  223. return !this->CustomCommands.insert(cc).second;
  224. }
  225. /// Called when we have seen the given custom command output.
  226. void SeenCustomCommandOutput(const std::string &output) {
  227. this->CustomCommandOutputs.insert(output);
  228. // We don't need the assumed dependencies anymore, because we have
  229. // an output.
  230. this->AssumedSourceDependencies.erase(output);
  231. }
  232. void AddAssumedSourceDependencies(const std::string &source,
  233. const cmNinjaDeps &deps) {
  234. std::set<std::string> &ASD = this->AssumedSourceDependencies[source];
  235. // Because we may see the same source file multiple times (same source
  236. // specified in multiple targets), compute the union of any assumed
  237. // dependencies.
  238. ASD.insert(deps.begin(), deps.end());
  239. }
  240. void AppendTargetOutputs(cmTarget const* target, cmNinjaDeps& outputs);
  241. void AppendTargetDepends(cmTarget const* target, cmNinjaDeps& outputs);
  242. void AddDependencyToAll(cmTarget* target);
  243. void AddDependencyToAll(const std::string& input);
  244. const std::vector<cmLocalGenerator*>& GetLocalGenerators() const {
  245. return LocalGenerators; }
  246. bool IsExcluded(cmLocalGenerator* root, cmTarget& target) {
  247. return cmGlobalGenerator::IsExcluded(root, target); }
  248. int GetRuleCmdLength(const std::string& name) {
  249. return RuleCmdLength[name]; }
  250. void AddTargetAlias(const std::string& alias, cmTarget* target);
  251. virtual void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const;
  252. std::string ninjaVersion() const;
  253. bool SupportsConsolePool() const;
  254. protected:
  255. /// Overloaded methods. @see cmGlobalGenerator::Generate()
  256. virtual void Generate();
  257. /// Overloaded methods.
  258. /// @see cmGlobalGenerator::CheckALLOW_DUPLICATE_CUSTOM_TARGETS()
  259. virtual bool CheckALLOW_DUPLICATE_CUSTOM_TARGETS() const { return true; }
  260. private:
  261. virtual std::string GetEditCacheCommand() const;
  262. void OpenBuildFileStream();
  263. void CloseBuildFileStream();
  264. void CloseCompileCommandsStream();
  265. void OpenRulesFileStream();
  266. void CloseRulesFileStream();
  267. /// Write the common disclaimer text at the top of each build file.
  268. void WriteDisclaimer(std::ostream& os);
  269. void WriteAssumedSourceDependencies();
  270. void WriteTargetAliases(std::ostream& os);
  271. void WriteUnknownExplicitDependencies(std::ostream& os);
  272. void WriteBuiltinTargets(std::ostream& os);
  273. void WriteTargetAll(std::ostream& os);
  274. void WriteTargetRebuildManifest(std::ostream& os);
  275. void WriteTargetClean(std::ostream& os);
  276. void WriteTargetHelp(std::ostream& os);
  277. std::string ninjaCmd() const;
  278. /// The file containing the build statement. (the relationship of the
  279. /// compilation DAG).
  280. cmGeneratedFileStream* BuildFileStream;
  281. /// The file containing the rule statements. (The action attached to each
  282. /// edge of the compilation DAG).
  283. cmGeneratedFileStream* RulesFileStream;
  284. cmGeneratedFileStream* CompileCommandsStream;
  285. /// The type used to store the set of rules added to the generated build
  286. /// system.
  287. typedef std::set<std::string> RulesSetType;
  288. /// The set of rules added to the generated build system.
  289. RulesSetType Rules;
  290. /// Length of rule command, used by rsp file evaluation
  291. std::map<std::string, int> RuleCmdLength;
  292. /// The set of dependencies to add to the "all" target.
  293. cmNinjaDeps AllDependencies;
  294. /// The set of custom commands we have seen.
  295. std::set<cmCustomCommand const*> CustomCommands;
  296. /// The set of custom command outputs we have seen.
  297. std::set<std::string> CustomCommandOutputs;
  298. /// The combined explicit dependencies of custom build commands
  299. std::set<std::string> CombinedCustomCommandExplicitDependencies;
  300. /// When combined with CombinedCustomCommandExplicitDependencies it allows
  301. /// us to detect the set of explicit dependencies that have
  302. std::set<std::string> CombinedBuildOutputs;
  303. /// The mapping from source file to assumed dependencies.
  304. std::map<std::string, std::set<std::string> > AssumedSourceDependencies;
  305. typedef std::map<std::string, cmTarget*> TargetAliasMap;
  306. TargetAliasMap TargetAliases;
  307. static cmLocalGenerator* LocalGenerator;
  308. static bool UsingMinGW;
  309. };
  310. #endif // ! cmGlobalNinjaGenerator_h