Skip to content

Commit 63eaf99

Browse files
committed
[Driver] Teach Barmetal toolchain about GCC installation
This patch introduces the baretmetal toolchain object about GCC Installation. Currently, if `--gcc-installation` ot `--gcc-install-dir` options are passed on commandline, then sysroot will be formed from there if paths will be valid. Otherwise, it will be fallback to as it already existed in the Baremetal toolchaibn object. Moreover, support for adding include paths for libstd C++ library is added as well. Additionally, the restriction to always use integrated assembler is removed because with valid gcc installation, gnu assembler can be invoked as well. This patch currently adds and modifies arm related test only. The riscv specific test will be added in the last PR when driver code related to calling of RISCVToolchain object will be removed. Currently in this PR, there is no way to test riscv target. RFC: https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524 Change-Id: Ibaeb569cf7e2cee03c022aa9ecd1abe29d5c30d4
1 parent 6263de9 commit 63eaf99

File tree

32 files changed

+386
-62
lines changed

32 files changed

+386
-62
lines changed

clang/docs/Toolchain.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,8 @@ workarounds for issues discovered in libstdc++, and these are removed
347347
as fixed libstdc++ becomes sufficiently old.
348348

349349
You can instruct Clang to use libstdc++ with the ``-stdlib=libstdc++`` flag.
350+
351+
GCC Installation
352+
=================
353+
Users can point to their GCC installation by using the ``-gcc-toolchain`` or by
354+
using ``-gcc-install-dir`` flag.

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,9 @@ def note_drv_available_multilibs : Note<
830830
"available multilibs are:%0">;
831831
def err_drv_multilib_custom_error : Error<
832832
"multilib configuration error: %0">;
833+
def warn_drv_multilib_not_available_for_target: Warning<
834+
"no multilib structure encoded for Arm, Aarch64 and PPC targets">,
835+
InGroup<DiagGroup<"multilib-not-found">>;
833836

834837
def err_drv_experimental_crel : Error<
835838
"-Wa,--allow-experimental-crel must be specified to use -Wa,--crel. "

clang/lib/Driver/ToolChains/BareMetal.cpp

Lines changed: 147 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,40 @@ using namespace clang::driver;
3333
using namespace clang::driver::tools;
3434
using namespace clang::driver::toolchains;
3535

36+
/// Is the triple {aarch64.aarch64_be}-none-elf?
37+
static bool isAArch64BareMetal(const llvm::Triple &Triple) {
38+
if (Triple.getArch() != llvm::Triple::aarch64 &&
39+
Triple.getArch() != llvm::Triple::aarch64_be)
40+
return false;
41+
42+
if (Triple.getVendor() != llvm::Triple::UnknownVendor)
43+
return false;
44+
45+
if (Triple.getOS() != llvm::Triple::UnknownOS)
46+
return false;
47+
48+
return Triple.getEnvironmentName() == "elf";
49+
}
50+
51+
static bool isRISCVBareMetal(const llvm::Triple &Triple) {
52+
if (!Triple.isRISCV())
53+
return false;
54+
55+
if (Triple.getVendor() != llvm::Triple::UnknownVendor)
56+
return false;
57+
58+
if (Triple.getOS() != llvm::Triple::UnknownOS)
59+
return false;
60+
61+
return Triple.getEnvironmentName() == "elf";
62+
}
63+
64+
/// Is the triple powerpc[64][le]-*-none-eabi?
65+
static bool isPPCBareMetal(const llvm::Triple &Triple) {
66+
return Triple.isPPC() && Triple.getOS() == llvm::Triple::UnknownOS &&
67+
Triple.getEnvironment() == llvm::Triple::EABI;
68+
}
69+
3670
static bool findRISCVMultilibs(const Driver &D,
3771
const llvm::Triple &TargetTriple,
3872
const ArgList &Args, DetectedMultilibs &Result) {
@@ -110,56 +144,99 @@ static std::string computeBaseSysRoot(const Driver &D, bool IncludeTriple) {
110144
return std::string(SysRootDir);
111145
}
112146

113-
BareMetal::BareMetal(const Driver &D, const llvm::Triple &Triple,
114-
const ArgList &Args)
115-
: ToolChain(D, Triple, Args),
116-
SysRoot(computeBaseSysRoot(D, /*IncludeTriple=*/true)) {
117-
getProgramPaths().push_back(getDriver().Dir);
118-
119-
findMultilibs(D, Triple, Args);
120-
SmallString<128> SysRoot(computeSysRoot());
121-
if (!SysRoot.empty()) {
122-
for (const Multilib &M : getOrderedMultilibs()) {
123-
SmallString<128> Dir(SysRoot);
124-
llvm::sys::path::append(Dir, M.osSuffix(), "lib");
125-
getFilePaths().push_back(std::string(Dir));
126-
getLibraryPaths().push_back(std::string(Dir));
127-
}
128-
}
147+
static bool hasGCCToolChainAlongSideClang(const Driver &D) {
148+
SmallString<128> GCCDir;
149+
llvm::sys::path::append(GCCDir, D.Dir, "..", D.getTargetTriple(),
150+
"lib/crt0.o");
151+
return llvm::sys::fs::exists(GCCDir);
129152
}
130153

131-
/// Is the triple {aarch64.aarch64_be}-none-elf?
132-
static bool isAArch64BareMetal(const llvm::Triple &Triple) {
133-
if (Triple.getArch() != llvm::Triple::aarch64 &&
134-
Triple.getArch() != llvm::Triple::aarch64_be)
135-
return false;
154+
// Users can specify their GCC toolchain using `-gcc-install-dir` or
155+
// `--gcc-toolchain`. If no sysroot is explicitly provided, the driver will
156+
// attempt to infer it from the values of the above flags.
157+
//
158+
// If neither flag is used, the sysroot defaults to either:
159+
//    - `bin/../<target-triple>`
160+
//    - `bin/../lib/clang-runtimes/<target-triple>`
161+
//
162+
// To use the `clang-runtimes` path, ensure that `../<target-triple>/lib/crt0.o`
163+
// does not exist relative to the driver.
164+
std::string BareMetal::computeSysRoot() const {
165+
if (!SysRoot.empty())
166+
return SysRoot;
136167

137-
if (Triple.getVendor() != llvm::Triple::UnknownVendor)
138-
return false;
168+
const Driver &D = getDriver();
169+
if (!D.SysRoot.empty())
170+
return D.SysRoot;
139171

140-
if (Triple.getOS() != llvm::Triple::UnknownOS)
141-
return false;
172+
// Verify the GCC installation from -gcc-install-dir, --gcc-toolchain, or
173+
// alongside clang. If valid, form the sysroot. Otherwise, check
174+
// lib/clang-runtimes above the driver.
175+
SmallString<128> SysRootDir;
176+
if (GCCInstallation.isValid()) {
177+
StringRef LibDir = GCCInstallation.getParentLibPath();
178+
StringRef TripleStr = GCCInstallation.getTriple().str();
179+
llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
180+
} else if (hasGCCToolChainAlongSideClang(D)) {
181+
// Use the triple as provided to the driver. Unlike the parsed triple
182+
// this has not been normalized to always contain every field.
183+
llvm::sys::path::append(SysRootDir, D.Dir, "..", D.getTargetTriple());
184+
}
142185

143-
return Triple.getEnvironmentName() == "elf";
186+
if (llvm::sys::fs::exists(SysRootDir))
187+
return std::string(SysRootDir);
188+
return computeBaseSysRoot(D, /*IncludeTriple*/ true);
144189
}
145190

146-
static bool isRISCVBareMetal(const llvm::Triple &Triple) {
147-
if (!Triple.isRISCV())
148-
return false;
149-
150-
if (Triple.getVendor() != llvm::Triple::UnknownVendor)
151-
return false;
152-
153-
if (Triple.getOS() != llvm::Triple::UnknownOS)
154-
return false;
155-
156-
return Triple.getEnvironmentName() == "elf";
191+
static void addMultilibsFilePaths(const Driver &D, const MultilibSet &Multilibs,
192+
const Multilib &Multilib,
193+
StringRef InstallPath,
194+
ToolChain::path_list &Paths) {
195+
if (const auto &PathsCallback = Multilibs.filePathsCallback())
196+
for (const auto &Path : PathsCallback(Multilib))
197+
addPathIfExists(D, InstallPath + Path, Paths);
157198
}
158199

159-
/// Is the triple powerpc[64][le]-*-none-eabi?
160-
static bool isPPCBareMetal(const llvm::Triple &Triple) {
161-
return Triple.isPPC() && Triple.getOS() == llvm::Triple::UnknownOS &&
162-
Triple.getEnvironment() == llvm::Triple::EABI;
200+
// GCC mutltilibs will only work for those targets that have their multlib
201+
// structure encoded into GCCInstallation. Baremetal toolchain support ARM,
202+
// AArch64, RISCV and PPC and of them only RISCV have GCC multilibs hardcoded
203+
// in GCCInstallation.
204+
BareMetal::BareMetal(const Driver &D, const llvm::Triple &Triple,
205+
const ArgList &Args)
206+
: Generic_ELF(D, Triple, Args) {
207+
GCCInstallation.init(Triple, Args);
208+
SysRoot = computeSysRoot();
209+
if (GCCInstallation.isValid()) {
210+
if (!isRISCVBareMetal(Triple))
211+
D.Diag(clang::diag::warn_drv_multilib_not_available_for_target);
212+
Multilibs = GCCInstallation.getMultilibs();
213+
SelectedMultilibs.assign({GCCInstallation.getMultilib()});
214+
path_list &Paths = getFilePaths();
215+
// Add toolchain/multilib specific file paths.
216+
addMultilibsFilePaths(D, Multilibs, SelectedMultilibs.back(),
217+
GCCInstallation.getInstallPath(), Paths);
218+
getFilePaths().push_back(GCCInstallation.getInstallPath().str());
219+
ToolChain::path_list &PPaths = getProgramPaths();
220+
// Multilib cross-compiler GCC installations put ld in a triple-prefixed
221+
// directory off of the parent of the GCC installation.
222+
PPaths.push_back(Twine(GCCInstallation.getParentLibPath() + "/../" +
223+
GCCInstallation.getTriple().str() + "/bin")
224+
.str());
225+
PPaths.push_back((GCCInstallation.getParentLibPath() + "/../bin").str());
226+
getFilePaths().push_back(SysRoot + "/lib");
227+
} else {
228+
getProgramPaths().push_back(getDriver().Dir);
229+
findMultilibs(D, Triple, Args);
230+
const SmallString<128> SysRootDir(computeSysRoot());
231+
if (!SysRootDir.empty()) {
232+
for (const Multilib &M : getOrderedMultilibs()) {
233+
SmallString<128> Dir(SysRootDir);
234+
llvm::sys::path::append(Dir, M.osSuffix(), "lib");
235+
getFilePaths().push_back(std::string(Dir));
236+
getLibraryPaths().push_back(std::string(Dir));
237+
}
238+
}
239+
}
163240
}
164241

165242
static void
@@ -244,7 +321,7 @@ void BareMetal::findMultilibs(const Driver &D, const llvm::Triple &Triple,
244321
Multilibs = Result.Multilibs;
245322
MultilibMacroDefines.append(CustomFlagMacroDefines.begin(),
246323
CustomFlagMacroDefines.end());
247-
} else if (isRISCVBareMetal(Triple)) {
324+
} else if (isRISCVBareMetal(Triple) && !hasGCCToolChainAlongSideClang(D)) {
248325
if (findRISCVMultilibs(D, Triple, Args, Result)) {
249326
SelectedMultilibs = Result.SelectedMultilibs;
250327
Multilibs = Result.Multilibs;
@@ -265,8 +342,6 @@ Tool *BareMetal::buildStaticLibTool() const {
265342
return new tools::baremetal::StaticLibTool(*this);
266343
}
267344

268-
std::string BareMetal::computeSysRoot() const { return SysRoot; }
269-
270345
BareMetal::OrderedMultilibs BareMetal::getOrderedMultilibs() const {
271346
// Get multilibs in reverse order because they're ordered most-specific last.
272347
if (!SelectedMultilibs.empty())
@@ -294,10 +369,10 @@ void BareMetal::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
294369
if (std::optional<std::string> Path = getStdlibIncludePath())
295370
addSystemInclude(DriverArgs, CC1Args, *Path);
296371

297-
const SmallString<128> SysRoot(computeSysRoot());
298-
if (!SysRoot.empty()) {
372+
const SmallString<128> SysRootDir(computeSysRoot());
373+
if (!SysRootDir.empty()) {
299374
for (const Multilib &M : getOrderedMultilibs()) {
300-
SmallString<128> Dir(SysRoot);
375+
SmallString<128> Dir(SysRootDir);
301376
llvm::sys::path::append(Dir, M.includeSuffix());
302377
llvm::sys::path::append(Dir, "include");
303378
addSystemInclude(DriverArgs, CC1Args, Dir.str());
@@ -311,6 +386,19 @@ void BareMetal::addClangTargetOptions(const ArgList &DriverArgs,
311386
CC1Args.push_back("-nostdsysteminc");
312387
}
313388

389+
void BareMetal::addLibStdCxxIncludePaths(
390+
const llvm::opt::ArgList &DriverArgs,
391+
llvm::opt::ArgStringList &CC1Args) const {
392+
if (!GCCInstallation.isValid())
393+
return;
394+
const GCCVersion &Version = GCCInstallation.getVersion();
395+
StringRef TripleStr = GCCInstallation.getTriple().str();
396+
const Multilib &Multilib = GCCInstallation.getMultilib();
397+
addLibStdCXXIncludePaths(computeSysRoot() + "/include/c++/" + Version.Text,
398+
TripleStr, Multilib.includeSuffix(), DriverArgs,
399+
CC1Args);
400+
}
401+
314402
void BareMetal::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
315403
ArgStringList &CC1Args) const {
316404
if (DriverArgs.hasArg(options::OPT_nostdinc, options::OPT_nostdlibinc,
@@ -341,23 +429,23 @@ void BareMetal::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
341429
};
342430

343431
switch (GetCXXStdlibType(DriverArgs)) {
344-
case ToolChain::CST_Libcxx: {
345-
SmallString<128> P(D.Dir);
346-
llvm::sys::path::append(P, "..", "include");
347-
AddCXXIncludePath(P);
348-
break;
349-
}
350-
case ToolChain::CST_Libstdcxx:
351-
// We only support libc++ toolchain installation.
352-
break;
432+
case ToolChain::CST_Libcxx: {
433+
SmallString<128> P(D.Dir);
434+
llvm::sys::path::append(P, "..", "include");
435+
AddCXXIncludePath(P);
436+
break;
437+
}
438+
case ToolChain::CST_Libstdcxx:
439+
addLibStdCxxIncludePaths(DriverArgs, CC1Args);
440+
break;
353441
}
354442

355-
std::string SysRoot(computeSysRoot());
356-
if (SysRoot.empty())
443+
std::string SysRootDir(computeSysRoot());
444+
if (SysRootDir.empty())
357445
return;
358446

359447
for (const Multilib &M : getOrderedMultilibs()) {
360-
SmallString<128> Dir(SysRoot);
448+
SmallString<128> Dir(SysRootDir);
361449
llvm::sys::path::append(Dir, M.gccSuffix());
362450
switch (GetCXXStdlibType(DriverArgs)) {
363451
case ToolChain::CST_Libcxx: {

clang/lib/Driver/ToolChains/BareMetal.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_BAREMETAL_H
1010
#define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_BAREMETAL_H
1111

12+
#include "ToolChains/Gnu.h"
1213
#include "clang/Driver/Tool.h"
1314
#include "clang/Driver/ToolChain.h"
1415

@@ -19,7 +20,7 @@ namespace driver {
1920

2021
namespace toolchains {
2122

22-
class LLVM_LIBRARY_VISIBILITY BareMetal : public ToolChain {
23+
class LLVM_LIBRARY_VISIBILITY BareMetal : public Generic_ELF {
2324
public:
2425
BareMetal(const Driver &D, const llvm::Triple &Triple,
2526
const llvm::opt::ArgList &Args);
@@ -35,7 +36,6 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public ToolChain {
3536
Tool *buildStaticLibTool() const override;
3637

3738
public:
38-
bool useIntegratedAs() const override { return true; }
3939
bool isBareMetal() const override { return true; }
4040
bool isCrossCompiling() const override { return true; }
4141
bool HasNativeLLVMSupport() const override { return true; }
@@ -48,6 +48,11 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public ToolChain {
4848

4949
StringRef getOSLibName() const override { return "baremetal"; }
5050

51+
UnwindTableLevel
52+
getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override {
53+
return UnwindTableLevel::None;
54+
}
55+
5156
RuntimeLibType GetDefaultRuntimeLibType() const override {
5257
return ToolChain::RLT_CompilerRT;
5358
}
@@ -67,6 +72,9 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public ToolChain {
6772
void AddClangCXXStdlibIncludeArgs(
6873
const llvm::opt::ArgList &DriverArgs,
6974
llvm::opt::ArgStringList &CC1Args) const override;
75+
void
76+
addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
77+
llvm::opt::ArgStringList &CC1Args) const override;
7078
std::string computeSysRoot() const override;
7179
SanitizerMask getSupportedSanitizers() const override;
7280

@@ -104,7 +112,7 @@ class LLVM_LIBRARY_VISIBILITY StaticLibTool : public Tool {
104112

105113
class LLVM_LIBRARY_VISIBILITY Linker final : public Tool {
106114
public:
107-
Linker(const ToolChain &TC) : Tool("baremetal::Linker", "ld.lld", TC) {}
115+
Linker(const ToolChain &TC) : Tool("baremetal::Linker", "linker", TC) {}
108116
bool isLinkJob() const override { return true; }
109117
bool hasIntegratedCPP() const override { return false; }
110118
void ConstructJob(Compilation &C, const JobAction &JA,

clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include/c++/8.2.1/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#!/bin/true

clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o

Whitespace-only changes.

clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtend.o

Whitespace-only changes.

clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crt0.o

Whitespace-only changes.

clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtbegin.o

Whitespace-only changes.

clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtend.o

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#!/bin/true

clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/include/c++/8.2.1/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/crt0.o

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#!/bin/true

clang/test/Driver/Inputs/basic_arm_gcc_tree/lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o

Whitespace-only changes.

clang/test/Driver/Inputs/basic_arm_gcc_tree/lib/gcc/armv6m-none-eabi/8.2.1/crtend.o

Whitespace-only changes.

clang/test/Driver/Inputs/basic_arm_nogcc_tree/armv6m-none-eabi/lib/crt0.o

Whitespace-only changes.

clang/test/Driver/Inputs/basic_arm_nogcc_tree/armv6m-none-eabi/lib/crtbegin.o

Whitespace-only changes.

clang/test/Driver/Inputs/basic_arm_nogcc_tree/armv6m-none-eabi/lib/crtend.o

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#!/bin/true

clang/test/Driver/aarch64-gnutools.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %clang --target=aarch64-none-elf --gcc-toolchain=%S/Inputs/basic_aarch64_gcc_tree -fno-integrated-as %s -### -c \
2+
// RUN: 2>&1 | FileCheck %s
3+
4+
// CHECK: "{{.*}}as{{(.exe)?}}"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// A basic clang -cc1 command-line, and simple environment check.
2+
3+
// The tests here are similar to those in aarch64-toolchain.c, however
4+
// these tests need to create symlinks to test directory trees in order to
5+
// set up the environment and therefore shell support is required.
6+
// REQUIRES: shell
7+
// UNSUPPORTED: system-windows
8+
9+
// If there is no GCC install detected then the driver searches for executables
10+
// and runtime starting from the directory tree above the driver itself.
11+
// The test below checks that the driver correctly finds the linker and
12+
// runtime if and only if they exist.
13+
//
14+
// RUN: rm -rf %t
15+
// RUN: mkdir -p %t/aarch64-nogcc/bin
16+
// RUN: ln -s %clang %t/aarch64-nogcc/bin/clang
17+
// RUN: ln -s %S/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf %t/aarch64-nogcc/aarch64-none-elf
18+
// RUN: %t/aarch64-nogcc/bin/clang %s -### -no-canonical-prefixes \
19+
// RUN: --gcc-toolchain=%t/aarch64-nogcc/invalid \
20+
// RUN: --target=aarch64-none-elf --rtlib=libgcc -fuse-ld=ld 2>&1 \
21+
// RUN: | FileCheck -check-prefix=C-ARM-BAREMETAL-NOGCC %s
22+
23+
// RUN: %t/aarch64-nogcc/bin/clang %s -### -no-canonical-prefixes \
24+
// RUN: --sysroot=%t/aarch64-nogcc/bin/../aarch64-none-elf \
25+
// RUN: --target=aarch64-none-elf --rtlib=libgcc -fuse-ld=ld 2>&1 \
26+
// RUN: | FileCheck -check-prefix=C-ARM-BAREMETAL-NOGCC %s
27+
28+
// C-ARM-BAREMETAL-NOGCC: "-internal-isystem" "{{.*}}/aarch64-nogcc/bin/../aarch64-none-elf/include"

0 commit comments

Comments
 (0)