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