cmGlobalNinjaGenerator.h 15 KB

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