Path: blob/main/contrib/llvm-project/clang/lib/Driver/ToolChains/Gnu.h
35269 views
//===--- Gnu.h - Gnu Tool and ToolChain Implementations ---------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H9#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H1011#include "Cuda.h"12#include "LazyDetector.h"13#include "ROCm.h"14#include "clang/Driver/Tool.h"15#include "clang/Driver/ToolChain.h"16#include <set>1718namespace clang {19namespace driver {2021struct DetectedMultilibs {22/// The set of multilibs that the detected installation supports.23MultilibSet Multilibs;2425/// The multilibs appropriate for the given flags.26llvm::SmallVector<Multilib> SelectedMultilibs;2728/// On Biarch systems, this corresponds to the default multilib when29/// targeting the non-default multilib. Otherwise, it is empty.30std::optional<Multilib> BiarchSibling;31};3233bool findMIPSMultilibs(const Driver &D, const llvm::Triple &TargetTriple,34StringRef Path, const llvm::opt::ArgList &Args,35DetectedMultilibs &Result);3637namespace tools {3839/// Directly call GNU Binutils' assembler and linker.40namespace gnutools {41class LLVM_LIBRARY_VISIBILITY Assembler : public Tool {42public:43Assembler(const ToolChain &TC) : Tool("GNU::Assembler", "assembler", TC) {}4445bool hasIntegratedCPP() const override { return false; }4647void ConstructJob(Compilation &C, const JobAction &JA,48const InputInfo &Output, const InputInfoList &Inputs,49const llvm::opt::ArgList &TCArgs,50const char *LinkingOutput) const override;51};5253class LLVM_LIBRARY_VISIBILITY Linker : public Tool {54public:55Linker(const ToolChain &TC) : Tool("GNU::Linker", "linker", TC) {}5657bool hasIntegratedCPP() const override { return false; }58bool isLinkJob() const override { return true; }5960void ConstructJob(Compilation &C, const JobAction &JA,61const InputInfo &Output, const InputInfoList &Inputs,62const llvm::opt::ArgList &TCArgs,63const char *LinkingOutput) const override;64};6566class LLVM_LIBRARY_VISIBILITY StaticLibTool : public Tool {67public:68StaticLibTool(const ToolChain &TC)69: Tool("GNU::StaticLibTool", "static-lib-linker", TC) {}7071bool hasIntegratedCPP() const override { return false; }72bool isLinkJob() const override { return true; }7374void ConstructJob(Compilation &C, const JobAction &JA,75const InputInfo &Output, const InputInfoList &Inputs,76const llvm::opt::ArgList &TCArgs,77const char *LinkingOutput) const override;78};79} // end namespace gnutools8081/// gcc - Generic GCC tool implementations.82namespace gcc {83class LLVM_LIBRARY_VISIBILITY Common : public Tool {84public:85Common(const char *Name, const char *ShortName, const ToolChain &TC)86: Tool(Name, ShortName, TC) {}8788// A gcc tool has an "integrated" assembler that it will call to produce an89// object. Let it use that assembler so that we don't have to deal with90// assembly syntax incompatibilities.91bool hasIntegratedAssembler() const override { return true; }92void ConstructJob(Compilation &C, const JobAction &JA,93const InputInfo &Output, const InputInfoList &Inputs,94const llvm::opt::ArgList &TCArgs,95const char *LinkingOutput) const override;9697/// RenderExtraToolArgs - Render any arguments necessary to force98/// the particular tool mode.99virtual void RenderExtraToolArgs(const JobAction &JA,100llvm::opt::ArgStringList &CmdArgs) const = 0;101};102103class LLVM_LIBRARY_VISIBILITY Preprocessor : public Common {104public:105Preprocessor(const ToolChain &TC)106: Common("gcc::Preprocessor", "gcc preprocessor", TC) {}107108bool hasGoodDiagnostics() const override { return true; }109bool hasIntegratedCPP() const override { return false; }110111void RenderExtraToolArgs(const JobAction &JA,112llvm::opt::ArgStringList &CmdArgs) const override;113};114115class LLVM_LIBRARY_VISIBILITY Compiler : public Common {116public:117Compiler(const ToolChain &TC) : Common("gcc::Compiler", "gcc frontend", TC) {}118119bool hasGoodDiagnostics() const override { return true; }120bool hasIntegratedCPP() const override { return true; }121122void RenderExtraToolArgs(const JobAction &JA,123llvm::opt::ArgStringList &CmdArgs) const override;124};125126class LLVM_LIBRARY_VISIBILITY Linker : public Common {127public:128Linker(const ToolChain &TC) : Common("gcc::Linker", "linker (via gcc)", TC) {}129130bool hasIntegratedCPP() const override { return false; }131bool isLinkJob() const override { return true; }132133void RenderExtraToolArgs(const JobAction &JA,134llvm::opt::ArgStringList &CmdArgs) const override;135};136} // end namespace gcc137} // end namespace tools138139namespace toolchains {140141/// Generic_GCC - A tool chain using the 'gcc' command to perform142/// all subcommands; this relies on gcc translating the majority of143/// command line options.144class LLVM_LIBRARY_VISIBILITY Generic_GCC : public ToolChain {145public:146/// Struct to store and manipulate GCC versions.147///148/// We rely on assumptions about the form and structure of GCC version149/// numbers: they consist of at most three '.'-separated components, and each150/// component is a non-negative integer except for the last component. For151/// the last component we are very flexible in order to tolerate release152/// candidates or 'x' wildcards.153///154/// Note that the ordering established among GCCVersions is based on the155/// preferred version string to use. For example we prefer versions without156/// a hard-coded patch number to those with a hard coded patch number.157///158/// Currently this doesn't provide any logic for textual suffixes to patches159/// in the way that (for example) Debian's version format does. If that ever160/// becomes necessary, it can be added.161struct GCCVersion {162/// The unparsed text of the version.163std::string Text;164165/// The parsed major, minor, and patch numbers.166int Major, Minor, Patch;167168/// The text of the parsed major, and major+minor versions.169std::string MajorStr, MinorStr;170171/// Any textual suffix on the patch number.172std::string PatchSuffix;173174static GCCVersion Parse(StringRef VersionText);175bool isOlderThan(int RHSMajor, int RHSMinor, int RHSPatch,176StringRef RHSPatchSuffix = StringRef()) const;177bool operator<(const GCCVersion &RHS) const {178return isOlderThan(RHS.Major, RHS.Minor, RHS.Patch, RHS.PatchSuffix);179}180bool operator>(const GCCVersion &RHS) const { return RHS < *this; }181bool operator<=(const GCCVersion &RHS) const { return !(*this > RHS); }182bool operator>=(const GCCVersion &RHS) const { return !(*this < RHS); }183};184185/// This is a class to find a viable GCC installation for Clang to186/// use.187///188/// This class tries to find a GCC installation on the system, and report189/// information about it. It starts from the host information provided to the190/// Driver, and has logic for fuzzing that where appropriate.191class GCCInstallationDetector {192bool IsValid;193llvm::Triple GCCTriple;194const Driver &D;195196// FIXME: These might be better as path objects.197std::string GCCInstallPath;198std::string GCCParentLibPath;199200/// The primary multilib appropriate for the given flags.201Multilib SelectedMultilib;202/// On Biarch systems, this corresponds to the default multilib when203/// targeting the non-default multilib. Otherwise, it is empty.204std::optional<Multilib> BiarchSibling;205206GCCVersion Version;207208// We retain the list of install paths that were considered and rejected in209// order to print out detailed information in verbose mode.210std::set<std::string> CandidateGCCInstallPaths;211212/// The set of multilibs that the detected installation supports.213MultilibSet Multilibs;214215// Gentoo-specific toolchain configurations are stored here.216const std::string GentooConfigDir = "/etc/env.d/gcc";217218public:219explicit GCCInstallationDetector(const Driver &D) : IsValid(false), D(D) {}220void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args);221222/// Check whether we detected a valid GCC install.223bool isValid() const { return IsValid; }224225/// Get the GCC triple for the detected install.226const llvm::Triple &getTriple() const { return GCCTriple; }227228/// Get the detected GCC installation path.229StringRef getInstallPath() const { return GCCInstallPath; }230231/// Get the detected GCC parent lib path.232StringRef getParentLibPath() const { return GCCParentLibPath; }233234/// Get the detected Multilib235const Multilib &getMultilib() const { return SelectedMultilib; }236237/// Get the whole MultilibSet238const MultilibSet &getMultilibs() const { return Multilibs; }239240/// Get the biarch sibling multilib (if it exists).241/// \return true iff such a sibling exists242bool getBiarchSibling(Multilib &M) const;243244/// Get the detected GCC version string.245const GCCVersion &getVersion() const { return Version; }246247/// Print information about the detected GCC installation.248void print(raw_ostream &OS) const;249250private:251static void252CollectLibDirsAndTriples(const llvm::Triple &TargetTriple,253const llvm::Triple &BiarchTriple,254SmallVectorImpl<StringRef> &LibDirs,255SmallVectorImpl<StringRef> &TripleAliases,256SmallVectorImpl<StringRef> &BiarchLibDirs,257SmallVectorImpl<StringRef> &BiarchTripleAliases);258259void AddDefaultGCCPrefixes(const llvm::Triple &TargetTriple,260SmallVectorImpl<std::string> &Prefixes,261StringRef SysRoot);262263bool ScanGCCForMultilibs(const llvm::Triple &TargetTriple,264const llvm::opt::ArgList &Args,265StringRef Path,266bool NeedsBiarchSuffix = false);267268void ScanLibDirForGCCTriple(const llvm::Triple &TargetArch,269const llvm::opt::ArgList &Args,270const std::string &LibDir,271StringRef CandidateTriple,272bool NeedsBiarchSuffix, bool GCCDirExists,273bool GCCCrossDirExists);274275bool ScanGentooConfigs(const llvm::Triple &TargetTriple,276const llvm::opt::ArgList &Args,277const SmallVectorImpl<StringRef> &CandidateTriples,278const SmallVectorImpl<StringRef> &BiarchTriples);279280bool ScanGentooGccConfig(const llvm::Triple &TargetTriple,281const llvm::opt::ArgList &Args,282StringRef CandidateTriple,283bool NeedsBiarchSuffix = false);284};285286protected:287GCCInstallationDetector GCCInstallation;288LazyDetector<CudaInstallationDetector> CudaInstallation;289LazyDetector<RocmInstallationDetector> RocmInstallation;290291public:292Generic_GCC(const Driver &D, const llvm::Triple &Triple,293const llvm::opt::ArgList &Args);294~Generic_GCC() override;295296void printVerboseInfo(raw_ostream &OS) const override;297298UnwindTableLevel299getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;300bool isPICDefault() const override;301bool isPIEDefault(const llvm::opt::ArgList &Args) const override;302bool isPICDefaultForced() const override;303bool IsIntegratedAssemblerDefault() const override;304llvm::opt::DerivedArgList *305TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,306Action::OffloadKind DeviceOffloadKind) const override;307308protected:309Tool *getTool(Action::ActionClass AC) const override;310Tool *buildAssembler() const override;311Tool *buildLinker() const override;312313/// \name ToolChain Implementation Helper Functions314/// @{315316/// Check whether the target triple's architecture is 64-bits.317bool isTarget64Bit() const { return getTriple().isArch64Bit(); }318319/// Check whether the target triple's architecture is 32-bits.320bool isTarget32Bit() const { return getTriple().isArch32Bit(); }321322void PushPPaths(ToolChain::path_list &PPaths);323void AddMultilibPaths(const Driver &D, const std::string &SysRoot,324const std::string &OSLibDir,325const std::string &MultiarchTriple,326path_list &Paths);327void AddMultiarchPaths(const Driver &D, const std::string &SysRoot,328const std::string &OSLibDir, path_list &Paths);329void AddMultilibIncludeArgs(const llvm::opt::ArgList &DriverArgs,330llvm::opt::ArgStringList &CC1Args) const;331332// FIXME: This should be final, but the CrossWindows toolchain does weird333// things that can't be easily generalized.334void AddClangCXXStdlibIncludeArgs(335const llvm::opt::ArgList &DriverArgs,336llvm::opt::ArgStringList &CC1Args) const override;337338virtual void339addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,340llvm::opt::ArgStringList &CC1Args) const;341virtual void342addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,343llvm::opt::ArgStringList &CC1Args) const;344345bool addGCCLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,346llvm::opt::ArgStringList &CC1Args,347StringRef DebianMultiarch) const;348349bool addLibStdCXXIncludePaths(Twine IncludeDir, StringRef Triple,350Twine IncludeSuffix,351const llvm::opt::ArgList &DriverArgs,352llvm::opt::ArgStringList &CC1Args,353bool DetectDebian = false) const;354355/// @}356357private:358mutable std::unique_ptr<tools::gcc::Preprocessor> Preprocess;359mutable std::unique_ptr<tools::gcc::Compiler> Compile;360};361362class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {363virtual void anchor();364365public:366Generic_ELF(const Driver &D, const llvm::Triple &Triple,367const llvm::opt::ArgList &Args)368: Generic_GCC(D, Triple, Args) {}369370void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,371llvm::opt::ArgStringList &CC1Args,372Action::OffloadKind DeviceOffloadKind) const override;373374virtual std::string getDynamicLinker(const llvm::opt::ArgList &Args) const {375return {};376}377378virtual void addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {}379};380381} // end namespace toolchains382} // end namespace driver383} // end namespace clang384385#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H386387388