cmLocalUnixMakefileGenerator3.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <iosfwd>
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "cmDepends.h"
  12. #include "cmGeneratorOptions.h"
  13. #include "cmLocalCommonGenerator.h"
  14. class cmCustomCommand;
  15. class cmCustomCommandGenerator;
  16. class cmGeneratorTarget;
  17. class cmGlobalGenerator;
  18. class cmMakefile;
  19. class cmSourceFile;
  20. /** \class cmLocalUnixMakefileGenerator3
  21. * \brief Write a LocalUnix makefiles.
  22. *
  23. * cmLocalUnixMakefileGenerator3 produces a LocalUnix makefile from its
  24. * member Makefile.
  25. */
  26. class cmLocalUnixMakefileGenerator3 : public cmLocalCommonGenerator
  27. {
  28. public:
  29. cmLocalUnixMakefileGenerator3(cmGlobalGenerator* gg, cmMakefile* mf);
  30. ~cmLocalUnixMakefileGenerator3() override;
  31. std::string const& GetConfigName() const;
  32. void ComputeHomeRelativeOutputPath() override;
  33. /**
  34. * Generate the makefile for this directory.
  35. */
  36. void Generate() override;
  37. // this returns the relative path between the HomeOutputDirectory and this
  38. // local generators StartOutputDirectory
  39. std::string const& GetHomeRelativeOutputPath();
  40. /**
  41. * Convert a file path to a Makefile target or dependency with
  42. * escaping and quoting suitable for the generator's make tool.
  43. */
  44. std::string ConvertToMakefilePath(std::string const& path) const;
  45. // Write out a make rule
  46. void WriteMakeRule(std::ostream& os, char const* comment,
  47. std::string const& target,
  48. std::vector<std::string> const& depends,
  49. std::vector<std::string> const& commands, bool symbolic,
  50. bool in_help = false);
  51. // write the main variables used by the makefiles
  52. void WriteMakeVariables(std::ostream& makefileStream);
  53. /**
  54. * Set max makefile variable size, default is 0 which means unlimited.
  55. */
  56. void SetMakefileVariableSize(int s) { this->MakefileVariableSize = s; }
  57. /**
  58. * Set whether passing a make target on a command line requires an
  59. * extra level of escapes.
  60. */
  61. void SetMakeCommandEscapeTargetTwice(bool b)
  62. {
  63. this->MakeCommandEscapeTargetTwice = b;
  64. }
  65. /**
  66. * Set whether the Borland curly brace command line hack should be
  67. * applied.
  68. */
  69. void SetBorlandMakeCurlyHack(bool b) { this->BorlandMakeCurlyHack = b; }
  70. // used in writing out Cmake files such as WriteDirectoryInformation
  71. static void WriteCMakeArgument(std::ostream& os, std::string const& s);
  72. /** creates the common disclaimer text at the top of each makefile */
  73. void WriteDisclaimer(std::ostream& os);
  74. // write a comment line #====... in the stream
  75. void WriteDivider(std::ostream& os);
  76. /** used to create a recursive make call */
  77. std::string GetRecursiveMakeCall(std::string const& makefile,
  78. std::string const& tgt);
  79. // append flags to a string
  80. void AppendFlags(std::string& flags,
  81. std::string const& newFlags) const override;
  82. using cmLocalCommonGenerator::AppendFlags;
  83. // append an echo command
  84. enum EchoColor
  85. {
  86. EchoNormal,
  87. EchoDepend,
  88. EchoBuild,
  89. EchoLink,
  90. EchoGenerate,
  91. EchoGlobal
  92. };
  93. struct EchoProgress
  94. {
  95. std::string Dir;
  96. std::string Arg;
  97. };
  98. void AppendEcho(std::vector<std::string>& commands, std::string const& text,
  99. EchoColor color = EchoNormal, EchoProgress const* = nullptr);
  100. /** Get whether the makefile is to have color. */
  101. bool GetColorMakefile() const { return this->ColorMakefile; }
  102. // create a command that cds to the start dir then runs the commands
  103. void CreateCDCommand(std::vector<std::string>& commands,
  104. std::string const& targetDir,
  105. std::string const& relDir);
  106. static std::string ConvertToQuotedOutputPath(std::string const& p,
  107. bool useWatcomQuote);
  108. std::string CreateMakeVariable(std::string const& sin,
  109. std::string const& s2in);
  110. /** Called from command-line hook to bring dependencies up to date
  111. for a target. */
  112. bool UpdateDependencies(std::string const& tgtInfo, bool verbose,
  113. bool color) override;
  114. /** Called from command-line hook to clear dependencies. */
  115. void ClearDependencies(cmMakefile* mf, bool verbose) override;
  116. /** write some extra rules such as make test etc */
  117. void WriteSpecialTargetsTop(std::ostream& makefileStream);
  118. void WriteSpecialTargetsBottom(std::ostream& makefileStream);
  119. std::string GetRelativeTargetDirectory(
  120. cmGeneratorTarget const* target) const;
  121. // File pairs for implicit dependency scanning. The key of the map
  122. // is the depender and the value is the explicit dependee.
  123. using ImplicitDependFileMap = cmDepends::DependencyMap;
  124. using ImplicitDependLanguageMap =
  125. std::map<std::string, ImplicitDependFileMap>;
  126. using ImplicitDependScannerMap =
  127. std::map<cmDependencyScannerKind, ImplicitDependLanguageMap>;
  128. using ImplicitDependTargetMap =
  129. std::map<std::string, ImplicitDependScannerMap>;
  130. ImplicitDependLanguageMap const& GetImplicitDepends(
  131. cmGeneratorTarget const* tgt,
  132. cmDependencyScannerKind scanner = cmDependencyScannerKind::CMake);
  133. void AddImplicitDepends(
  134. cmGeneratorTarget const* tgt, std::string const& lang,
  135. std::string const& obj, std::string const& src,
  136. cmDependencyScannerKind scanner = cmDependencyScannerKind::CMake);
  137. // write the target rules for the local Makefile into the stream
  138. void WriteLocalAllRules(std::ostream& ruleFileStream);
  139. std::vector<std::string> const& GetLocalHelp() { return this->LocalHelp; }
  140. /** Get whether to create rules to generate preprocessed and
  141. assembly sources. This could be converted to a variable lookup
  142. later. */
  143. bool GetCreatePreprocessedSourceRules() const
  144. {
  145. return !this->SkipPreprocessedSourceRules;
  146. }
  147. bool GetCreateAssemblySourceRules() const
  148. {
  149. return !this->SkipAssemblySourceRules;
  150. }
  151. // Fill the vector with the target names for the object files,
  152. // preprocessed files and assembly files. Currently only used by the
  153. // Eclipse generator.
  154. void GetIndividualFileTargets(std::vector<std::string>& targets);
  155. std::string GetLinkDependencyFile(cmGeneratorTarget* target,
  156. std::string const& config) const override;
  157. protected:
  158. void WriteLocalMakefile();
  159. // write the target rules for the local Makefile into the stream
  160. void WriteLocalMakefileTargets(std::ostream& ruleFileStream,
  161. std::set<std::string>& emitted);
  162. // this method Writes the Directory information files
  163. void WriteDirectoryInformationFile();
  164. // write the depend info
  165. void WriteDependLanguageInfo(std::ostream& cmakefileStream,
  166. cmGeneratorTarget* tgt);
  167. // this converts a file name that is relative to the StartOutputDirectory
  168. // into a full path
  169. std::string ConvertToFullPath(std::string const& localPath);
  170. void WriteConvenienceRule(std::ostream& ruleFileStream,
  171. std::string const& realTarget,
  172. std::string const& helpTarget);
  173. void AppendRuleDepend(std::vector<std::string>& depends,
  174. char const* ruleFileName);
  175. void AppendRuleDepends(std::vector<std::string>& depends,
  176. std::vector<std::string> const& ruleFiles);
  177. void AppendCustomDepends(std::vector<std::string>& depends,
  178. std::vector<cmCustomCommand> const& ccs);
  179. void AppendCustomDepend(std::vector<std::string>& depends,
  180. cmCustomCommandGenerator const& cc);
  181. void AppendCustomCommands(std::vector<std::string>& commands,
  182. std::vector<cmCustomCommand> const& ccs,
  183. cmGeneratorTarget* target,
  184. std::string const& relative);
  185. void AppendCustomCommand(std::vector<std::string>& commands,
  186. cmCustomCommandGenerator const& ccg,
  187. cmGeneratorTarget* target,
  188. std::string const& relative,
  189. bool echo_comment = false,
  190. std::ostream* content = nullptr);
  191. void AppendCleanCommand(std::vector<std::string>& commands,
  192. std::set<std::string> const& files,
  193. cmGeneratorTarget* target,
  194. char const* filename = nullptr);
  195. void AppendDirectoryCleanCommand(std::vector<std::string>& commands);
  196. // Helper methods for dependency updates.
  197. bool ScanDependencies(std::string const& targetDir,
  198. std::string const& dependFile,
  199. std::string const& internalDependFile,
  200. cmDepends::DependencyMap& validDeps);
  201. void CheckMultipleOutputs(bool verbose);
  202. private:
  203. std::string MaybeConvertWatcomShellCommand(std::string const& cmd);
  204. friend class cmMakefileTargetGenerator;
  205. friend class cmMakefileExecutableTargetGenerator;
  206. friend class cmMakefileLibraryTargetGenerator;
  207. friend class cmMakefileUtilityTargetGenerator;
  208. friend class cmGlobalUnixMakefileGenerator3;
  209. ImplicitDependTargetMap ImplicitDepends;
  210. std::string HomeRelativeOutputPath;
  211. struct LocalObjectEntry
  212. {
  213. cmGeneratorTarget* Target = nullptr;
  214. std::string Language;
  215. LocalObjectEntry() = default;
  216. LocalObjectEntry(cmGeneratorTarget* t, std::string lang)
  217. : Target(t)
  218. , Language(std::move(lang))
  219. {
  220. }
  221. };
  222. struct LocalObjectInfo : public std::vector<LocalObjectEntry>
  223. {
  224. bool HasSourceExtension = false;
  225. bool HasPreprocessRule = false;
  226. bool HasAssembleRule = false;
  227. };
  228. void GetLocalObjectFiles(
  229. std::map<std::string, LocalObjectInfo>& localObjectFiles);
  230. void WriteObjectConvenienceRule(std::ostream& ruleFileStream,
  231. char const* comment,
  232. std::string const& output,
  233. LocalObjectInfo const& info);
  234. std::vector<std::string> LocalHelp;
  235. /* does the work for each target */
  236. std::map<std::string, std::string> MakeVariableMap;
  237. std::map<std::string, std::string> ShortMakeVariableMap;
  238. int MakefileVariableSize;
  239. bool MakeCommandEscapeTargetTwice;
  240. bool BorlandMakeCurlyHack;
  241. bool ColorMakefile;
  242. bool SkipPreprocessedSourceRules;
  243. bool SkipAssemblySourceRules;
  244. std::set<cmSourceFile const*>& GetCommandsVisited(
  245. cmGeneratorTarget const* target)
  246. {
  247. return this->CommandsVisited[target];
  248. }
  249. std::map<cmGeneratorTarget const*, std::set<cmSourceFile const*>>
  250. CommandsVisited;
  251. };