xref: /freebsd/contrib/llvm-project/clang/lib/Driver/ToolChains/Hurd.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===--- Hurd.cpp - Hurd ToolChain Implementations --------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "Hurd.h"
10*0b57cec5SDimitry Andric #include "CommonArgs.h"
11*0b57cec5SDimitry Andric #include "clang/Config/config.h"
12*0b57cec5SDimitry Andric #include "clang/Driver/Driver.h"
13*0b57cec5SDimitry Andric #include "clang/Driver/Options.h"
14*0b57cec5SDimitry Andric #include "llvm/Support/Path.h"
15*0b57cec5SDimitry Andric #include "llvm/Support/VirtualFileSystem.h"
16*0b57cec5SDimitry Andric 
17*0b57cec5SDimitry Andric using namespace clang::driver;
18*0b57cec5SDimitry Andric using namespace clang::driver::toolchains;
19*0b57cec5SDimitry Andric using namespace clang;
20*0b57cec5SDimitry Andric using namespace llvm::opt;
21*0b57cec5SDimitry Andric 
22*0b57cec5SDimitry Andric using tools::addPathIfExists;
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric /// Get our best guess at the multiarch triple for a target.
25*0b57cec5SDimitry Andric ///
26*0b57cec5SDimitry Andric /// Debian-based systems are starting to use a multiarch setup where they use
27*0b57cec5SDimitry Andric /// a target-triple directory in the library and header search paths.
28*0b57cec5SDimitry Andric /// Unfortunately, this triple does not align with the vanilla target triple,
29*0b57cec5SDimitry Andric /// so we provide a rough mapping here.
30*0b57cec5SDimitry Andric static std::string getMultiarchTriple(const Driver &D,
31*0b57cec5SDimitry Andric                                       const llvm::Triple &TargetTriple,
32*0b57cec5SDimitry Andric                                       StringRef SysRoot) {
33*0b57cec5SDimitry Andric   if (TargetTriple.getArch() == llvm::Triple::x86) {
34*0b57cec5SDimitry Andric     // We use the existence of '/lib/<triple>' as a directory to detect some
35*0b57cec5SDimitry Andric     // common hurd triples that don't quite match the Clang triple for both
36*0b57cec5SDimitry Andric     // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
37*0b57cec5SDimitry Andric     // regardless of what the actual target triple is.
38*0b57cec5SDimitry Andric     if (D.getVFS().exists(SysRoot + "/lib/i386-gnu"))
39*0b57cec5SDimitry Andric       return "i386-gnu";
40*0b57cec5SDimitry Andric   }
41*0b57cec5SDimitry Andric 
42*0b57cec5SDimitry Andric   // For most architectures, just use whatever we have rather than trying to be
43*0b57cec5SDimitry Andric   // clever.
44*0b57cec5SDimitry Andric   return TargetTriple.str();
45*0b57cec5SDimitry Andric }
46*0b57cec5SDimitry Andric 
47*0b57cec5SDimitry Andric static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
48*0b57cec5SDimitry Andric   // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and
49*0b57cec5SDimitry Andric   // using that variant while targeting other architectures causes problems
50*0b57cec5SDimitry Andric   // because the libraries are laid out in shared system roots that can't cope
51*0b57cec5SDimitry Andric   // with a 'lib32' library search path being considered. So we only enable
52*0b57cec5SDimitry Andric   // them when we know we may need it.
53*0b57cec5SDimitry Andric   //
54*0b57cec5SDimitry Andric   // FIXME: This is a bit of a hack. We should really unify this code for
55*0b57cec5SDimitry Andric   // reasoning about oslibdir spellings with the lib dir spellings in the
56*0b57cec5SDimitry Andric   // GCCInstallationDetector, but that is a more significant refactoring.
57*0b57cec5SDimitry Andric 
58*0b57cec5SDimitry Andric   if (Triple.getArch() == llvm::Triple::x86)
59*0b57cec5SDimitry Andric     return "lib32";
60*0b57cec5SDimitry Andric 
61*0b57cec5SDimitry Andric   return Triple.isArch32Bit() ? "lib" : "lib64";
62*0b57cec5SDimitry Andric }
63*0b57cec5SDimitry Andric 
64*0b57cec5SDimitry Andric Hurd::Hurd(const Driver &D, const llvm::Triple &Triple,
65*0b57cec5SDimitry Andric            const ArgList &Args)
66*0b57cec5SDimitry Andric     : Generic_ELF(D, Triple, Args) {
67*0b57cec5SDimitry Andric   std::string SysRoot = computeSysRoot();
68*0b57cec5SDimitry Andric   path_list &Paths = getFilePaths();
69*0b57cec5SDimitry Andric 
70*0b57cec5SDimitry Andric   const std::string OSLibDir = getOSLibDir(Triple, Args);
71*0b57cec5SDimitry Andric   const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
72*0b57cec5SDimitry Andric 
73*0b57cec5SDimitry Andric   // If we are currently running Clang inside of the requested system root, add
74*0b57cec5SDimitry Andric   // its parent library paths to those searched.
75*0b57cec5SDimitry Andric   // FIXME: It's not clear whether we should use the driver's installed
76*0b57cec5SDimitry Andric   // directory ('Dir' below) or the ResourceDir.
77*0b57cec5SDimitry Andric   if (StringRef(D.Dir).startswith(SysRoot)) {
78*0b57cec5SDimitry Andric     addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
79*0b57cec5SDimitry Andric     addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
80*0b57cec5SDimitry Andric   }
81*0b57cec5SDimitry Andric 
82*0b57cec5SDimitry Andric   addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
83*0b57cec5SDimitry Andric   addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
84*0b57cec5SDimitry Andric 
85*0b57cec5SDimitry Andric   addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
86*0b57cec5SDimitry Andric   addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
87*0b57cec5SDimitry Andric 
88*0b57cec5SDimitry Andric   // If we are currently running Clang inside of the requested system root, add
89*0b57cec5SDimitry Andric   // its parent library path to those searched.
90*0b57cec5SDimitry Andric   // FIXME: It's not clear whether we should use the driver's installed
91*0b57cec5SDimitry Andric   // directory ('Dir' below) or the ResourceDir.
92*0b57cec5SDimitry Andric   if (StringRef(D.Dir).startswith(SysRoot))
93*0b57cec5SDimitry Andric     addPathIfExists(D, D.Dir + "/../lib", Paths);
94*0b57cec5SDimitry Andric 
95*0b57cec5SDimitry Andric   addPathIfExists(D, SysRoot + "/lib", Paths);
96*0b57cec5SDimitry Andric   addPathIfExists(D, SysRoot + "/usr/lib", Paths);
97*0b57cec5SDimitry Andric }
98*0b57cec5SDimitry Andric 
99*0b57cec5SDimitry Andric bool Hurd::HasNativeLLVMSupport() const { return true; }
100*0b57cec5SDimitry Andric 
101*0b57cec5SDimitry Andric Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
102*0b57cec5SDimitry Andric 
103*0b57cec5SDimitry Andric Tool *Hurd::buildAssembler() const {
104*0b57cec5SDimitry Andric   return new tools::gnutools::Assembler(*this);
105*0b57cec5SDimitry Andric }
106*0b57cec5SDimitry Andric 
107*0b57cec5SDimitry Andric std::string Hurd::computeSysRoot() const {
108*0b57cec5SDimitry Andric   if (!getDriver().SysRoot.empty())
109*0b57cec5SDimitry Andric     return getDriver().SysRoot;
110*0b57cec5SDimitry Andric 
111*0b57cec5SDimitry Andric   return std::string();
112*0b57cec5SDimitry Andric }
113*0b57cec5SDimitry Andric 
114*0b57cec5SDimitry Andric std::string Hurd::getDynamicLinker(const ArgList &Args) const {
115*0b57cec5SDimitry Andric   if (getArch() == llvm::Triple::x86)
116*0b57cec5SDimitry Andric     return "/lib/ld.so";
117*0b57cec5SDimitry Andric 
118*0b57cec5SDimitry Andric   llvm_unreachable("unsupported architecture");
119*0b57cec5SDimitry Andric }
120*0b57cec5SDimitry Andric 
121*0b57cec5SDimitry Andric void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
122*0b57cec5SDimitry Andric                                      ArgStringList &CC1Args) const {
123*0b57cec5SDimitry Andric   const Driver &D = getDriver();
124*0b57cec5SDimitry Andric   std::string SysRoot = computeSysRoot();
125*0b57cec5SDimitry Andric 
126*0b57cec5SDimitry Andric   if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
127*0b57cec5SDimitry Andric     return;
128*0b57cec5SDimitry Andric 
129*0b57cec5SDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
130*0b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
131*0b57cec5SDimitry Andric 
132*0b57cec5SDimitry Andric   if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
133*0b57cec5SDimitry Andric     SmallString<128> P(D.ResourceDir);
134*0b57cec5SDimitry Andric     llvm::sys::path::append(P, "include");
135*0b57cec5SDimitry Andric     addSystemInclude(DriverArgs, CC1Args, P);
136*0b57cec5SDimitry Andric   }
137*0b57cec5SDimitry Andric 
138*0b57cec5SDimitry Andric   if (DriverArgs.hasArg(options::OPT_nostdlibinc))
139*0b57cec5SDimitry Andric     return;
140*0b57cec5SDimitry Andric 
141*0b57cec5SDimitry Andric   // Check for configure-time C include directories.
142*0b57cec5SDimitry Andric   StringRef CIncludeDirs(C_INCLUDE_DIRS);
143*0b57cec5SDimitry Andric   if (CIncludeDirs != "") {
144*0b57cec5SDimitry Andric     SmallVector<StringRef, 5> Dirs;
145*0b57cec5SDimitry Andric     CIncludeDirs.split(Dirs, ":");
146*0b57cec5SDimitry Andric     for (StringRef Dir : Dirs) {
147*0b57cec5SDimitry Andric       StringRef Prefix =
148*0b57cec5SDimitry Andric           llvm::sys::path::is_absolute(Dir) ? StringRef(SysRoot) : "";
149*0b57cec5SDimitry Andric       addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
150*0b57cec5SDimitry Andric     }
151*0b57cec5SDimitry Andric     return;
152*0b57cec5SDimitry Andric   }
153*0b57cec5SDimitry Andric 
154*0b57cec5SDimitry Andric   // Lacking those, try to detect the correct set of system includes for the
155*0b57cec5SDimitry Andric   // target triple.
156*0b57cec5SDimitry Andric   if (getTriple().getArch() == llvm::Triple::x86) {
157*0b57cec5SDimitry Andric     std::string Path = SysRoot + "/usr/include/i386-gnu";
158*0b57cec5SDimitry Andric     if (D.getVFS().exists(Path))
159*0b57cec5SDimitry Andric       addExternCSystemInclude(DriverArgs, CC1Args, Path);
160*0b57cec5SDimitry Andric   }
161*0b57cec5SDimitry Andric 
162*0b57cec5SDimitry Andric   // Add an include of '/include' directly. This isn't provided by default by
163*0b57cec5SDimitry Andric   // system GCCs, but is often used with cross-compiling GCCs, and harmless to
164*0b57cec5SDimitry Andric   // add even when Clang is acting as-if it were a system compiler.
165*0b57cec5SDimitry Andric   addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
166*0b57cec5SDimitry Andric 
167*0b57cec5SDimitry Andric   addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
168*0b57cec5SDimitry Andric }
169