cmGlobalUnixMakefileGenerator3.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 cmGlobalUnixMakefileGenerator3_h
  4. #define cmGlobalUnixMakefileGenerator3_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <cstddef>
  7. #include <iosfwd>
  8. #include <map>
  9. #include <memory>
  10. #include <set>
  11. #include <string>
  12. #include <vector>
  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. /** Get the documentation entry for this generator. */
  80. static void GetDocumentation(cmDocumentationEntry& entry);
  81. std::unique_ptr<cmLocalGenerator> CreateLocalGenerator(
  82. cmMakefile* mf) override;
  83. /**
  84. * Try to determine system information such as shared library
  85. * extension, pthreads, byte order etc.
  86. */
  87. void EnableLanguage(std::vector<std::string> const& languages, cmMakefile*,
  88. bool optional) override;
  89. void Configure() override;
  90. /**
  91. * Generate the all required files for building this project/tree. This
  92. * basically creates a series of LocalGenerators for each directory and
  93. * requests that they Generate.
  94. */
  95. void Generate() override;
  96. void WriteMainCMakefileLanguageRules(
  97. cmGeneratedFileStream& cmakefileStream,
  98. std::vector<std::unique_ptr<cmLocalGenerator>>&);
  99. // write out the help rule listing the valid targets
  100. void WriteHelpRule(std::ostream& ruleFileStream,
  101. cmLocalUnixMakefileGenerator3*);
  102. // write the top level target rules
  103. void WriteConvenienceRules(std::ostream& ruleFileStream,
  104. std::set<std::string>& emitted);
  105. /** Get the command to use for a target that has no rule. This is
  106. used for multiple output dependencies and for cmake_force. */
  107. std::string GetEmptyRuleHackCommand() { return this->EmptyRuleHackCommand; }
  108. /** Get the fake dependency to use when a rule has no real commands
  109. or dependencies. */
  110. std::string GetEmptyRuleHackDepends() { return this->EmptyRuleHackDepends; }
  111. // change the build command for speed
  112. std::vector<GeneratedMakeCommand> GenerateBuildCommand(
  113. const std::string& makeProgram, const std::string& projectName,
  114. const std::string& projectDir, std::vector<std::string> const& targetNames,
  115. const std::string& config, bool fast, int jobs, bool verbose,
  116. std::vector<std::string> const& makeOptions =
  117. std::vector<std::string>()) override;
  118. /** Record per-target progress information. */
  119. void RecordTargetProgress(cmMakefileTargetGenerator* tg);
  120. void AddCXXCompileCommand(const std::string& sourceFile,
  121. const std::string& workingDirectory,
  122. const std::string& compileCommand);
  123. /** Does the make tool tolerate .NOTPARALLEL? */
  124. virtual bool AllowNotParallel() const { return true; }
  125. /** Does the make tool tolerate .DELETE_ON_ERROR? */
  126. virtual bool AllowDeleteOnError() const { return true; }
  127. bool IsIPOSupported() const override { return true; }
  128. void ComputeTargetObjectDirectory(cmGeneratorTarget* gt) const override;
  129. std::string IncludeDirective;
  130. bool DefineWindowsNULL;
  131. bool PassMakeflags;
  132. bool UnixCD;
  133. protected:
  134. void WriteMainMakefile2();
  135. void WriteMainCMakefile();
  136. void WriteConvenienceRules2(std::ostream& ruleFileStream,
  137. cmLocalUnixMakefileGenerator3&);
  138. void WriteDirectoryRule2(std::ostream& ruleFileStream,
  139. DirectoryTarget const& dt, const char* pass,
  140. bool check_all, bool check_relink,
  141. std::vector<std::string> const& commands = {});
  142. void WriteDirectoryRules2(std::ostream& ruleFileStream,
  143. DirectoryTarget const& dt);
  144. void AppendGlobalTargetDepends(std::vector<std::string>& depends,
  145. cmGeneratorTarget* target);
  146. // Target name hooks for superclass.
  147. const char* GetAllTargetName() const override { return "all"; }
  148. const char* GetInstallTargetName() const override { return "install"; }
  149. const char* GetInstallLocalTargetName() const override
  150. {
  151. return "install/local";
  152. }
  153. const char* GetInstallStripTargetName() const override
  154. {
  155. return "install/strip";
  156. }
  157. const char* GetPreinstallTargetName() const override { return "preinstall"; }
  158. const char* GetTestTargetName() const override { return "test"; }
  159. const char* GetPackageTargetName() const override { return "package"; }
  160. const char* GetPackageSourceTargetName() const override
  161. {
  162. return "package_source";
  163. }
  164. const char* GetEditCacheTargetName() const override { return "edit_cache"; }
  165. const char* GetRebuildCacheTargetName() const override
  166. {
  167. return "rebuild_cache";
  168. }
  169. const char* GetCleanTargetName() const override { return "clean"; }
  170. bool CheckALLOW_DUPLICATE_CUSTOM_TARGETS() const override { return true; }
  171. // Some make programs (Borland) do not keep a rule if there are no
  172. // dependencies or commands. This is a problem for creating rules
  173. // that might not do anything but might have other dependencies
  174. // added later. If non-empty this variable holds a fake dependency
  175. // that can be added.
  176. std::string EmptyRuleHackDepends;
  177. // Some make programs (Watcom) do not like rules with no commands.
  178. // If non-empty this variable holds a bogus command that may be put
  179. // in the rule to satisfy the make program.
  180. std::string EmptyRuleHackCommand;
  181. // Store per-target progress counters.
  182. struct TargetProgress
  183. {
  184. unsigned long NumberOfActions = 0;
  185. std::string VariableFile;
  186. std::vector<unsigned long> Marks;
  187. void WriteProgressVariables(unsigned long total, unsigned long& current);
  188. };
  189. using ProgressMapType = std::map<cmGeneratorTarget const*, TargetProgress,
  190. cmGeneratorTarget::StrictTargetComparison>;
  191. ProgressMapType ProgressMap;
  192. size_t CountProgressMarksInTarget(
  193. cmGeneratorTarget const* target,
  194. std::set<cmGeneratorTarget const*>& emitted);
  195. size_t CountProgressMarksInAll(const cmLocalGenerator& lg);
  196. std::unique_ptr<cmGeneratedFileStream> CommandDatabase;
  197. private:
  198. const char* GetBuildIgnoreErrorsFlag() const override { return "-i"; }
  199. std::string GetEditCacheCommand() const override;
  200. std::map<cmStateSnapshot, std::set<cmGeneratorTarget const*>,
  201. cmStateSnapshot::StrictWeakOrder>
  202. DirectoryTargetsMap;
  203. void InitializeProgressMarks() override;
  204. };
  205. #endif