clang 17.0.0git
Gnu.h
Go to the documentation of this file.
1//===--- Gnu.h - Gnu Tool and ToolChain Implementations ---------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H
10#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H
11
12#include "Cuda.h"
13#include "LazyDetector.h"
14#include "ROCm.h"
15#include "clang/Driver/Tool.h"
17#include <set>
18
19namespace clang {
20namespace driver {
21
23 /// The set of multilibs that the detected installation supports.
25
26 /// The primary multilib appropriate for the given flags.
28
29 /// On Biarch systems, this corresponds to the default multilib when
30 /// targeting the non-default multilib. Otherwise, it is empty.
31 std::optional<Multilib> BiarchSibling;
32};
33
34bool findMIPSMultilibs(const Driver &D, const llvm::Triple &TargetTriple,
35 StringRef Path, const llvm::opt::ArgList &Args,
37
38namespace tools {
39
40/// Directly call GNU Binutils' assembler and linker.
41namespace gnutools {
42class LLVM_LIBRARY_VISIBILITY Assembler : public Tool {
43public:
44 Assembler(const ToolChain &TC) : Tool("GNU::Assembler", "assembler", TC) {}
45
46 bool hasIntegratedCPP() const override { return false; }
47
48 void ConstructJob(Compilation &C, const JobAction &JA,
49 const InputInfo &Output, const InputInfoList &Inputs,
50 const llvm::opt::ArgList &TCArgs,
51 const char *LinkingOutput) const override;
52};
53
54class LLVM_LIBRARY_VISIBILITY Linker : public Tool {
55public:
56 Linker(const ToolChain &TC) : Tool("GNU::Linker", "linker", TC) {}
57
58 bool hasIntegratedCPP() const override { return false; }
59 bool isLinkJob() const override { return true; }
60
61 void ConstructJob(Compilation &C, const JobAction &JA,
62 const InputInfo &Output, const InputInfoList &Inputs,
63 const llvm::opt::ArgList &TCArgs,
64 const char *LinkingOutput) const override;
65};
66
67class LLVM_LIBRARY_VISIBILITY StaticLibTool : public Tool {
68public:
70 : Tool("GNU::StaticLibTool", "static-lib-linker", TC) {}
71
72 bool hasIntegratedCPP() const override { return false; }
73 bool isLinkJob() const override { return true; }
74
75 void ConstructJob(Compilation &C, const JobAction &JA,
76 const InputInfo &Output, const InputInfoList &Inputs,
77 const llvm::opt::ArgList &TCArgs,
78 const char *LinkingOutput) const override;
79};
80} // end namespace gnutools
81
82/// gcc - Generic GCC tool implementations.
83namespace gcc {
84class LLVM_LIBRARY_VISIBILITY Common : public Tool {
85public:
86 Common(const char *Name, const char *ShortName, const ToolChain &TC)
87 : Tool(Name, ShortName, TC) {}
88
89 // A gcc tool has an "integrated" assembler that it will call to produce an
90 // object. Let it use that assembler so that we don't have to deal with
91 // assembly syntax incompatibilities.
92 bool hasIntegratedAssembler() const override { return true; }
93 void ConstructJob(Compilation &C, const JobAction &JA,
94 const InputInfo &Output, const InputInfoList &Inputs,
95 const llvm::opt::ArgList &TCArgs,
96 const char *LinkingOutput) const override;
97
98 /// RenderExtraToolArgs - Render any arguments necessary to force
99 /// the particular tool mode.
100 virtual void RenderExtraToolArgs(const JobAction &JA,
101 llvm::opt::ArgStringList &CmdArgs) const = 0;
102};
103
104class LLVM_LIBRARY_VISIBILITY Preprocessor : public Common {
105public:
107 : Common("gcc::Preprocessor", "gcc preprocessor", TC) {}
108
109 bool hasGoodDiagnostics() const override { return true; }
110 bool hasIntegratedCPP() const override { return false; }
111
112 void RenderExtraToolArgs(const JobAction &JA,
113 llvm::opt::ArgStringList &CmdArgs) const override;
114};
115
116class LLVM_LIBRARY_VISIBILITY Compiler : public Common {
117public:
118 Compiler(const ToolChain &TC) : Common("gcc::Compiler", "gcc frontend", TC) {}
119
120 bool hasGoodDiagnostics() const override { return true; }
121 bool hasIntegratedCPP() const override { return true; }
122
123 void RenderExtraToolArgs(const JobAction &JA,
124 llvm::opt::ArgStringList &CmdArgs) const override;
125};
126
127class LLVM_LIBRARY_VISIBILITY Linker : public Common {
128public:
129 Linker(const ToolChain &TC) : Common("gcc::Linker", "linker (via gcc)", TC) {}
130
131 bool hasIntegratedCPP() const override { return false; }
132 bool isLinkJob() const override { return true; }
133
134 void RenderExtraToolArgs(const JobAction &JA,
135 llvm::opt::ArgStringList &CmdArgs) const override;
136};
137} // end namespace gcc
138} // end namespace tools
139
140namespace toolchains {
141
142/// Generic_GCC - A tool chain using the 'gcc' command to perform
143/// all subcommands; this relies on gcc translating the majority of
144/// command line options.
145class LLVM_LIBRARY_VISIBILITY Generic_GCC : public ToolChain {
146public:
147 /// Struct to store and manipulate GCC versions.
148 ///
149 /// We rely on assumptions about the form and structure of GCC version
150 /// numbers: they consist of at most three '.'-separated components, and each
151 /// component is a non-negative integer except for the last component. For
152 /// the last component we are very flexible in order to tolerate release
153 /// candidates or 'x' wildcards.
154 ///
155 /// Note that the ordering established among GCCVersions is based on the
156 /// preferred version string to use. For example we prefer versions without
157 /// a hard-coded patch number to those with a hard coded patch number.
158 ///
159 /// Currently this doesn't provide any logic for textual suffixes to patches
160 /// in the way that (for example) Debian's version format does. If that ever
161 /// becomes necessary, it can be added.
162 struct GCCVersion {
163 /// The unparsed text of the version.
164 std::string Text;
165
166 /// The parsed major, minor, and patch numbers.
167 int Major, Minor, Patch;
168
169 /// The text of the parsed major, and major+minor versions.
170 std::string MajorStr, MinorStr;
171
172 /// Any textual suffix on the patch number.
173 std::string PatchSuffix;
174
175 static GCCVersion Parse(StringRef VersionText);
176 bool isOlderThan(int RHSMajor, int RHSMinor, int RHSPatch,
177 StringRef RHSPatchSuffix = StringRef()) const;
178 bool operator<(const GCCVersion &RHS) const {
179 return isOlderThan(RHS.Major, RHS.Minor, RHS.Patch, RHS.PatchSuffix);
180 }
181 bool operator>(const GCCVersion &RHS) const { return RHS < *this; }
182 bool operator<=(const GCCVersion &RHS) const { return !(*this > RHS); }
183 bool operator>=(const GCCVersion &RHS) const { return !(*this < RHS); }
184 };
185
186 /// This is a class to find a viable GCC installation for Clang to
187 /// use.
188 ///
189 /// This class tries to find a GCC installation on the system, and report
190 /// information about it. It starts from the host information provided to the
191 /// Driver, and has logic for fuzzing that where appropriate.
193 bool IsValid;
194 llvm::Triple GCCTriple;
195 const Driver &D;
196
197 // FIXME: These might be better as path objects.
198 std::string GCCInstallPath;
199 std::string GCCParentLibPath;
200
201 /// The primary multilib appropriate for the given flags.
202 Multilib SelectedMultilib;
203 /// On Biarch systems, this corresponds to the default multilib when
204 /// targeting the non-default multilib. Otherwise, it is empty.
205 std::optional<Multilib> BiarchSibling;
206
207 GCCVersion Version;
208
209 // We retain the list of install paths that were considered and rejected in
210 // order to print out detailed information in verbose mode.
211 std::set<std::string> CandidateGCCInstallPaths;
212
213 /// The set of multilibs that the detected installation supports.
214 MultilibSet Multilibs;
215
216 // Gentoo-specific toolchain configurations are stored here.
217 const std::string GentooConfigDir = "/etc/env.d/gcc";
218
219 public:
220 explicit GCCInstallationDetector(const Driver &D) : IsValid(false), D(D) {}
221 void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args,
222 ArrayRef<std::string> ExtraTripleAliases = std::nullopt);
223
224 /// Check whether we detected a valid GCC install.
225 bool isValid() const { return IsValid; }
226
227 /// Get the GCC triple for the detected install.
228 const llvm::Triple &getTriple() const { return GCCTriple; }
229
230 /// Get the detected GCC installation path.
231 StringRef getInstallPath() const { return GCCInstallPath; }
232
233 /// Get the detected GCC parent lib path.
234 StringRef getParentLibPath() const { return GCCParentLibPath; }
235
236 /// Get the detected Multilib
237 const Multilib &getMultilib() const { return SelectedMultilib; }
238
239 /// Get the whole MultilibSet
240 const MultilibSet &getMultilibs() const { return Multilibs; }
241
242 /// Get the biarch sibling multilib (if it exists).
243 /// \return true iff such a sibling exists
244 bool getBiarchSibling(Multilib &M) const;
245
246 /// Get the detected GCC version string.
247 const GCCVersion &getVersion() const { return Version; }
248
249 /// Print information about the detected GCC installation.
250 void print(raw_ostream &OS) const;
251
252 private:
253 static void
254 CollectLibDirsAndTriples(const llvm::Triple &TargetTriple,
255 const llvm::Triple &BiarchTriple,
257 SmallVectorImpl<StringRef> &TripleAliases,
258 SmallVectorImpl<StringRef> &BiarchLibDirs,
259 SmallVectorImpl<StringRef> &BiarchTripleAliases);
260
261 void AddDefaultGCCPrefixes(const llvm::Triple &TargetTriple,
263 StringRef SysRoot);
264
265 bool ScanGCCForMultilibs(const llvm::Triple &TargetTriple,
266 const llvm::opt::ArgList &Args,
267 StringRef Path,
268 bool NeedsBiarchSuffix = false);
269
270 void ScanLibDirForGCCTriple(const llvm::Triple &TargetArch,
271 const llvm::opt::ArgList &Args,
272 const std::string &LibDir,
273 StringRef CandidateTriple,
274 bool NeedsBiarchSuffix, bool GCCDirExists,
275 bool GCCCrossDirExists);
276
277 bool ScanGentooConfigs(const llvm::Triple &TargetTriple,
278 const llvm::opt::ArgList &Args,
279 const SmallVectorImpl<StringRef> &CandidateTriples,
280 const SmallVectorImpl<StringRef> &BiarchTriples);
281
282 bool ScanGentooGccConfig(const llvm::Triple &TargetTriple,
283 const llvm::opt::ArgList &Args,
284 StringRef CandidateTriple,
285 bool NeedsBiarchSuffix = false);
286 };
287
288protected:
292
293public:
294 Generic_GCC(const Driver &D, const llvm::Triple &Triple,
295 const llvm::opt::ArgList &Args);
296 ~Generic_GCC() override;
297
298 void printVerboseInfo(raw_ostream &OS) const override;
299
301 getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
302 bool isPICDefault() const override;
303 bool isPIEDefault(const llvm::opt::ArgList &Args) const override;
304 bool isPICDefaultForced() const override;
305 bool IsIntegratedAssemblerDefault() const override;
306 llvm::opt::DerivedArgList *
307 TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
308 Action::OffloadKind DeviceOffloadKind) const override;
309
310protected:
311 Tool *getTool(Action::ActionClass AC) const override;
312 Tool *buildAssembler() const override;
313 Tool *buildLinker() const override;
314
315 /// \name ToolChain Implementation Helper Functions
316 /// @{
317
318 /// Check whether the target triple's architecture is 64-bits.
319 bool isTarget64Bit() const { return getTriple().isArch64Bit(); }
320
321 /// Check whether the target triple's architecture is 32-bits.
322 bool isTarget32Bit() const { return getTriple().isArch32Bit(); }
323
324 void PushPPaths(ToolChain::path_list &PPaths);
325 void AddMultilibPaths(const Driver &D, const std::string &SysRoot,
326 const std::string &OSLibDir,
327 const std::string &MultiarchTriple,
328 path_list &Paths);
329 void AddMultiarchPaths(const Driver &D, const std::string &SysRoot,
330 const std::string &OSLibDir, path_list &Paths);
331 void AddMultilibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
332 llvm::opt::ArgStringList &CC1Args) const;
333
334 // FIXME: This should be final, but the CrossWindows toolchain does weird
335 // things that can't be easily generalized.
336 void AddClangCXXStdlibIncludeArgs(
337 const llvm::opt::ArgList &DriverArgs,
338 llvm::opt::ArgStringList &CC1Args) const override;
339
340 virtual void
341 addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
342 llvm::opt::ArgStringList &CC1Args) const;
343 virtual void
344 addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
345 llvm::opt::ArgStringList &CC1Args) const;
346
347 bool addGCCLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
348 llvm::opt::ArgStringList &CC1Args,
349 StringRef DebianMultiarch) const;
350
351 bool addLibStdCXXIncludePaths(Twine IncludeDir, StringRef Triple,
352 Twine IncludeSuffix,
353 const llvm::opt::ArgList &DriverArgs,
354 llvm::opt::ArgStringList &CC1Args,
355 bool DetectDebian = false) const;
356
357 /// @}
358
359private:
360 mutable std::unique_ptr<tools::gcc::Preprocessor> Preprocess;
361 mutable std::unique_ptr<tools::gcc::Compiler> Compile;
362};
363
364class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
365 virtual void anchor();
366
367public:
368 Generic_ELF(const Driver &D, const llvm::Triple &Triple,
369 const llvm::opt::ArgList &Args)
370 : Generic_GCC(D, Triple, Args) {}
371
372 void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
373 llvm::opt::ArgStringList &CC1Args,
374 Action::OffloadKind DeviceOffloadKind) const override;
375
376 virtual std::string getDynamicLinker(const llvm::opt::ArgList &Args) const {
377 return {};
378 }
379
380 virtual void addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {}
381};
382
383} // end namespace toolchains
384} // end namespace driver
385} // end namespace clang
386
387#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H
static void print(llvm::raw_ostream &OS, const T &V, ASTContext &, QualType)
Definition: InterpFrame.cpp:89
Simple wrapper for toolchain detector with costly initialization.
Definition: LazyDetector.h:21
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:77
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
See also MultilibSetBuilder for combining multilibs into a set.
Definition: Multilib.h:78
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag.
Definition: Multilib.h:30
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
Tool - Information on a specific compilation tool.
Definition: Tool.h:32
virtual std::string getDynamicLinker(const llvm::opt::ArgList &Args) const
Definition: Gnu.h:376
Generic_ELF(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Gnu.h:368
virtual void addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const
Definition: Gnu.h:380
This is a class to find a viable GCC installation for Clang to use.
Definition: Gnu.h:192
const Multilib & getMultilib() const
Get the detected Multilib.
Definition: Gnu.h:237
const llvm::Triple & getTriple() const
Get the GCC triple for the detected install.
Definition: Gnu.h:228
bool isValid() const
Check whether we detected a valid GCC install.
Definition: Gnu.h:225
const MultilibSet & getMultilibs() const
Get the whole MultilibSet.
Definition: Gnu.h:240
StringRef getParentLibPath() const
Get the detected GCC parent lib path.
Definition: Gnu.h:234
StringRef getInstallPath() const
Get the detected GCC installation path.
Definition: Gnu.h:231
const GCCVersion & getVersion() const
Get the detected GCC version string.
Definition: Gnu.h:247
Generic_GCC - A tool chain using the 'gcc' command to perform all subcommands; this relies on gcc tra...
Definition: Gnu.h:145
LazyDetector< CudaInstallationDetector > CudaInstallation
Definition: Gnu.h:290
GCCInstallationDetector GCCInstallation
Definition: Gnu.h:289
bool isTarget32Bit() const
Check whether the target triple's architecture is 32-bits.
Definition: Gnu.h:322
bool isTarget64Bit() const
Check whether the target triple's architecture is 64-bits.
Definition: Gnu.h:319
LazyDetector< RocmInstallationDetector > RocmInstallation
Definition: Gnu.h:291
bool hasIntegratedAssembler() const override
Definition: Gnu.h:92
Common(const char *Name, const char *ShortName, const ToolChain &TC)
Definition: Gnu.h:86
virtual void RenderExtraToolArgs(const JobAction &JA, llvm::opt::ArgStringList &CmdArgs) const =0
RenderExtraToolArgs - Render any arguments necessary to force the particular tool mode.
bool hasIntegratedCPP() const override
Definition: Gnu.h:121
bool hasGoodDiagnostics() const override
Does this tool have "good" standardized diagnostics, or should the driver add an additional "command ...
Definition: Gnu.h:120
Compiler(const ToolChain &TC)
Definition: Gnu.h:118
Linker(const ToolChain &TC)
Definition: Gnu.h:129
bool hasIntegratedCPP() const override
Definition: Gnu.h:131
bool isLinkJob() const override
Definition: Gnu.h:132
bool hasIntegratedCPP() const override
Definition: Gnu.h:110
bool hasGoodDiagnostics() const override
Does this tool have "good" standardized diagnostics, or should the driver add an additional "command ...
Definition: Gnu.h:109
Preprocessor(const ToolChain &TC)
Definition: Gnu.h:106
bool hasIntegratedCPP() const override
Definition: Gnu.h:46
Assembler(const ToolChain &TC)
Definition: Gnu.h:44
bool isLinkJob() const override
Definition: Gnu.h:59
bool hasIntegratedCPP() const override
Definition: Gnu.h:58
Linker(const ToolChain &TC)
Definition: Gnu.h:56
StaticLibTool(const ToolChain &TC)
Definition: Gnu.h:69
bool hasIntegratedCPP() const override
Definition: Gnu.h:72
bool isLinkJob() const override
Definition: Gnu.h:73
bool findMIPSMultilibs(const Driver &D, const llvm::Triple &TargetTriple, StringRef Path, const llvm::opt::ArgList &Args, DetectedMultilibs &Result)
@ Result
The result type of a method or function.
#define false
Definition: stdbool.h:22
Multilib SelectedMultilib
The primary multilib appropriate for the given flags.
Definition: Gnu.h:27
MultilibSet Multilibs
The set of multilibs that the detected installation supports.
Definition: Gnu.h:24
std::optional< Multilib > BiarchSibling
On Biarch systems, this corresponds to the default multilib when targeting the non-default multilib.
Definition: Gnu.h:31
Struct to store and manipulate GCC versions.
Definition: Gnu.h:162
std::string MajorStr
The text of the parsed major, and major+minor versions.
Definition: Gnu.h:170
bool operator<(const GCCVersion &RHS) const
Definition: Gnu.h:178
bool operator<=(const GCCVersion &RHS) const
Definition: Gnu.h:182
bool operator>=(const GCCVersion &RHS) const
Definition: Gnu.h:183
std::string PatchSuffix
Any textual suffix on the patch number.
Definition: Gnu.h:173
bool operator>(const GCCVersion &RHS) const
Definition: Gnu.h:181
int Major
The parsed major, minor, and patch numbers.
Definition: Gnu.h:167
std::string Text
The unparsed text of the version.
Definition: Gnu.h:164