Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
static std::string getMultiarchTriple(const Driver &D,
31
                                      const llvm::Triple &TargetTriple,
32
5
                                      StringRef SysRoot) {
33
5
  if (TargetTriple.getArch() == llvm::Triple::x86) {
34
3
    // We use the existence of '/lib/<triple>' as a directory to detect some
35
3
    // common hurd triples that don't quite match the Clang triple for both
36
3
    // 32-bit and 64-bit targets. Multiarch fixes its install triples to these
37
3
    // regardless of what the actual target triple is.
38
3
    if (D.getVFS().exists(SysRoot + "/lib/i386-gnu"))
39
3
      return "i386-gnu";
40
2
  }
41
2
42
2
  // For most architectures, just use whatever we have rather than trying to be
43
2
  // clever.
44
2
  return TargetTriple.str();
45
2
}
46
47
5
static StringRef getOSLibDir(const llvm::Triple &Triple, const ArgList &Args) {
48
5
  // It happens that only x86 and PPC use the 'lib32' variant of oslibdir, and
49
5
  // using that variant while targeting other architectures causes problems
50
5
  // because the libraries are laid out in shared system roots that can't cope
51
5
  // with a 'lib32' library search path being considered. So we only enable
52
5
  // them when we know we may need it.
53
5
  //
54
5
  // FIXME: This is a bit of a hack. We should really unify this code for
55
5
  // reasoning about oslibdir spellings with the lib dir spellings in the
56
5
  // GCCInstallationDetector, but that is a more significant refactoring.
57
5
58
5
  if (Triple.getArch() == llvm::Triple::x86)
59
3
    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,
65
           const ArgList &Args)
66
5
    : Generic_ELF(D, Triple, Args) {
67
5
  std::string SysRoot = computeSysRoot();
68
5
  path_list &Paths = getFilePaths();
69
5
70
5
  const std::string OSLibDir = getOSLibDir(Triple, Args);
71
5
  const std::string MultiarchTriple = getMultiarchTriple(D, Triple, SysRoot);
72
5
73
5
  // If we are currently running Clang inside of the requested system root, add
74
5
  // its parent library paths to those searched.
75
5
  // FIXME: It's not clear whether we should use the driver's installed
76
5
  // directory ('Dir' below) or the ResourceDir.
77
5
  if (StringRef(D.Dir).startswith(SysRoot)) {
78
2
    addPathIfExists(D, D.Dir + "/../lib/" + MultiarchTriple, Paths);
79
2
    addPathIfExists(D, D.Dir + "/../" + OSLibDir, Paths);
80
2
  }
81
5
82
5
  addPathIfExists(D, SysRoot + "/lib/" + MultiarchTriple, Paths);
83
5
  addPathIfExists(D, SysRoot + "/lib/../" + OSLibDir, Paths);
84
5
85
5
  addPathIfExists(D, SysRoot + "/usr/lib/" + MultiarchTriple, Paths);
86
5
  addPathIfExists(D, SysRoot + "/usr/lib/../" + OSLibDir, Paths);
87
5
88
5
  // If we are currently running Clang inside of the requested system root, add
89
5
  // its parent library path to those searched.
90
5
  // FIXME: It's not clear whether we should use the driver's installed
91
5
  // directory ('Dir' below) or the ResourceDir.
92
5
  if (StringRef(D.Dir).startswith(SysRoot))
93
2
    addPathIfExists(D, D.Dir + "/../lib", Paths);
94
5
95
5
  addPathIfExists(D, SysRoot + "/lib", Paths);
96
5
  addPathIfExists(D, SysRoot + "/usr/lib", Paths);
97
5
}
98
99
3
bool Hurd::HasNativeLLVMSupport() const { return true; }
100
101
3
Tool *Hurd::buildLinker() const { return new tools::gnutools::Linker(*this); }
102
103
0
Tool *Hurd::buildAssembler() const {
104
0
  return new tools::gnutools::Assembler(*this);
105
0
}
106
107
10
std::string Hurd::computeSysRoot() const {
108
10
  if (!getDriver().SysRoot.empty())
109
6
    return getDriver().SysRoot;
110
4
111
4
  return std::string();
112
4
}
113
114
1
std::string Hurd::getDynamicLinker(const ArgList &Args) const {
115
1
  if (getArch() == llvm::Triple::x86)
116
1
    return "/lib/ld.so";
117
0
118
0
  llvm_unreachable("unsupported architecture");
119
0
}
120
121
void Hurd::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
122
5
                                     ArgStringList &CC1Args) const {
123
5
  const Driver &D = getDriver();
124
5
  std::string SysRoot = computeSysRoot();
125
5
126
5
  if (DriverArgs.hasArg(clang::driver::options::OPT_nostdinc))
127
0
    return;
128
5
129
5
  if (!DriverArgs.hasArg(options::OPT_nostdlibinc))
130
5
    addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
131
5
132
5
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
133
5
    SmallString<128> P(D.ResourceDir);
134
5
    llvm::sys::path::append(P, "include");
135
5
    addSystemInclude(DriverArgs, CC1Args, P);
136
5
  }
137
5
138
5
  if (DriverArgs.hasArg(options::OPT_nostdlibinc))
139
0
    return;
140
5
141
5
  // Check for configure-time C include directories.
142
5
  StringRef CIncludeDirs(C_INCLUDE_DIRS);
143
5
  if (CIncludeDirs != "") {
144
0
    SmallVector<StringRef, 5> Dirs;
145
0
    CIncludeDirs.split(Dirs, ":");
146
0
    for (StringRef Dir : Dirs) {
147
0
      StringRef Prefix =
148
0
          llvm::sys::path::is_absolute(Dir) ? StringRef(SysRoot) : "";
149
0
      addExternCSystemInclude(DriverArgs, CC1Args, Prefix + Dir);
150
0
    }
151
0
    return;
152
0
  }
153
5
154
5
  // Lacking those, try to detect the correct set of system includes for the
155
5
  // target triple.
156
5
  if (getTriple().getArch() == llvm::Triple::x86) {
157
3
    std::string Path = SysRoot + "/usr/include/i386-gnu";
158
3
    if (D.getVFS().exists(Path))
159
3
      addExternCSystemInclude(DriverArgs, CC1Args, Path);
160
3
  }
161
5
162
5
  // Add an include of '/include' directly. This isn't provided by default by
163
5
  // system GCCs, but is often used with cross-compiling GCCs, and harmless to
164
5
  // add even when Clang is acting as-if it were a system compiler.
165
5
  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/include");
166
5
167
5
  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
168
5
}