cmGlobalNinjaGenerator.h 14 KB

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