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