cmGlobalNinjaGenerator.h 16 KB

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