1
0

cmLocalGenerator.h 19 KB

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