cmGlobalNinjaGenerator.h 17 KB

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