cmLocalGenerator.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmLocalGenerator_h
  11. #define cmLocalGenerator_h
  12. #include "cmStandardIncludes.h"
  13. #include "cmState.h"
  14. #include "cmake.h"
  15. #include "cmOutputConverter.h"
  16. class cmMakefile;
  17. class cmGlobalGenerator;
  18. class cmGeneratorTarget;
  19. class cmTarget;
  20. class cmTargetManifest;
  21. class cmSourceFile;
  22. class cmCustomCommand;
  23. class cmCustomCommandGenerator;
  24. /** \class cmLocalGenerator
  25. * \brief Create required build files for a directory.
  26. *
  27. * Subclasses of this abstract class generate makefiles, DSP, etc for various
  28. * platforms. This class should never be constructed directly. A
  29. * GlobalGenerator will create it and invoke the appropriate commands on it.
  30. */
  31. class cmLocalGenerator : public cmOutputConverter
  32. {
  33. public:
  34. cmLocalGenerator(cmGlobalGenerator* gg, cmLocalGenerator* parent,
  35. cmState::Snapshot snapshot);
  36. virtual ~cmLocalGenerator();
  37. /**
  38. * Generate the makefile for this directory.
  39. */
  40. virtual void Generate() {}
  41. virtual void ComputeHomeRelativeOutputPath() {}
  42. /**
  43. * Calls TraceVSDependencies() on all targets of this generator.
  44. */
  45. void TraceDependencies();
  46. virtual void AddHelperCommands() {}
  47. /**
  48. * Generate the install rules files in this directory.
  49. */
  50. void GenerateInstallRules();
  51. /**
  52. * Generate the test files for tests.
  53. */
  54. void GenerateTestFiles();
  55. /**
  56. * Generate a manifest of target files that will be built.
  57. */
  58. void GenerateTargetManifest();
  59. ///! Get the makefile for this generator
  60. cmMakefile *GetMakefile() {
  61. return this->Makefile; }
  62. ///! Get the makefile for this generator, const version
  63. const cmMakefile *GetMakefile() const {
  64. return this->Makefile; }
  65. ///! Get the GlobalGenerator this is associated with
  66. cmGlobalGenerator *GetGlobalGenerator() {
  67. return this->GlobalGenerator; }
  68. const cmGlobalGenerator *GetGlobalGenerator() const {
  69. return this->GlobalGenerator; }
  70. cmState* GetState() const;
  71. cmState::Snapshot GetStateSnapshot() const;
  72. ///! set/get the parent generator
  73. cmLocalGenerator* GetParent() const {return this->Parent;}
  74. ///! set/get the children
  75. void AddChild(cmLocalGenerator* g) { this->Children.push_back(g); }
  76. std::vector<cmLocalGenerator*>& GetChildren() { return this->Children; }
  77. void AddArchitectureFlags(std::string& flags,
  78. cmGeneratorTarget const* target,
  79. const std::string&lang, const std::string& config);
  80. void AddLanguageFlags(std::string& flags, const std::string& lang,
  81. const std::string& config);
  82. void AddCMP0018Flags(std::string &flags, cmTarget const* target,
  83. std::string const& lang, const std::string& config);
  84. void AddVisibilityPresetFlags(std::string &flags, cmTarget const* target,
  85. const std::string& lang);
  86. void AddConfigVariableFlags(std::string& flags, const std::string& var,
  87. const std::string& config);
  88. void AddCompilerRequirementFlag(std::string &flags, cmTarget const* target,
  89. const std::string& lang);
  90. ///! Append flags to a string.
  91. virtual void AppendFlags(std::string& flags, const std::string& newFlags);
  92. virtual void AppendFlags(std::string& flags, const char* newFlags);
  93. virtual void AppendFlagEscape(std::string& flags,
  94. const std::string& rawFlag);
  95. ///! Get the include flags for the current makefile and language
  96. std::string GetIncludeFlags(const std::vector<std::string> &includes,
  97. cmGeneratorTarget* target,
  98. const std::string& lang,
  99. bool forceFullPaths = false,
  100. bool forResponseFile = false,
  101. const std::string& config = "");
  102. /**
  103. * Encode a list of preprocessor definitions for the compiler
  104. * command line.
  105. */
  106. void AppendDefines(std::set<std::string>& defines,
  107. const char* defines_list);
  108. void AppendDefines(std::set<std::string>& defines,
  109. std::string defines_list)
  110. {
  111. this->AppendDefines(defines, defines_list.c_str());
  112. }
  113. void AppendDefines(std::set<std::string>& defines,
  114. const std::vector<std::string> &defines_vec);
  115. /**
  116. * Join a set of defines into a definesString with a space separator.
  117. */
  118. void JoinDefines(const std::set<std::string>& defines,
  119. std::string &definesString,
  120. const std::string& lang);
  121. /** Lookup and append options associated with a particular feature. */
  122. void AppendFeatureOptions(std::string& flags, const std::string& lang,
  123. const char* feature);
  124. const char* GetFeature(const std::string& feature,
  125. const std::string& config);
  126. /** \brief Get absolute path to dependency \a name
  127. *
  128. * Translate a dependency as given in CMake code to the name to
  129. * appear in a generated build file.
  130. * - If \a name is a utility target, returns false.
  131. * - If \a name is a CMake target, it will be transformed to the real output
  132. * location of that target for the given configuration.
  133. * - If \a name is the full path to a file, it will be returned.
  134. * - Otherwise \a name is treated as a relative path with respect to
  135. * the source directory of this generator. This should only be
  136. * used for dependencies of custom commands.
  137. */
  138. bool GetRealDependency(const std::string& name, const std::string& config,
  139. std::string& dep);
  140. virtual std::string ConvertToIncludeReference(std::string const& path,
  141. OutputFormat format = SHELL,
  142. bool forceFullPaths = false);
  143. /** Called from command-line hook to clear dependencies. */
  144. virtual void ClearDependencies(cmMakefile* /* mf */,
  145. bool /* verbose */) {}
  146. /** Called from command-line hook to update dependencies. */
  147. virtual bool UpdateDependencies(const char* /* tgtInfo */,
  148. bool /*verbose*/,
  149. bool /*color*/)
  150. { return true; }
  151. /** Get the include flags for the current makefile and language. */
  152. void GetIncludeDirectories(std::vector<std::string>& dirs,
  153. cmGeneratorTarget* target,
  154. const std::string& lang = "C",
  155. const std::string& config = "",
  156. bool stripImplicitInclDirs = true) const;
  157. void AddCompileOptions(std::string& flags, cmTarget* target,
  158. const std::string& lang, const std::string& config);
  159. void AddCompileDefinitions(std::set<std::string>& defines,
  160. cmTarget const* target,
  161. const std::string& config,
  162. const std::string& lang);
  163. /** Compute the language used to compile the given source file. */
  164. std::string GetSourceFileLanguage(const cmSourceFile& source);
  165. // Fill the vector with the target names for the object files,
  166. // preprocessed files and assembly files.
  167. virtual void GetIndividualFileTargets(std::vector<std::string>&) {}
  168. // Create a struct to hold the varibles passed into
  169. // ExpandRuleVariables
  170. struct RuleVariables
  171. {
  172. RuleVariables()
  173. {
  174. memset(this, 0, sizeof(*this));
  175. }
  176. cmTarget* CMTarget;
  177. const char* TargetPDB;
  178. const char* TargetCompilePDB;
  179. const char* TargetVersionMajor;
  180. const char* TargetVersionMinor;
  181. const char* Language;
  182. const char* Objects;
  183. const char* Target;
  184. const char* LinkLibraries;
  185. const char* Source;
  186. const char* AssemblySource;
  187. const char* PreprocessedSource;
  188. const char* Output;
  189. const char* Object;
  190. const char* ObjectDir;
  191. const char* ObjectFileDir;
  192. const char* Flags;
  193. const char* ObjectsQuoted;
  194. const char* SONameFlag;
  195. const char* TargetSOName;
  196. const char* TargetInstallNameDir;
  197. const char* LinkFlags;
  198. const char* LanguageCompileFlags;
  199. const char* Defines;
  200. const char* Includes;
  201. const char* RuleLauncher;
  202. const char* DependencyFile;
  203. const char* FilterPrefix;
  204. };
  205. /**
  206. * Get the relative path from the generator output directory to a
  207. * per-target support directory.
  208. */
  209. virtual std::string GetTargetDirectory(cmTarget const& target) const;
  210. /**
  211. * Get the level of backwards compatibility requested by the project
  212. * in this directory. This is the value of the CMake variable
  213. * CMAKE_BACKWARDS_COMPATIBILITY whose format is
  214. * "major.minor[.patch]". The returned integer is encoded as
  215. *
  216. * CMake_VERSION_ENCODE(major, minor, patch)
  217. *
  218. * and is monotonically increasing with the CMake version.
  219. */
  220. cmIML_INT_uint64_t GetBackwardsCompatibility();
  221. /**
  222. * Test whether compatibility is set to a given version or lower.
  223. */
  224. bool NeedBackwardsCompatibility_2_4();
  225. /**
  226. * Generate a Mac OS X application bundle Info.plist file.
  227. */
  228. void GenerateAppleInfoPList(cmTarget* target, const std::string& targetName,
  229. const char* fname);
  230. /**
  231. * Generate a Mac OS X framework Info.plist file.
  232. */
  233. void GenerateFrameworkInfoPList(cmTarget* target,
  234. const std::string& targetName,
  235. const char* fname);
  236. /** Construct a comment for a custom command. */
  237. std::string ConstructComment(cmCustomCommandGenerator const& ccg,
  238. const char* default_comment = "");
  239. // Compute object file names.
  240. std::string GetObjectFileNameWithoutTarget(const cmSourceFile& source,
  241. std::string const& dir_max,
  242. bool* hasSourceExtension = 0);
  243. /** Fill out the static linker flags for the given target. */
  244. void GetStaticLibraryFlags(std::string& flags,
  245. std::string const& config,
  246. cmTarget* target);
  247. /** Fill out these strings for the given target. Libraries to link,
  248. * flags, and linkflags. */
  249. void GetTargetFlags(std::string& linkLibs,
  250. std::string& flags,
  251. std::string& linkFlags,
  252. std::string& frameworkPath,
  253. std::string& linkPath,
  254. cmGeneratorTarget* target,
  255. bool useWatcomQuote);
  256. virtual void ComputeObjectFilenames(
  257. std::map<cmSourceFile const*, std::string>& mapping,
  258. cmGeneratorTarget const* gt = 0);
  259. bool IsWindowsShell() const;
  260. bool IsWatcomWMake() const;
  261. bool IsMinGWMake() const;
  262. bool IsNMake() const;
  263. void IssueMessage(cmake::MessageType t, std::string const& text) const;
  264. void ComputeObjectMaxPath();
  265. protected:
  266. ///! put all the libraries for a target on into the given stream
  267. void OutputLinkLibraries(std::string& linkLibraries,
  268. std::string& frameworkPath,
  269. std::string& linkPath,
  270. cmGeneratorTarget &,
  271. bool relink,
  272. bool forResponseFile,
  273. bool useWatcomQuote);
  274. // Expand rule variables in CMake of the type found in language rules
  275. void ExpandRuleVariables(std::string& string,
  276. const RuleVariables& replaceValues);
  277. // Expand rule variables in a single string
  278. std::string ExpandRuleVariable(std::string const& variable,
  279. const RuleVariables& replaceValues);
  280. const char* GetRuleLauncher(cmTarget* target, const std::string& prop);
  281. void InsertRuleLauncher(std::string& s, cmTarget* target,
  282. const std::string& prop);
  283. /** Convert a target to a utility target for unsupported
  284. * languages of a generator */
  285. void AddBuildTargetRule(const std::string& llang,
  286. cmGeneratorTarget& target);
  287. ///! add a custom command to build a .o file that is part of a target
  288. void AddCustomCommandToCreateObject(const char* ofname,
  289. const std::string& lang,
  290. cmSourceFile& source,
  291. cmGeneratorTarget& target);
  292. // Create Custom Targets and commands for unsupported languages
  293. // The set passed in should contain the languages supported by the
  294. // generator directly. Any targets containing files that are not
  295. // of the types listed will be compiled as custom commands and added
  296. // to a custom target.
  297. void CreateCustomTargetsAndCommands(std::set<std::string> const&);
  298. // Handle old-style install rules stored in the targets.
  299. void GenerateTargetInstallRules(
  300. std::ostream& os, const std::string& config,
  301. std::vector<std::string> const& configurationTypes);
  302. std::string& CreateSafeUniqueObjectFileName(const std::string& sin,
  303. std::string const& dir_max);
  304. virtual std::string ConvertToLinkReference(std::string const& lib,
  305. OutputFormat format = SHELL);
  306. /** Check whether the native build system supports the given
  307. definition. Issues a warning. */
  308. virtual bool CheckDefinition(std::string const& define) const;
  309. cmMakefile *Makefile;
  310. cmState::Snapshot StateSnapshot;
  311. cmGlobalGenerator *GlobalGenerator;
  312. cmLocalGenerator* Parent;
  313. std::vector<cmLocalGenerator*> Children;
  314. std::map<std::string, std::string> UniqueObjectNamesMap;
  315. std::string::size_type ObjectPathMax;
  316. std::set<std::string> ObjectMaxPathViolations;
  317. std::set<cmTarget const*> WarnCMP0063;
  318. bool EmitUniversalBinaryFlags;
  319. // Hack for ExpandRuleVariable until object-oriented version is
  320. // committed.
  321. std::string TargetImplib;
  322. cmIML_INT_uint64_t BackwardsCompatibility;
  323. bool BackwardsCompatibilityFinal;
  324. private:
  325. void AddSharedFlags(std::string& flags, const std::string& lang,
  326. bool shared);
  327. bool GetShouldUseOldFlags(bool shared, const std::string &lang) const;
  328. void AddPositionIndependentFlags(std::string& flags, std::string const& l,
  329. int targetType);
  330. };
  331. #endif