1 //===--- Gnu.h - Gnu Tool and ToolChain Implementations ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H
11 #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 /// \brief Base class for all GNU tools that provide the same behavior when
40 /// it comes to response files support
41 class LLVM_LIBRARY_VISIBILITY GnuTool : public Tool {
42 virtual void anchor();
45 GnuTool(const char *Name, const char *ShortName, const ToolChain &TC)
46 : Tool(Name, ShortName, TC, RF_Full, llvm::sys::WEM_CurrentCodePage) {}
49 /// Directly call GNU Binutils' assembler and linker.
51 class LLVM_LIBRARY_VISIBILITY Assembler : public GnuTool {
53 Assembler(const ToolChain &TC) : GnuTool("GNU::Assembler", "assembler", TC) {}
55 bool hasIntegratedCPP() const override { return false; }
57 void ConstructJob(Compilation &C, const JobAction &JA,
58 const InputInfo &Output, const InputInfoList &Inputs,
59 const llvm::opt::ArgList &TCArgs,
60 const char *LinkingOutput) const override;
63 class LLVM_LIBRARY_VISIBILITY Linker : public GnuTool {
65 Linker(const ToolChain &TC) : GnuTool("GNU::Linker", "linker", TC) {}
67 bool hasIntegratedCPP() const override { return false; }
68 bool isLinkJob() const override { return true; }
70 void ConstructJob(Compilation &C, const JobAction &JA,
71 const InputInfo &Output, const InputInfoList &Inputs,
72 const llvm::opt::ArgList &TCArgs,
73 const char *LinkingOutput) const override;
75 } // end namespace gnutools
77 /// gcc - Generic GCC tool implementations.
79 class LLVM_LIBRARY_VISIBILITY Common : public GnuTool {
81 Common(const char *Name, const char *ShortName, const ToolChain &TC)
82 : GnuTool(Name, ShortName, TC) {}
84 // A gcc tool has an "integrated" assembler that it will call to produce an
85 // object. Let it use that assembler so that we don't have to deal with
86 // assembly syntax incompatibilities.
87 bool hasIntegratedAssembler() const override { return true; }
88 void ConstructJob(Compilation &C, const JobAction &JA,
89 const InputInfo &Output, const InputInfoList &Inputs,
90 const llvm::opt::ArgList &TCArgs,
91 const char *LinkingOutput) const override;
93 /// RenderExtraToolArgs - Render any arguments necessary to force
94 /// the particular tool mode.
95 virtual void RenderExtraToolArgs(const JobAction &JA,
96 llvm::opt::ArgStringList &CmdArgs) const = 0;
99 class LLVM_LIBRARY_VISIBILITY Preprocessor : public Common {
101 Preprocessor(const ToolChain &TC)
102 : Common("gcc::Preprocessor", "gcc preprocessor", TC) {}
104 bool hasGoodDiagnostics() const override { return true; }
105 bool hasIntegratedCPP() const override { return false; }
107 void RenderExtraToolArgs(const JobAction &JA,
108 llvm::opt::ArgStringList &CmdArgs) const override;
111 class LLVM_LIBRARY_VISIBILITY Compiler : public Common {
113 Compiler(const ToolChain &TC) : Common("gcc::Compiler", "gcc frontend", TC) {}
115 bool hasGoodDiagnostics() const override { return true; }
116 bool hasIntegratedCPP() const override { return true; }
118 void RenderExtraToolArgs(const JobAction &JA,
119 llvm::opt::ArgStringList &CmdArgs) const override;
122 class LLVM_LIBRARY_VISIBILITY Linker : public Common {
124 Linker(const ToolChain &TC) : Common("gcc::Linker", "linker (via gcc)", TC) {}
126 bool hasIntegratedCPP() const override { return false; }
127 bool isLinkJob() const override { return true; }
129 void RenderExtraToolArgs(const JobAction &JA,
130 llvm::opt::ArgStringList &CmdArgs) const override;
132 } // end namespace gcc
133 } // end namespace tools
135 namespace toolchains {
137 /// Generic_GCC - A tool chain using the 'gcc' command to perform
138 /// all subcommands; this relies on gcc translating the majority of
139 /// command line options.
140 class LLVM_LIBRARY_VISIBILITY Generic_GCC : public ToolChain {
142 /// \brief Struct to store and manipulate GCC versions.
144 /// We rely on assumptions about the form and structure of GCC version
145 /// numbers: they consist of at most three '.'-separated components, and each
146 /// component is a non-negative integer except for the last component. For
147 /// the last component we are very flexible in order to tolerate release
148 /// candidates or 'x' wildcards.
150 /// Note that the ordering established among GCCVersions is based on the
151 /// preferred version string to use. For example we prefer versions without
152 /// a hard-coded patch number to those with a hard coded patch number.
154 /// Currently this doesn't provide any logic for textual suffixes to patches
155 /// in the way that (for example) Debian's version format does. If that ever
156 /// becomes necessary, it can be added.
158 /// \brief The unparsed text of the version.
161 /// \brief The parsed major, minor, and patch numbers.
162 int Major, Minor, Patch;
164 /// \brief The text of the parsed major, and major+minor versions.
165 std::string MajorStr, MinorStr;
167 /// \brief Any textual suffix on the patch number.
168 std::string PatchSuffix;
170 static GCCVersion Parse(StringRef VersionText);
171 bool isOlderThan(int RHSMajor, int RHSMinor, int RHSPatch,
172 StringRef RHSPatchSuffix = StringRef()) const;
173 bool operator<(const GCCVersion &RHS) const {
174 return isOlderThan(RHS.Major, RHS.Minor, RHS.Patch, RHS.PatchSuffix);
176 bool operator>(const GCCVersion &RHS) const { return RHS < *this; }
177 bool operator<=(const GCCVersion &RHS) const { return !(*this > RHS); }
178 bool operator>=(const GCCVersion &RHS) const { return !(*this < RHS); }
181 /// \brief This is a class to find a viable GCC installation for Clang to
184 /// This class tries to find a GCC installation on the system, and report
185 /// information about it. It starts from the host information provided to the
186 /// Driver, and has logic for fuzzing that where appropriate.
187 class GCCInstallationDetector {
189 llvm::Triple GCCTriple;
192 // FIXME: These might be better as path objects.
193 std::string GCCInstallPath;
194 std::string GCCParentLibPath;
196 /// The primary multilib appropriate for the given flags.
197 Multilib SelectedMultilib;
198 /// On Biarch systems, this corresponds to the default multilib when
199 /// targeting the non-default multilib. Otherwise, it is empty.
200 llvm::Optional<Multilib> BiarchSibling;
204 // We retain the list of install paths that were considered and rejected in
205 // order to print out detailed information in verbose mode.
206 std::set<std::string> CandidateGCCInstallPaths;
208 /// The set of multilibs that the detected installation supports.
209 MultilibSet Multilibs;
212 explicit GCCInstallationDetector(const Driver &D) : IsValid(false), D(D) {}
213 void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args,
214 ArrayRef<std::string> ExtraTripleAliases = None);
216 /// \brief Check whether we detected a valid GCC install.
217 bool isValid() const { return IsValid; }
219 /// \brief Get the GCC triple for the detected install.
220 const llvm::Triple &getTriple() const { return GCCTriple; }
222 /// \brief Get the detected GCC installation path.
223 StringRef getInstallPath() const { return GCCInstallPath; }
225 /// \brief Get the detected GCC parent lib path.
226 StringRef getParentLibPath() const { return GCCParentLibPath; }
228 /// \brief Get the detected Multilib
229 const Multilib &getMultilib() const { return SelectedMultilib; }
231 /// \brief Get the whole MultilibSet
232 const MultilibSet &getMultilibs() const { return Multilibs; }
234 /// Get the biarch sibling multilib (if it exists).
235 /// \return true iff such a sibling exists
236 bool getBiarchSibling(Multilib &M) const;
238 /// \brief Get the detected GCC version string.
239 const GCCVersion &getVersion() const { return Version; }
241 /// \brief Print information about the detected GCC installation.
242 void print(raw_ostream &OS) const;
246 CollectLibDirsAndTriples(const llvm::Triple &TargetTriple,
247 const llvm::Triple &BiarchTriple,
248 SmallVectorImpl<StringRef> &LibDirs,
249 SmallVectorImpl<StringRef> &TripleAliases,
250 SmallVectorImpl<StringRef> &BiarchLibDirs,
251 SmallVectorImpl<StringRef> &BiarchTripleAliases);
253 bool ScanGCCForMultilibs(const llvm::Triple &TargetTriple,
254 const llvm::opt::ArgList &Args,
256 bool NeedsBiarchSuffix = false);
258 void ScanLibDirForGCCTriple(const llvm::Triple &TargetArch,
259 const llvm::opt::ArgList &Args,
260 const std::string &LibDir,
261 StringRef CandidateTriple,
262 bool NeedsBiarchSuffix = false);
264 void scanLibDirForGCCTripleSolaris(const llvm::Triple &TargetArch,
265 const llvm::opt::ArgList &Args,
266 const std::string &LibDir,
267 StringRef CandidateTriple,
268 bool NeedsBiarchSuffix = false);
270 bool ScanGentooGccConfig(const llvm::Triple &TargetTriple,
271 const llvm::opt::ArgList &Args,
272 StringRef CandidateTriple,
273 bool NeedsBiarchSuffix = false);
277 GCCInstallationDetector GCCInstallation;
278 CudaInstallationDetector CudaInstallation;
281 Generic_GCC(const Driver &D, const llvm::Triple &Triple,
282 const llvm::opt::ArgList &Args);
283 ~Generic_GCC() override;
285 void printVerboseInfo(raw_ostream &OS) const override;
287 bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override;
288 bool isPICDefault() const override;
289 bool isPIEDefault() const override;
290 bool isPICDefaultForced() const override;
291 bool IsIntegratedAssemblerDefault() const override;
292 llvm::opt::DerivedArgList *
293 TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
294 Action::OffloadKind DeviceOffloadKind) const override;
297 Tool *getTool(Action::ActionClass AC) const override;
298 Tool *buildAssembler() const override;
299 Tool *buildLinker() const override;
301 /// \name ToolChain Implementation Helper Functions
304 /// \brief Check whether the target triple's architecture is 64-bits.
305 bool isTarget64Bit() const { return getTriple().isArch64Bit(); }
307 /// \brief Check whether the target triple's architecture is 32-bits.
308 bool isTarget32Bit() const { return getTriple().isArch32Bit(); }
310 // FIXME: This should be final, but the Solaris tool chain does weird
311 // things we can't easily represent.
312 void AddClangCXXStdlibIncludeArgs(
313 const llvm::opt::ArgList &DriverArgs,
314 llvm::opt::ArgStringList &CC1Args) const override;
316 virtual std::string findLibCxxIncludePath() const;
318 addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
319 llvm::opt::ArgStringList &CC1Args) const;
321 bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix, StringRef GCCTriple,
322 StringRef GCCMultiarchTriple,
323 StringRef TargetMultiarchTriple,
325 const llvm::opt::ArgList &DriverArgs,
326 llvm::opt::ArgStringList &CC1Args) const;
331 mutable std::unique_ptr<tools::gcc::Preprocessor> Preprocess;
332 mutable std::unique_ptr<tools::gcc::Compiler> Compile;
335 class LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
336 virtual void anchor();
339 Generic_ELF(const Driver &D, const llvm::Triple &Triple,
340 const llvm::opt::ArgList &Args)
341 : Generic_GCC(D, Triple, Args) {}
343 void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
344 llvm::opt::ArgStringList &CC1Args,
345 Action::OffloadKind DeviceOffloadKind) const override;
348 } // end namespace toolchains
349 } // end namespace driver
350 } // end namespace clang
352 #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H