cmLocalUnixMakefileGenerator3.h 11 KB

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