cmGlobalNinjaGenerator.h 14 KB

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