cmLocalGenerator.h 15 KB

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