cmLocalGenerator.h 18 KB

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