cmGlobalUnixMakefileGenerator3.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <cstddef>
  6. #include <iosfwd>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include <vector>
  12. #include "cmBuildOptions.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmGlobalCommonGenerator.h"
  15. #include "cmGlobalGeneratorFactory.h"
  16. #include "cmStateSnapshot.h"
  17. class cmGeneratedFileStream;
  18. class cmLocalGenerator;
  19. class cmLocalUnixMakefileGenerator3;
  20. class cmMakefile;
  21. class cmMakefileTargetGenerator;
  22. class cmake;
  23. /** \class cmGlobalUnixMakefileGenerator3
  24. * \brief Write a Unix makefiles.
  25. *
  26. * cmGlobalUnixMakefileGenerator3 manages UNIX build process for a tree
  27. The basic approach of this generator is to produce Makefiles that will all
  28. be run with the current working directory set to the Home Output
  29. directory. The one exception to this is the subdirectory Makefiles which are
  30. created as a convenience and just cd up to the Home Output directory and
  31. invoke the main Makefiles.
  32. The make process starts with Makefile. Makefile should only contain the
  33. targets the user is likely to invoke directly from a make command line. No
  34. internal targets should be in this file. Makefile2 contains the internal
  35. targets that are required to make the process work.
  36. Makefile2 in turn will recursively make targets in the correct order. Each
  37. target has its own directory \<target\>.dir and its own makefile build.make in
  38. that directory. Also in that directory is a couple makefiles per source file
  39. used by the target. Typically these are named source.obj.build.make and
  40. source.obj.build.depend.make. The source.obj.build.make contains the rules
  41. for building, cleaning, and computing dependencies for the given source
  42. file. The build.depend.make contains additional dependencies that were
  43. computed during dependency scanning. An additional file called
  44. source.obj.depend is used as a marker to indicate when dependencies must be
  45. rescanned.
  46. Rules for custom commands follow the same model as rules for source files.
  47. */
  48. class cmGlobalUnixMakefileGenerator3 : public cmGlobalCommonGenerator
  49. {
  50. public:
  51. cmGlobalUnixMakefileGenerator3(cmake* cm);
  52. static std::unique_ptr<cmGlobalGeneratorFactory> NewFactory()
  53. {
  54. return std::unique_ptr<cmGlobalGeneratorFactory>(
  55. new cmGlobalGeneratorSimpleFactory<cmGlobalUnixMakefileGenerator3>());
  56. }
  57. ~cmGlobalUnixMakefileGenerator3() override;
  58. cmGlobalUnixMakefileGenerator3(const cmGlobalUnixMakefileGenerator3&) =
  59. delete;
  60. cmGlobalUnixMakefileGenerator3& operator=(
  61. const cmGlobalUnixMakefileGenerator3&) = delete;
  62. //! Get the name for the generator.
  63. std::string GetName() const override
  64. {
  65. return cmGlobalUnixMakefileGenerator3::GetActualName();
  66. }
  67. static std::string GetActualName() { return "Unix Makefiles"; }
  68. /**
  69. * Utilized by the generator factory to determine if this generator
  70. * supports toolsets.
  71. */
  72. static bool SupportsToolset() { return false; }
  73. /**
  74. * Utilized by the generator factory to determine if this generator
  75. * supports platforms.
  76. */
  77. static bool SupportsPlatform() { return false; }
  78. /**
  79. * Utilized to determine if this generator
  80. * supports DEPFILE option.
  81. */
  82. bool SupportsCustomCommandDepfile() const override { return true; }
  83. /**
  84. * Utilized to determine if this generator
  85. * supports linker dependency file.
  86. */
  87. bool SupportsLinkerDependencyFile() const override { return true; }
  88. /** Get the documentation entry for this generator. */
  89. static cmDocumentationEntry GetDocumentation();
  90. std::unique_ptr<cmLocalGenerator> CreateLocalGenerator(
  91. cmMakefile* mf) override;
  92. /**
  93. * Try to determine system information such as shared library
  94. * extension, pthreads, byte order etc.
  95. */
  96. void EnableLanguage(std::vector<std::string> const& languages, cmMakefile*,
  97. bool optional) override;
  98. void Configure() override;
  99. bool IsGNUMakeJobServerAware() const override { return true; }
  100. /**
  101. * Generate the all required files for building this project/tree. This
  102. * basically creates a series of LocalGenerators for each directory and
  103. * requests that they Generate.
  104. */
  105. void Generate() override;
  106. void WriteMainCMakefileLanguageRules(
  107. cmGeneratedFileStream& cmakefileStream,
  108. std::vector<std::unique_ptr<cmLocalGenerator>>&);
  109. // write out the help rule listing the valid targets
  110. void WriteHelpRule(std::ostream& ruleFileStream,
  111. cmLocalUnixMakefileGenerator3*);
  112. // write the top level target rules
  113. void WriteConvenienceRules(std::ostream& ruleFileStream,
  114. std::set<std::string>& emitted);
  115. // Make tool supports dependency files generated by compiler
  116. bool SupportsCompilerDependencies() const
  117. {
  118. return this->ToolSupportsCompilerDependencies;
  119. }
  120. // Make tool supports long line dependencies
  121. bool SupportsLongLineDependencies() const
  122. {
  123. return this->ToolSupportsLongLineDependencies;
  124. }
  125. /** Get the command to use for a target that has no rule. This is
  126. used for multiple output dependencies and for cmake_force. */
  127. std::string GetEmptyRuleHackCommand() { return this->EmptyRuleHackCommand; }
  128. /** Get the fake dependency to use when a rule has no real commands
  129. or dependencies. */
  130. std::string GetEmptyRuleHackDepends() { return this->EmptyRuleHackDepends; }
  131. /**
  132. * Convert a file path to a Makefile target or dependency with
  133. * escaping and quoting suitable for the generator's make tool.
  134. */
  135. std::string ConvertToMakefilePath(std::string const& path) const;
  136. // change the build command for speed
  137. std::vector<GeneratedMakeCommand> GenerateBuildCommand(
  138. const std::string& makeProgram, const std::string& projectName,
  139. const std::string& projectDir, std::vector<std::string> const& targetNames,
  140. const std::string& config, int jobs, bool verbose,
  141. const cmBuildOptions& buildOptions = cmBuildOptions(),
  142. std::vector<std::string> const& makeOptions =
  143. std::vector<std::string>()) override;
  144. /** Record per-target progress information. */
  145. void RecordTargetProgress(cmMakefileTargetGenerator* tg);
  146. void AddCXXCompileCommand(const std::string& sourceFile,
  147. const std::string& workingDirectory,
  148. const std::string& compileCommand,
  149. const std::string& objPath);
  150. /** Does the make tool tolerate .NOTPARALLEL? */
  151. virtual bool AllowNotParallel() const { return true; }
  152. /** Does the make tool tolerate .DELETE_ON_ERROR? */
  153. virtual bool AllowDeleteOnError() const { return true; }
  154. /** Does the make tool interpret '\#' as '#'? */
  155. virtual bool CanEscapeOctothorpe() const;
  156. bool IsIPOSupported() const override { return true; }
  157. void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const override;
  158. std::string IncludeDirective;
  159. std::string LineContinueDirective;
  160. bool DefineWindowsNULL;
  161. bool PassMakeflags;
  162. bool UnixCD;
  163. protected:
  164. void WriteMainMakefile2();
  165. void WriteMainCMakefile();
  166. void WriteConvenienceRules2(std::ostream& ruleFileStream,
  167. cmLocalUnixMakefileGenerator3& rootLG,
  168. cmLocalUnixMakefileGenerator3& lg);
  169. void WriteDirectoryRule2(std::ostream& ruleFileStream,
  170. cmLocalUnixMakefileGenerator3& rootLG,
  171. DirectoryTarget const& dt, const char* pass,
  172. bool check_all, bool check_relink,
  173. std::vector<std::string> const& commands = {});
  174. void WriteDirectoryRules2(std::ostream& ruleFileStream,
  175. cmLocalUnixMakefileGenerator3& rootLG,
  176. DirectoryTarget const& dt);
  177. void AppendGlobalTargetDepends(std::vector<std::string>& depends,
  178. cmGeneratorTarget* target);
  179. // Target name hooks for superclass.
  180. const char* GetAllTargetName() const override { return "all"; }
  181. const char* GetInstallTargetName() const override { return "install"; }
  182. const char* GetInstallLocalTargetName() const override
  183. {
  184. return "install/local";
  185. }
  186. const char* GetInstallStripTargetName() const override
  187. {
  188. return "install/strip";
  189. }
  190. const char* GetPreinstallTargetName() const override { return "preinstall"; }
  191. const char* GetTestTargetName() const override { return "test"; }
  192. const char* GetPackageTargetName() const override { return "package"; }
  193. const char* GetPackageSourceTargetName() const override
  194. {
  195. return "package_source";
  196. }
  197. const char* GetRebuildCacheTargetName() const override
  198. {
  199. return "rebuild_cache";
  200. }
  201. const char* GetCleanTargetName() const override { return "clean"; }
  202. bool CheckALLOW_DUPLICATE_CUSTOM_TARGETS() const override { return true; }
  203. // Specify if the make tool is able to consume dependency files
  204. // generated by the compiler
  205. bool ToolSupportsCompilerDependencies = true;
  206. // some Make generator, such as Borland not support long line dependencies,
  207. // we add SupportsLongLineDependencies to predicate.
  208. bool ToolSupportsLongLineDependencies = true;
  209. // Some make programs (Borland) do not keep a rule if there are no
  210. // dependencies or commands. This is a problem for creating rules
  211. // that might not do anything but might have other dependencies
  212. // added later. If non-empty this variable holds a fake dependency
  213. // that can be added.
  214. std::string EmptyRuleHackDepends;
  215. // Some make programs (Watcom) do not like rules with no commands.
  216. // If non-empty this variable holds a bogus command that may be put
  217. // in the rule to satisfy the make program.
  218. std::string EmptyRuleHackCommand;
  219. // Store per-target progress counters.
  220. struct TargetProgress
  221. {
  222. unsigned long NumberOfActions = 0;
  223. std::string VariableFile;
  224. std::vector<unsigned long> Marks;
  225. void WriteProgressVariables(unsigned long total, unsigned long& current);
  226. };
  227. using ProgressMapType = std::map<cmGeneratorTarget const*, TargetProgress,
  228. cmGeneratorTarget::StrictTargetComparison>;
  229. ProgressMapType ProgressMap;
  230. size_t CountProgressMarksInTarget(
  231. cmGeneratorTarget const* target,
  232. std::set<cmGeneratorTarget const*>& emitted);
  233. size_t CountProgressMarksInAll(const cmLocalGenerator& lg);
  234. std::unique_ptr<cmGeneratedFileStream> CommandDatabase;
  235. private:
  236. const char* GetBuildIgnoreErrorsFlag() const override { return "-i"; }
  237. std::map<cmStateSnapshot, std::set<cmGeneratorTarget const*>,
  238. cmStateSnapshot::StrictWeakOrder>
  239. DirectoryTargetsMap;
  240. void InitializeProgressMarks() override;
  241. };