cmGlobalNinjaGenerator.h 17 KB

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