Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/Hurd.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Hurd.cpp - Hurd 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
#include "Hurd.h"
10
#include "CommonArgs.h"
11
#include "clang/Config/config.h"
12
#include "clang/Driver/Driver.h"
13
#include "clang/Driver/Options.h"
14
#include "llvm/Support/Path.h"
15
#include "llvm/Support/VirtualFileSystem.h"
16
17
using namespace clang::driver;
18
using namespace clang::driver::toolchains;
19
using namespace clang;
20
using namespace llvm::opt;
21
22
using tools::addPathIfExists;
23
24
/// Get our best guess at the multiarch triple for a target.
25
///
26
/// Debian-based systems are starting to use a multiarch setup where they use
27
/// a target-triple directory in the library and header search paths.
28
/// Unfortunately, this triple does not align with the vanilla target triple,
29
/// so we provide a rough mapping here.
30
std::string Hurd::getMultiarchTriple(const Driver &D,
31
                                     const llvm::Triple &TargetTriple,
32
6
                                     StringRef SysRoot) const {
33
6
  if (TargetTriple.getArch() == llvm::Triple::x86) {
34
    // We use the existence of '/lib/<triple>' as a directory to detect some
35
    // common hurd triples that don't quite match the Clang triple for both
36
    // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
37
    // regardless of what the actual target triple is.
38
4
    if (D.getVFS().exists(SysRoot + "/lib/i386-gnu"))
39
3
      return "i386-gnu";
40
3
  }
41
3
42
  // For most architectures, just use whatever we have rather than trying to be
43
  // clever.
44
3
  return TargetTriple.str();
45
3
}
46
47
6
static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
48
  // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and
49
  // using that variant while targeting other architectures causes problems
50
  // because the libraries are laid out in shared system roots that can't cope
51
  // with a 'lib32' library search path being considered. So we only enable
52
  // them when we know we may need it.
53
  //
54
  // FIXME: This is a bit of a hack. We should really unify this code for
55
  // reasoning about oslibdir spellings with the lib dir spellings in the
56
  // GCCInstallationDetector, but that is a more significant refactoring.
57
6
58
6
  if (Triple.getArch() == llvm::Triple::x86)
59
4
    return "lib32";
60
2
61
2
  return Triple.isArch32Bit() ? "lib" : 
"lib64"0
;
62
2
}
63
64
Hurd::Hurd(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
65
6
    : Generic_ELF(D, Triple, Args) {
66
6
  GCCInstallation.init(Triple, Args);
67
6
  Multilibs = GCCInstallation.getMultilibs();
68
6
  SelectedMultilib = GCCInstallation.getMultilib();
69
6
  std::string SysRoot = computeSysRoot();
70
6
  ToolChain::path_list &PPaths = getProgramPaths();
71
6
72
6
  Generic_GCC::PushPPaths(PPaths);
73
6
74
  // The selection of paths to try here is designed to match the patterns which
75
  // the GCC driver itself uses, as this is part of the GCC-compatible driver.
76
  // This was determined by running GCC in a fake filesystem, creating all
77
  // possible permutations of these directories, and seeing which ones it added
78
  // to the link paths.
79
6
  path_list &Paths = getFilePaths();
80
6
81
6
  const std::string OSLibDir = std::string(getOSLibDir(Triple, Args));
82
6
  const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
83
6
84
#ifdef ENABLE_LINKER_BUILD_ID
85
  ExtraOpts.push_back("--build-id");
86
#endif
87
6
88
6
  Generic_GCC::AddMultilibPaths(D, SysRoot, OSLibDir, MultiarchTriple, Paths);
89
6
90
  // Similar to the logic for GCC above, if we currently running Clang inside
91
  // of the requested system root, add its parent library paths to
92
  // those searched.
93
  // FIXME: It's not clear whether we should use the driver's installed
94
  // directory ('Dir' below) or the ResourceDir.
95
6
  if (StringRef(D.Dir).startswith(SysRoot)) {
96
3
    addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
97
3
    addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
98
3
  }
99
6
100
6
  addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
101
6
  addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
102
6
103
6
  addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
104
6
  addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
105
6
106
6
  Generic_GCC::AddMultiarchPaths(D, SysRoot, OSLibDir, Paths);
107
6
108
  // Similar to the logic for GCC above, if we are currently running Clang
109
  // inside of the requested system root, add its parent library path to those
110
  // searched.
111
  // FIXME: It's not clear whether we should use the driver's installed
112
  // directory ('Dir' below) or the ResourceDir.
113
6
  if (StringRef(D.Dir).startswith(SysRoot))
114
3
    addPathIfExists(D, D.Dir + "/../lib", Paths);
115
6
116
6
  addPathIfExists(D, SysRoot + "/lib", Paths);
117
6
  addPathIfExists(D, SysRoot + "/usr/lib", Paths);
118
6
}
119
120
4
bool Hurd::HasNativeLLVMSupport() const { return true; }
121
122
4
Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
123
124
1
Tool *Hurd::buildAssembler() const {
125
1
  return new tools::gnutools::Assembler(*this);
126
1
}
127
128
2
std::string Hurd::getDynamicLinker(const ArgList &Args) const {
129
2
  if (getArch() == llvm::Triple::x86)
130
2
    return "/lib/ld.so";
131
0
132
0
  llvm_unreachable("unsupported architecture");
133
0
}
134
135
void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
136
6
                                     ArgStringList &CC1Args) const {
137
6
  const Driver &D = getDriver();
138
6
  std::string SysRoot = computeSysRoot();
139
6
140
6
  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
141
0
    return;
142
6
143
6
  if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
144
6
    addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
145
6
146
6
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
147
6
    SmallString<128> P(D.ResourceDir);
148
6
    llvm::sys::path::append(P, "include");
149
6
    addSystemInclude(DriverArgs, CC1Args, P);
150
6
  }
151
6
152
6
  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
153
0
    return;
154
6
155
  // Check for configure-time C include directories.
156
6
  StringRef CIncludeDirs(C_INCLUDE_DIRS);
157
6
  if (CIncludeDirs != "") {
158
0
    SmallVector<StringRef, 5> Dirs;
159
0
    CIncludeDirs.split(Dirs, ":");
160
0
    for (StringRef Dir : Dirs) {
161
0
      StringRef Prefix =
162
0
          llvm::sys::path::is_absolute(Dir) ? "" : StringRef(SysRoot);
163
0
      addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
164
0
    }
165
0
    return;
166
0
  }
167
6
168
  // Lacking those, try to detect the correct set of system includes for the
169
  // target triple.
170
6
171
6
  AddMultilibIncludeArgs(DriverArgs, CC1Args);
172
6
173
6
  if (getTriple().getArch() == llvm::Triple::x86) {
174
4
    std::string Path = SysRoot + "/usr/include/i386-gnu";
175
4
    if (D.getVFS().exists(Path))
176
3
      addExternCSystemInclude(DriverArgs, CC1Args, Path);
177
4
  }
178
6
179
  // Add an include of '/include' directly. This isn't provided by default by
180
  // system GCCs, but is often used with cross-compiling GCCs, and harmless to
181
  // add even when Clang is acting as-if it were a system compiler.
182
6
  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
183
6
184
6
  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
185
6
}
186
187
4
void Hurd::addExtraOpts(llvm::opt::ArgStringList &CmdArgs) const {
188
4
  for (const auto &Opt : ExtraOpts)
189
0
    CmdArgs.push_back(Opt.c_str());
190
4
}