cmMakefileTargetGenerator.h 8.3 KB

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