cmLocalUnixMakefileGenerator3.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 std::string& 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. using cmLocalCommonGenerator::AppendFlags;
  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 std::string& 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 std::string& 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(
  115. cmGeneratorTarget const* target) const;
  116. // File pairs for implicit dependency scanning. The key of the map
  117. // is the depender and the value is the explicit dependee.
  118. struct ImplicitDependFileMap : public cmDepends::DependencyMap
  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 std::string& obj,
  133. const std::string& 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. protected:
  153. void WriteLocalMakefile();
  154. // write the target rules for the local Makefile into the stream
  155. void WriteLocalMakefileTargets(std::ostream& ruleFileStream,
  156. std::set<std::string>& emitted);
  157. // this method Writes the Directory information files
  158. void WriteDirectoryInformationFile();
  159. // write the depend info
  160. void WriteDependLanguageInfo(std::ostream& cmakefileStream,
  161. cmGeneratorTarget* tgt);
  162. // this converts a file name that is relative to the StartOuputDirectory
  163. // into a full path
  164. std::string ConvertToFullPath(const std::string& localPath);
  165. void WriteConvenienceRule(std::ostream& ruleFileStream,
  166. const std::string& realTarget,
  167. const std::string& helpTarget);
  168. void AppendRuleDepend(std::vector<std::string>& depends,
  169. const char* ruleFileName);
  170. void AppendRuleDepends(std::vector<std::string>& depends,
  171. std::vector<std::string> const& ruleFiles);
  172. void AppendCustomDepends(std::vector<std::string>& depends,
  173. const std::vector<cmCustomCommand>& ccs);
  174. void AppendCustomDepend(std::vector<std::string>& depends,
  175. cmCustomCommandGenerator const& cc);
  176. void AppendCustomCommands(std::vector<std::string>& commands,
  177. const std::vector<cmCustomCommand>& ccs,
  178. cmGeneratorTarget* target,
  179. std::string const& relative);
  180. void AppendCustomCommand(std::vector<std::string>& commands,
  181. cmCustomCommandGenerator const& ccg,
  182. cmGeneratorTarget* target,
  183. std::string const& relative,
  184. bool echo_comment = false,
  185. std::ostream* content = nullptr);
  186. void AppendCleanCommand(std::vector<std::string>& commands,
  187. const std::set<std::string>& files,
  188. cmGeneratorTarget* target,
  189. const char* filename = nullptr);
  190. void AppendDirectoryCleanCommand(std::vector<std::string>& commands);
  191. // Helper methods for dependency updates.
  192. bool ScanDependencies(std::string const& targetDir,
  193. std::string const& dependFile,
  194. std::string const& internalDependFile,
  195. cmDepends::DependencyMap& validDeps);
  196. void CheckMultipleOutputs(bool verbose);
  197. private:
  198. std::string MaybeConvertWatcomShellCommand(std::string const& cmd);
  199. friend class cmMakefileTargetGenerator;
  200. friend class cmMakefileExecutableTargetGenerator;
  201. friend class cmMakefileLibraryTargetGenerator;
  202. friend class cmMakefileUtilityTargetGenerator;
  203. friend class cmGlobalUnixMakefileGenerator3;
  204. ImplicitDependTargetMap ImplicitDepends;
  205. std::string HomeRelativeOutputPath;
  206. struct LocalObjectEntry
  207. {
  208. cmGeneratorTarget* Target = nullptr;
  209. std::string Language;
  210. LocalObjectEntry() = default;
  211. LocalObjectEntry(cmGeneratorTarget* t, std::string lang)
  212. : Target(t)
  213. , Language(std::move(lang))
  214. {
  215. }
  216. };
  217. struct LocalObjectInfo : public std::vector<LocalObjectEntry>
  218. {
  219. bool HasSourceExtension = false;
  220. bool HasPreprocessRule = false;
  221. bool HasAssembleRule = false;
  222. };
  223. void GetLocalObjectFiles(
  224. std::map<std::string, LocalObjectInfo>& localObjectFiles);
  225. void WriteObjectConvenienceRule(std::ostream& ruleFileStream,
  226. const char* comment,
  227. const std::string& output,
  228. LocalObjectInfo const& info);
  229. std::vector<std::string> LocalHelp;
  230. /* does the work for each target */
  231. std::map<std::string, std::string> MakeVariableMap;
  232. std::map<std::string, std::string> ShortMakeVariableMap;
  233. int MakefileVariableSize;
  234. bool MakeCommandEscapeTargetTwice;
  235. bool BorlandMakeCurlyHack;
  236. bool ColorMakefile;
  237. bool SkipPreprocessedSourceRules;
  238. bool SkipAssemblySourceRules;
  239. };
  240. #endif