cmLocalUnixMakefileGenerator3.h 10 KB

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