cmLocalGenerator.h 20 KB

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