cmLocalGenerator.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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(cmTargetManifest&);
  64. ///! Get the makefile for this generator
  65. cmMakefile *GetMakefile() {
  66. return this->Makefile; };
  67. ///! Get the GlobalGenerator this is associated with
  68. cmGlobalGenerator *GetGlobalGenerator() {
  69. return this->GlobalGenerator; };
  70. ///! Set the Global Generator, done on creation by the GlobalGenerator
  71. void SetGlobalGenerator(cmGlobalGenerator *gg);
  72. /**
  73. * Convert something to something else. This is a centralized coversion
  74. * routine used by the generators to handle relative paths and the like.
  75. * The flags determine what is actually done.
  76. *
  77. * relative: treat the argument as a directory and convert it to make it
  78. * relative or full or unchanged. If relative (HOME, START etc) then that
  79. * specifies what it should be relative to.
  80. *
  81. * output: make the result suitable for output to a...
  82. *
  83. * optional: should any relative path operation be controlled by the rel
  84. * path setting
  85. */
  86. enum RelativeRoot { NONE, FULL, HOME, START, HOME_OUTPUT, START_OUTPUT };
  87. enum OutputFormat { UNCHANGED, MAKEFILE, SHELL };
  88. std::string Convert(const char* source,
  89. RelativeRoot relative,
  90. OutputFormat output = UNCHANGED,
  91. bool optional = false);
  92. /**
  93. * Convert the given path to an output path that is optionally
  94. * relative based on the cache option CMAKE_USE_RELATIVE_PATHS. The
  95. * remote path must use forward slashes and not already be escaped
  96. * or quoted.
  97. */
  98. std::string ConvertToOptionallyRelativeOutputPath(const char* remote);
  99. ///! set/get the parent generator
  100. cmLocalGenerator* GetParent(){return this->Parent;}
  101. void SetParent(cmLocalGenerator* g) { this->Parent = g; g->AddChild(this); }
  102. ///! set/get the children
  103. void AddChild(cmLocalGenerator* g) { this->Children.push_back(g); }
  104. std::vector<cmLocalGenerator*>& GetChildren() { return this->Children; };
  105. void AddLanguageFlags(std::string& flags, const char* lang,
  106. const char* config);
  107. void AddSharedFlags(std::string& flags, const char* lang, bool shared);
  108. void AddConfigVariableFlags(std::string& flags, const char* var,
  109. const char* config);
  110. void AppendFlags(std::string& flags, const char* newFlags);
  111. ///! Get the include flags for the current makefile and language
  112. const char* GetIncludeFlags(const char* lang);
  113. /** Translate a dependency as given in CMake code to the name to
  114. appear in a generated build file. If the given name is that of
  115. a CMake target it will be transformed to the real output
  116. location of that target for the given configuration. If the
  117. given name is the full path to a file it will be returned.
  118. Otherwise the name is treated as a relative path with respect to
  119. the source directory of this generator. This should only be
  120. used for dependencies of custom commands. */
  121. std::string GetRealDependency(const char* name, const char* config);
  122. /** Translate a command as given in CMake code to the location of the
  123. executable if the command is the name of a CMake executable target.
  124. If that's not the case, just return the original name. */
  125. std::string GetRealLocation(const char* inName, const char* config);
  126. ///! for existing files convert to output path and short path if spaces
  127. std::string ConvertToOutputForExisting(const char* p);
  128. /** Called from command-line hook to check dependencies. */
  129. virtual void CheckDependencies(cmMakefile* /* mf */,
  130. bool /* verbose */,
  131. bool /* clear */) {};
  132. /** Called from command-line hook to scan dependencies. */
  133. virtual bool ScanDependencies(const char* /* tgtInfo */) { return true; }
  134. /** Compute the list of link libraries and directories for the given
  135. target and configuration. */
  136. void ComputeLinkInformation(cmTarget& target, const char* config,
  137. std::vector<cmStdString>& outLibs,
  138. std::vector<cmStdString>& outDirs,
  139. std::vector<cmStdString>* fullPathLibs=0);
  140. /** Get the include flags for the current makefile and language. */
  141. void GetIncludeDirectories(std::vector<std::string>& dirs,
  142. bool filter_system_dirs = true);
  143. /** Compute the language used to compile the given source file. */
  144. const char* GetSourceFileLanguage(const cmSourceFile& source);
  145. // Create a struct to hold the varibles passed into
  146. // ExpandRuleVariables
  147. struct RuleVariables
  148. {
  149. RuleVariables()
  150. {
  151. memset(this, 0, sizeof(*this));
  152. }
  153. const char* TargetPDB;
  154. const char* TargetVersionMajor;
  155. const char* TargetVersionMinor;
  156. const char* Language;
  157. const char* Objects;
  158. const char* Target;
  159. const char* LinkLibraries;
  160. const char* Source;
  161. const char* AssemblySource;
  162. const char* PreprocessedSource;
  163. const char* Object;
  164. const char* ObjectDir;
  165. const char* Flags;
  166. const char* ObjectsQuoted;
  167. const char* TargetSOName;
  168. const char* TargetInstallNameDir;
  169. const char* LinkFlags;
  170. const char* LanguageCompileFlags;
  171. };
  172. /** Escape the given string to be used as a command line argument in
  173. the native build system shell. Optionally allow the build
  174. system to replace make variable references. Optionally adjust
  175. escapes for the special case of passing to the native echo
  176. command. */
  177. std::string EscapeForShell(const char* str, bool makeVars = false,
  178. bool forEcho = false);
  179. /** Backwards-compatibility version of EscapeForShell. */
  180. std::string EscapeForShellOldStyle(const char* str);
  181. /** Return the directories into which object files will be put.
  182. * There maybe more than one for fat binary systems like OSX.
  183. */
  184. virtual void
  185. GetTargetObjectFileDirectories(cmTarget* target,
  186. std::vector<std::string>&
  187. dirs);
  188. // return the source name for the object file
  189. virtual std::string GetSourceObjectName(cmSourceFile& );
  190. /**
  191. * Convert the given remote path to a relative path with respect to
  192. * the given local path. The local path must be given in component
  193. * form (see SystemTools::SplitPath) without a trailing slash. The
  194. * remote path must use forward slashes and not already be escaped
  195. * or quoted.
  196. */
  197. std::string ConvertToRelativePath(const std::vector<std::string>& local,
  198. const char* remote);
  199. protected:
  200. /** Construct a comment for a custom command. */
  201. std::string ConstructComment(const cmCustomCommand& cc,
  202. const char* default_comment = "");
  203. /** Fill out these strings for the given target. Libraries to link,
  204. * flags, and linkflags. */
  205. void GetTargetFlags(std::string& linkLibs,
  206. std::string& flags,
  207. std::string& linkFlags,
  208. cmTarget&target);
  209. ///! put all the libraries for a target on into the given stream
  210. virtual void OutputLinkLibraries(std::ostream&, cmTarget&, bool relink);
  211. // Expand rule variables in CMake of the type found in language rules
  212. void ExpandRuleVariables(std::string& string,
  213. const RuleVariables& replaceValues);
  214. // Expand rule variables in a single string
  215. std::string ExpandRuleVariable(std::string const& variable,
  216. const RuleVariables& replaceValues);
  217. /** Convert a target to a utility target for unsupported
  218. * languages of a generator */
  219. void AddBuildTargetRule(const char* llang, cmTarget& target);
  220. ///! add a custom command to build a .o file that is part of a target
  221. void AddCustomCommandToCreateObject(const char* ofname,
  222. const char* lang,
  223. cmSourceFile& source,
  224. cmTarget& target);
  225. // Create Custom Targets and commands for unsupported languages
  226. // The set passed in should contain the languages supported by the
  227. // generator directly. Any targets containing files that are not
  228. // of the types listed will be compiled as custom commands and added
  229. // to a custom target.
  230. void CreateCustomTargetsAndCommands(std::set<cmStdString> const&);
  231. // Handle old-style install rules stored in the targets.
  232. void GenerateTargetInstallRules(
  233. std::ostream& os, const char* config,
  234. std::vector<std::string> const& configurationTypes);
  235. // Compute object file names.
  236. std::string GetObjectFileNameWithoutTarget(const cmSourceFile& source,
  237. std::string::size_type dir_len);
  238. std::string& CreateSafeUniqueObjectFileName(const char* sin,
  239. std::string::size_type dir_len);
  240. void ConfigureRelativePaths();
  241. std::string FindRelativePathTopSource();
  242. std::string FindRelativePathTopBinary();
  243. void SetupPathConversions();
  244. cmMakefile *Makefile;
  245. cmGlobalGenerator *GlobalGenerator;
  246. // members used for relative path function ConvertToMakefilePath
  247. std::string RelativePathToSourceDir;
  248. std::string RelativePathToBinaryDir;
  249. std::vector<std::string> HomeDirectoryComponents;
  250. std::vector<std::string> StartDirectoryComponents;
  251. std::vector<std::string> HomeOutputDirectoryComponents;
  252. std::vector<std::string> StartOutputDirectoryComponents;
  253. cmLocalGenerator* Parent;
  254. std::vector<cmLocalGenerator*> Children;
  255. std::map<cmStdString, cmStdString> LanguageToIncludeFlags;
  256. std::map<cmStdString, cmStdString> UniqueObjectNamesMap;
  257. bool WindowsShell;
  258. bool WindowsVSIDE;
  259. bool WatcomWMake;
  260. bool ForceUnixPath;
  261. bool MSYSShell;
  262. bool UseRelativePaths;
  263. bool IgnoreLibPrefix;
  264. bool Configured;
  265. bool EmitUniversalBinaryFlags;
  266. // A type flag is not nice. It's used only in TraceDependencies().
  267. bool IsMakefileGenerator;
  268. // Hack for ExpandRuleVariable until object-oriented version is
  269. // committed.
  270. std::string TargetImplib;
  271. // The top-most directories for relative path conversion. Both the
  272. // source and destination location of a relative path conversion
  273. // must be underneath one of these directories (both under source or
  274. // both under binary) in order for the relative path to be evaluated
  275. // safely by the build tools.
  276. std::string RelativePathTopSource;
  277. std::string RelativePathTopBinary;
  278. bool RelativePathsConfigured;
  279. bool PathConversionsSetup;
  280. };
  281. #endif