Gnu.h revision 317019
1139804Simp//===--- Gnu.h - Gnu Tool and ToolChain Implementations ---------*- C++ -*-===//
21541Srgrimes//
31541Srgrimes//                     The LLVM Compiler Infrastructure
41541Srgrimes//
51541Srgrimes// This file is distributed under the University of Illinois Open Source
61541Srgrimes// License. See LICENSE.TXT for details.
71541Srgrimes//
81541Srgrimes//===----------------------------------------------------------------------===//
91541Srgrimes
101541Srgrimes#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H
111541Srgrimes#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H
121541Srgrimes
131541Srgrimes#include "Cuda.h"
141541Srgrimes#include "clang/Driver/Tool.h"
151541Srgrimes#include "clang/Driver/ToolChain.h"
161541Srgrimes#include <set>
171541Srgrimes
181541Srgrimesnamespace clang {
191541Srgrimesnamespace driver {
201541Srgrimes
211541Srgrimesstruct DetectedMultilibs {
221541Srgrimes  /// The set of multilibs that the detected installation supports.
231541Srgrimes  MultilibSet Multilibs;
241541Srgrimes
251541Srgrimes  /// The primary multilib appropriate for the given flags.
261541Srgrimes  Multilib SelectedMultilib;
271541Srgrimes
281541Srgrimes  /// On Biarch systems, this corresponds to the default multilib when
291541Srgrimes  /// targeting the non-default multilib. Otherwise, it is empty.
301541Srgrimes  llvm::Optional<Multilib> BiarchSibling;
311541Srgrimes};
321541Srgrimes
331541Srgrimesbool findMIPSMultilibs(const Driver &D, const llvm::Triple &TargetTriple,
341541Srgrimes                       StringRef Path, const llvm::opt::ArgList &Args,
351541Srgrimes                       DetectedMultilibs &Result);
361541Srgrimes
37116182Sobriennamespace tools {
38116182Sobrien
39116182Sobrien/// \brief Base class for all GNU tools that provide the same behavior when
4013203Swollman/// it comes to response files support
41101127Srwatsonclass LLVM_LIBRARY_VISIBILITY GnuTool : public Tool {
42144613Sjeff  virtual void anchor();
4313203Swollman
441541Srgrimespublic:
452112Swollman  GnuTool(const char *Name, const char *ShortName, const ToolChain &TC)
4669664Speter      : Tool(Name, ShortName, TC, RF_Full, llvm::sys::WEM_CurrentCodePage) {}
47177785Skib};
4876166Smarkm
4989316Salfred/// Directly call GNU Binutils' assembler and linker.
501541Srgrimesnamespace gnutools {
511541Srgrimesclass LLVM_LIBRARY_VISIBILITY Assembler : public GnuTool {
521541Srgrimespublic:
531541Srgrimes  Assembler(const ToolChain &TC) : GnuTool("GNU::Assembler", "assembler", TC) {}
541541Srgrimes
55141471Sjhb  bool hasIntegratedCPP() const override { return false; }
56144613Sjeff
571541Srgrimes  void ConstructJob(Compilation &C, const JobAction &JA,
581541Srgrimes                    const InputInfo &Output, const InputInfoList &Inputs,
591541Srgrimes                    const llvm::opt::ArgList &TCArgs,
601541Srgrimes                    const char *LinkingOutput) const override;
61155334Srwatson};
62163606Srwatson
63155334Srwatsonclass LLVM_LIBRARY_VISIBILITY Linker : public GnuTool {
6492751Sjeffpublic:
6532011Sbde  Linker(const ToolChain &TC) : GnuTool("GNU::Linker", "linker", TC) {}
66155168Sjeff
67138345Sphk  bool hasIntegratedCPP() const override { return false; }
68138345Sphk  bool isLinkJob() const override { return true; }
691541Srgrimes
7069664Speter  void ConstructJob(Compilation &C, const JobAction &JA,
7169664Speter                    const InputInfo &Output, const InputInfoList &Inputs,
7292751Sjeff                    const llvm::opt::ArgList &TCArgs,
73166167Skib                    const char *LinkingOutput) const override;
74166167Skib};
75166167Skib} // end namespace gnutools
76166167Skib
7769664Speter/// gcc - Generic GCC tool implementations.
7869664Speternamespace gcc {
7969664Speterclass LLVM_LIBRARY_VISIBILITY Common : public GnuTool {
8069664Speterpublic:
81168138Srwatson  Common(const char *Name, const char *ShortName, const ToolChain &TC)
82168138Srwatson      : GnuTool(Name, ShortName, TC) {}
8392654Sjeff
8492654Sjeff  // A gcc tool has an "integrated" assembler that it will call to produce an
85168138Srwatson  // object. Let it use that assembler so that we don't have to deal with
86168138Srwatson  // assembly syntax incompatibilities.
87168138Srwatson  bool hasIntegratedAssembler() const override { return true; }
88176519Sattilio  void ConstructJob(Compilation &C, const JobAction &JA,
8969664Speter                    const InputInfo &Output, const InputInfoList &Inputs,
90177253Srwatson                    const llvm::opt::ArgList &TCArgs,
9169664Speter                    const char *LinkingOutput) const override;
92144613Sjeff
93144613Sjeff  /// RenderExtraToolArgs - Render any arguments necessary to force
94144613Sjeff  /// the particular tool mode.
95144613Sjeff  virtual void RenderExtraToolArgs(const JobAction &JA,
96144613Sjeff                                   llvm::opt::ArgStringList &CmdArgs) const = 0;
97144613Sjeff};
98144613Sjeff
99144613Sjeffclass LLVM_LIBRARY_VISIBILITY Preprocessor : public Common {
10069664Speterpublic:
101161010Srwatson  Preprocessor(const ToolChain &TC)
1021541Srgrimes      : Common("gcc::Preprocessor", "gcc preprocessor", TC) {}
1031541Srgrimes
1041541Srgrimes  bool hasGoodDiagnostics() const override { return true; }
1051541Srgrimes  bool hasIntegratedCPP() const override { return false; }
1061541Srgrimes
1071541Srgrimes  void RenderExtraToolArgs(const JobAction &JA,
1081541Srgrimes                           llvm::opt::ArgStringList &CmdArgs) const override;
1091541Srgrimes};
1101541Srgrimes
1111541Srgrimesclass LLVM_LIBRARY_VISIBILITY Compiler : public Common {
1121541Srgrimespublic:
1131541Srgrimes  Compiler(const ToolChain &TC) : Common("gcc::Compiler", "gcc frontend", TC) {}
1141541Srgrimes
1151541Srgrimes  bool hasGoodDiagnostics() const override { return true; }
1161541Srgrimes  bool hasIntegratedCPP() const override { return true; }
1171541Srgrimes
1181541Srgrimes  void RenderExtraToolArgs(const JobAction &JA,
1191541Srgrimes                           llvm::opt::ArgStringList &CmdArgs) const override;
1201541Srgrimes};
121161011Srwatson
1221541Srgrimesclass LLVM_LIBRARY_VISIBILITY Linker : public Common {
123161011Srwatsonpublic:
124161011Srwatson  Linker(const ToolChain &TC) : Common("gcc::Linker", "linker (via gcc)", TC) {}
125161011Srwatson
1261541Srgrimes  bool hasIntegratedCPP() const override { return false; }
1271541Srgrimes  bool isLinkJob() const override { return true; }
1281541Srgrimes
1291541Srgrimes  void RenderExtraToolArgs(const JobAction &JA,
13083366Sjulian                           llvm::opt::ArgStringList &CmdArgs) const override;
13183366Sjulian};
132140714Sjeff} // end namespace gcc
1331541Srgrimes} // end namespace tools
134150164Scsjp
135150164Scsjpnamespace toolchains {
13691419Sjhb
13783366Sjulian/// Generic_GCC - A tool chain using the 'gcc' command to perform
13842408Seivind/// all subcommands; this relies on gcc translating the majority of
13942453Seivind/// command line options.
14042408Seivindclass LLVM_LIBRARY_VISIBILITY Generic_GCC : public ToolChain {
14142453Seivindpublic:
142144613Sjeff  /// \brief Struct to store and manipulate GCC versions.
143144613Sjeff  ///
14483366Sjulian  /// We rely on assumptions about the form and structure of GCC version
1451541Srgrimes  /// numbers: they consist of at most three '.'-separated components, and each
1461541Srgrimes  /// component is a non-negative integer except for the last component. For
1471541Srgrimes  /// the last component we are very flexible in order to tolerate release
1481541Srgrimes  /// candidates or 'x' wildcards.
1491541Srgrimes  ///
1501541Srgrimes  /// Note that the ordering established among GCCVersions is based on the
151111119Simp  /// preferred version string to use. For example we prefer versions without
1521541Srgrimes  /// a hard-coded patch number to those with a hard coded patch number.
1531541Srgrimes  ///
15436735Sdfr  /// Currently this doesn't provide any logic for textual suffixes to patches
1551541Srgrimes  /// in the way that (for example) Debian's version format does. If that ever
1561541Srgrimes  /// becomes necessary, it can be added.
15736735Sdfr  struct GCCVersion {
15820069Sbde    /// \brief The unparsed text of the version.
159155334Srwatson    std::string Text;
160155334Srwatson
161155334Srwatson    /// \brief The parsed major, minor, and patch numbers.
162155334Srwatson    int Major, Minor, Patch;
163155334Srwatson
164155334Srwatson    /// \brief The text of the parsed major, and major+minor versions.
16520069Sbde    std::string MajorStr, MinorStr;
16620069Sbde
16720069Sbde    /// \brief Any textual suffix on the patch number.
16820069Sbde    std::string PatchSuffix;
16920069Sbde
17020069Sbde    static GCCVersion Parse(StringRef VersionText);
1711541Srgrimes    bool isOlderThan(int RHSMajor, int RHSMinor, int RHSPatch,
17292751Sjeff                     StringRef RHSPatchSuffix = StringRef()) const;
173100613Srwatson    bool operator<(const GCCVersion &RHS) const {
174100613Srwatson      return isOlderThan(RHS.Major, RHS.Minor, RHS.Patch, RHS.PatchSuffix);
175100613Srwatson    }
176100613Srwatson    bool operator>(const GCCVersion &RHS) const { return RHS < *this; }
1771541Srgrimes    bool operator<=(const GCCVersion &RHS) const { return !(*this > RHS); }
1781541Srgrimes    bool operator>=(const GCCVersion &RHS) const { return !(*this < RHS); }
1791541Srgrimes  };
1801541Srgrimes
1811541Srgrimes  /// \brief This is a class to find a viable GCC installation for Clang to
18297994Sjhb  /// use.
18397994Sjhb  ///
18497994Sjhb  /// This class tries to find a GCC installation on the system, and report
18597994Sjhb  /// information about it. It starts from the host information provided to the
18697994Sjhb  /// Driver, and has logic for fuzzing that where appropriate.
1871541Srgrimes  class GCCInstallationDetector {
1881541Srgrimes    bool IsValid;
1891541Srgrimes    llvm::Triple GCCTriple;
1901541Srgrimes    const Driver &D;
1911541Srgrimes
192168355Srwatson    // FIXME: These might be better as path objects.
19333360Sdyson    std::string GCCInstallPath;
19451649Sphk    std::string GCCParentLibPath;
19533360Sdyson
196177785Skib    /// The primary multilib appropriate for the given flags.
197177785Skib    Multilib SelectedMultilib;
198177785Skib    /// On Biarch systems, this corresponds to the default multilib when
199177785Skib    /// targeting the non-default multilib. Otherwise, it is empty.
200177785Skib    llvm::Optional<Multilib> BiarchSibling;
201177785Skib
202177785Skib    GCCVersion Version;
203177785Skib
204177785Skib    // We retain the list of install paths that were considered and rejected in
205177785Skib    // order to print out detailed information in verbose mode.
206177785Skib    std::set<std::string> CandidateGCCInstallPaths;
207177785Skib
208177785Skib    /// The set of multilibs that the detected installation supports.
209177785Skib    MultilibSet Multilibs;
210177785Skib
211177785Skib  public:
212177785Skib    explicit GCCInstallationDetector(const Driver &D) : IsValid(false), D(D) {}
213177785Skib    void init(const llvm::Triple &TargetTriple, const llvm::opt::ArgList &Args,
214177785Skib              ArrayRef<std::string> ExtraTripleAliases = None);
215177785Skib
216177785Skib    /// \brief Check whether we detected a valid GCC install.
217177785Skib    bool isValid() const { return IsValid; }
218140714Sjeff
2191541Srgrimes    /// \brief Get the GCC triple for the detected install.
2201541Srgrimes    const llvm::Triple &getTriple() const { return GCCTriple; }
2211541Srgrimes
2221541Srgrimes    /// \brief Get the detected GCC installation path.
2231541Srgrimes    StringRef getInstallPath() const { return GCCInstallPath; }
2241541Srgrimes
2251541Srgrimes    /// \brief Get the detected GCC parent lib path.
2261541Srgrimes    StringRef getParentLibPath() const { return GCCParentLibPath; }
227140714Sjeff
2281541Srgrimes    /// \brief Get the detected Multilib
2291541Srgrimes    const Multilib &getMultilib() const { return SelectedMultilib; }
2301541Srgrimes
2311541Srgrimes    /// \brief Get the whole MultilibSet
2321541Srgrimes    const MultilibSet &getMultilibs() const { return Multilibs; }
233140714Sjeff
2341541Srgrimes    /// Get the biarch sibling multilib (if it exists).
2351541Srgrimes    /// \return true iff such a sibling exists
236140714Sjeff    bool getBiarchSibling(Multilib &M) const;
237140714Sjeff
2381541Srgrimes    /// \brief Get the detected GCC version string.
2393148Sphk    const GCCVersion &getVersion() const { return Version; }
2403148Sphk
24192751Sjeff    /// \brief Print information about the detected GCC installation.
242100613Srwatson    void print(raw_ostream &OS) const;
243100613Srwatson
244100613Srwatson  private:
245100613Srwatson    static void
2461541Srgrimes    CollectLibDirsAndTriples(const llvm::Triple &TargetTriple,
2471541Srgrimes                             const llvm::Triple &BiarchTriple,
248140714Sjeff                             SmallVectorImpl<StringRef> &LibDirs,
249140714Sjeff                             SmallVectorImpl<StringRef> &TripleAliases,
2501541Srgrimes                             SmallVectorImpl<StringRef> &BiarchLibDirs,
2511541Srgrimes                             SmallVectorImpl<StringRef> &BiarchTripleAliases);
2521541Srgrimes
2531541Srgrimes    bool ScanGCCForMultilibs(const llvm::Triple &TargetTriple,
254100613Srwatson                             const llvm::opt::ArgList &Args,
25592751Sjeff                             StringRef Path,
256100613Srwatson                             bool NeedsBiarchSuffix = false);
257100613Srwatson
258100613Srwatson    void ScanLibDirForGCCTriple(const llvm::Triple &TargetArch,
259100613Srwatson                                const llvm::opt::ArgList &Args,
260100613Srwatson                                const std::string &LibDir,
2611541Srgrimes                                StringRef CandidateTriple,
26232286Sdyson                                bool NeedsBiarchSuffix = false);
263140714Sjeff
264140714Sjeff    void scanLibDirForGCCTripleSolaris(const llvm::Triple &TargetArch,
265140714Sjeff                                       const llvm::opt::ArgList &Args,
266140714Sjeff                                       const std::string &LibDir,
2671541Srgrimes                                       StringRef CandidateTriple,
2681541Srgrimes                                       bool NeedsBiarchSuffix = false);
2691541Srgrimes
2701541Srgrimes    bool ScanGentooGccConfig(const llvm::Triple &TargetTriple,
2711541Srgrimes                             const llvm::opt::ArgList &Args,
2721541Srgrimes                             StringRef CandidateTriple,
273101127Srwatson                             bool NeedsBiarchSuffix = false);
274105479Srwatson  };
275172930Srwatson
276105479Srwatsonprotected:
277105479Srwatson  GCCInstallationDetector GCCInstallation;
278105479Srwatson  CudaInstallationDetector CudaInstallation;
279105479Srwatson
280101127Srwatsonpublic:
2811541Srgrimes  Generic_GCC(const Driver &D, const llvm::Triple &Triple,
282111119Simp              const llvm::opt::ArgList &Args);
2831541Srgrimes  ~Generic_GCC() override;
2841541Srgrimes
2851541Srgrimes  void printVerboseInfo(raw_ostream &OS) const override;
2861541Srgrimes
2871541Srgrimes  bool IsUnwindTablesDefault() const override;
2881541Srgrimes  bool isPICDefault() const override;
2891541Srgrimes  bool isPIEDefault() const override;
2901541Srgrimes  bool isPICDefaultForced() const override;
2911541Srgrimes  bool IsIntegratedAssemblerDefault() const override;
29283366Sjulian  llvm::opt::DerivedArgList *
2931541Srgrimes  TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch,
2943148Sphk                Action::OffloadKind DeviceOffloadKind) const override;
2953148Sphk
2961541Srgrimesprotected:
29792751Sjeff  Tool *getTool(Action::ActionClass AC) const override;
2981541Srgrimes  Tool *buildAssembler() const override;
2991541Srgrimes  Tool *buildLinker() const override;
3001541Srgrimes
30178692Sdillon  /// \name ToolChain Implementation Helper Functions
30278692Sdillon  /// @{
30392751Sjeff
30478692Sdillon  /// \brief Check whether the target triple's architecture is 64-bits.
30578692Sdillon  bool isTarget64Bit() const { return getTriple().isArch64Bit(); }
30678692Sdillon
3071541Srgrimes  /// \brief Check whether the target triple's architecture is 32-bits.
3081541Srgrimes  bool isTarget32Bit() const { return getTriple().isArch32Bit(); }
30992751Sjeff
3101541Srgrimes  // FIXME: This should be final, but the Solaris tool chain does weird
3111541Srgrimes  // things we can't easily represent.
3121541Srgrimes  void AddClangCXXStdlibIncludeArgs(
3131541Srgrimes      const llvm::opt::ArgList &DriverArgs,
3141541Srgrimes      llvm::opt::ArgStringList &CC1Args) const override;
31592751Sjeff
3161541Srgrimes  virtual std::string findLibCxxIncludePath() const;
3171541Srgrimes  virtual void
3181541Srgrimes  addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
3191541Srgrimes                           llvm::opt::ArgStringList &CC1Args) const;
3201541Srgrimes
3211541Srgrimes  bool addLibStdCXXIncludePaths(Twine Base, Twine Suffix, StringRef GCCTriple,
3221541Srgrimes                                StringRef GCCMultiarchTriple,
32392751Sjeff                                StringRef TargetMultiarchTriple,
324100613Srwatson                                Twine IncludeSuffix,
325100613Srwatson                                const llvm::opt::ArgList &DriverArgs,
326100613Srwatson                                llvm::opt::ArgStringList &CC1Args) const;
327100613Srwatson
328144833Sjeff  /// @}
329144833Sjeff
3301541Srgrimesprivate:
331140714Sjeff  mutable std::unique_ptr<tools::gcc::Preprocessor> Preprocess;
3321541Srgrimes  mutable std::unique_ptr<tools::gcc::Compiler> Compile;
3331541Srgrimes};
3341541Srgrimes
335162288Smohansclass LLVM_LIBRARY_VISIBILITY Generic_ELF : public Generic_GCC {
336162288Smohans  virtual void anchor();
337162288Smohans
338162310Smohanspublic:
339162310Smohans  Generic_ELF(const Driver &D, const llvm::Triple &Triple,
340162288Smohans              const llvm::opt::ArgList &Args)
341162288Smohans      : Generic_GCC(D, Triple, Args) {}
342162288Smohans
343162288Smohans  void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
344162288Smohans                             llvm::opt::ArgStringList &CC1Args) const override;
345162288Smohans};
3461541Srgrimes
3471541Srgrimes} // end namespace toolchains
3481541Srgrimes} // end namespace driver
3491541Srgrimes} // end namespace clang
3501541Srgrimes
3511541Srgrimes#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_GNU_H
3521541Srgrimes