Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions clang/include/clang/Driver/Multilib.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ class Driver;
class Multilib {
public:
using flags_list = std::vector<std::string>;
using includepath_list = std::vector<std::string>;

private:
std::string GCCSuffix;
std::string OSSuffix;
std::string IncludeSuffix;
flags_list Flags;
includepath_list IncludePath;

// Optionally, a multilib can be assigned a string tag indicating that it's
// part of a group of mutually exclusive possibilities. If two or more
Expand All @@ -62,6 +64,7 @@ class Multilib {
/// This is enforced with an assert in the constructor.
Multilib(StringRef GCCSuffix = {}, StringRef OSSuffix = {},
StringRef IncludeSuffix = {}, const flags_list &Flags = flags_list(),
const includepath_list &IncludePath = includepath_list(),
StringRef ExclusiveGroup = {},
std::optional<StringRef> Error = std::nullopt);

Expand All @@ -81,6 +84,10 @@ class Multilib {
/// All elements begin with either '-' or '!'
const flags_list &flags() const { return Flags; }

/// Get the include paths specified in multilib.yaml under the 'IncludePath'
/// field
const includepath_list &includePath() const { return IncludePath; }

/// Get the exclusive group label.
const std::string &exclusiveGroup() const { return ExclusiveGroup; }

Expand Down
15 changes: 12 additions & 3 deletions clang/lib/Driver/Multilib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ using namespace llvm::sys;

Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
StringRef IncludeSuffix, const flags_list &Flags,
const includepath_list &IncludePath,
StringRef ExclusiveGroup, std::optional<StringRef> Error)
: GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix),
Flags(Flags), ExclusiveGroup(ExclusiveGroup), Error(Error) {
Flags(Flags), IncludePath(IncludePath), ExclusiveGroup(ExclusiveGroup),
Error(Error) {
assert(GCCSuffix.empty() ||
(StringRef(GCCSuffix).front() == '/' && GCCSuffix.size() > 1));
assert(OSSuffix.empty() ||
Expand Down Expand Up @@ -299,6 +301,7 @@ struct MultilibSerialization {
std::string Dir; // if this record successfully selects a library dir
std::string Error; // if this record reports a fatal error message
std::vector<std::string> Flags;
std::vector<std::string> IncludePath;
std::string Group;
};

Expand Down Expand Up @@ -350,6 +353,7 @@ template <> struct llvm::yaml::MappingTraits<MultilibSerialization> {
io.mapOptional("Dir", V.Dir);
io.mapOptional("Error", V.Error);
io.mapRequired("Flags", V.Flags);
io.mapOptional("IncludePath", V.IncludePath);
io.mapOptional("Group", V.Group);
}
static std::string validate(IO &io, MultilibSerialization &V) {
Expand All @@ -359,6 +363,10 @@ template <> struct llvm::yaml::MappingTraits<MultilibSerialization> {
return "the 'Dir' and 'Error' keys may not both be specified";
if (StringRef(V.Dir).starts_with("/"))
return "paths must be relative but \"" + V.Dir + "\" starts with \"/\"";
for (const auto &Path : V.IncludePath) {
if (StringRef(Path).starts_with("/"))
return "paths must be relative but \"" + Path + "\" starts with \"/\"";
}
return std::string{};
}
};
Expand Down Expand Up @@ -489,7 +497,8 @@ MultilibSet::parseYaml(llvm::MemoryBufferRef Input,
Multilibs.reserve(MS.Multilibs.size());
for (const auto &M : MS.Multilibs) {
if (!M.Error.empty()) {
Multilibs.emplace_back("", "", "", M.Flags, M.Group, M.Error);
Multilibs.emplace_back("", "", "", M.Flags, M.IncludePath, M.Group,
M.Error);
} else {
std::string Dir;
if (M.Dir != ".")
Expand All @@ -498,7 +507,7 @@ MultilibSet::parseYaml(llvm::MemoryBufferRef Input,
// Multilib constructor. If we later support more than one type of group,
// we'll have to look up the group name in MS.Groups, check its type, and
// decide what to do here.
Multilibs.emplace_back(Dir, Dir, Dir, M.Flags, M.Group);
Multilibs.emplace_back(Dir, Dir, Dir, M.Flags, M.IncludePath, M.Group);
}
}

Expand Down
28 changes: 24 additions & 4 deletions clang/lib/Driver/ToolChains/BareMetal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,20 @@ void BareMetal::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
const SmallString<128> SysRootDir(computeSysRoot());
if (!SysRootDir.empty()) {
for (const Multilib &M : getOrderedMultilibs()) {
SmallString<128> Dir(SysRootDir);
llvm::sys::path::append(Dir, M.includeSuffix());
llvm::sys::path::append(Dir, "include");
addSystemInclude(DriverArgs, CC1Args, Dir.str());
if (!M.includePath().empty()) {
// Add include paths specified in multilib.yaml under the 'IncludePath'
// field
for (const std::string &Path : M.includePath()) {
SmallString<128> Dir(SysRoot);
llvm::sys::path::append(Dir, Path);
addSystemInclude(DriverArgs, CC1Args, Dir.str());
}
} else {
SmallString<128> Dir(SysRootDir);
llvm::sys::path::append(Dir, M.includeSuffix());
llvm::sys::path::append(Dir, "include");
addSystemInclude(DriverArgs, CC1Args, Dir.str());
}
}
}
}
Expand Down Expand Up @@ -501,6 +511,16 @@ void BareMetal::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
addSystemInclude(DriverArgs, CC1Args, TargetDir.str());
break;
}
if (!M.includePath().empty()) {
// Add include paths specified in multilib.yaml under the 'IncludePath'
// field
for (const std::string &Path : M.includePath()) {
Dir = SysRoot;
llvm::sys::path::append(Dir, Path, "c++", "v1");
addSystemInclude(DriverArgs, CC1Args, Dir.str());
}
break;
}
// Add generic path if nothing else succeeded so far.
llvm::sys::path::append(Dir, "include", "c++", "v1");
addSystemInclude(DriverArgs, CC1Args, Dir.str());
Expand Down
28 changes: 28 additions & 0 deletions clang/test/Driver/baremetal-multilib-includepath-error.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

# REQUIRES: shell
# UNSUPPORTED: system-windows

# This test demonstrates the new "IncludePath" field.
# This field allows specifying include paths through
# multilib.yaml configuration using relative paths.
# Absolute paths should be rejected by the driver
# with an appropriate diagnostic message.
#
# This test verifies that passing an absolute path
# (/include) to IncludePath triggers the expected
# error.

# # RUN: %clang --target=aarch64-none-elf --multi-lib-config=%s %s -### -o /dev/null 2>&1 \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit surprised that this double # doesn't stop the run from happening!

Also slightly surprised that you didn't need a not on the command line. Does the clang driver not return a failure status when it finds an error in multilib.yaml?

Copy link
Contributor Author

@simpal01 simpal01 Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. It did not give me any failure

-- Testing: 1 tests, 1 workers --
PASS: Clang :: Driver/baremetal-multilib-includepath-error.yaml (1 of 1)
Exit Code: 0

Command Output (stderr):
--
/data_nvme1n1/LLVM-PROJECT/build-driver/bin/clang --target=aarch64-none-elf --multi-lib-config=/data_nvme1n1/LLVM-PROJECT/llvm-project-fork/clang/test/Driver/baremetal-multilib-includepath-error.yaml /data_nvme1n1/LLVM-PROJECT/llvm-project-fork/clang/test/Driver/baremetal-multilib-includepath-error.yaml -### -o /dev/null 2>&1  | /data_nvme1n1/LLVM-PROJECT/build-driver/bin/FileCheck /data_nvme1n1/LLVM-PROJECT/llvm-project-fork/clang/test/Driver/baremetal-multilib-includepath-error.yaml --check-prefix=CHECK-ERROR # RUN: at line 15
+ /data_nvme1n1/LLVM-PROJECT/build-driver/bin/clang --target=aarch64-none-elf --multi-lib-config=/data_nvme1n1/LLVM-PROJECT/llvm-project-fork/clang/test/Driver/baremetal-multilib-includepath-error.yaml /data_nvme1n1/LLVM-PROJECT/llvm-project-fork/clang/test/Driver/baremetal-multilib-includepath-error.yaml -### -o /dev/null
+ /data_nvme1n1/LLVM-PROJECT/build-driver/bin/FileCheck /data_nvme1n1/LLVM-PROJECT/llvm-project-fork/clang/test/Driver/baremetal-multilib-includepath-error.yaml --check-prefix=CHECK-ERROR

--

********************

Testing Time: 0.07s

Total Discovered Tests: 1
  Passed: 1 (100.00%)

When running the command manually:

$ /data_nvme1n1/LLVM-PROJECT/build-driver/bin/clang --target=aarch64-none-elf --multi-lib-config=/data_nvme1n1/LLVM-PROJECT/llvm-project-fork/clang/test/Driver/baremetal-multilib-includepath-error.yaml /data_nvme1n1/LLVM-PROJECT/llvm-project-fork/clang/test/Driver/baremetal-multilib-includepath-error.yaml -### -o /dev/null 2>&1

/data_nvme1n1/LLVM-PROJECT/llvm-project-fork/clang/test/Driver/baremetal-multilib-includepath-error.yaml:22:3: error: paths must be relative but "/include" starts with "/"
- Dir: aarch64-none-elf/aarch64a_exn_rtti_unaligned
  ^
clang version 21.0.0git ([email protected]:simpal01/llvm-project-fork.git fe8fcbffbfc7c8a16285d3100f089b6528fb1329)
Target: aarch64-unknown-none-elf
Thread model: posix
InstalledDir: /data_nvme1n1/LLVM-PROJECT/build-driver/bin

ah! This might be because I unintentionally included -###, which only prints the driver’s command sequence without actually performing compilation.

After removing -###, it became necessary to include not to ensure the llvm-lit test passed.

Good catch Simon!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will update the test accordingly

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see! Well found.

# RUN: | FileCheck %s --check-prefix=CHECK-ERROR
# CHECK-ERROR: error: paths must be relative but "/include" starts with "/"


MultilibVersion: 1.0
Variants:
- Dir: aarch64-none-elf/aarch64a_exn_rtti_unaligned
Flags:
- --target=aarch64-unknown-none-elf
- -munaligned-access
IncludePath:
- /include
- aarch64-none-elf/include
31 changes: 31 additions & 0 deletions clang/test/Driver/baremetal-multilib-includepath.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# REQUIRES: shell
# UNSUPPORTED: system-windows

# This test demonstrates the new "IncludePath" field.
# This field allows specifying include paths through
# multilib.yaml configuration. When this field is present
# it is appended to the sysroot during header path
# resolution ignoring the default bare-metal assumptions.

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

---
MultilibVersion: 1.0
Variants:
- Dir: soft
Flags:
- -mfloat-abi=soft
IncludePath:
- include
- include-libunwind
- soft/include
...
Loading