cmLocalGenerator.h 14 KB

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