cmLocalGenerator.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 "cmOutputConverter.h"
  15. class cmMakefile;
  16. class cmGlobalGenerator;
  17. class cmGeneratorTarget;
  18. class cmTarget;
  19. class cmTargetManifest;
  20. class cmSourceFile;
  21. class cmCustomCommand;
  22. class cmCustomCommandGenerator;
  23. /** \class cmLocalGenerator
  24. * \brief Create required build files for a directory.
  25. *
  26. * Subclasses of this abstract class generate makefiles, DSP, etc for various
  27. * platforms. This class should never be constructed directly. A
  28. * GlobalGenerator will create it and invoke the appropriate commands on it.
  29. */
  30. class cmLocalGenerator : public cmOutputConverter
  31. {
  32. public:
  33. cmLocalGenerator(cmGlobalGenerator* gg, cmLocalGenerator* parent,
  34. cmState::Snapshot snapshot);
  35. virtual ~cmLocalGenerator();
  36. /// @return whether we are processing the top CMakeLists.txt file.
  37. bool IsRootMakefile() const;
  38. /**
  39. * Generate the makefile for this directory.
  40. */
  41. virtual void Generate() {}
  42. /**
  43. * Calls TraceVSDependencies() on all targets of this generator.
  44. */
  45. void TraceDependencies();
  46. virtual void AddHelperCommands() {}
  47. /**
  48. * Perform any final calculations prior to generation
  49. */
  50. void ConfigureFinalPass();
  51. /**
  52. * Generate the install rules files in this directory.
  53. */
  54. void GenerateInstallRules();
  55. /**
  56. * Generate the test files for tests.
  57. */
  58. void GenerateTestFiles();
  59. /**
  60. * Generate a manifest of target files that will be built.
  61. */
  62. void GenerateTargetManifest();
  63. ///! Get the makefile for this generator
  64. cmMakefile *GetMakefile() {
  65. return this->Makefile; }
  66. ///! Get the makefile for this generator, const version
  67. const cmMakefile *GetMakefile() const {
  68. return this->Makefile; }
  69. ///! Get the GlobalGenerator this is associated with
  70. cmGlobalGenerator *GetGlobalGenerator() {
  71. return this->GlobalGenerator; }
  72. const cmGlobalGenerator *GetGlobalGenerator() const {
  73. return this->GlobalGenerator; }
  74. cmState* GetState() const;
  75. cmState::Snapshot GetStateSnapshot() const;
  76. ///! set/get the parent generator
  77. cmLocalGenerator* GetParent() const {return this->Parent;}
  78. ///! set/get the children
  79. void AddChild(cmLocalGenerator* g) { this->Children.push_back(g); }
  80. std::vector<cmLocalGenerator*>& GetChildren() { return this->Children; }
  81. void AddArchitectureFlags(std::string& flags,
  82. cmGeneratorTarget const* target,
  83. const std::string&lang, const std::string& config);
  84. void AddLanguageFlags(std::string& flags, const std::string& lang,
  85. const std::string& config);
  86. void AddCMP0018Flags(std::string &flags, cmTarget const* target,
  87. std::string const& lang, const std::string& config);
  88. void AddVisibilityPresetFlags(std::string &flags, cmTarget const* target,
  89. const std::string& lang);
  90. void AddConfigVariableFlags(std::string& flags, const std::string& var,
  91. const std::string& config);
  92. void AddCompilerRequirementFlag(std::string &flags, cmTarget const* target,
  93. const std::string& lang);
  94. ///! Append flags to a string.
  95. virtual void AppendFlags(std::string& flags, const std::string& newFlags);
  96. virtual void AppendFlags(std::string& flags, const char* newFlags);
  97. virtual void AppendFlagEscape(std::string& flags,
  98. const std::string& rawFlag);
  99. ///! Get the include flags for the current makefile and language
  100. std::string GetIncludeFlags(const std::vector<std::string> &includes,
  101. cmGeneratorTarget* target,
  102. const std::string& lang,
  103. bool forceFullPaths = false,
  104. bool forResponseFile = false,
  105. const std::string& config = "");
  106. /**
  107. * Encode a list of preprocessor definitions for the compiler
  108. * command line.
  109. */
  110. void AppendDefines(std::set<std::string>& defines,
  111. const char* defines_list);
  112. void AppendDefines(std::set<std::string>& defines,
  113. std::string defines_list)
  114. {
  115. this->AppendDefines(defines, defines_list.c_str());
  116. }
  117. void AppendDefines(std::set<std::string>& defines,
  118. const std::vector<std::string> &defines_vec);
  119. /**
  120. * Join a set of defines into a definesString with a space separator.
  121. */
  122. void JoinDefines(const std::set<std::string>& defines,
  123. std::string &definesString,
  124. const std::string& lang);
  125. /** Lookup and append options associated with a particular feature. */
  126. void AppendFeatureOptions(std::string& flags, const std::string& lang,
  127. const char* feature);
  128. /** \brief Get absolute path to dependency \a name
  129. *
  130. * Translate a dependency as given in CMake code to the name to
  131. * appear in a generated build file.
  132. * - If \a name is a utility target, returns false.
  133. * - If \a name is a CMake target, it will be transformed to the real output
  134. * location of that target for the given configuration.
  135. * - If \a name is the full path to a file, it will be returned.
  136. * - Otherwise \a name is treated as a relative path with respect to
  137. * the source directory of this generator. This should only be
  138. * used for dependencies of custom commands.
  139. */
  140. bool GetRealDependency(const std::string& name, const std::string& config,
  141. std::string& dep);
  142. virtual std::string ConvertToIncludeReference(std::string const& path,
  143. OutputFormat format = SHELL,
  144. bool forceFullPaths = false);
  145. /** Called from command-line hook to clear dependencies. */
  146. virtual void ClearDependencies(cmMakefile* /* mf */,
  147. bool /* verbose */) {}
  148. /** Called from command-line hook to update dependencies. */
  149. virtual bool UpdateDependencies(const char* /* tgtInfo */,
  150. bool /*verbose*/,
  151. bool /*color*/)
  152. { return true; }
  153. /** Get the include flags for the current makefile and language. */
  154. void GetIncludeDirectories(std::vector<std::string>& dirs,
  155. cmGeneratorTarget* target,
  156. const std::string& lang = "C",
  157. const std::string& config = "",
  158. bool stripImplicitInclDirs = true);
  159. void AddCompileOptions(std::string& flags, cmTarget* target,
  160. const std::string& lang, const std::string& config);
  161. void AddCompileDefinitions(std::set<std::string>& defines,
  162. cmTarget const* target,
  163. const std::string& config,
  164. const std::string& lang);
  165. /** Compute the language used to compile the given source file. */
  166. std::string GetSourceFileLanguage(const cmSourceFile& source);
  167. // Fill the vector with the target names for the object files,
  168. // preprocessed files and assembly files.
  169. virtual void GetIndividualFileTargets(std::vector<std::string>&) {}
  170. // Create a struct to hold the varibles passed into
  171. // ExpandRuleVariables
  172. struct RuleVariables
  173. {
  174. RuleVariables()
  175. {
  176. memset(this, 0, sizeof(*this));
  177. }
  178. cmTarget* CMTarget;
  179. const char* TargetPDB;
  180. const char* TargetCompilePDB;
  181. const char* TargetVersionMajor;
  182. const char* TargetVersionMinor;
  183. const char* Language;
  184. const char* Objects;
  185. const char* Target;
  186. const char* LinkLibraries;
  187. const char* Source;
  188. const char* AssemblySource;
  189. const char* PreprocessedSource;
  190. const char* Output;
  191. const char* Object;
  192. const char* ObjectDir;
  193. const char* ObjectFileDir;
  194. const char* Flags;
  195. const char* ObjectsQuoted;
  196. const char* SONameFlag;
  197. const char* TargetSOName;
  198. const char* TargetInstallNameDir;
  199. const char* LinkFlags;
  200. const char* LanguageCompileFlags;
  201. const char* Defines;
  202. const char* RuleLauncher;
  203. const char* DependencyFile;
  204. const char* FilterPrefix;
  205. };
  206. /**
  207. * Get the relative path from the generator output directory to a
  208. * per-target support directory.
  209. */
  210. virtual std::string GetTargetDirectory(cmTarget const& target) const;
  211. /**
  212. * Get the level of backwards compatibility requested by the project
  213. * in this directory. This is the value of the CMake variable
  214. * CMAKE_BACKWARDS_COMPATIBILITY whose format is
  215. * "major.minor[.patch]". The returned integer is encoded as
  216. *
  217. * CMake_VERSION_ENCODE(major, minor, patch)
  218. *
  219. * and is monotonically increasing with the CMake version.
  220. */
  221. cmIML_INT_uint64_t GetBackwardsCompatibility();
  222. /**
  223. * Test whether compatibility is set to a given version or lower.
  224. */
  225. bool NeedBackwardsCompatibility_2_4();
  226. /**
  227. * Generate a Mac OS X application bundle Info.plist file.
  228. */
  229. void GenerateAppleInfoPList(cmTarget* target, const std::string& targetName,
  230. const char* fname);
  231. /**
  232. * Generate a Mac OS X framework Info.plist file.
  233. */
  234. void GenerateFrameworkInfoPList(cmTarget* target,
  235. const std::string& targetName,
  236. const char* fname);
  237. /** Construct a comment for a custom command. */
  238. std::string ConstructComment(cmCustomCommandGenerator const& ccg,
  239. const char* default_comment = "");
  240. // Compute object file names.
  241. std::string GetObjectFileNameWithoutTarget(const cmSourceFile& source,
  242. std::string const& dir_max,
  243. bool* hasSourceExtension = 0);
  244. /** Fill out the static linker flags for the given target. */
  245. void GetStaticLibraryFlags(std::string& flags,
  246. std::string const& config,
  247. cmTarget* target);
  248. /** Fill out these strings for the given target. Libraries to link,
  249. * flags, and linkflags. */
  250. void GetTargetFlags(std::string& linkLibs,
  251. std::string& flags,
  252. std::string& linkFlags,
  253. std::string& frameworkPath,
  254. std::string& linkPath,
  255. cmGeneratorTarget* target,
  256. bool useWatcomQuote);
  257. virtual void ComputeObjectFilenames(
  258. std::map<cmSourceFile const*, std::string>& mapping,
  259. cmGeneratorTarget const* gt = 0);
  260. bool IsWindowsShell() const;
  261. bool IsWatcomWMake() const;
  262. bool IsMinGWMake() const;
  263. bool IsNMake() 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