cmLocalGenerator.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 cmCustomCommandGenerator;
  21. /** \class cmLocalGenerator
  22. * \brief Create required build files for a directory.
  23. *
  24. * Subclasses of this abstract class generate makefiles, DSP, etc for various
  25. * platforms. This class should never be constructed directly. A
  26. * GlobalGenerator will create it and invoke the appropriate commands on it.
  27. */
  28. class cmLocalGenerator
  29. {
  30. public:
  31. cmLocalGenerator();
  32. virtual ~cmLocalGenerator();
  33. /**
  34. * Generate the makefile for this directory.
  35. */
  36. virtual void Generate() {}
  37. /**
  38. * Process the CMakeLists files for this directory to fill in the
  39. * Makefile ivar
  40. */
  41. virtual void Configure();
  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. ///! Set the Global Generator, done on creation by the GlobalGenerator
  75. void SetGlobalGenerator(cmGlobalGenerator *gg);
  76. /**
  77. * Convert something to something else. This is a centralized conversion
  78. * routine used by the generators to handle relative paths and the like.
  79. * The flags determine what is actually done.
  80. *
  81. * relative: treat the argument as a directory and convert it to make it
  82. * relative or full or unchanged. If relative (HOME, START etc) then that
  83. * specifies what it should be relative to.
  84. *
  85. * output: make the result suitable for output to a...
  86. *
  87. * optional: should any relative path operation be controlled by the rel
  88. * path setting
  89. */
  90. enum RelativeRoot { NONE, FULL, HOME, START, HOME_OUTPUT, START_OUTPUT };
  91. enum OutputFormat { UNCHANGED, MAKERULE, SHELL, WATCOMQUOTE, RESPONSE };
  92. std::string ConvertToOutputFormat(const std::string& source,
  93. OutputFormat output);
  94. std::string Convert(const std::string& remote, RelativeRoot local,
  95. OutputFormat output = UNCHANGED,
  96. bool optional = false);
  97. std::string Convert(RelativeRoot remote, const std::string& local,
  98. OutputFormat output = UNCHANGED,
  99. bool optional = false);
  100. /**
  101. * Get path for the specified relative root.
  102. */
  103. const char* GetRelativeRootPath(RelativeRoot relroot);
  104. /**
  105. * Convert the given path to an output path that is optionally
  106. * relative based on the cache option CMAKE_USE_RELATIVE_PATHS. The
  107. * remote path must use forward slashes and not already be escaped
  108. * or quoted.
  109. */
  110. std::string ConvertToOptionallyRelativeOutputPath(const std::string& remote);
  111. ///! set/get the parent generator
  112. cmLocalGenerator* GetParent() const {return this->Parent;}
  113. void SetParent(cmLocalGenerator* g) { this->Parent = g; g->AddChild(this); }
  114. ///! set/get the children
  115. void AddChild(cmLocalGenerator* g) { this->Children.push_back(g); }
  116. std::vector<cmLocalGenerator*>& GetChildren() { return this->Children; }
  117. void AddArchitectureFlags(std::string& flags, cmGeneratorTarget* target,
  118. const std::string&lang, const std::string& config);
  119. void AddLanguageFlags(std::string& flags, const std::string& lang,
  120. const std::string& config);
  121. void AddCMP0018Flags(std::string &flags, cmTarget* target,
  122. std::string const& lang, const std::string& config);
  123. void AddVisibilityPresetFlags(std::string &flags, cmTarget* target,
  124. const std::string& lang);
  125. void AddConfigVariableFlags(std::string& flags, const std::string& var,
  126. const std::string& config);
  127. void AddCompilerRequirementFlag(std::string &flags, cmTarget* target,
  128. const std::string& lang);
  129. ///! Append flags to a string.
  130. virtual void AppendFlags(std::string& flags, const std::string& newFlags);
  131. virtual void AppendFlags(std::string& flags, const char* newFlags);
  132. virtual void AppendFlagEscape(std::string& flags,
  133. const std::string& rawFlag);
  134. ///! Get the include flags for the current makefile and language
  135. std::string GetIncludeFlags(const std::vector<std::string> &includes,
  136. cmGeneratorTarget* target,
  137. const std::string& lang,
  138. bool forceFullPaths = false,
  139. bool forResponseFile = false,
  140. const std::string& config = "");
  141. /**
  142. * Encode a list of preprocessor definitions for the compiler
  143. * command line.
  144. */
  145. void AppendDefines(std::set<std::string>& defines,
  146. const char* defines_list);
  147. void AppendDefines(std::set<std::string>& defines,
  148. std::string defines_list)
  149. {
  150. this->AppendDefines(defines, defines_list.c_str());
  151. }
  152. void AppendDefines(std::set<std::string>& defines,
  153. const std::vector<std::string> &defines_vec);
  154. /**
  155. * Join a set of defines into a definesString with a space separator.
  156. */
  157. void JoinDefines(const std::set<std::string>& defines,
  158. std::string &definesString,
  159. const std::string& lang);
  160. /** Lookup and append options associated with a particular feature. */
  161. void AppendFeatureOptions(std::string& flags, const std::string& lang,
  162. const char* feature);
  163. /** \brief Get absolute path to dependency \a name
  164. *
  165. * Translate a dependency as given in CMake code to the name to
  166. * appear in a generated build file.
  167. * - If \a name is a utility target, returns false.
  168. * - If \a name is a CMake target, it will be transformed to the real output
  169. * location of that target for the given configuration.
  170. * - If \a name is the full path to a file, it will be returned.
  171. * - Otherwise \a name is treated as a relative path with respect to
  172. * the source directory of this generator. This should only be
  173. * used for dependencies of custom commands.
  174. */
  175. bool GetRealDependency(const std::string& name, const std::string& config,
  176. std::string& dep);
  177. ///! for existing files convert to output path and short path if spaces
  178. std::string ConvertToOutputForExisting(const std::string& remote,
  179. RelativeRoot local = START_OUTPUT,
  180. OutputFormat format = SHELL);
  181. /** For existing path identified by RelativeRoot convert to output
  182. path and short path if spaces. */
  183. std::string ConvertToOutputForExisting(RelativeRoot remote,
  184. const std::string& local = "",
  185. OutputFormat format = SHELL);
  186. virtual std::string ConvertToIncludeReference(std::string const& path,
  187. OutputFormat format = SHELL,
  188. bool forceFullPaths = false);
  189. /** Called from command-line hook to clear dependencies. */
  190. virtual void ClearDependencies(cmMakefile* /* mf */,
  191. bool /* verbose */) {}
  192. /** Called from command-line hook to update dependencies. */
  193. virtual bool UpdateDependencies(const char* /* tgtInfo */,
  194. bool /*verbose*/,
  195. bool /*color*/)
  196. { return true; }
  197. /** Get the include flags for the current makefile and language. */
  198. void GetIncludeDirectories(std::vector<std::string>& dirs,
  199. cmGeneratorTarget* target,
  200. const std::string& lang = "C",
  201. const std::string& config = "",
  202. bool stripImplicitInclDirs = true);
  203. void AddCompileOptions(std::string& flags, cmTarget* target,
  204. const std::string& lang, const std::string& config);
  205. void AddCompileDefinitions(std::set<std::string>& defines,
  206. cmTarget const* target,
  207. const std::string& config,
  208. const std::string& lang);
  209. /** Compute the language used to compile the given source file. */
  210. std::string GetSourceFileLanguage(const cmSourceFile& source);
  211. // Fill the vector with the target names for the object files,
  212. // preprocessed files and assembly files.
  213. virtual void GetIndividualFileTargets(std::vector<std::string>&) {}
  214. // Create a struct to hold the varibles passed into
  215. // ExpandRuleVariables
  216. struct RuleVariables
  217. {
  218. RuleVariables()
  219. {
  220. memset(this, 0, sizeof(*this));
  221. }
  222. cmTarget* CMTarget;
  223. const char* TargetPDB;
  224. const char* TargetCompilePDB;
  225. const char* TargetVersionMajor;
  226. const char* TargetVersionMinor;
  227. const char* Language;
  228. const char* Objects;
  229. const char* Target;
  230. const char* LinkLibraries;
  231. const char* Source;
  232. const char* AssemblySource;
  233. const char* PreprocessedSource;
  234. const char* Output;
  235. const char* Object;
  236. const char* ObjectDir;
  237. const char* ObjectFileDir;
  238. const char* Flags;
  239. const char* ObjectsQuoted;
  240. const char* SONameFlag;
  241. const char* TargetSOName;
  242. const char* TargetInstallNameDir;
  243. const char* LinkFlags;
  244. const char* LanguageCompileFlags;
  245. const char* Defines;
  246. const char* RuleLauncher;
  247. const char* DependencyFile;
  248. const char* FilterPrefix;
  249. };
  250. /** Set whether to treat conversions to SHELL as a link script shell. */
  251. void SetLinkScriptShell(bool b) { this->LinkScriptShell = b; }
  252. /** Escape the given string to be used as a command line argument in
  253. the native build system shell. Optionally allow the build
  254. system to replace make variable references. Optionally adjust
  255. escapes for the special case of passing to the native echo
  256. command. */
  257. std::string EscapeForShell(const std::string& str, bool makeVars = false,
  258. bool forEcho = false,
  259. bool useWatcomQuote = false);
  260. /** Backwards-compatibility version of EscapeForShell. */
  261. std::string EscapeForShellOldStyle(const std::string& str);
  262. /** Escape the given string as an argument in a CMake script. */
  263. static std::string EscapeForCMake(const std::string& str);
  264. enum FortranFormat
  265. {
  266. FortranFormatNone,
  267. FortranFormatFixed,
  268. FortranFormatFree
  269. };
  270. FortranFormat GetFortranFormat(const char* value);
  271. /**
  272. * Convert the given remote path to a relative path with respect to
  273. * the given local path. The local path must be given in component
  274. * form (see SystemTools::SplitPath) without a trailing slash. The
  275. * remote path must use forward slashes and not already be escaped
  276. * or quoted.
  277. */
  278. std::string ConvertToRelativePath(const std::vector<std::string>& local,
  279. const std::string& remote,
  280. bool force=false);
  281. /**
  282. * Get the relative path from the generator output directory to a
  283. * per-target support directory.
  284. */
  285. virtual std::string GetTargetDirectory(cmTarget const& target) const;
  286. /**
  287. * Get the level of backwards compatibility requested by the project
  288. * in this directory. This is the value of the CMake variable
  289. * CMAKE_BACKWARDS_COMPATIBILITY whose format is
  290. * "major.minor[.patch]". The returned integer is encoded as
  291. *
  292. * CMake_VERSION_ENCODE(major, minor, patch)
  293. *
  294. * and is monotonically increasing with the CMake version.
  295. */
  296. cmIML_INT_uint64_t GetBackwardsCompatibility();
  297. /**
  298. * Test whether compatibility is set to a given version or lower.
  299. */
  300. bool NeedBackwardsCompatibility_2_4();
  301. /**
  302. * Generate a Mac OS X application bundle Info.plist file.
  303. */
  304. void GenerateAppleInfoPList(cmTarget* target, const std::string& targetName,
  305. const char* fname);
  306. /**
  307. * Generate a Mac OS X framework Info.plist file.
  308. */
  309. void GenerateFrameworkInfoPList(cmTarget* target,
  310. const std::string& targetName,
  311. const char* fname);
  312. /** Construct a comment for a custom command. */
  313. std::string ConstructComment(cmCustomCommandGenerator const& ccg,
  314. const char* default_comment = "");
  315. // Compute object file names.
  316. std::string GetObjectFileNameWithoutTarget(const cmSourceFile& source,
  317. std::string const& dir_max,
  318. bool* hasSourceExtension = 0);
  319. /** Fill out the static linker flags for the given target. */
  320. void GetStaticLibraryFlags(std::string& flags,
  321. std::string const& config,
  322. cmTarget* target);
  323. /** Fill out these strings for the given target. Libraries to link,
  324. * flags, and linkflags. */
  325. void GetTargetFlags(std::string& linkLibs,
  326. std::string& flags,
  327. std::string& linkFlags,
  328. std::string& frameworkPath,
  329. std::string& linkPath,
  330. cmGeneratorTarget* target,
  331. bool useWatcomQuote);
  332. virtual void ComputeObjectFilenames(
  333. std::map<cmSourceFile const*, std::string>& mapping,
  334. cmGeneratorTarget const* gt = 0);
  335. protected:
  336. ///! put all the libraries for a target on into the given stream
  337. virtual void OutputLinkLibraries(std::string& linkLibraries,
  338. std::string& frameworkPath,
  339. std::string& linkPath,
  340. cmGeneratorTarget &,
  341. bool relink,
  342. bool forResponseFile,
  343. bool useWatcomQuote);
  344. // Expand rule variables in CMake of the type found in language rules
  345. void ExpandRuleVariables(std::string& string,
  346. const RuleVariables& replaceValues);
  347. // Expand rule variables in a single string
  348. std::string ExpandRuleVariable(std::string const& variable,
  349. const RuleVariables& replaceValues);
  350. const char* GetRuleLauncher(cmTarget* target, const std::string& prop);
  351. void InsertRuleLauncher(std::string& s, cmTarget* target,
  352. const std::string& prop);
  353. /** Convert a target to a utility target for unsupported
  354. * languages of a generator */
  355. void AddBuildTargetRule(const std::string& llang,
  356. cmGeneratorTarget& target);
  357. ///! add a custom command to build a .o file that is part of a target
  358. void AddCustomCommandToCreateObject(const char* ofname,
  359. const std::string& lang,
  360. cmSourceFile& source,
  361. cmGeneratorTarget& target);
  362. // Create Custom Targets and commands for unsupported languages
  363. // The set passed in should contain the languages supported by the
  364. // generator directly. Any targets containing files that are not
  365. // of the types listed will be compiled as custom commands and added
  366. // to a custom target.
  367. void CreateCustomTargetsAndCommands(std::set<std::string> const&);
  368. // Handle old-style install rules stored in the targets.
  369. void GenerateTargetInstallRules(
  370. std::ostream& os, const std::string& config,
  371. std::vector<std::string> const& configurationTypes);
  372. std::string& CreateSafeUniqueObjectFileName(const std::string& sin,
  373. std::string const& dir_max);
  374. void ComputeObjectMaxPath();
  375. void ConfigureRelativePaths();
  376. std::string FindRelativePathTopSource();
  377. std::string FindRelativePathTopBinary();
  378. void SetupPathConversions();
  379. virtual std::string ConvertToLinkReference(std::string const& lib,
  380. OutputFormat format = SHELL);
  381. /** Check whether the native build system supports the given
  382. definition. Issues a warning. */
  383. virtual bool CheckDefinition(std::string const& define) const;
  384. /** Read the input CMakeLists.txt file. */
  385. void ReadInputFile();
  386. cmMakefile *Makefile;
  387. cmGlobalGenerator *GlobalGenerator;
  388. // members used for relative path function ConvertToMakefilePath
  389. std::string RelativePathToSourceDir;
  390. std::string RelativePathToBinaryDir;
  391. std::vector<std::string> HomeDirectoryComponents;
  392. std::vector<std::string> StartDirectoryComponents;
  393. std::vector<std::string> HomeOutputDirectoryComponents;
  394. std::vector<std::string> StartOutputDirectoryComponents;
  395. cmLocalGenerator* Parent;
  396. std::vector<cmLocalGenerator*> Children;
  397. std::map<std::string, std::string> UniqueObjectNamesMap;
  398. std::string::size_type ObjectPathMax;
  399. std::set<std::string> ObjectMaxPathViolations;
  400. bool WindowsShell;
  401. bool WindowsVSIDE;
  402. bool WatcomWMake;
  403. bool MinGWMake;
  404. bool NMake;
  405. bool ForceUnixPath;
  406. bool MSYSShell;
  407. bool LinkScriptShell;
  408. bool UseRelativePaths;
  409. bool IgnoreLibPrefix;
  410. bool Configured;
  411. bool EmitUniversalBinaryFlags;
  412. // Hack for ExpandRuleVariable until object-oriented version is
  413. // committed.
  414. std::string TargetImplib;
  415. // The top-most directories for relative path conversion. Both the
  416. // source and destination location of a relative path conversion
  417. // must be underneath one of these directories (both under source or
  418. // both under binary) in order for the relative path to be evaluated
  419. // safely by the build tools.
  420. std::string RelativePathTopSource;
  421. std::string RelativePathTopBinary;
  422. bool RelativePathsConfigured;
  423. bool PathConversionsSetup;
  424. cmIML_INT_uint64_t BackwardsCompatibility;
  425. bool BackwardsCompatibilityFinal;
  426. private:
  427. std::string ConvertToOutputForExistingCommon(const std::string& remote,
  428. std::string const& result,
  429. OutputFormat format);
  430. void AddSharedFlags(std::string& flags, const std::string& lang,
  431. bool shared);
  432. bool GetShouldUseOldFlags(bool shared, const std::string &lang) const;
  433. void AddPositionIndependentFlags(std::string& flags, std::string const& l,
  434. int targetType);
  435. };
  436. #endif