cmLocalGenerator.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmLocalGenerator_h
  4. #define cmLocalGenerator_h
  5. #include <cmConfigure.h>
  6. #include "cmListFileCache.h"
  7. #include "cmOutputConverter.h"
  8. #include "cmPolicies.h"
  9. #include "cmState.h"
  10. #include "cmake.h"
  11. #include <cm_kwiml.h>
  12. #include <iosfwd>
  13. #include <map>
  14. #include <set>
  15. #include <string.h>
  16. #include <string>
  17. #include <vector>
  18. class cmCustomCommandGenerator;
  19. class cmGeneratorTarget;
  20. class cmGlobalGenerator;
  21. class cmMakefile;
  22. class cmSourceFile;
  23. /** \class cmLocalGenerator
  24. * \brief Create required build files for a directory.
  25. *
  26. * Subclasses of this abstract class generate makefiles, DSP, etc for various
  27. * platforms. This class should never be constructed directly. A
  28. * GlobalGenerator will create it and invoke the appropriate commands on it.
  29. */
  30. class cmLocalGenerator : public cmOutputConverter
  31. {
  32. public:
  33. cmLocalGenerator(cmGlobalGenerator* gg, cmMakefile* makefile);
  34. virtual ~cmLocalGenerator();
  35. /**
  36. * Generate the makefile for this directory.
  37. */
  38. virtual void Generate() {}
  39. virtual void ComputeHomeRelativeOutputPath() {}
  40. /**
  41. * Calls TraceVSDependencies() on all targets of this generator.
  42. */
  43. void TraceDependencies();
  44. virtual void AddHelperCommands() {}
  45. /**
  46. * Generate the install rules files in this directory.
  47. */
  48. void GenerateInstallRules();
  49. /**
  50. * Generate the test files for tests.
  51. */
  52. void GenerateTestFiles();
  53. /**
  54. * Generate a manifest of target files that will be built.
  55. */
  56. void ComputeTargetManifest();
  57. bool IsRootMakefile() const;
  58. ///! Get the makefile for this generator
  59. cmMakefile* GetMakefile() { return this->Makefile; }
  60. ///! Get the makefile for this generator, const version
  61. const cmMakefile* GetMakefile() const { return this->Makefile; }
  62. ///! Get the GlobalGenerator this is associated with
  63. cmGlobalGenerator* GetGlobalGenerator() { return this->GlobalGenerator; }
  64. const cmGlobalGenerator* GetGlobalGenerator() const
  65. {
  66. return this->GlobalGenerator;
  67. }
  68. std::string GetLinkLibsCMP0065(std::string const& linkLanguage,
  69. cmGeneratorTarget& tgt) const;
  70. cmState* GetState() const;
  71. cmState::Snapshot GetStateSnapshot() const;
  72. void AddArchitectureFlags(std::string& flags,
  73. cmGeneratorTarget const* target,
  74. const std::string& lang,
  75. const std::string& config);
  76. void AddLanguageFlags(std::string& flags, const std::string& lang,
  77. const std::string& config);
  78. void AddCMP0018Flags(std::string& flags, cmGeneratorTarget const* target,
  79. std::string const& lang, const std::string& config);
  80. void AddVisibilityPresetFlags(std::string& flags,
  81. cmGeneratorTarget const* target,
  82. const std::string& lang);
  83. void AddConfigVariableFlags(std::string& flags, const std::string& var,
  84. const std::string& config);
  85. void AddCompilerRequirementFlag(std::string& flags,
  86. cmGeneratorTarget const* target,
  87. const std::string& lang);
  88. ///! Append flags to a string.
  89. virtual void AppendFlags(std::string& flags, const std::string& newFlags);
  90. virtual void AppendFlags(std::string& flags, const char* newFlags);
  91. virtual void AppendFlagEscape(std::string& flags,
  92. const std::string& rawFlag);
  93. ///! Get the include flags for the current makefile and language
  94. std::string GetIncludeFlags(const std::vector<std::string>& includes,
  95. cmGeneratorTarget* target,
  96. const std::string& lang,
  97. bool forceFullPaths = false,
  98. bool forResponseFile = false,
  99. const std::string& config = "");
  100. const std::vector<cmGeneratorTarget*>& GetGeneratorTargets() const
  101. {
  102. return this->GeneratorTargets;
  103. }
  104. const std::vector<cmGeneratorTarget*>& GetImportedGeneratorTargets() const
  105. {
  106. return this->ImportedGeneratorTargets;
  107. }
  108. void AddGeneratorTarget(cmGeneratorTarget* gt);
  109. void AddImportedGeneratorTarget(cmGeneratorTarget* gt);
  110. void AddOwnedImportedGeneratorTarget(cmGeneratorTarget* gt);
  111. cmGeneratorTarget* FindLocalNonAliasGeneratorTarget(
  112. const std::string& name) const;
  113. cmGeneratorTarget* FindGeneratorTargetToUse(const std::string& name) const;
  114. /**
  115. * Encode a list of preprocessor definitions for the compiler
  116. * command line.
  117. */
  118. void AppendDefines(std::set<std::string>& defines,
  119. const char* defines_list) const;
  120. void AppendDefines(std::set<std::string>& defines,
  121. std::string defines_list) const
  122. {
  123. this->AppendDefines(defines, defines_list.c_str());
  124. }
  125. void AppendDefines(std::set<std::string>& defines,
  126. const std::vector<std::string>& defines_vec) const;
  127. /**
  128. * Join a set of defines into a definesString with a space separator.
  129. */
  130. void JoinDefines(const std::set<std::string>& defines,
  131. std::string& definesString, const std::string& lang);
  132. /** Lookup and append options associated with a particular feature. */
  133. void AppendFeatureOptions(std::string& flags, const std::string& lang,
  134. const char* feature);
  135. const char* GetFeature(const std::string& feature,
  136. const std::string& config);
  137. /** \brief Get absolute path to dependency \a name
  138. *
  139. * Translate a dependency as given in CMake code to the name to
  140. * appear in a generated build file.
  141. * - If \a name is a utility target, returns false.
  142. * - If \a name is a CMake target, it will be transformed to the real output
  143. * location of that target for the given configuration.
  144. * - If \a name is the full path to a file, it will be returned.
  145. * - Otherwise \a name is treated as a relative path with respect to
  146. * the source directory of this generator. This should only be
  147. * used for dependencies of custom commands.
  148. */
  149. bool GetRealDependency(const std::string& name, const std::string& config,
  150. std::string& dep);
  151. virtual std::string ConvertToIncludeReference(
  152. std::string const& path,
  153. cmOutputConverter::OutputFormat format = cmOutputConverter::SHELL,
  154. bool forceFullPaths = false);
  155. /** Called from command-line hook to clear dependencies. */
  156. virtual void ClearDependencies(cmMakefile* /* mf */, bool /* verbose */) {}
  157. /** Called from command-line hook to update dependencies. */
  158. virtual bool UpdateDependencies(const char* /* tgtInfo */, bool /*verbose*/,
  159. bool /*color*/)
  160. {
  161. return true;
  162. }
  163. /** Get the include flags for the current makefile and language. */
  164. void GetIncludeDirectories(std::vector<std::string>& dirs,
  165. cmGeneratorTarget const* target,
  166. const std::string& lang = "C",
  167. const std::string& config = "",
  168. bool stripImplicitInclDirs = true) const;
  169. void AddCompileOptions(std::string& flags, cmGeneratorTarget* target,
  170. const std::string& lang, const std::string& config);
  171. void AddCompileDefinitions(std::set<std::string>& defines,
  172. cmGeneratorTarget const* target,
  173. const std::string& config,
  174. const std::string& lang) const;
  175. std::string GetProjectName() const;
  176. /** Compute the language used to compile the given source file. */
  177. std::string GetSourceFileLanguage(const cmSourceFile& source);
  178. // Fill the vector with the target names for the object files,
  179. // preprocessed files and assembly files.
  180. void GetIndividualFileTargets(std::vector<std::string>&) {}
  181. // Create a struct to hold the varibles passed into
  182. // ExpandRuleVariables
  183. struct RuleVariables
  184. {
  185. RuleVariables() { memset(this, 0, sizeof(*this)); }
  186. cmGeneratorTarget* CMTarget;
  187. const char* TargetPDB;
  188. const char* TargetCompilePDB;
  189. const char* TargetVersionMajor;
  190. const char* TargetVersionMinor;
  191. const char* Language;
  192. const char* Objects;
  193. const char* Target;
  194. const char* LinkLibraries;
  195. const char* Source;
  196. const char* AssemblySource;
  197. const char* PreprocessedSource;
  198. const char* Output;
  199. const char* Object;
  200. const char* ObjectDir;
  201. const char* ObjectFileDir;
  202. const char* Flags;
  203. const char* ObjectsQuoted;
  204. const char* SONameFlag;
  205. const char* TargetSOName;
  206. const char* TargetInstallNameDir;
  207. const char* LinkFlags;
  208. const char* Manifests;
  209. const char* LanguageCompileFlags;
  210. const char* Defines;
  211. const char* Includes;
  212. const char* RuleLauncher;
  213. const char* DependencyFile;
  214. const char* FilterPrefix;
  215. };
  216. /**
  217. * Get the relative path from the generator output directory to a
  218. * per-target support directory.
  219. */
  220. virtual std::string GetTargetDirectory(
  221. cmGeneratorTarget const* target) const;
  222. /**
  223. * Get the level of backwards compatibility requested by the project
  224. * in this directory. This is the value of the CMake variable
  225. * CMAKE_BACKWARDS_COMPATIBILITY whose format is
  226. * "major.minor[.patch]". The returned integer is encoded as
  227. *
  228. * CMake_VERSION_ENCODE(major, minor, patch)
  229. *
  230. * and is monotonically increasing with the CMake version.
  231. */
  232. KWIML_INT_uint64_t GetBackwardsCompatibility();
  233. /**
  234. * Test whether compatibility is set to a given version or lower.
  235. */
  236. bool NeedBackwardsCompatibility_2_4();
  237. cmPolicies::PolicyStatus GetPolicyStatus(cmPolicies::PolicyID id) const;
  238. cmake* GetCMakeInstance() const;
  239. const char* GetSourceDirectory() const;
  240. const char* GetBinaryDirectory() const;
  241. const char* GetCurrentBinaryDirectory() const;
  242. const char* GetCurrentSourceDirectory() const;
  243. /**
  244. * Generate a Mac OS X application bundle Info.plist file.
  245. */
  246. void GenerateAppleInfoPList(cmGeneratorTarget* target,
  247. const std::string& targetName,
  248. const char* fname);
  249. /**
  250. * Generate a Mac OS X framework Info.plist file.
  251. */
  252. void GenerateFrameworkInfoPList(cmGeneratorTarget* target,
  253. const std::string& targetName,
  254. const char* fname);
  255. /** Construct a comment for a custom command. */
  256. std::string ConstructComment(cmCustomCommandGenerator const& ccg,
  257. const char* default_comment = "");
  258. // Compute object file names.
  259. std::string GetObjectFileNameWithoutTarget(
  260. const cmSourceFile& source, std::string const& dir_max,
  261. bool* hasSourceExtension = CM_NULLPTR);
  262. /** Fill out the static linker flags for the given target. */
  263. void GetStaticLibraryFlags(std::string& flags, std::string const& config,
  264. cmGeneratorTarget* target);
  265. /** Fill out these strings for the given target. Libraries to link,
  266. * flags, and linkflags. */
  267. void GetTargetFlags(const std::string& config, std::string& linkLibs,
  268. std::string& flags, std::string& linkFlags,
  269. std::string& frameworkPath, std::string& linkPath,
  270. cmGeneratorTarget* target, bool useWatcomQuote);
  271. void GetTargetDefines(cmGeneratorTarget const* target,
  272. std::string const& config, std::string const& lang,
  273. std::set<std::string>& defines) const;
  274. void GetTargetCompileFlags(cmGeneratorTarget* target,
  275. std::string const& config,
  276. std::string const& lang, std::string& flags);
  277. std::string GetFrameworkFlags(std::string const& l,
  278. std::string const& config,
  279. cmGeneratorTarget* target);
  280. virtual std::string GetTargetFortranFlags(cmGeneratorTarget const* target,
  281. std::string const& config);
  282. virtual void ComputeObjectFilenames(
  283. std::map<cmSourceFile const*, std::string>& mapping,
  284. cmGeneratorTarget const* gt = CM_NULLPTR);
  285. bool IsWindowsShell() const;
  286. bool IsWatcomWMake() const;
  287. bool IsMinGWMake() const;
  288. bool IsNMake() const;
  289. void IssueMessage(cmake::MessageType t, std::string const& text) const;
  290. void CreateEvaluationFileOutputs(const std::string& config);
  291. void ProcessEvaluationFiles(std::vector<std::string>& generatedFiles);
  292. protected:
  293. ///! put all the libraries for a target on into the given stream
  294. void OutputLinkLibraries(std::string& linkLibraries,
  295. std::string& frameworkPath, std::string& linkPath,
  296. cmGeneratorTarget&, bool relink,
  297. bool forResponseFile, bool useWatcomQuote);
  298. // Expand rule variables in CMake of the type found in language rules
  299. void ExpandRuleVariables(std::string& string,
  300. const RuleVariables& replaceValues);
  301. // Expand rule variables in a single string
  302. std::string ExpandRuleVariable(std::string const& variable,
  303. const RuleVariables& replaceValues);
  304. const char* GetRuleLauncher(cmGeneratorTarget* target,
  305. const std::string& prop);
  306. void InsertRuleLauncher(std::string& s, cmGeneratorTarget* target,
  307. const std::string& prop);
  308. // Handle old-style install rules stored in the targets.
  309. void GenerateTargetInstallRules(
  310. std::ostream& os, const std::string& config,
  311. std::vector<std::string> const& configurationTypes);
  312. std::string& CreateSafeUniqueObjectFileName(const std::string& sin,
  313. std::string const& dir_max);
  314. virtual std::string ConvertToLinkReference(
  315. std::string const& lib,
  316. cmOutputConverter::OutputFormat format = cmOutputConverter::SHELL);
  317. /** Check whether the native build system supports the given
  318. definition. Issues a warning. */
  319. virtual bool CheckDefinition(std::string const& define) const;
  320. cmMakefile* Makefile;
  321. cmState::Snapshot StateSnapshot;
  322. cmListFileBacktrace DirectoryBacktrace;
  323. cmGlobalGenerator* GlobalGenerator;
  324. std::map<std::string, std::string> UniqueObjectNamesMap;
  325. std::string::size_type ObjectPathMax;
  326. std::set<std::string> ObjectMaxPathViolations;
  327. std::set<cmGeneratorTarget const*> WarnCMP0063;
  328. std::vector<cmGeneratorTarget*> GeneratorTargets;
  329. std::vector<cmGeneratorTarget*> ImportedGeneratorTargets;
  330. std::vector<cmGeneratorTarget*> OwnedImportedGeneratorTargets;
  331. std::map<std::string, std::string> AliasTargets;
  332. bool EmitUniversalBinaryFlags;
  333. // Hack for ExpandRuleVariable until object-oriented version is
  334. // committed.
  335. std::string TargetImplib;
  336. KWIML_INT_uint64_t BackwardsCompatibility;
  337. bool BackwardsCompatibilityFinal;
  338. private:
  339. void AddSharedFlags(std::string& flags, const std::string& lang,
  340. bool shared);
  341. bool GetShouldUseOldFlags(bool shared, const std::string& lang) const;
  342. void AddPositionIndependentFlags(std::string& flags, std::string const& l,
  343. int targetType);
  344. void ComputeObjectMaxPath();
  345. };
  346. #if defined(CMAKE_BUILD_WITH_CMAKE)
  347. bool cmLocalGeneratorCheckObjectName(std::string& objName,
  348. std::string::size_type dir_len,
  349. std::string::size_type max_total_len);
  350. #endif
  351. #endif