Skip to content

Commit f8fd25a

Browse files
committed
[Downstream change][Multilib] Extend the Multilib system to support an IncludeDirs field. (#447)
Downstream issue:#446. This patch extends the Multilib yaml format to allow specifying an IncludeDirs for the header path/s per multilib variant. The goal is to enable fine-grained control over header search paths for each multilib variant. This feature is especially useful in setups where header paths deviate from the default bare-metal assumptions. For example, when headers are shared across target triples, it becomes necessary to organise them under target-triple-specific directories to ensure correct resolution. This is already in upstream review llvm/llvm-project#146651. Since the review is pending, a downstream patch is made to enable IncludeDirs support in multilib.yaml
1 parent 580f8c6 commit f8fd25a

File tree

5 files changed

+132
-7
lines changed

5 files changed

+132
-7
lines changed

clang/include/clang/Driver/Multilib.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,18 @@ class Driver;
3535
class Multilib {
3636
public:
3737
using flags_list = std::vector<std::string>;
38+
// Downstream issue: #446 (Extend the Multilib system to support an
39+
// IncludeDirs field)
40+
using includedirs_list = std::vector<std::string>;
3841

3942
private:
4043
std::string GCCSuffix;
4144
std::string OSSuffix;
4245
std::string IncludeSuffix;
4346
flags_list Flags;
47+
// Downstream issue: #446 (Extend the Multilib system to support an
48+
// IncludeDirs field)
49+
includedirs_list IncludeDirs;
4450

4551
// Optionally, a multilib can be assigned a string tag indicating that it's
4652
// part of a group of mutually exclusive possibilities. If two or more
@@ -62,6 +68,9 @@ class Multilib {
6268
/// This is enforced with an assert in the constructor.
6369
Multilib(StringRef GCCSuffix = {}, StringRef OSSuffix = {},
6470
StringRef IncludeSuffix = {}, const flags_list &Flags = flags_list(),
71+
// Downstream issue: #446 (Extend the Multilib system to support an
72+
// IncludeDirs field)
73+
const includedirs_list &IncludeDirs = includedirs_list(),
6574
StringRef ExclusiveGroup = {},
6675
std::optional<StringRef> Error = std::nullopt);
6776

@@ -81,6 +90,12 @@ class Multilib {
8190
/// All elements begin with either '-' or '!'
8291
const flags_list &flags() const { return Flags; }
8392

93+
// Downstream issue: #446 (Extend the Multilib system to support an
94+
// IncludeDirs field)
95+
/// Get the include directories specified in multilib.yaml under the
96+
/// 'IncludeDirs' field
97+
const includedirs_list &includeDirs() const { return IncludeDirs; }
98+
8499
/// Get the exclusive group label.
85100
const std::string &exclusiveGroup() const { return ExclusiveGroup; }
86101

clang/lib/Driver/Multilib.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ using namespace llvm::sys;
2929

3030
Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
3131
StringRef IncludeSuffix, const flags_list &Flags,
32+
// Downstream issue: #446 (Extend the Multilib system to
33+
// support an IncludeDirs field)
34+
const includedirs_list &IncludeDirs,
3235
StringRef ExclusiveGroup, std::optional<StringRef> Error)
3336
: GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix),
34-
Flags(Flags), ExclusiveGroup(ExclusiveGroup), Error(Error) {
37+
Flags(Flags),
38+
// Downstream issue: #446 (Extend the Multilib system to support an
39+
// IncludeDirs field)
40+
IncludeDirs(IncludeDirs), ExclusiveGroup(ExclusiveGroup), Error(Error) {
3541
assert(GCCSuffix.empty() ||
3642
(StringRef(GCCSuffix).front() == '/' && GCCSuffix.size() > 1));
3743
assert(OSSuffix.empty() ||
@@ -299,6 +305,9 @@ struct MultilibSerialization {
299305
std::string Dir; // if this record successfully selects a library dir
300306
std::string Error; // if this record reports a fatal error message
301307
std::vector<std::string> Flags;
308+
// Downstream issue: #446 (Extend the Multilib system to support an
309+
// IncludeDirs field)
310+
std::vector<std::string> IncludeDirs;
302311
std::string Group;
303312
};
304313

@@ -350,6 +359,9 @@ template <> struct llvm::yaml::MappingTraits<MultilibSerialization> {
350359
io.mapOptional("Dir", V.Dir);
351360
io.mapOptional("Error", V.Error);
352361
io.mapRequired("Flags", V.Flags);
362+
// Downstream issue: #446 (Extend the Multilib system to support an
363+
// IncludeDirs field)
364+
io.mapOptional("IncludeDirs", V.IncludeDirs);
353365
io.mapOptional("Group", V.Group);
354366
}
355367
static std::string validate(IO &io, MultilibSerialization &V) {
@@ -359,6 +371,12 @@ template <> struct llvm::yaml::MappingTraits<MultilibSerialization> {
359371
return "the 'Dir' and 'Error' keys may not both be specified";
360372
if (StringRef(V.Dir).starts_with("/"))
361373
return "paths must be relative but \"" + V.Dir + "\" starts with \"/\"";
374+
// Downstream issue: #446 (Extend the Multilib system to support an
375+
// IncludeDirs field)
376+
for (const auto &Path : V.IncludeDirs) {
377+
if (StringRef(Path).starts_with("/"))
378+
return "paths must be relative but \"" + Path + "\" starts with \"/\"";
379+
}
362380
return std::string{};
363381
}
364382
};
@@ -489,7 +507,10 @@ MultilibSet::parseYaml(llvm::MemoryBufferRef Input,
489507
Multilibs.reserve(MS.Multilibs.size());
490508
for (const auto &M : MS.Multilibs) {
491509
if (!M.Error.empty()) {
492-
Multilibs.emplace_back("", "", "", M.Flags, M.Group, M.Error);
510+
// Downstream issue: #446 (Extend the Multilib system to support an
511+
// IncludeDirs field)
512+
Multilibs.emplace_back("", "", "", M.Flags, M.IncludeDirs, M.Group,
513+
M.Error);
493514
} else {
494515
std::string Dir;
495516
if (M.Dir != ".")
@@ -498,7 +519,9 @@ MultilibSet::parseYaml(llvm::MemoryBufferRef Input,
498519
// Multilib constructor. If we later support more than one type of group,
499520
// we'll have to look up the group name in MS.Groups, check its type, and
500521
// decide what to do here.
501-
Multilibs.emplace_back(Dir, Dir, Dir, M.Flags, M.Group);
522+
// Downstream issue: #446 (Extend the Multilib system to support an
523+
// IncludeDirs field)
524+
Multilibs.emplace_back(Dir, Dir, Dir, M.Flags, M.IncludeDirs, M.Group);
502525
}
503526
}
504527

clang/lib/Driver/ToolChains/BareMetal.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,22 @@ void BareMetal::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
427427
const SmallString<128> SysRootDir(computeSysRoot());
428428
if (!SysRootDir.empty()) {
429429
for (const Multilib &M : getOrderedMultilibs()) {
430-
SmallString<128> Dir(SysRootDir);
431-
llvm::sys::path::append(Dir, M.includeSuffix());
432-
llvm::sys::path::append(Dir, "include");
433-
addSystemInclude(DriverArgs, CC1Args, Dir.str());
430+
// Downstream issue: #446 (Extend the Multilib system to support an
431+
// IncludeDirs field)
432+
if (!M.includeDirs().empty()) {
433+
// Add include directories specified in multilib.yaml under the
434+
// 'IncludeDirs' field
435+
for (const std::string &Path : M.includeDirs()) {
436+
SmallString<128> Dir(SysRoot);
437+
llvm::sys::path::append(Dir, Path);
438+
addSystemInclude(DriverArgs, CC1Args, Dir.str());
439+
}
440+
} else {
441+
SmallString<128> Dir(SysRootDir);
442+
llvm::sys::path::append(Dir, M.includeSuffix());
443+
llvm::sys::path::append(Dir, "include");
444+
addSystemInclude(DriverArgs, CC1Args, Dir.str());
445+
}
434446
}
435447
}
436448
}
@@ -511,6 +523,18 @@ void BareMetal::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
511523
addSystemInclude(DriverArgs, CC1Args, TargetDir.str());
512524
break;
513525
}
526+
// Downstream issue: #446 (Extend the Multilib system to support an
527+
// IncludeDirs field)
528+
if (!M.includeDirs().empty()) {
529+
// Add include directories specified in multilib.yaml under the
530+
// 'IncludeDirs' field
531+
for (const std::string &Path : M.includeDirs()) {
532+
Dir = SysRoot;
533+
llvm::sys::path::append(Dir, Path, "c++", "v1");
534+
addSystemInclude(DriverArgs, CC1Args, Dir.str());
535+
}
536+
break;
537+
}
514538
// Add generic path if nothing else succeeded so far.
515539
llvm::sys::path::append(Dir, "include", "c++", "v1");
516540
addSystemInclude(DriverArgs, CC1Args, Dir.str());
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Downstream issue: #446 (Extend the Multilib system to support an IncludeDirs field)
2+
# REQUIRES: shell
3+
# UNSUPPORTED: system-windows
4+
5+
# This test demonstrates the new "IncludeDirs" field.
6+
# This field allows specifying include directories through
7+
# multilib.yaml configuration using relative paths.
8+
# Absolute paths should be rejected by the driver
9+
# with an appropriate diagnostic message.
10+
#
11+
# This test verifies that passing an absolute path
12+
# (/include) to IncludeDirs triggers the expected
13+
# error.
14+
15+
# RUN: not %clang --target=aarch64-none-elf --multi-lib-config=%s %s -o /dev/null 2>&1 \
16+
# RUN: | FileCheck %s --check-prefix=CHECK-ERROR
17+
# CHECK-ERROR: error: paths must be relative but "/include" starts with "/"
18+
19+
---
20+
MultilibVersion: 1.0
21+
Variants:
22+
- Dir: aarch64-none-elf/aarch64a_exn_rtti_unaligned
23+
Flags:
24+
- --target=aarch64-unknown-none-elf
25+
- -munaligned-access
26+
IncludeDirs:
27+
- /include
28+
- aarch64-none-elf/include
29+
30+
...
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Downstream issue: #446 (Extend the Multilib system to support an IncludeDirs field)
2+
# REQUIRES: shell
3+
# UNSUPPORTED: system-windows
4+
5+
# This test demonstrates the new "IncludeDirs" field.
6+
# This field allows specifying include directories through
7+
# multilib.yaml configuration. When this field is present
8+
# it is appended to the sysroot during header path
9+
# resolution ignoring the default bare-metal assumptions.
10+
11+
# RUN: %clang --multi-lib-config=%s -no-canonical-prefixes -x c++ %s -### -o %t.out 2>&1 \
12+
# RUN: --target=thumbv7m-none-eabi -mfloat-abi=soft --sysroot= \
13+
# RUN: | FileCheck %s
14+
# CHECK: "-cc1" "-triple" "thumbv7m-unknown-none-eabi"
15+
# CHECK-SAME: "-internal-isystem" "[[SYSROOT:[^"]*]]/bin/../lib/clang-runtimes/include/c++/v1"
16+
# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/include-libunwind/c++/v1"
17+
# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/soft/include/c++/v1"
18+
# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/include"
19+
# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/include-libunwind"
20+
# CHECK-SAME: "-internal-isystem" "[[SYSROOT]]/bin/../lib/clang-runtimes/soft/include"
21+
22+
---
23+
MultilibVersion: 1.0
24+
Variants:
25+
- Dir: soft
26+
Flags:
27+
- -mfloat-abi=soft
28+
IncludeDirs:
29+
- include
30+
- include-libunwind
31+
- soft/include
32+
33+
...

0 commit comments

Comments
 (0)