1 //===--- Gnu.h - Gnu Tool and ToolChain Implementations ---------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H
10 #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H
14 #include "clang/Driver/Tool.h"
15 #include "clang/Driver/ToolChain.h"
21 struct DetectedMultilibs {
22 /// The set of multilibs that the detected installation supports.
23 MultilibSet Multilibs;
25 /// The primary multilib appropriate for the given flags.
26 Multilib SelectedMultilib;
28 /// On Biarch systems, this corresponds to the default multilib when
29 /// targeting the non-default multilib. Otherwise, it is empty.
30 llvm::Optional<Multilib> BiarchSibling;
33 bool findMIPSMultilibs(const Driver &D, const llvm::Triple &TargetTriple,
34 StringRef Path, const llvm::opt::ArgList &Args,
35 DetectedMultilibs &Result);
39 /// Directly call GNU Binutils' assembler and linker.
41 class LLVM_LIBRARY_VISIBILITY Assembler : public Tool {
43 Assembler(const ToolChain &TC) : Tool("GNU::Assembler", "assembler", TC) {}
45 bool hasIntegratedCPP() const override { return false; }
47 void ConstructJob(Compilation &C, const JobAction &JA,
48 const InputInfo &Output, const InputInfoList &Inputs,
49 const llvm::opt::ArgList &TCArgs,
50 const char *LinkingOutput) const override;
53 class LLVM_LIBRARY_VISIBILITY Linker : public Tool {
55 Linker(const ToolChain &TC) : Tool("GNU::Linker", "linker", TC) {}
57 bool hasIntegratedCPP() const override { return false; }
58 bool isLinkJob() const override { return true; }
60 void ConstructJob(Compilation &C, const JobAction &JA,
61 const InputInfo &Output, const InputInfoList &Inputs,
62 const llvm::opt::ArgList &TCArgs,
63 const char *LinkingOutput) const override;
66 class LLVM_LIBRARY_VISIBILITY StaticLibTool : public Tool {
68 StaticLibTool(const ToolChain &TC)
69 : Tool("GNU::StaticLibTool", "static-lib-linker", TC) {}
71 bool hasIntegratedCPP() const override { return false; }
72 bool isLinkJob() const override { return true; }
74 void ConstructJob(Compilation &C, const JobAction &JA,
75 const InputInfo &Output, const InputInfoList &Inputs,
76 const llvm::opt::ArgList &TCArgs,
77 const char *LinkingOutput) const override;
79 } // end namespace gnutools
81 /// gcc - Generic GCC tool implementations.
83 class LLVM_LIBRARY_VISIBILITY Common : public Tool {
85 Common(const char *Name, const char *ShortName, const ToolChain &TC)
86 : Tool(Name, ShortName, TC) {}
88 // A gcc tool has an "integrated" assembler that it will call to produce an
89 // object. Let it use that assembler so that we don't have to deal with
90 // assembly syntax incompatibilities.
91 bool hasIntegratedAssembler() const override { return true; }
92 void ConstructJob(Compilation &C, const JobAction &JA,
93 const InputInfo &Output, const InputInfoList &Inputs,
94 const llvm::opt::ArgList &TCArgs,
95 const char *LinkingOutput) const override;
97 /// RenderExtraToolArgs - Render any arguments necessary to force
98 /// the particular tool mode.
99 virtual void RenderExtraToolArgs(const JobAction &JA,
100 llvm::opt::ArgStringList &CmdArgs) const = 0;
103 class LLVM_LIBRARY_VISIBILITY Preprocessor : public Common {
105 Preprocessor(const ToolChain &TC)
106 : Common("gcc::Preprocessor", "gcc preprocessor", TC) {}
108 bool hasGoodDiagnostics() const override { return true; }
109 bool hasIntegratedCPP() const override { return false; }
111 void RenderExtraToolArgs(const JobAction &JA,
112 llvm::opt::ArgStringList &CmdArgs) const override;
115 class LLVM_LIBRARY_VISIBILITY Compiler : public Common {
117 Compiler(const ToolChain &TC) : Common("gcc::Compiler", "gcc frontend", TC) {}
119 bool hasGoodDiagnostics() const override { return true; }
120 bool hasIntegratedCPP() const override { return true; }
122 void RenderExtraToolArgs(const JobAction &JA,
123 llvm::opt::ArgStringList &CmdArgs) const override;
126 class LLVM_LIBRARY_VISIBILITY Linker : public Common {
128 Linker(const ToolChain &TC) : Common("gcc::Linker", "linker (via gcc)", TC) {}
130 bool hasIntegratedCPP() const override { return false; }
131 bool isLinkJob() const override { return true; }
133 void RenderExtraToolArgs(const JobAction &JA,
134 llvm::opt::ArgStringList &CmdArgs) const override;
136 } // end namespace gcc
137 } // end namespace tools
139 namespace toolchains {
141 /// Generic_GCC - A tool chain using the 'gcc' command to perform
142 /// all subcommands; this relies on gcc translating the majority of
143 /// command line options.
144 class LLVM_LIBRARY_VISIBILITY Generic_GCC : public ToolChain {
146 /// Struct to store and manipulate GCC versions.
148 /// We rely on assumptions about the form and structure of GCC version
149 /// numbers: they consist of at most three '.'-separated components, and each
150 /// component is a non-negative integer except for the last component. For
151 /// the last component we are very flexible in order to tolerate release
152 /// candidates or 'x' wildcards.
154 /// Note that the ordering established among GCCVersions is based on the
155 /// preferred version string to use. For example we prefer versions without
156 /// a hard-coded patch number to those with a hard coded patch number.
158 /// Currently this doesn't provide any logic for textual suffixes to patches
159 /// in the way that (for example) Debian's version format does. If that ever
160 /// becomes necessary, it can be added.
162 /// The unparsed text of the version.
165 /// The parsed major, minor, and patch numbers.
166 int Major, Minor, Patch;
168 /// The text of the parsed major, and major+minor versions.
169 std::string MajorStr, MinorStr;
171 /// Any textual suffix on the patch number.
172 std::string PatchSuffix;
174 static GCCVersion Parse(StringRef VersionText);
175 bool isOlderThan(int RHSMajor, int RHSMinor, int RHSPatch,
176 StringRef RHSPatchSuffix = StringRef()) const;
177 bool operator<(const GCCVersion &RHS) const {
178 return isOlderThan(RHS.Major, RHS.Minor, RHS.Patch, RHS.PatchSuffix);
180 bool operator>(const GCCVersion &RHS) const { return RHS < *this; }
181 bool operator<=(const GCCVersion &RHS) const { return !(*this > RHS); }
182 bool operator>=(const GCCVersion &RHS) const { return !(*this < RHS); }
185 /// This is a class to find a viable GCC installation for Clang to
188 /// This class tries to find a GCC installation on the system, and report
189 /// information about it. It starts from the host information provided to the
190 /// Driver, and has logic for fuzzing that where appropriate.
191 class GCCInstallationDetector {
193 llvm::Triple GCCTriple;
196 // FIXME: These might be better as path objects.
197 std::string GCCInstallPath;
198 std::string GCCParentLibPath;
200 /// The primary multilib appropriate for the given flags.
201 Multilib SelectedMultilib;
202 /// On Biarch systems, this corresponds to the default multilib when
203 /// targeting the non-default multilib. Otherwise, it is empty.
204 llvm::Optional<Multilib> BiarchSibling;
208 // We retain the list of install paths that were considered and rejected in
209 // order to print out detailed information in verbose mode.
210 std::set<std::string> CandidateGCCInstallPaths;
212 /// The set of multilibs that the detected installation supports.
213 MultilibSet Multilibs;
216 explicit GCCInstallationDetector(const Driver &D) : IsValid(false), D(D) {}
217 void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args,
218 ArrayRef<std::string> ExtraTripleAliases = None);
220 /// Check whether we detected a valid GCC install.
221 bool isValid() const { return IsValid; }
223 /// Get the GCC triple for the detected install.
224 const llvm::Triple &getTriple() const { return GCCTriple; }
226 /// Get the detected GCC installation path.
227 StringRef getInstallPath() const { return GCCInstallPath; }
229 /// Get the detected GCC parent lib path.
230 StringRef getParentLibPath() const { return GCCParentLibPath; }
232 /// Get the detected Multilib
233 const Multilib &getMultilib() const { return SelectedMultilib; }
235 /// Get the whole MultilibSet
236 const MultilibSet &getMultilibs() const { return Multilibs; }
238 /// Get the biarch sibling multilib (if it exists).
239 /// \return true iff such a sibling exists
240 bool getBiarchSibling(Multilib &M) const;
242 /// Get the detected GCC version string.
243 const GCCVersion &getVersion() const { return Version; }
245 /// Print information about the detected GCC installation.
246 void print(raw_ostream &OS) const;
250 CollectLibDirsAndTriples(const llvm::Triple &TargetTriple,
251 const llvm::Triple &BiarchTriple,
252 SmallVectorImpl<StringRef> &LibDirs,
253 SmallVectorImpl<StringRef> &TripleAliases,
254 SmallVectorImpl<StringRef> &BiarchLibDirs,
255 SmallVectorImpl<StringRef> &BiarchTripleAliases);
257 void AddDefaultGCCPrefixes(const llvm::Triple &TargetTriple,
258 SmallVectorImpl<std::string> &Prefixes,
261 bool ScanGCCForMultilibs(const llvm::Triple &TargetTriple,
262 const llvm::opt::ArgList &Args,
264 bool NeedsBiarchSuffix = false);
266 void ScanLibDirForGCCTriple(const llvm::Triple &TargetArch,
267 const llvm::opt::ArgList &Args,
268 const std::string &LibDir,
269 StringRef CandidateTriple,
270 bool NeedsBiarchSuffix = false);
272 bool ScanGentooConfigs(const llvm::Triple &TargetTriple,
273 const llvm::opt::ArgList &Args,
274 const SmallVectorImpl<StringRef> &CandidateTriples,
275 const SmallVectorImpl<StringRef> &BiarchTriples);
277 bool ScanGentooGccConfig(const llvm::Triple &TargetTriple,
278 const llvm::opt::ArgList &Args,
279 StringRef CandidateTriple,
280 bool NeedsBiarchSuffix = false);
284 GCCInstallationDetector GCCInstallation;
285 CudaInstallationDetector CudaInstallation;
286 RocmInstallationDetector RocmInstallation;
289 Generic_GCC(const Driver &D, const llvm::Triple &Triple,
290 const llvm::opt::ArgList &Args);
291 ~Generic_GCC() override;
293 void printVerboseInfo(raw_ostream &OS) const override;
295 bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override;
296 bool isPICDefault() const override;
297 bool isPIEDefault() const override;
298 bool isPICDefaultForced() const override;
299 bool IsIntegratedAssemblerDefault() const override;
300 llvm::opt::DerivedArgList *
301 TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
302 Action::OffloadKind DeviceOffloadKind) const override;
305 Tool *getTool(Action::ActionClass AC) const override;
306 Tool *buildAssembler() const override;
307 Tool *buildLinker() const override;
309 virtual std::string getMultiarchTriple(const Driver &D,
310 const llvm::Triple &TargetTriple,
311 StringRef SysRoot) const
312 { return TargetTriple.str(); }
314 /// \name ToolChain Implementation Helper Functions
317 /// Check whether the target triple's architecture is 64-bits.
318 bool isTarget64Bit() const { return getTriple().isArch64Bit(); }
320 /// Check whether the target triple's architecture is 32-bits.
321 bool isTarget32Bit() const { return getTriple().isArch32Bit(); }
323 void PushPPaths(ToolChain::path_list &PPaths);
324 void AddMultilibPaths(const Driver &D, const std::string &SysRoot,
325 const std::string &OSLibDir,
326 const std::string &MultiarchTriple,
328 void AddMultiarchPaths(const Driver &D, const std::string &SysRoot,
329 const std::string &OSLibDir, path_list &Paths);
330 void AddMultilibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
331 llvm::opt::ArgStringList &CC1Args) const;
333 // FIXME: This should be final, but the CrossWindows toolchain does weird
334 // things that can't be easily generalized.
335 void AddClangCXXStdlibIncludeArgs(
336 const llvm::opt::ArgList &DriverArgs,
337 llvm::opt::ArgStringList &CC1Args) const override;
340 addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
341 llvm::opt::ArgStringList &CC1Args) const;
343 addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
344 llvm::opt::ArgStringList &CC1Args) const;
347 addGCCLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
348 llvm::opt::ArgStringList &CC1Args) const;
350 bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix, StringRef GCCTriple,
351 StringRef GCCMultiarchTriple,
352 StringRef TargetMultiarchTriple,
354 const llvm::opt::ArgList &DriverArgs,
355 llvm::opt::ArgStringList &CC1Args) const;
360 mutable std::unique_ptr<tools::gcc::Preprocessor> Preprocess;
361 mutable std::unique_ptr<tools::gcc::Compiler> Compile;
364 class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
365 virtual void anchor();
368 Generic_ELF(const Driver &D, const llvm::Triple &Triple,
369 const llvm::opt::ArgList &Args)
370 : Generic_GCC(D, Triple, Args) {}
372 void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
373 llvm::opt::ArgStringList &CC1Args,
374 Action::OffloadKind DeviceOffloadKind) const override;
376 virtual std::string getDynamicLinker(const llvm::opt::ArgList &Args) const {
380 virtual void addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {}
383 } // end namespace toolchains
384 } // end namespace driver
385 } // end namespace clang
387 #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H