Skip to content

Commit 49f9756

Browse files
committed
[ATFE] Extend the Multilib system to support an IncludeDirs field via downstream patching.
The corresponding llvm patch is currently under upstream review: llvm/llvm-project#146651
1 parent fba94b3 commit 49f9756

File tree

1 file changed

+290
-0
lines changed

1 file changed

+290
-0
lines changed
Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
From 9e875497e4830d2532ebedc47c5817fc3260fe25 Mon Sep 17 00:00:00 2001
2+
From: Simi Pallipurath <[email protected]>
3+
Date: Mon, 28 Jul 2025 19:58:58 +0100
4+
Subject: [Multilib] Extend the Multilib system to support an
5+
IncludeDirs field.
6+
MIME-Version: 1.0
7+
Content-Type: text/plain; charset=UTF-8
8+
Content-Transfer-Encoding: 8bit
9+
10+
This patch extends the Multilib Yaml format to allow
11+
specifying an IncludeDirs for the header path/s per
12+
multilib variant. The goal is to enable fine-grained
13+
control over header search paths for each multilib variant.
14+
This feature is especially useful in setups where header
15+
paths deviate from the default bare-metal assumptions.
16+
For example, when headers are shared across target triples,
17+
it becomes necessary to organize them under target-triple-specific
18+
directories to ensure correct resolution.
19+
20+
In the Clang driver, GCCSuffix, OSSuffix and IncludeSuffix
21+
are path suffixes that help the compiler locate the correct
22+
set of headers and libraries for the selected target.
23+
Clang’s multilib infrastructure uses the Multilib class
24+
to encapsulate these suffixes. Currently, in the bare-metal
25+
Clang driver, all of these suffixes — GCCSuffix, OSSuffix,
26+
and IncludeSuffix are effectively mapped to the Dir field
27+
specified in the multilib.yaml configuration.
28+
29+
This patch allows it to be configured independently of Dir,
30+
enabling finer-grained control over header search directories
31+
for each multilib variant.
32+
33+
Key Changes:
34+
35+
New Field: IncludeDirs in multilib.yaml (header directory or
36+
list of header directories, strings, optional).
37+
38+
When present, this path is passed to the Multilib constructor
39+
and appended to the sysroot during header path resolution. If
40+
omitted, behaviour defaults to preserving current behaviour.
41+
42+
For example,
43+
44+
Dir: aarch64-none-elf/aarch64a_exn_rtti_unaligned
45+
Flags:
46+
--target=aarch64-unknown-none-elf
47+
IncludeDirs:
48+
include
49+
include-libunwind
50+
aarch64-none-elf/include
51+
aarch64-none-elf/aarch64a_exn_rtti_unaligned/include
52+
53+
Implementation Notes:
54+
55+
1. Extend the YAML parser to read the IncludeDirs key.
56+
2. Update the Multilib constructor to store the content from this field.
57+
3. Ensure the driver logic reads and applies IncludeDirs correctly.
58+
---
59+
clang/include/clang/Driver/Multilib.h | 7 ++++
60+
clang/lib/Driver/Multilib.cpp | 15 +++++++--
61+
clang/lib/Driver/ToolChains/BareMetal.cpp | 28 +++++++++++++---
62+
.../baremetal-multilib-includedirs-error.yaml | 29 +++++++++++++++++
63+
.../baremetal-multilib-includedirs.yaml | 32 +++++++++++++++++++
64+
5 files changed, 104 insertions(+), 7 deletions(-)
65+
create mode 100644 clang/test/Driver/baremetal-multilib-includedirs-error.yaml
66+
create mode 100644 clang/test/Driver/baremetal-multilib-includedirs.yaml
67+
68+
diff --git a/clang/include/clang/Driver/Multilib.h b/clang/include/clang/Driver/Multilib.h
69+
index fc071ef48ca0..7c643b996d9c 100644
70+
--- a/clang/include/clang/Driver/Multilib.h
71+
+++ b/clang/include/clang/Driver/Multilib.h
72+
@@ -35,12 +35,14 @@ class Driver;
73+
class Multilib {
74+
public:
75+
using flags_list = std::vector<std::string>;
76+
+ using includedirs_list = std::vector<std::string>;
77+
78+
private:
79+
std::string GCCSuffix;
80+
std::string OSSuffix;
81+
std::string IncludeSuffix;
82+
flags_list Flags;
83+
+ includedirs_list IncludeDirs;
84+
85+
// Optionally, a multilib can be assigned a string tag indicating that it's
86+
// part of a group of mutually exclusive possibilities. If two or more
87+
@@ -62,6 +64,7 @@ public:
88+
/// This is enforced with an assert in the constructor.
89+
Multilib(StringRef GCCSuffix = {}, StringRef OSSuffix = {},
90+
StringRef IncludeSuffix = {}, const flags_list &Flags = flags_list(),
91+
+ const includedirs_list &IncludeDirs = includedirs_list(),
92+
StringRef ExclusiveGroup = {},
93+
std::optional<StringRef> Error = std::nullopt);
94+
95+
@@ -81,6 +84,10 @@ public:
96+
/// All elements begin with either '-' or '!'
97+
const flags_list &flags() const { return Flags; }
98+
99+
+ /// Get the include directories specified in multilib.yaml under the
100+
+ /// 'IncludeDirs' field
101+
+ const includedirs_list &includeDirs() const { return IncludeDirs; }
102+
+
103+
/// Get the exclusive group label.
104+
const std::string &exclusiveGroup() const { return ExclusiveGroup; }
105+
106+
diff --git a/clang/lib/Driver/Multilib.cpp b/clang/lib/Driver/Multilib.cpp
107+
index 87fa1af54a8e..05ab26fb402b 100644
108+
--- a/clang/lib/Driver/Multilib.cpp
109+
+++ b/clang/lib/Driver/Multilib.cpp
110+
@@ -29,9 +29,11 @@ using namespace llvm::sys;
111+
112+
Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
113+
StringRef IncludeSuffix, const flags_list &Flags,
114+
+ const includedirs_list &IncludeDirs,
115+
StringRef ExclusiveGroup, std::optional<StringRef> Error)
116+
: GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix),
117+
- Flags(Flags), ExclusiveGroup(ExclusiveGroup), Error(Error) {
118+
+ Flags(Flags), IncludeDirs(IncludeDirs), ExclusiveGroup(ExclusiveGroup),
119+
+ Error(Error) {
120+
assert(GCCSuffix.empty() ||
121+
(StringRef(GCCSuffix).front() == '/' && GCCSuffix.size() > 1));
122+
assert(OSSuffix.empty() ||
123+
@@ -299,6 +301,7 @@ struct MultilibSerialization {
124+
std::string Dir; // if this record successfully selects a library dir
125+
std::string Error; // if this record reports a fatal error message
126+
std::vector<std::string> Flags;
127+
+ std::vector<std::string> IncludeDirs;
128+
std::string Group;
129+
};
130+
131+
@@ -350,6 +353,7 @@ template <> struct llvm::yaml::MappingTraits<MultilibSerialization> {
132+
io.mapOptional("Dir", V.Dir);
133+
io.mapOptional("Error", V.Error);
134+
io.mapRequired("Flags", V.Flags);
135+
+ io.mapOptional("IncludeDirs", V.IncludeDirs);
136+
io.mapOptional("Group", V.Group);
137+
}
138+
static std::string validate(IO &io, MultilibSerialization &V) {
139+
@@ -359,6 +363,10 @@ template <> struct llvm::yaml::MappingTraits<MultilibSerialization> {
140+
return "the 'Dir' and 'Error' keys may not both be specified";
141+
if (StringRef(V.Dir).starts_with("/"))
142+
return "paths must be relative but \"" + V.Dir + "\" starts with \"/\"";
143+
+ for (const auto &Path : V.IncludeDirs) {
144+
+ if (StringRef(Path).starts_with("/"))
145+
+ return "paths must be relative but \"" + Path + "\" starts with \"/\"";
146+
+ }
147+
return std::string{};
148+
}
149+
};
150+
@@ -489,7 +497,8 @@ MultilibSet::parseYaml(llvm::MemoryBufferRef Input,
151+
Multilibs.reserve(MS.Multilibs.size());
152+
for (const auto &M : MS.Multilibs) {
153+
if (!M.Error.empty()) {
154+
- Multilibs.emplace_back("", "", "", M.Flags, M.Group, M.Error);
155+
+ Multilibs.emplace_back("", "", "", M.Flags, M.IncludeDirs, M.Group,
156+
+ M.Error);
157+
} else {
158+
std::string Dir;
159+
if (M.Dir != ".")
160+
@@ -498,7 +507,7 @@ MultilibSet::parseYaml(llvm::MemoryBufferRef Input,
161+
// Multilib constructor. If we later support more than one type of group,
162+
// we'll have to look up the group name in MS.Groups, check its type, and
163+
// decide what to do here.
164+
- Multilibs.emplace_back(Dir, Dir, Dir, M.Flags, M.Group);
165+
+ Multilibs.emplace_back(Dir, Dir, Dir, M.Flags, M.IncludeDirs, M.Group);
166+
}
167+
}
168+
169+
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp b/clang/lib/Driver/ToolChains/BareMetal.cpp
170+
index 497f3330237b..aee28ee1bbc3 100644
171+
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
172+
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
173+
@@ -427,10 +427,20 @@ void BareMetal::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
174+
const SmallString<128> SysRootDir(computeSysRoot());
175+
if (!SysRootDir.empty()) {
176+
for (const Multilib &M : getOrderedMultilibs()) {
177+
- SmallString<128> Dir(SysRootDir);
178+
- llvm::sys::path::append(Dir, M.includeSuffix());
179+
- llvm::sys::path::append(Dir, "include");
180+
- addSystemInclude(DriverArgs, CC1Args, Dir.str());
181+
+ if (!M.includeDirs().empty()) {
182+
+ // Add include directories specified in multilib.yaml under the
183+
+ // 'IncludeDirs' field
184+
+ for (const std::string &Path : M.includeDirs()) {
185+
+ SmallString<128> Dir(SysRoot);
186+
+ llvm::sys::path::append(Dir, Path);
187+
+ addSystemInclude(DriverArgs, CC1Args, Dir.str());
188+
+ }
189+
+ } else {
190+
+ SmallString<128> Dir(SysRootDir);
191+
+ llvm::sys::path::append(Dir, M.includeSuffix());
192+
+ llvm::sys::path::append(Dir, "include");
193+
+ addSystemInclude(DriverArgs, CC1Args, Dir.str());
194+
+ }
195+
}
196+
}
197+
}
198+
@@ -511,6 +521,16 @@ void BareMetal::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
199+
addSystemInclude(DriverArgs, CC1Args, TargetDir.str());
200+
break;
201+
}
202+
+ if (!M.includeDirs().empty()) {
203+
+ // Add include directories specified in multilib.yaml under the
204+
+ // 'IncludeDirs' field
205+
+ for (const std::string &Path : M.includeDirs()) {
206+
+ Dir = SysRoot;
207+
+ llvm::sys::path::append(Dir, Path, "c++", "v1");
208+
+ addSystemInclude(DriverArgs, CC1Args, Dir.str());
209+
+ }
210+
+ break;
211+
+ }
212+
// Add generic path if nothing else succeeded so far.
213+
llvm::sys::path::append(Dir, "include", "c++", "v1");
214+
addSystemInclude(DriverArgs, CC1Args, Dir.str());
215+
diff --git a/clang/test/Driver/baremetal-multilib-includedirs-error.yaml b/clang/test/Driver/baremetal-multilib-includedirs-error.yaml
216+
new file mode 100644
217+
index 000000000000..f6360135f8c8
218+
--- /dev/null
219+
+++ b/clang/test/Driver/baremetal-multilib-includedirs-error.yaml
220+
@@ -0,0 +1,29 @@
221+
+# REQUIRES: shell
222+
+# UNSUPPORTED: system-windows
223+
+
224+
+# This test demonstrates the new "IncludeDirs" field.
225+
+# This field allows specifying include directories through
226+
+# multilib.yaml configuration using relative paths.
227+
+# Absolute paths should be rejected by the driver
228+
+# with an appropriate diagnostic message.
229+
+#
230+
+# This test verifies that passing an absolute path
231+
+# (/include) to IncludeDirs triggers the expected
232+
+# error.
233+
+
234+
+# RUN: not %clang --target=aarch64-none-elf --multi-lib-config=%s %s -o /dev/null 2>&1 \
235+
+# RUN: | FileCheck %s --check-prefix=CHECK-ERROR
236+
+# CHECK-ERROR: error: paths must be relative but "/include" starts with "/"
237+
+
238+
+---
239+
+MultilibVersion: 1.0
240+
+Variants:
241+
+- Dir: aarch64-none-elf/aarch64a_exn_rtti_unaligned
242+
+ Flags:
243+
+ - --target=aarch64-unknown-none-elf
244+
+ - -munaligned-access
245+
+ IncludeDirs:
246+
+ - /include
247+
+ - aarch64-none-elf/include
248+
+
249+
+...
250+
diff --git a/clang/test/Driver/baremetal-multilib-includedirs.yaml b/clang/test/Driver/baremetal-multilib-includedirs.yaml
251+
new file mode 100644
252+
index 000000000000..2c5a4bc622c4
253+
--- /dev/null
254+
+++ b/clang/test/Driver/baremetal-multilib-includedirs.yaml
255+
@@ -0,0 +1,32 @@
256+
+# REQUIRES: shell
257+
+# UNSUPPORTED: system-windows
258+
+
259+
+# This test demonstrates the new "IncludeDirs" field.
260+
+# This field allows specifying include directories through
261+
+# multilib.yaml configuration. When this field is present
262+
+# it is appended to the sysroot during header path
263+
+# resolution ignoring the default bare-metal assumptions.
264+
+
265+
+# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -x c++ %s -### -o %t.out 2>&1 \
266+
+# RUN: --target=thumbv7m-none-eabi -mfloat-abi=soft --sysroot= \
267+
+# RUN: | FileCheck %s
268+
+# CHECK: "-cc1" "-triple" "thumbv7m-unknown-none-eabi"
269+
+# CHECK-SAME: "-internal-isystem" "[[SYSROOT:[^"]*]]/bin/../lib/clang-runtimes/include/c++/v1"
270+
+# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/include-libunwind/c++/v1"
271+
+# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/soft/include/c++/v1"
272+
+# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/include"
273+
+# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/include-libunwind"
274+
+# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/soft/include"
275+
+
276+
+---
277+
+MultilibVersion: 1.0
278+
+Variants:
279+
+- Dir: soft
280+
+ Flags:
281+
+ - -mfloat-abi=soft
282+
+ IncludeDirs:
283+
+ - include
284+
+ - include-libunwind
285+
+ - soft/include
286+
+
287+
+...
288+
--
289+
2.34.1
290+

0 commit comments

Comments
 (0)