cmLocalUnixMakefileGenerator3.h 11 KB

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