cmGlobalNinjaGenerator.h 16 KB

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