cmLocalGenerator.h 18 KB

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