Skip to content

[Clang][Driver] Add option to provide path for multilib's YAML config file #109640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -5558,6 +5558,9 @@ def mthumb : Flag<["-"], "mthumb">, Group<m_Group>;
def mtune_EQ : Joined<["-"], "mtune=">, Group<m_Group>,
Visibility<[ClangOption, FlangOption]>,
HelpText<"Only supported on AArch64, PowerPC, RISC-V, SPARC, SystemZ, and X86">;
def multi_lib_config : Joined<["-", "--"], "multi-lib-config=">,
HelpText<"Path to the YAML configuration file to be used for multilib selection">,
MetaVarName<"<file>">;
def multi__module : Flag<["-"], "multi_module">;
def multiply__defined__unused : Separate<["-"], "multiply_defined_unused">;
def multiply__defined : Separate<["-"], "multiply_defined">;
Expand Down
68 changes: 40 additions & 28 deletions clang/lib/Driver/ToolChains/BareMetal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,23 @@ static bool findRISCVMultilibs(const Driver &D,
return false;
}

static std::string computeBaseSysRoot(const Driver &D, bool IncludeTriple) {
if (!D.SysRoot.empty())
return D.SysRoot;

SmallString<128> SysRootDir(D.Dir);
llvm::sys::path::append(SysRootDir, "..", "lib", "clang-runtimes");

if (IncludeTriple)
llvm::sys::path::append(SysRootDir, D.getTargetTriple());

return std::string(SysRootDir);
}

BareMetal::BareMetal(const Driver &D, const llvm::Triple &Triple,
const ArgList &Args)
: ToolChain(D, Triple, Args) {
: ToolChain(D, Triple, Args),
SysRoot(computeBaseSysRoot(D, /*IncludeTriple=*/true)) {
getProgramPaths().push_back(getDriver().Dir);

findMultilibs(D, Triple, Args);
Expand Down Expand Up @@ -194,37 +208,37 @@ static void findMultilibsFromYAML(const ToolChain &TC, const Driver &D,

static constexpr llvm::StringLiteral MultilibFilename = "multilib.yaml";

// Get the sysroot, before multilib takes effect.
static std::string computeBaseSysRoot(const Driver &D,
const llvm::Triple &Triple) {
if (!D.SysRoot.empty())
return D.SysRoot;

SmallString<128> SysRootDir(D.Dir);
llvm::sys::path::append(SysRootDir, "..", "lib", "clang-runtimes");

SmallString<128> MultilibPath(SysRootDir);
llvm::sys::path::append(MultilibPath, MultilibFilename);

// New behaviour: if multilib.yaml is found then use clang-runtimes as the
// sysroot.
if (D.getVFS().exists(MultilibPath))
return std::string(SysRootDir);

// Otherwise fall back to the old behaviour of appending the target triple.
llvm::sys::path::append(SysRootDir, D.getTargetTriple());
return std::string(SysRootDir);
static std::optional<llvm::SmallString<128>>
getMultilibConfigPath(const Driver &D, const llvm::Triple &Triple,
const ArgList &Args) {
llvm::SmallString<128> MultilibPath;
if (Arg *ConfigFileArg = Args.getLastArg(options::OPT_multi_lib_config)) {
MultilibPath = ConfigFileArg->getValue();
if (!D.getVFS().exists(MultilibPath)) {
D.Diag(clang::diag::err_drv_no_such_file) << MultilibPath.str();
return {};
}
} else {
MultilibPath = computeBaseSysRoot(D, /*IncludeTriple=*/false);
llvm::sys::path::append(MultilibPath, MultilibFilename);
}
return MultilibPath;
}

void BareMetal::findMultilibs(const Driver &D, const llvm::Triple &Triple,
const ArgList &Args) {
DetectedMultilibs Result;
// Look for a multilib.yaml before trying target-specific hardwired logic.
// If it exists, always do what it specifies.
llvm::SmallString<128> MultilibPath(computeBaseSysRoot(D, Triple));
llvm::sys::path::append(MultilibPath, MultilibFilename);
if (D.getVFS().exists(MultilibPath)) {
findMultilibsFromYAML(*this, D, MultilibPath, Args, Result);
std::optional<llvm::SmallString<128>> MultilibPath =
getMultilibConfigPath(D, Triple, Args);
if (!MultilibPath)
return;
if (D.getVFS().exists(*MultilibPath)) {
// If multilib.yaml is found, update sysroot so it doesn't use a target
// specific suffix
SysRoot = computeBaseSysRoot(D, /*IncludeTriple=*/false);
findMultilibsFromYAML(*this, D, *MultilibPath, Args, Result);
SelectedMultilibs = Result.SelectedMultilibs;
Multilibs = Result.Multilibs;
} else if (isRISCVBareMetal(Triple)) {
Expand All @@ -248,9 +262,7 @@ Tool *BareMetal::buildStaticLibTool() const {
return new tools::baremetal::StaticLibTool(*this);
}

std::string BareMetal::computeSysRoot() const {
return computeBaseSysRoot(getDriver(), getTriple());
}
std::string BareMetal::computeSysRoot() const { return SysRoot; }

BareMetal::OrderedMultilibs BareMetal::getOrderedMultilibs() const {
// Get multilibs in reverse order because they're ordered most-specific last.
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Driver/ToolChains/BareMetal.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public ToolChain {
using OrderedMultilibs =
llvm::iterator_range<llvm::SmallVector<Multilib>::const_reverse_iterator>;
OrderedMultilibs getOrderedMultilibs() const;

std::string SysRoot;
};

} // namespace toolchains
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Driver/Inputs/multilib/empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

MultilibVersion: 1.0

Variants:

Mappings:

...
17 changes: 5 additions & 12 deletions clang/test/Driver/baremetal-multilib-custom-error.yaml
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
# REQUIRES: shell
# UNSUPPORTED: system-windows

# RUN: rm -rf %t
# RUN: mkdir -p %t/bin
# RUN: mkdir -p %t/lib/clang-runtimes/arm-none-eabi/thumb/v8-m.main/fp/lib
# RUN: touch %t/lib/clang-runtimes/arm-none-eabi/thumb/v8-m.main/fp/lib/libclang_rt.builtins.a
# RUN: ln -s %clang %t/bin/clang
# RUN: ln -s %s %t/lib/clang-runtimes/multilib.yaml

# RUN: %t/bin/clang -no-canonical-prefixes -print-multi-directory 2>&1 \
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -print-multi-directory 2>&1 \
# RUN: --target=thumbv8m.main-none-eabi -march=armv8.1m.main --sysroot= \
# RUN: | FileCheck --check-prefix=CHECK-PRINT-MULTI-DIRECTORY-NOMVE-SOFTFLOAT %s
# CHECK-PRINT-MULTI-DIRECTORY-NOMVE-SOFTFLOAT: nomve-softfloat

# RUN: not %t/bin/clang -no-canonical-prefixes -print-multi-directory 2>&1 \
# RUN: not %clang --multi-lib-config=%s -no-canonical-prefixes -print-multi-directory 2>&1 \
# RUN: --target=thumbv8m.main-none-eabi -march=armv8.1m.main+mve --sysroot= \
# RUN: | FileCheck --check-prefix=CHECK-ERROR %s
# CHECK-ERROR: multilib configuration error: mve-softfloat is not supported

# RUN: %t/bin/clang -no-canonical-prefixes -print-multi-directory 2>&1 \
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -print-multi-directory 2>&1 \
# RUN: --target=thumbv8m.main-none-eabi -march=armv8.1m.main+mve -mfloat-abi=hard --sysroot= \
# RUN: | FileCheck --check-prefix=CHECK-PRINT-MULTI-DIRECTORY-MVE-HARDFLOAT %s
# CHECK-PRINT-MULTI-DIRECTORY-MVE-HARDFLOAT: mve-hardfloat

# RUN: %t/bin/clang -no-canonical-prefixes -print-multi-lib 2>&1 \
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -print-multi-lib 2>&1 \
# RUN: --target=arm-none-eabi --sysroot= \
# RUN: | FileCheck --check-prefix=CHECK-PRINT-MULTI-LIB %s
# CHECK-PRINT-MULTI-LIB: nomve-softfloat;@-target=thumbv8.1m.main-unknown-none-eabi
# CHECK-PRINT-MULTI-LIB-NEXT: mve-hardfloat;@-target=thumbv8.1m.main-unknown-none-eabihf@march=thumbv8.1m.main+mve@mfloat-abi=hard

# RUN: %t/bin/clang -no-canonical-prefixes -print-multi-directory 2>&1 \
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -print-multi-directory 2>&1 \
# RUN: --target=arm-none-eabi --sysroot= \
# RUN: | FileCheck --check-prefix=CHECK-NO-MATCH-WARNING %s
# CHECK-NO-MATCH-WARNING: clang: note: available multilibs are:
Expand Down
20 changes: 6 additions & 14 deletions clang/test/Driver/baremetal-multilib-exclusive-group.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
# UNSUPPORTED: system-windows

# RUN: rm -rf %t
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -x c++ %s -### -o %t.out --target=thumbv7em-none-unknown-eabi --sysroot= 2>%t.err

# RUN: mkdir -p %t/baremetal_multilib/bin
# RUN: ln -s %clang %t/baremetal_multilib/bin/clang

# RUN: mkdir -p %t/baremetal_multilib/lib/clang-runtimes
# RUN: ln -s %s %t/baremetal_multilib/lib/clang-runtimes/multilib.yaml

# RUN: %t/baremetal_multilib/bin/clang -no-canonical-prefixes -x c++ %s -### -o %t.out --target=thumbv7em-none-unknown-eabi --sysroot= 2>%t.err

# RUN: FileCheck -DSYSROOT=%t/baremetal_multilib %s < %t.err --check-prefix=POS
# RUN: FileCheck -DSYSROOT=%t/baremetal_multilib %s < %t.err --check-prefix=NEG
# RUN: FileCheck %s < %t.err --check-prefix=POS
# RUN: FileCheck %s < %t.err --check-prefix=NEG

# Expected results:
#
Expand All @@ -25,14 +17,14 @@
# So we expect five of these seven directories to show up in the clang-cc1
# command line, but not testdir1_exclusive or testdir2_exclusive.

# POS-DAG: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/testdir1_non_exclusive/include/c++/v1"
# POS-DAG: "-internal-isystem" "[[SYSROOT:[^"]*]]/bin/../lib/clang-runtimes/testdir1_non_exclusive/include/c++/v1"
# POS-DAG: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/testdir2_non_exclusive/include/c++/v1"
# POS-DAG: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/testdir3_exclusive/include/c++/v1"
# POS-DAG: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/testdir1_own_group/include/c++/v1"
# POS-DAG: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/testdir2_own_group/include/c++/v1"

# NEG-NOT: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/testdir1_exclusive/include/c++/v1"
# NEG-NOT: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/testdir2_exclusive/include/c++/v1"
# NEG-NOT: "-internal-isystem" "{{[^"]*}}/bin/../lib/clang-runtimes/testdir1_exclusive/include/c++/v1"
# NEG-NOT: "-internal-isystem" "{{[^"]*}}/bin/../lib/clang-runtimes/testdir2_exclusive/include/c++/v1"

---
MultilibVersion: 1.0
Expand Down
10 changes: 1 addition & 9 deletions clang/test/Driver/baremetal-multilib-group-error.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
# UNSUPPORTED: system-windows

# RUN: rm -rf %t

# RUN: mkdir -p %t/baremetal_multilib/bin
# RUN: ln -s %clang %t/baremetal_multilib/bin/clang

# RUN: mkdir -p %t/baremetal_multilib/lib/clang-runtimes
# RUN: ln -s %s %t/baremetal_multilib/lib/clang-runtimes/multilib.yaml

# RUN: %t/baremetal_multilib/bin/clang -no-canonical-prefixes -x c++ %s -### -o %t.out --target=thumbv7em-none-unknown-eabi --sysroot= 2>%t.err
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -x c++ %s -### -o %t.out --target=thumbv7em-none-unknown-eabi --sysroot= 2>%t.err
# RUN: FileCheck %s < %t.err

---
Expand Down
14 changes: 4 additions & 10 deletions clang/test/Driver/baremetal-multilib-layered.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,18 @@
# However -fno-exceptions is not yet supported for multilib selection
# so we use a more contrived -mfloat-abi example instead.

# RUN: rm -rf %T/baremetal_multilib_layered
# RUN: mkdir -p %T/baremetal_multilib_layered/bin
# RUN: mkdir -p %T/baremetal_multilib_layered/lib/clang-runtimes
# RUN: ln -s %clang %T/baremetal_multilib_layered/bin/clang
# RUN: ln -s %s %T/baremetal_multilib_layered/lib/clang-runtimes/multilib.yaml

# RUN: %T/baremetal_multilib_layered/bin/clang -no-canonical-prefixes -x c++ %s -### -o %t.out 2>&1 \
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -x c++ %s -### -o %t.out 2>&1 \
# RUN: --target=thumbv7m-none-eabi -mfloat-abi=softfp --sysroot= \
# RUN: | FileCheck -DSYSROOT=%T/baremetal_multilib_layered %s
# RUN: | FileCheck %s
# CHECK: "-cc1" "-triple" "thumbv7m-unknown-none-eabi"
# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/softfp/include/c++/v1"
# CHECK-SAME: "-internal-isystem" "[[SYSROOT:[^"]*]]/bin/../lib/clang-runtimes/softfp/include/c++/v1"
# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/soft/include/c++/v1"
# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/softfp/include"
# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/soft/include"
# CHECK-NEXT: "-L[[SYSROOT]]/bin/../lib/clang-runtimes/softfp/lib"
# CHECK-SAME: "-L[[SYSROOT]]/bin/../lib/clang-runtimes/soft/lib"

# RUN: %T/baremetal_multilib_layered/bin/clang -no-canonical-prefixes -print-multi-directory 2>&1 \
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -print-multi-directory 2>&1 \
# RUN: --target=arm-none-eabi -mfloat-abi=softfp --sysroot= \
# RUN: | FileCheck --check-prefix=CHECK-PRINT-MULTI-DIRECTORY %s
# CHECK-PRINT-MULTI-DIRECTORY: soft
Expand Down
21 changes: 7 additions & 14 deletions clang/test/Driver/baremetal-multilib.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
# REQUIRES: shell
# UNSUPPORTED: system-windows

# RUN: rm -rf %T/baremetal_multilib
# RUN: mkdir -p %T/baremetal_multilib/bin
# RUN: mkdir -p %T/baremetal_multilib/lib/clang-runtimes/arm-none-eabi/thumb/v8-m.main/fp/lib
# RUN: touch %T/baremetal_multilib/lib/clang-runtimes/arm-none-eabi/thumb/v8-m.main/fp/lib/libclang_rt.builtins.a
# RUN: ln -s %clang %T/baremetal_multilib/bin/clang
# RUN: ln -s %s %T/baremetal_multilib/lib/clang-runtimes/multilib.yaml

# RUN: %T/baremetal_multilib/bin/clang -no-canonical-prefixes -x c++ %s -### -o %t.out 2>&1 \
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -x c++ %s -### -o %t.out 2>&1 \
# RUN: --target=thumbv8m.main-none-eabihf --sysroot= \
# RUN: | FileCheck -DSYSROOT=%T/baremetal_multilib %s
# RUN: | FileCheck %s
# CHECK: "-cc1" "-triple" "thumbv8m.main-unknown-none-eabihf"
# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/arm-none-eabi/thumb/v8-m.main/fp/include/c++/v1"
# CHECK-SAME: "-internal-isystem" "[[SYSROOT:[^"]*]]/bin/../lib/clang-runtimes/arm-none-eabi/thumb/v8-m.main/fp/include/c++/v1"
# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/arm-none-eabi/thumb/v8-m.main/fp/include"
# CHECK-SAME: "-x" "c++" "{{.*}}baremetal-multilib.yaml"
# CHECK-NEXT: ld{{(.exe)?}}" "{{.*}}.o" "-Bstatic"
# CHECK-SAME: "-L[[SYSROOT]]/bin/../lib/clang-runtimes/arm-none-eabi/thumb/v8-m.main/fp/lib"
# CHECK-SAME: "-lc" "-lm" "{{[^"]*}}libclang_rt.builtins.a"
# CHECK-SAME: "-o" "{{.*}}.tmp.out"

# RUN: %T/baremetal_multilib/bin/clang -no-canonical-prefixes -x c++ %s -### -o %t.out 2>&1 \
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -x c++ %s -### -o %t.out 2>&1 \
# RUN: --target=thumbv7em-none-eabi -mfpu=fpv4-sp-d16 --sysroot= \
# RUN: | FileCheck --check-prefix=CHECK-NO-MATCH %s
# CHECK-NO-MATCH: warning: no multilib found matching flags:
Expand All @@ -30,12 +23,12 @@
# CHECK-NO-MATCH: --target=thumbv7m-unknown-none-eabi -mfpu=none
# CHECK-NO-MATCH: --target=thumbv7em-unknown-none-eabi -mfpu=none

# RUN: %T/baremetal_multilib/bin/clang -no-canonical-prefixes -print-multi-directory 2>&1 \
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -print-multi-directory 2>&1 \
# RUN: --target=thumbv8m.main-none-eabihf --sysroot= \
# RUN: | FileCheck --check-prefix=CHECK-PRINT-MULTI-DIRECTORY %s
# CHECK-PRINT-MULTI-DIRECTORY: arm-none-eabi/thumb/v8-m.main/fp

# RUN: %T/baremetal_multilib/bin/clang -no-canonical-prefixes -print-multi-lib 2>&1 \
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -print-multi-lib 2>&1 \
# RUN: --target=arm-none-eabi --sysroot= \
# RUN: | FileCheck --check-prefix=CHECK-PRINT-MULTI-LIB %s
# CHECK-PRINT-MULTI-LIB: arm-none-eabi/thumb/v6-m/nofp;@-target=thumbv6m-unknown-none-eabi@mfpu=none
Expand All @@ -49,7 +42,7 @@
# CHECK-PRINT-MULTI-LIB: arm-none-eabi/thumb/v8.1-m.main/fp;@-target=thumbv8.1m.main-unknown-none-eabihf@mfpu=fp-armv8-fullfp16-sp-d16
# CHECK-PRINT-MULTI-LIB: arm-none-eabi/thumb/v8.1-m.main/nofp/mve;@-target=thumbv8.1m.main-unknown-none-eabihf@march=thumbv8.1m.main+mve@mfpu=none

# RUN: %T/baremetal_multilib/bin/clang -no-canonical-prefixes -x assembler -mexecute-only \
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -x assembler -mexecute-only \
# RUN: --target=arm-none-eabi --sysroot= %s -c -### 2>&1 \
# RUN: | FileCheck %s --check-prefix=CHECK-NO-EXECUTE-ONLY-ASM
# CHECK-NO-EXECUTE-ONLY-ASM: warning: argument unused during compilation: '-mexecute-only'
Expand Down
Loading
Loading