cmLocalGenerator.h 18 KB

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