cmLocalUnixMakefileGenerator3.h 10.0 KB

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