cmGlobalUnixMakefileGenerator3.h 10 KB

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