cmLocalGenerator.h 16 KB

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