cmLocalGenerator.h 14 KB

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