cmLocalGenerator.h 17 KB

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