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