cmGlobalNinjaGenerator.h 14 KB

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