cmLocalGenerator.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmLocalGenerator_h
  14. #define cmLocalGenerator_h
  15. #include "cmStandardIncludes.h"
  16. class cmMakefile;
  17. class cmGlobalGenerator;
  18. class cmTarget;
  19. class cmTargetManifest;
  20. class cmSourceFile;
  21. class cmCustomCommand;
  22. /** \class cmLocalGenerator
  23. * \brief Create required build files for a directory.
  24. *
  25. * Subclasses of this abstract class generate makefiles, DSP, etc for various
  26. * platforms. This class should never be constructued directly. A
  27. * GlobalGenerator will create it and invoke the appropriate commands on it.
  28. */
  29. class cmLocalGenerator
  30. {
  31. public:
  32. cmLocalGenerator();
  33. virtual ~cmLocalGenerator();
  34. /**
  35. * Generate the makefile for this directory.
  36. */
  37. virtual void Generate() {}
  38. /**
  39. * Process the CMakeLists files for this directory to fill in the
  40. * Makefile ivar
  41. */
  42. virtual void Configure();
  43. /**
  44. * Calls TraceVSDependencies() on all targets of this generator.
  45. */
  46. virtual void TraceDependencies();
  47. virtual void AddHelperCommands() {}
  48. /**
  49. * Perform any final calculations prior to generation
  50. */
  51. virtual void ConfigureFinalPass();
  52. /**
  53. * Generate the install rules files in this directory.
  54. */
  55. virtual void GenerateInstallRules();
  56. /**
  57. * Generate the test files for tests.
  58. */
  59. virtual void GenerateTestFiles();
  60. /**
  61. * Generate a manifest of target files that will be built.
  62. */
  63. virtual void GenerateTargetManifest();
  64. ///! Get the makefile for this generator
  65. cmMakefile *GetMakefile() {
  66. return this->Makefile; };
  67. ///! Get the makefile for this generator, const version
  68. const cmMakefile *GetMakefile() const {
  69. return this->Makefile; };
  70. ///! Get the GlobalGenerator this is associated with
  71. cmGlobalGenerator *GetGlobalGenerator() {
  72. return this->GlobalGenerator; };
  73. ///! Set the Global Generator, done on creation by the GlobalGenerator
  74. void SetGlobalGenerator(cmGlobalGenerator *gg);
  75. /**
  76. * Convert something to something else. This is a centralized coversion
  77. * routine used by the generators to handle relative paths and the like.
  78. * The flags determine what is actually done.
  79. *
  80. * relative: treat the argument as a directory and convert it to make it
  81. * relative or full or unchanged. If relative (HOME, START etc) then that
  82. * specifies what it should be relative to.
  83. *
  84. * output: make the result suitable for output to a...
  85. *
  86. * optional: should any relative path operation be controlled by the rel
  87. * path setting
  88. */
  89. enum RelativeRoot { NONE, FULL, HOME, START, HOME_OUTPUT, START_OUTPUT };
  90. enum OutputFormat { UNCHANGED, MAKEFILE, SHELL };
  91. std::string ConvertToOutputFormat(const char* source, OutputFormat output);
  92. std::string Convert(const char* remote, RelativeRoot local,
  93. OutputFormat output = UNCHANGED,
  94. bool optional = false);
  95. std::string Convert(RelativeRoot remote, const char* local,
  96. OutputFormat output = UNCHANGED,
  97. bool optional = false);
  98. /**
  99. * Get path for the specified relative root.
  100. */
  101. const char* GetRelativeRootPath(RelativeRoot relroot);
  102. /**
  103. * Convert the given path to an output path that is optionally
  104. * relative based on the cache option CMAKE_USE_RELATIVE_PATHS. The
  105. * remote path must use forward slashes and not already be escaped
  106. * or quoted.
  107. */
  108. std::string ConvertToOptionallyRelativeOutputPath(const char* remote);
  109. ///! set/get the parent generator
  110. cmLocalGenerator* GetParent(){return this->Parent;}
  111. void SetParent(cmLocalGenerator* g) { this->Parent = g; g->AddChild(this); }
  112. ///! set/get the children
  113. void AddChild(cmLocalGenerator* g) { this->Children.push_back(g); }
  114. std::vector<cmLocalGenerator*>& GetChildren() { return this->Children; };
  115. void AddLanguageFlags(std::string& flags, const char* lang,
  116. const char* config);
  117. void AddSharedFlags(std::string& flags, const char* lang, bool shared);
  118. void AddConfigVariableFlags(std::string& flags, const char* var,
  119. const char* config);
  120. virtual void AppendFlags(std::string& flags, const char* newFlags);
  121. ///! Get the include flags for the current makefile and language
  122. const char* GetIncludeFlags(const char* lang);
  123. /**
  124. * Encode a list of preprocessor definitions for the compiler
  125. * command line.
  126. */
  127. void AppendDefines(std::string& defines, const char* defines_list,
  128. const char* lang);
  129. /** Translate a dependency as given in CMake code to the name to
  130. appear in a generated build file. If the given name is that of
  131. a CMake target it will be transformed to the real output
  132. location of that target for the given configuration. If the
  133. given name is the full path to a file it will be returned.
  134. Otherwise the 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. std::string GetRealDependency(const char* name, const char* config);
  138. /** Translate a command as given in CMake code to the location of the
  139. executable if the command is the name of a CMake executable target.
  140. If that's not the case, just return the original name. */
  141. std::string GetRealLocation(const char* inName, const char* config);
  142. ///! for existing files convert to output path and short path if spaces
  143. std::string ConvertToOutputForExisting(const char* remote,
  144. RelativeRoot local = START_OUTPUT);
  145. /** For existing path identified by RelativeRoot convert to output
  146. path and short path if spaces. */
  147. std::string ConvertToOutputForExisting(RelativeRoot remote,
  148. const char* local = 0);
  149. /** Called from command-line hook to clear dependencies. */
  150. virtual void ClearDependencies(cmMakefile* /* mf */,
  151. bool /* verbose */) {}
  152. /** Called from command-line hook to update dependencies. */
  153. virtual bool UpdateDependencies(const char* /* tgtInfo */,
  154. bool /*verbose*/,
  155. bool /*color*/)
  156. { return true; }
  157. /** Get the include flags for the current makefile and language. */
  158. void GetIncludeDirectories(std::vector<std::string>& dirs,
  159. const char* lang = "C");
  160. /** Compute the language used to compile the given source file. */
  161. const char* GetSourceFileLanguage(const cmSourceFile& source);
  162. // Create a struct to hold the varibles passed into
  163. // ExpandRuleVariables
  164. struct RuleVariables
  165. {
  166. RuleVariables()
  167. {
  168. memset(this, 0, sizeof(*this));
  169. }
  170. cmTarget* CMTarget;
  171. const char* TargetPDB;
  172. const char* TargetVersionMajor;
  173. const char* TargetVersionMinor;
  174. const char* Language;
  175. const char* Objects;
  176. const char* Target;
  177. const char* LinkLibraries;
  178. const char* Source;
  179. const char* AssemblySource;
  180. const char* PreprocessedSource;
  181. const char* Output;
  182. const char* Object;
  183. const char* ObjectDir;
  184. const char* Flags;
  185. const char* ObjectsQuoted;
  186. const char* TargetSOName;
  187. const char* TargetInstallNameDir;
  188. const char* LinkFlags;
  189. const char* LanguageCompileFlags;
  190. const char* Defines;
  191. const char* RuleLauncher;
  192. };
  193. /** Set whether to treat conversions to SHELL as a link script shell. */
  194. void SetLinkScriptShell(bool b) { this->LinkScriptShell = b; }
  195. /** Escape the given string to be used as a command line argument in
  196. the native build system shell. Optionally allow the build
  197. system to replace make variable references. Optionally adjust
  198. escapes for the special case of passing to the native echo
  199. command. */
  200. std::string EscapeForShell(const char* str, bool makeVars = false,
  201. bool forEcho = false);
  202. /** Backwards-compatibility version of EscapeForShell. */
  203. std::string EscapeForShellOldStyle(const char* str);
  204. /** Escape the given string as an argument in a CMake script. */
  205. std::string EscapeForCMake(const char* str);
  206. /** Return the directories into which object files will be put.
  207. * There maybe more than one for fat binary systems like OSX.
  208. */
  209. virtual void
  210. GetTargetObjectFileDirectories(cmTarget* target,
  211. std::vector<std::string>&
  212. dirs);
  213. /**
  214. * Convert the given remote path to a relative path with respect to
  215. * the given local path. The local path must be given in component
  216. * form (see SystemTools::SplitPath) without a trailing slash. The
  217. * remote path must use forward slashes and not already be escaped
  218. * or quoted.
  219. */
  220. std::string ConvertToRelativePath(const std::vector<std::string>& local,
  221. const char* remote);
  222. /**
  223. * Get the relative path from the generator output directory to a
  224. * per-target support directory.
  225. */
  226. virtual std::string GetTargetDirectory(cmTarget const& target) const;
  227. /**
  228. * Get the level of backwards compatibility requested by the project
  229. * in this directory. This is the value of the CMake variable
  230. * CMAKE_BACKWARDS_COMPATIBILITY whose format is
  231. * "major.minor[.patch]". The returned integer is encoded as
  232. *
  233. * CMake_VERSION_ENCODE(major, minor, patch)
  234. *
  235. * and is monotonically increasing with the CMake version.
  236. */
  237. unsigned int GetBackwardsCompatibility();
  238. /**
  239. * Test whether compatibility is set to a given version or lower.
  240. */
  241. bool NeedBackwardsCompatibility(unsigned int major,
  242. unsigned int minor,
  243. unsigned int patch = 0xFFu);
  244. /**
  245. * Generate a Mac OS X application bundle Info.plist file.
  246. */
  247. void GenerateAppleInfoPList(cmTarget* target, const char* targetName,
  248. const char* fname);
  249. /**
  250. * Generate a Mac OS X framework Info.plist file.
  251. */
  252. void GenerateFrameworkInfoPList(cmTarget* target,
  253. const char* targetName,
  254. const char* fname);
  255. /** Construct a comment for a custom command. */
  256. std::string ConstructComment(const cmCustomCommand& cc,
  257. const char* default_comment = "");
  258. protected:
  259. /** Fill out these strings for the given target. Libraries to link,
  260. * flags, and linkflags. */
  261. void GetTargetFlags(std::string& linkLibs,
  262. std::string& flags,
  263. std::string& linkFlags,
  264. cmTarget&target);
  265. ///! put all the libraries for a target on into the given stream
  266. virtual void OutputLinkLibraries(std::ostream&, cmTarget&, bool relink);
  267. // Expand rule variables in CMake of the type found in language rules
  268. void ExpandRuleVariables(std::string& string,
  269. const RuleVariables& replaceValues);
  270. // Expand rule variables in a single string
  271. std::string ExpandRuleVariable(std::string const& variable,
  272. const RuleVariables& replaceValues);
  273. const char* GetRuleLauncher(cmTarget* target, const char* prop);
  274. void InsertRuleLauncher(std::string& s, cmTarget* target,
  275. const char* prop);
  276. /** Convert a target to a utility target for unsupported
  277. * languages of a generator */
  278. void AddBuildTargetRule(const char* llang, cmTarget& target);
  279. ///! add a custom command to build a .o file that is part of a target
  280. void AddCustomCommandToCreateObject(const char* ofname,
  281. const char* lang,
  282. cmSourceFile& source,
  283. cmTarget& target);
  284. // Create Custom Targets and commands for unsupported languages
  285. // The set passed in should contain the languages supported by the
  286. // generator directly. Any targets containing files that are not
  287. // of the types listed will be compiled as custom commands and added
  288. // to a custom target.
  289. void CreateCustomTargetsAndCommands(std::set<cmStdString> const&);
  290. // Handle old-style install rules stored in the targets.
  291. void GenerateTargetInstallRules(
  292. std::ostream& os, const char* config,
  293. std::vector<std::string> const& configurationTypes);
  294. // Compute object file names.
  295. std::string GetObjectFileNameWithoutTarget(const cmSourceFile& source,
  296. std::string const& dir_max,
  297. bool* hasSourceExtension = 0);
  298. std::string& CreateSafeUniqueObjectFileName(const char* sin,
  299. std::string const& dir_max);
  300. void ConfigureRelativePaths();
  301. std::string FindRelativePathTopSource();
  302. std::string FindRelativePathTopBinary();
  303. void SetupPathConversions();
  304. std::string ConvertToLinkReference(std::string const& lib);
  305. /** Check whether the native build system supports the given
  306. definition. Issues a warning. */
  307. virtual bool CheckDefinition(std::string const& define) const;
  308. cmMakefile *Makefile;
  309. cmGlobalGenerator *GlobalGenerator;
  310. // members used for relative path function ConvertToMakefilePath
  311. std::string RelativePathToSourceDir;
  312. std::string RelativePathToBinaryDir;
  313. std::vector<std::string> HomeDirectoryComponents;
  314. std::vector<std::string> StartDirectoryComponents;
  315. std::vector<std::string> HomeOutputDirectoryComponents;
  316. std::vector<std::string> StartOutputDirectoryComponents;
  317. cmLocalGenerator* Parent;
  318. std::vector<cmLocalGenerator*> Children;
  319. std::map<cmStdString, cmStdString> LanguageToIncludeFlags;
  320. std::map<cmStdString, cmStdString> UniqueObjectNamesMap;
  321. std::string::size_type ObjectPathMax;
  322. std::set<cmStdString> ObjectMaxPathViolations;
  323. bool WindowsShell;
  324. bool WindowsVSIDE;
  325. bool WatcomWMake;
  326. bool MinGWMake;
  327. bool NMake;
  328. bool ForceUnixPath;
  329. bool MSYSShell;
  330. bool LinkScriptShell;
  331. bool UseRelativePaths;
  332. bool IgnoreLibPrefix;
  333. bool Configured;
  334. bool EmitUniversalBinaryFlags;
  335. // A type flag is not nice. It's used only in TraceDependencies().
  336. bool IsMakefileGenerator;
  337. // Hack for ExpandRuleVariable until object-oriented version is
  338. // committed.
  339. std::string TargetImplib;
  340. // The top-most directories for relative path conversion. Both the
  341. // source and destination location of a relative path conversion
  342. // must be underneath one of these directories (both under source or
  343. // both under binary) in order for the relative path to be evaluated
  344. // safely by the build tools.
  345. std::string RelativePathTopSource;
  346. std::string RelativePathTopBinary;
  347. bool RelativePathsConfigured;
  348. bool PathConversionsSetup;
  349. unsigned int BackwardsCompatibility;
  350. bool BackwardsCompatibilityFinal;
  351. private:
  352. std::string ConvertToOutputForExistingCommon(const char* remote,
  353. std::string const& result);
  354. };
  355. #endif