cmLocalGenerator.h 17 KB

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