cmMakefileTargetGenerator.h 8.4 KB

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