Skip to content

Commit de61aa3

Browse files
committed
[RISCV] Improve sysroot computation if no GCC install detected
If a GCC installed is not detected, the driver would default to the root of the filesystem. This is not ideal when this doesn't match the install directory of the toolchain and can cause undesireable behavior such as picking up system libraries or the system linker when cross-compiling. Differential Revision: https://reviews.llvm.org/D68391
1 parent 2fdd58c commit de61aa3

File tree

11 files changed

+82
-7
lines changed

11 files changed

+82
-7
lines changed

clang/lib/Driver/ToolChains/RISCVToolchain.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ RISCVToolChain::RISCVToolChain(const Driver &D, const llvm::Triple &Triple,
3333
getFilePaths().push_back(GCCInstallation.getInstallPath().str());
3434
getProgramPaths().push_back(
3535
(GCCInstallation.getParentLibPath() + "/../bin").str());
36+
} else {
37+
getProgramPaths().push_back(D.Dir);
3638
}
3739
}
3840

@@ -74,17 +76,22 @@ std::string RISCVToolChain::computeSysRoot() const {
7476
if (!getDriver().SysRoot.empty())
7577
return getDriver().SysRoot;
7678

77-
if (!GCCInstallation.isValid())
78-
return std::string();
79-
80-
StringRef LibDir = GCCInstallation.getParentLibPath();
81-
StringRef TripleStr = GCCInstallation.getTriple().str();
82-
std::string SysRootDir = LibDir.str() + "/../" + TripleStr.str();
79+
SmallString<128> SysRootDir;
80+
if (GCCInstallation.isValid()) {
81+
StringRef LibDir = GCCInstallation.getParentLibPath();
82+
StringRef TripleStr = GCCInstallation.getTriple().str();
83+
llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
84+
} else {
85+
// Use the triple as provided to the driver. Unlike the parsed triple
86+
// this has not been normalized to always contain every field.
87+
llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
88+
getDriver().getTargetTriple());
89+
}
8390

8491
if (!llvm::sys::fs::exists(SysRootDir))
8592
return std::string();
8693

87-
return SysRootDir;
94+
return SysRootDir.str();
8895
}
8996

9097
void RISCV::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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_riscv32_nogcc_tree/riscv32-unknown-elf/lib/crt0.o

Whitespace-only changes.

clang/test/Driver/Inputs/basic_riscv32_nogcc_tree/riscv32-unknown-elf/lib/crtbegin.o

Whitespace-only changes.

clang/test/Driver/Inputs/basic_riscv32_nogcc_tree/riscv32-unknown-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_riscv64_nogcc_tree/riscv64-unknown-elf/lib/crt0.o

Whitespace-only changes.

clang/test/Driver/Inputs/basic_riscv64_nogcc_tree/riscv64-unknown-elf/lib/crtbegin.o

Whitespace-only changes.

clang/test/Driver/Inputs/basic_riscv64_nogcc_tree/riscv64-unknown-elf/lib/crtend.o

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// A basic clang -cc1 command-line, and simple environment check.
2+
3+
// The tests here are similar to those in riscv32-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: mkdir -p %T/testroot-riscv32-baremetal-nogcc/bin
15+
// RUN: [ ! -s %T/testroot-riscv32-baremetal-nogcc/bin/clang ] || rm %T/testroot-riscv32-baremetal-nogcc/bin/clang
16+
// RUN: [ ! -s %T/testroot-riscv32-baremetal-nogcc/bin/riscv32-unknown-elf-ld ] || rm %T/testroot-riscv32-baremetal-nogcc/bin/riscv32-unknown-elf-ld
17+
// RUN: [ ! -s %T/testroot-riscv32-baremetal-nogcc/riscv32-unknown-elf ] || rm %T/testroot-riscv32-baremetal-nogcc/riscv32-unknown-elf
18+
// RUN: ln -s %clang %T/testroot-riscv32-baremetal-nogcc/bin/clang
19+
// RUN: ln -s %S/Inputs/basic_riscv32_nogcc_tree/bin/riscv32-unknown-elf-ld %T/testroot-riscv32-baremetal-nogcc/bin/riscv32-unknown-elf-ld
20+
// RUN: ln -s %S/Inputs/basic_riscv32_nogcc_tree/riscv32-unknown-elf %T/testroot-riscv32-baremetal-nogcc/riscv32-unknown-elf
21+
// RUN: %T/testroot-riscv32-baremetal-nogcc/bin/clang %s -### -no-canonical-prefixes \
22+
// RUN: -target riscv32-unknown-elf 2>&1 \
23+
// RUN: | FileCheck -check-prefix=C-RV32-BAREMETAL-ILP32-NOGCC %s
24+
25+
// C-RV32-BAREMETAL-ILP32-NOGCC: InstalledDir: [[DRIVERDIR:.*]]
26+
// C-RV32-BAREMETAL-ILP32-NOGCC: "-fuse-init-array"
27+
// C-RV32-BAREMETAL-ILP32-NOGCC: "-internal-isystem" "[[DRIVERDIR]]/../riscv32-unknown-elf/include"
28+
// C-RV32-BAREMETAL-ILP32-NOGCC: "[[DRIVERDIR]]/riscv32-unknown-elf-ld"
29+
// C-RV32-BAREMETAL-ILP32-NOGCC: "[[DRIVERDIR]]/../riscv32-unknown-elf/lib/crt0.o"
30+
// C-RV32-BAREMETAL-ILP32-NOGCC: "[[DRIVERDIR]]/../riscv32-unknown-elf/lib/crtbegin.o"
31+
// C-RV32-BAREMETAL-ILP32-NOGCC: "-L[[DRIVERDIR]]/../riscv32-unknown-elf/lib"
32+
// C-RV32-BAREMETAL-ILP32-NOGCC: "--start-group" "-lc" "-lgloss" "--end-group" "-lgcc"
33+
// C-RV32-BAREMETAL-ILP32-NOGCC: "[[DRIVERDIR]]/../riscv32-unknown-elf/lib/crtend.o"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// A basic clang -cc1 command-line, and simple environment check.
2+
3+
// The tests here are similar to those in riscv64-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: mkdir -p %T/testroot-riscv64-baremetal-nogcc/bin
15+
// RUN: [ ! -s %T/testroot-riscv64-baremetal-nogcc/bin/clang ] || rm %T/testroot-riscv64-baremetal-nogcc/bin/clang
16+
// RUN: [ ! -s %T/testroot-riscv64-baremetal-nogcc/bin/riscv64-unknown-elf-ld ] || rm %T/testroot-riscv64-baremetal-nogcc/bin/riscv64-unknown-elf-ld
17+
// RUN: [ ! -s %T/testroot-riscv64-baremetal-nogcc/riscv64-unknown-elf ] || rm %T/testroot-riscv64-baremetal-nogcc/riscv64-unknown-elf
18+
// RUN: ln -s %clang %T/testroot-riscv64-baremetal-nogcc/bin/clang
19+
// RUN: ln -s %S/Inputs/basic_riscv64_nogcc_tree/bin/riscv64-unknown-elf-ld %T/testroot-riscv64-baremetal-nogcc/bin/riscv64-unknown-elf-ld
20+
// RUN: ln -s %S/Inputs/basic_riscv64_nogcc_tree/riscv64-unknown-elf %T/testroot-riscv64-baremetal-nogcc/riscv64-unknown-elf
21+
// RUN: %T/testroot-riscv64-baremetal-nogcc/bin/clang %s -### -no-canonical-prefixes \
22+
// RUN: -target riscv64-unknown-elf 2>&1 \
23+
// RUN: | FileCheck -check-prefix=C-RV64-BAREMETAL-LP64-NOGCC %s
24+
25+
// C-RV64-BAREMETAL-LP64-NOGCC: InstalledDir: [[DRIVERDIR:.*]]
26+
// C-RV64-BAREMETAL-LP64-NOGCC: "-fuse-init-array"
27+
// C-RV64-BAREMETAL-LP64-NOGCC: "-internal-isystem" "[[DRIVERDIR]]/../riscv64-unknown-elf/include"
28+
// C-RV64-BAREMETAL-LP64-NOGCC: "[[DRIVERDIR]]/riscv64-unknown-elf-ld"
29+
// C-RV64-BAREMETAL-LP64-NOGCC: "[[DRIVERDIR]]/../riscv64-unknown-elf/lib/crt0.o"
30+
// C-RV64-BAREMETAL-LP64-NOGCC: "[[DRIVERDIR]]/../riscv64-unknown-elf/lib/crtbegin.o"
31+
// C-RV64-BAREMETAL-LP64-NOGCC: "-L[[DRIVERDIR]]/../riscv64-unknown-elf/lib"
32+
// C-RV64-BAREMETAL-LP64-NOGCC: "--start-group" "-lc" "-lgloss" "--end-group" "-lgcc"
33+
// C-RV64-BAREMETAL-LP64-NOGCC: "[[DRIVERDIR]]/../riscv64-unknown-elf/lib/crtend.o"

0 commit comments

Comments
 (0)