cmLocalUnixMakefileGenerator3.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. // 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 std::string& 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 std::string& 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 : public cmDepends::DependencyMap
  117. {
  118. };
  119. struct ImplicitDependLanguageMap
  120. : public std::map<std::string, ImplicitDependFileMap>
  121. {
  122. };
  123. struct ImplicitDependTargetMap
  124. : public std::map<std::string, ImplicitDependLanguageMap>
  125. {
  126. };
  127. ImplicitDependLanguageMap const& GetImplicitDepends(
  128. cmGeneratorTarget const* tgt);
  129. void AddImplicitDepends(cmGeneratorTarget const* tgt,
  130. const std::string& lang, const std::string& obj,
  131. const std::string& src);
  132. // write the target rules for the local Makefile into the stream
  133. void WriteLocalAllRules(std::ostream& ruleFileStream);
  134. std::vector<std::string> const& GetLocalHelp() { return this->LocalHelp; }
  135. /** Get whether to create rules to generate preprocessed and
  136. assembly sources. This could be converted to a variable lookup
  137. later. */
  138. bool GetCreatePreprocessedSourceRules()
  139. {
  140. return !this->SkipPreprocessedSourceRules;
  141. }
  142. bool GetCreateAssemblySourceRules()
  143. {
  144. return !this->SkipAssemblySourceRules;
  145. }
  146. // Fill the vector with the target names for the object files,
  147. // preprocessed files and assembly files. Currently only used by the
  148. // Eclipse generator.
  149. void GetIndividualFileTargets(std::vector<std::string>& targets);
  150. protected:
  151. void WriteLocalMakefile();
  152. // write the target rules for the local Makefile into the stream
  153. void WriteLocalMakefileTargets(std::ostream& ruleFileStream,
  154. std::set<std::string>& emitted);
  155. // this method Writes the Directory information files
  156. void WriteDirectoryInformationFile();
  157. // write the depend info
  158. void WriteDependLanguageInfo(std::ostream& cmakefileStream,
  159. cmGeneratorTarget* tgt);
  160. // this converts a file name that is relative to the StartOuputDirectory
  161. // into a full path
  162. std::string ConvertToFullPath(const std::string& localPath);
  163. void WriteConvenienceRule(std::ostream& ruleFileStream,
  164. const std::string& realTarget,
  165. const std::string& helpTarget);
  166. void AppendRuleDepend(std::vector<std::string>& depends,
  167. const char* ruleFileName);
  168. void AppendRuleDepends(std::vector<std::string>& depends,
  169. std::vector<std::string> const& ruleFiles);
  170. void AppendCustomDepends(std::vector<std::string>& depends,
  171. const std::vector<cmCustomCommand>& ccs);
  172. void AppendCustomDepend(std::vector<std::string>& depends,
  173. cmCustomCommandGenerator const& cc);
  174. void AppendCustomCommands(std::vector<std::string>& commands,
  175. const std::vector<cmCustomCommand>& ccs,
  176. cmGeneratorTarget* target,
  177. std::string const& relative);
  178. void AppendCustomCommand(std::vector<std::string>& commands,
  179. cmCustomCommandGenerator const& ccg,
  180. cmGeneratorTarget* target,
  181. std::string const& relative,
  182. bool echo_comment = false,
  183. std::ostream* content = nullptr);
  184. void AppendCleanCommand(std::vector<std::string>& commands,
  185. const std::set<std::string>& files,
  186. cmGeneratorTarget* target,
  187. const char* filename = nullptr);
  188. void AppendDirectoryCleanCommand(std::vector<std::string>& commands);
  189. // Helper methods for dependency updates.
  190. bool ScanDependencies(std::string const& targetDir,
  191. std::string const& dependFile,
  192. std::string const& internalDependFile,
  193. cmDepends::DependencyMap& validDeps);
  194. void CheckMultipleOutputs(bool verbose);
  195. private:
  196. std::string MaybeConvertWatcomShellCommand(std::string const& cmd);
  197. friend class cmMakefileTargetGenerator;
  198. friend class cmMakefileExecutableTargetGenerator;
  199. friend class cmMakefileLibraryTargetGenerator;
  200. friend class cmMakefileUtilityTargetGenerator;
  201. friend class cmGlobalUnixMakefileGenerator3;
  202. ImplicitDependTargetMap ImplicitDepends;
  203. std::string HomeRelativeOutputPath;
  204. struct LocalObjectEntry
  205. {
  206. cmGeneratorTarget* Target = nullptr;
  207. std::string Language;
  208. LocalObjectEntry() = default;
  209. LocalObjectEntry(cmGeneratorTarget* t, std::string lang)
  210. : Target(t)
  211. , Language(std::move(lang))
  212. {
  213. }
  214. };
  215. struct LocalObjectInfo : public std::vector<LocalObjectEntry>
  216. {
  217. bool HasSourceExtension = false;
  218. bool HasPreprocessRule = false;
  219. bool HasAssembleRule = false;
  220. };
  221. void GetLocalObjectFiles(
  222. std::map<std::string, LocalObjectInfo>& localObjectFiles);
  223. void WriteObjectConvenienceRule(std::ostream& ruleFileStream,
  224. const char* comment,
  225. const std::string& output,
  226. LocalObjectInfo const& info);
  227. std::vector<std::string> LocalHelp;
  228. /* does the work for each target */
  229. std::map<std::string, std::string> MakeVariableMap;
  230. std::map<std::string, std::string> ShortMakeVariableMap;
  231. int MakefileVariableSize;
  232. bool MakeCommandEscapeTargetTwice;
  233. bool BorlandMakeCurlyHack;
  234. bool ColorMakefile;
  235. bool SkipPreprocessedSourceRules;
  236. bool SkipAssemblySourceRules;
  237. };
  238. #endif