cmLocalUnixMakefileGenerator3.h 9.9 KB

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