cmGlobalNinjaGenerator.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmGlobalNinjaGenerator_h
  4. #define cmGlobalNinjaGenerator_h
  5. #include <cmConfigure.h>
  6. #include "cmGlobalCommonGenerator.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmGlobalGeneratorFactory.h"
  9. #include "cmNinjaTypes.h"
  10. #include "cmPolicies.h"
  11. #include <iosfwd>
  12. #include <map>
  13. #include <set>
  14. #include <string>
  15. #include <utility>
  16. #include <vector>
  17. class cmCustomCommand;
  18. class cmMakefile;
  19. class cmake;
  20. struct cmDocumentationEntry;
  21. class cmGeneratedFileStream;
  22. class cmGeneratorTarget;
  23. class cmLocalGenerator;
  24. /**
  25. * \class cmGlobalNinjaGenerator
  26. * \brief Write a build.ninja file.
  27. *
  28. * The main differences between this generator and the UnixMakefile
  29. * generator family are:
  30. * - We don't care about VERBOSE variable or RULE_MESSAGES property since
  31. * it is handle by Ninja's -v option.
  32. * - We don't care about computing any progress status since Ninja manages
  33. * it itself.
  34. * - We don't care about generating a clean target since Ninja already have
  35. * a clean tool.
  36. * - We generate one build.ninja and one rules.ninja per project.
  37. * - We try to minimize the number of generated rules: one per target and
  38. * language.
  39. * - We use Ninja special variable $in and $out to produce nice output.
  40. * - We extensively use Ninja variable overloading system to minimize the
  41. * number of generated rules.
  42. */
  43. class cmGlobalNinjaGenerator : public cmGlobalCommonGenerator
  44. {
  45. public:
  46. /// The default name of Ninja's build file. Typically: build.ninja.
  47. static const char* NINJA_BUILD_FILE;
  48. /// The default name of Ninja's rules file. Typically: rules.ninja.
  49. /// It is included in the main build.ninja file.
  50. static const char* NINJA_RULES_FILE;
  51. /// The indentation string used when generating Ninja's build file.
  52. static const char* INDENT;
  53. /// Write @a count times INDENT level to output stream @a os.
  54. static void Indent(std::ostream& os, int count);
  55. /// Write a divider in the given output stream @a os.
  56. static void WriteDivider(std::ostream& os);
  57. static std::string EncodeRuleName(std::string const& name);
  58. static std::string EncodeIdent(const std::string& ident, std::ostream& vars);
  59. static std::string EncodeLiteral(const std::string& lit);
  60. std::string EncodePath(const std::string& path);
  61. static std::string EncodeDepfileSpace(const std::string& path);
  62. /**
  63. * Write the given @a comment to the output stream @a os. It
  64. * handles new line character properly.
  65. */
  66. static void WriteComment(std::ostream& os, const std::string& comment);
  67. /**
  68. * Utilized by the generator factory to determine if this generator
  69. * supports toolsets.
  70. */
  71. static bool SupportsToolset() { return false; }
  72. /**
  73. * Utilized by the generator factory to determine if this generator
  74. * supports platforms.
  75. */
  76. static bool SupportsPlatform() { return false; }
  77. /**
  78. * Write a build statement to @a os with the @a comment using
  79. * the @a rule the list of @a outputs files and inputs.
  80. * It also writes the variables bound to this build statement.
  81. * @warning no escaping of any kind is done here.
  82. */
  83. void WriteBuild(std::ostream& os, const std::string& comment,
  84. const std::string& rule, const cmNinjaDeps& outputs,
  85. const cmNinjaDeps& implicitOuts,
  86. const cmNinjaDeps& explicitDeps,
  87. const cmNinjaDeps& implicitDeps,
  88. const cmNinjaDeps& orderOnlyDeps,
  89. const cmNinjaVars& variables,
  90. const std::string& rspfile = std::string(),
  91. int cmdLineLimit = 0, bool* usedResponseFile = CM_NULLPTR);
  92. /**
  93. * Helper to write a build statement with the special 'phony' rule.
  94. */
  95. void WritePhonyBuild(std::ostream& os, const std::string& comment,
  96. const cmNinjaDeps& outputs,
  97. const cmNinjaDeps& explicitDeps,
  98. const cmNinjaDeps& implicitDeps = cmNinjaDeps(),
  99. const cmNinjaDeps& orderOnlyDeps = cmNinjaDeps(),
  100. const cmNinjaVars& variables = cmNinjaVars());
  101. void WriteCustomCommandBuild(const std::string& command,
  102. const std::string& description,
  103. const std::string& comment,
  104. const std::string& depfile, bool uses_terminal,
  105. bool restat, const cmNinjaDeps& outputs,
  106. const cmNinjaDeps& deps = cmNinjaDeps(),
  107. const cmNinjaDeps& orderOnly = cmNinjaDeps());
  108. void WriteMacOSXContentBuild(const std::string& input,
  109. const std::string& output);
  110. /**
  111. * Write a rule statement named @a name to @a os with the @a comment,
  112. * the mandatory @a command, the @a depfile and the @a description.
  113. * It also writes the variables bound to this rule statement.
  114. * @warning no escaping of any kind is done here.
  115. */
  116. static void WriteRule(std::ostream& os, const std::string& name,
  117. const std::string& command,
  118. const std::string& description,
  119. const std::string& comment, const std::string& depfile,
  120. const std::string& deptype, const std::string& rspfile,
  121. const std::string& rspcontent,
  122. const std::string& restat, bool generator);
  123. /**
  124. * Write a variable named @a name to @a os with value @a value and an
  125. * optional @a comment. An @a indent level can be specified.
  126. * @warning no escaping of any kind is done here.
  127. */
  128. static void WriteVariable(std::ostream& os, const std::string& name,
  129. const std::string& value,
  130. const std::string& comment = "", int indent = 0);
  131. /**
  132. * Write an include statement including @a filename with an optional
  133. * @a comment to the @a os stream.
  134. */
  135. static void WriteInclude(std::ostream& os, const std::string& filename,
  136. const std::string& comment = "");
  137. /**
  138. * Write a default target statement specifying @a targets as
  139. * the default targets.
  140. */
  141. static void WriteDefault(std::ostream& os, const cmNinjaDeps& targets,
  142. const std::string& comment = "");
  143. bool IsGCCOnWindows() const { return UsingGCCOnWindows; }
  144. public:
  145. cmGlobalNinjaGenerator(cmake* cm);
  146. static cmGlobalGeneratorFactory* NewFactory()
  147. {
  148. return new cmGlobalGeneratorSimpleFactory<cmGlobalNinjaGenerator>();
  149. }
  150. ~cmGlobalNinjaGenerator() CM_OVERRIDE {}
  151. cmLocalGenerator* CreateLocalGenerator(cmMakefile* mf) CM_OVERRIDE;
  152. std::string GetName() const CM_OVERRIDE
  153. {
  154. return cmGlobalNinjaGenerator::GetActualName();
  155. }
  156. static std::string GetActualName() { return "Ninja"; }
  157. static void GetDocumentation(cmDocumentationEntry& entry);
  158. void EnableLanguage(std::vector<std::string> const& languages,
  159. cmMakefile* mf, bool optional) CM_OVERRIDE;
  160. void GenerateBuildCommand(std::vector<std::string>& makeCommand,
  161. const std::string& makeProgram,
  162. const std::string& projectName,
  163. const std::string& projectDir,
  164. const std::string& targetName,
  165. const std::string& config, bool fast, bool verbose,
  166. std::vector<std::string> const& makeOptions =
  167. std::vector<std::string>()) CM_OVERRIDE;
  168. // Setup target names
  169. const char* GetAllTargetName() const CM_OVERRIDE { return "all"; }
  170. const char* GetInstallTargetName() const CM_OVERRIDE { return "install"; }
  171. const char* GetInstallLocalTargetName() const CM_OVERRIDE
  172. {
  173. return "install/local";
  174. }
  175. const char* GetInstallStripTargetName() const CM_OVERRIDE
  176. {
  177. return "install/strip";
  178. }
  179. const char* GetTestTargetName() const CM_OVERRIDE { return "test"; }
  180. const char* GetPackageTargetName() const CM_OVERRIDE { return "package"; }
  181. const char* GetPackageSourceTargetName() const CM_OVERRIDE
  182. {
  183. return "package_source";
  184. }
  185. const char* GetEditCacheTargetName() const CM_OVERRIDE
  186. {
  187. return "edit_cache";
  188. }
  189. const char* GetRebuildCacheTargetName() const CM_OVERRIDE
  190. {
  191. return "rebuild_cache";
  192. }
  193. const char* GetCleanTargetName() const CM_OVERRIDE { return "clean"; }
  194. cmGeneratedFileStream* GetBuildFileStream() const
  195. {
  196. return this->BuildFileStream;
  197. }
  198. cmGeneratedFileStream* GetRulesFileStream() const
  199. {
  200. return this->RulesFileStream;
  201. }
  202. std::string ConvertToNinjaPath(const std::string& path);
  203. std::string ConvertToNinjaFolderRule(const std::string& path);
  204. struct MapToNinjaPathImpl
  205. {
  206. cmGlobalNinjaGenerator* GG;
  207. MapToNinjaPathImpl(cmGlobalNinjaGenerator* gg)
  208. : GG(gg)
  209. {
  210. }
  211. std::string operator()(std::string const& path)
  212. {
  213. return this->GG->ConvertToNinjaPath(path);
  214. }
  215. };
  216. MapToNinjaPathImpl MapToNinjaPath() { return MapToNinjaPathImpl(this); }
  217. void AddCXXCompileCommand(const std::string& commandLine,
  218. const std::string& sourceFile);
  219. /**
  220. * Add a rule to the generated build system.
  221. * Call WriteRule() behind the scene but perform some check before like:
  222. * - Do not add twice the same rule.
  223. */
  224. void AddRule(const std::string& name, const std::string& command,
  225. const std::string& description, const std::string& comment,
  226. const std::string& depfile, const std::string& deptype,
  227. const std::string& rspfile, const std::string& rspcontent,
  228. const std::string& restat, bool generator);
  229. bool HasRule(const std::string& name);
  230. void AddCustomCommandRule();
  231. void AddMacOSXContentRule();
  232. bool HasCustomCommandOutput(const std::string& output)
  233. {
  234. return this->CustomCommandOutputs.find(output) !=
  235. this->CustomCommandOutputs.end();
  236. }
  237. /// Called when we have seen the given custom command. Returns true
  238. /// if we has seen it before.
  239. bool SeenCustomCommand(cmCustomCommand const* cc)
  240. {
  241. return !this->CustomCommands.insert(cc).second;
  242. }
  243. /// Called when we have seen the given custom command output.
  244. void SeenCustomCommandOutput(const std::string& output)
  245. {
  246. this->CustomCommandOutputs.insert(output);
  247. // We don't need the assumed dependencies anymore, because we have
  248. // an output.
  249. this->AssumedSourceDependencies.erase(output);
  250. }
  251. void AddAssumedSourceDependencies(const std::string& source,
  252. const cmNinjaDeps& deps)
  253. {
  254. std::set<std::string>& ASD = this->AssumedSourceDependencies[source];
  255. // Because we may see the same source file multiple times (same source
  256. // specified in multiple targets), compute the union of any assumed
  257. // dependencies.
  258. ASD.insert(deps.begin(), deps.end());
  259. }
  260. void AppendTargetOutputs(cmGeneratorTarget const* target,
  261. cmNinjaDeps& outputs);
  262. void AppendTargetDepends(cmGeneratorTarget const* target,
  263. cmNinjaDeps& outputs);
  264. void AppendTargetDependsClosure(cmGeneratorTarget const* target,
  265. cmNinjaDeps& outputs);
  266. void AddDependencyToAll(cmGeneratorTarget* target);
  267. void AddDependencyToAll(const std::string& input);
  268. const std::vector<cmLocalGenerator*>& GetLocalGenerators() const
  269. {
  270. return LocalGenerators;
  271. }
  272. bool IsExcluded(cmLocalGenerator* root, cmGeneratorTarget* target)
  273. {
  274. return cmGlobalGenerator::IsExcluded(root, target);
  275. }
  276. int GetRuleCmdLength(const std::string& name) { return RuleCmdLength[name]; }
  277. void AddTargetAlias(const std::string& alias, cmGeneratorTarget* target);
  278. void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const CM_OVERRIDE;
  279. // Ninja generator uses 'deps' and 'msvc_deps_prefix' introduced in 1.3
  280. static std::string RequiredNinjaVersion() { return "1.3"; }
  281. static std::string RequiredNinjaVersionForConsolePool() { return "1.5"; }
  282. static std::string RequiredNinjaVersionForImplicitOuts() { return "1.7"; }
  283. bool SupportsConsolePool() const;
  284. bool SupportsImplicitOuts() const;
  285. std::string NinjaOutputPath(std::string const& path);
  286. bool HasOutputPathPrefix() const { return !this->OutputPathPrefix.empty(); }
  287. void StripNinjaOutputPathPrefixAsSuffix(std::string& path);
  288. bool WriteDyndepFile(std::string const& dir_top_src,
  289. std::string const& dir_top_bld,
  290. std::string const& dir_cur_src,
  291. std::string const& dir_cur_bld,
  292. std::string const& arg_dd,
  293. std::vector<std::string> const& arg_ddis,
  294. std::string const& module_dir,
  295. std::vector<std::string> const& linked_target_dirs);
  296. protected:
  297. void Generate() CM_OVERRIDE;
  298. bool CheckALLOW_DUPLICATE_CUSTOM_TARGETS() const CM_OVERRIDE { return true; }
  299. private:
  300. std::string GetEditCacheCommand() const CM_OVERRIDE;
  301. void FindMakeProgram(cmMakefile* mf) CM_OVERRIDE;
  302. void CheckNinjaFeatures();
  303. bool CheckLanguages(std::vector<std::string> const& languages,
  304. cmMakefile* mf) const CM_OVERRIDE;
  305. bool CheckFortran(cmMakefile* mf) const;
  306. void OpenBuildFileStream();
  307. void CloseBuildFileStream();
  308. void CloseCompileCommandsStream();
  309. void OpenRulesFileStream();
  310. void CloseRulesFileStream();
  311. /// Write the common disclaimer text at the top of each build file.
  312. void WriteDisclaimer(std::ostream& os);
  313. void WriteAssumedSourceDependencies();
  314. void WriteTargetAliases(std::ostream& os);
  315. void WriteFolderTargets(std::ostream& os);
  316. void WriteUnknownExplicitDependencies(std::ostream& os);
  317. void WriteBuiltinTargets(std::ostream& os);
  318. void WriteTargetAll(std::ostream& os);
  319. void WriteTargetRebuildManifest(std::ostream& os);
  320. void WriteTargetClean(std::ostream& os);
  321. void WriteTargetHelp(std::ostream& os);
  322. void ComputeTargetDependsClosure(
  323. cmGeneratorTarget const* target,
  324. std::set<cmGeneratorTarget const*>& depends);
  325. std::string ninjaCmd() const;
  326. /// The file containing the build statement. (the relationship of the
  327. /// compilation DAG).
  328. cmGeneratedFileStream* BuildFileStream;
  329. /// The file containing the rule statements. (The action attached to each
  330. /// edge of the compilation DAG).
  331. cmGeneratedFileStream* RulesFileStream;
  332. cmGeneratedFileStream* CompileCommandsStream;
  333. /// The type used to store the set of rules added to the generated build
  334. /// system.
  335. typedef std::set<std::string> RulesSetType;
  336. /// The set of rules added to the generated build system.
  337. RulesSetType Rules;
  338. /// Length of rule command, used by rsp file evaluation
  339. std::map<std::string, int> RuleCmdLength;
  340. /// The set of dependencies to add to the "all" target.
  341. cmNinjaDeps AllDependencies;
  342. bool UsingGCCOnWindows;
  343. /// The set of custom commands we have seen.
  344. std::set<cmCustomCommand const*> CustomCommands;
  345. /// The set of custom command outputs we have seen.
  346. std::set<std::string> CustomCommandOutputs;
  347. /// Whether we are collecting known build outputs and needed
  348. /// dependencies to determine unknown dependencies.
  349. bool ComputingUnknownDependencies;
  350. cmPolicies::PolicyStatus PolicyCMP0058;
  351. /// The combined explicit dependencies of custom build commands
  352. std::set<std::string> CombinedCustomCommandExplicitDependencies;
  353. /// When combined with CombinedCustomCommandExplicitDependencies it allows
  354. /// us to detect the set of explicit dependencies that have
  355. std::set<std::string> CombinedBuildOutputs;
  356. /// The mapping from source file to assumed dependencies.
  357. std::map<std::string, std::set<std::string> > AssumedSourceDependencies;
  358. typedef std::map<std::string, cmGeneratorTarget*> TargetAliasMap;
  359. TargetAliasMap TargetAliases;
  360. typedef std::map<cmGeneratorTarget const*,
  361. std::set<cmGeneratorTarget const*> >
  362. TargetDependsClosureMap;
  363. TargetDependsClosureMap TargetDependsClosures;
  364. std::string NinjaCommand;
  365. std::string NinjaVersion;
  366. bool NinjaSupportsConsolePool;
  367. bool NinjaSupportsImplicitOuts;
  368. unsigned long NinjaSupportsDyndeps;
  369. private:
  370. void InitOutputPathPrefix();
  371. std::string OutputPathPrefix;
  372. std::string TargetAll;
  373. std::string CMakeCacheFile;
  374. };
  375. #endif // ! cmGlobalNinjaGenerator_h