cmLocalUnixMakefileGenerator3.h 10 KB

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