cmLocalGenerator.h 15 KB

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