cmGlobalNinjaGenerator.h 16 KB

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