cmMakefileTargetGenerator.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 cmMakefileTargetGenerator_h
  4. #define cmMakefileTargetGenerator_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <iosfwd>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include <vector>
  12. #include "cmCommonTargetGenerator.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmLocalUnixMakefileGenerator3.h"
  15. #include "cmOSXBundleGenerator.h"
  16. class cmCustomCommandGenerator;
  17. class cmGeneratedFileStream;
  18. class cmGlobalUnixMakefileGenerator3;
  19. class cmLinkLineComputer;
  20. class cmOutputConverter;
  21. class cmSourceFile;
  22. class cmStateDirectory;
  23. /** \class cmMakefileTargetGenerator
  24. * \brief Support Routines for writing makefiles
  25. *
  26. */
  27. class cmMakefileTargetGenerator : public cmCommonTargetGenerator
  28. {
  29. public:
  30. // constructor to set the ivars
  31. cmMakefileTargetGenerator(cmGeneratorTarget* target);
  32. ~cmMakefileTargetGenerator() override;
  33. // construct using this factory call
  34. static cmMakefileTargetGenerator* New(cmGeneratorTarget* tgt);
  35. /* the main entry point for this class. Writes the Makefiles associated
  36. with this target */
  37. virtual void WriteRuleFiles() = 0;
  38. /* return the number of actions that have progress reporting on them */
  39. virtual unsigned long GetNumberOfProgressActions()
  40. {
  41. return this->NumberOfProgressActions;
  42. }
  43. std::string GetProgressFileNameFull() { return this->ProgressFileNameFull; }
  44. cmGeneratorTarget* GetGeneratorTarget() { return this->GeneratorTarget; }
  45. protected:
  46. void GetTargetLinkFlags(std::string& flags, const std::string& linkLanguage);
  47. // create the file and directory etc
  48. void CreateRuleFile();
  49. // outputs the rules for object files and custom commands used by
  50. // this target
  51. void WriteTargetBuildRules();
  52. // write some common code at the top of build.make
  53. void WriteCommonCodeRules();
  54. void WriteTargetLanguageFlags();
  55. // write the clean rules for this target
  56. void WriteTargetCleanRules();
  57. // write the depend rules for this target
  58. void WriteTargetDependRules();
  59. // write rules for macOS Application Bundle content.
  60. struct MacOSXContentGeneratorType
  61. : cmOSXBundleGenerator::MacOSXContentGeneratorType
  62. {
  63. MacOSXContentGeneratorType(cmMakefileTargetGenerator* gen)
  64. : Generator(gen)
  65. {
  66. }
  67. void operator()(cmSourceFile const& source, const char* pkgloc) override;
  68. private:
  69. cmMakefileTargetGenerator* Generator;
  70. };
  71. friend struct MacOSXContentGeneratorType;
  72. // write the rules for an object
  73. void WriteObjectRuleFiles(cmSourceFile const& source);
  74. // write the build rule for an object
  75. void WriteObjectBuildFile(std::string& obj, const std::string& lang,
  76. cmSourceFile const& source,
  77. std::vector<std::string>& depends);
  78. // write the depend.make file for an object
  79. void WriteObjectDependRules(cmSourceFile const& source,
  80. std::vector<std::string>& depends);
  81. // write the build rule for a custom command
  82. void GenerateCustomRuleFile(cmCustomCommandGenerator const& ccg);
  83. // write a rule to drive building of more than one output from
  84. // another rule
  85. void GenerateExtraOutput(const char* out, const char* in,
  86. bool symbolic = false);
  87. void MakeEchoProgress(cmLocalUnixMakefileGenerator3::EchoProgress&) const;
  88. // write out the variable that lists the objects for this target
  89. void WriteObjectsVariable(std::string& variableName,
  90. std::string& variableNameExternal,
  91. bool useWatcomQuote);
  92. void WriteObjectsStrings(std::vector<std::string>& objStrings,
  93. std::string::size_type limit = std::string::npos);
  94. // write the driver rule to build target outputs
  95. void WriteTargetDriverRule(const std::string& main_output, bool relink);
  96. void DriveCustomCommands(std::vector<std::string>& depends);
  97. // append intertarget dependencies
  98. void AppendTargetDepends(std::vector<std::string>& depends);
  99. // Append object file dependencies.
  100. void AppendObjectDepends(std::vector<std::string>& depends);
  101. // Append link rule dependencies (objects, etc.).
  102. void AppendLinkDepends(std::vector<std::string>& depends,
  103. const std::string& linkLanguage);
  104. // Lookup the link rule for this target.
  105. std::string GetLinkRule(const std::string& linkRuleVar);
  106. /** Create a script to hold link rules and a command to invoke the
  107. script at build time. */
  108. void CreateLinkScript(const char* name,
  109. std::vector<std::string> const& link_commands,
  110. std::vector<std::string>& makefile_commands,
  111. std::vector<std::string>& makefile_depends);
  112. cmLinkLineComputer* CreateLinkLineComputer(
  113. cmOutputConverter* outputConverter, cmStateDirectory const& stateDir);
  114. /** Create a response file with the given set of options. Returns
  115. the relative path from the target build working directory to the
  116. response file name. */
  117. std::string CreateResponseFile(const char* name, std::string const& options,
  118. std::vector<std::string>& makefile_depends);
  119. bool CheckUseResponseFileForObjects(std::string const& l) const;
  120. bool CheckUseResponseFileForLibraries(std::string const& l) const;
  121. /** Create list of flags for link libraries. */
  122. void CreateLinkLibs(cmLinkLineComputer* linkLineComputer,
  123. std::string& linkLibs, bool useResponseFile,
  124. std::vector<std::string>& makefile_depends);
  125. /** Create lists of object files for linking and cleaning. */
  126. void CreateObjectLists(bool useLinkScript, bool useArchiveRules,
  127. bool useResponseFile, std::string& buildObjs,
  128. std::vector<std::string>& makefile_depends,
  129. bool useWatcomQuote);
  130. /** Add commands for generate def files */
  131. void GenDefFile(std::vector<std::string>& real_link_commands);
  132. void AddIncludeFlags(std::string& flags, const std::string& lang) override;
  133. virtual void CloseFileStreams();
  134. cmLocalUnixMakefileGenerator3* LocalGenerator;
  135. cmGlobalUnixMakefileGenerator3* GlobalGenerator;
  136. enum CustomCommandDriveType
  137. {
  138. OnBuild,
  139. OnDepends,
  140. OnUtility
  141. };
  142. CustomCommandDriveType CustomCommandDriver;
  143. // the full path to the build file
  144. std::string BuildFileName;
  145. std::string BuildFileNameFull;
  146. // the full path to the progress file
  147. std::string ProgressFileNameFull;
  148. unsigned long NumberOfProgressActions;
  149. bool NoRuleMessages;
  150. // the path to the directory the build file is in
  151. std::string TargetBuildDirectory;
  152. std::string TargetBuildDirectoryFull;
  153. // the stream for the build file
  154. cmGeneratedFileStream* BuildFileStream;
  155. // the stream for the flag file
  156. std::string FlagFileNameFull;
  157. cmGeneratedFileStream* FlagFileStream;
  158. class StringList : public std::vector<std::string>
  159. {
  160. };
  161. std::map<std::string, StringList> FlagFileDepends;
  162. // the stream for the info file
  163. std::string InfoFileNameFull;
  164. cmGeneratedFileStream* InfoFileStream;
  165. // files to clean
  166. std::set<std::string> CleanFiles;
  167. // objects used by this target
  168. std::vector<std::string> Objects;
  169. std::vector<std::string> ExternalObjects;
  170. // Set of object file names that will be built in this directory.
  171. std::set<std::string> ObjectFiles;
  172. // Set of extra output files to be driven by the build.
  173. std::set<std::string> ExtraFiles;
  174. using MultipleOutputPairsType = std::map<std::string, std::string>;
  175. MultipleOutputPairsType MultipleOutputPairs;
  176. bool WriteMakeRule(std::ostream& os, const char* comment,
  177. const std::vector<std::string>& outputs,
  178. const std::vector<std::string>& depends,
  179. const std::vector<std::string>& commands,
  180. bool in_help = false);
  181. // Target name info.
  182. cmGeneratorTarget::Names TargetNames;
  183. // macOS content info.
  184. std::set<std::string> MacContentFolders;
  185. std::unique_ptr<cmOSXBundleGenerator> OSXBundleGenerator;
  186. MacOSXContentGeneratorType* MacOSXContentGenerator;
  187. };
  188. #endif