cmLocalGenerator.h 18 KB

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