cmLocalGenerator.h 15 KB

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