[clang] [Clang] Include clang standard lib header directory from Linux (PR #175593)
Joseph Huber via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 29 06:33:34 PST 2026
https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/175593
>From 25429d459ce8ed555b9b9270fb0c7f4845581821 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 12 Jan 2026 11:43:19 -0600
Subject: [PATCH 1/5] [Clang] Include LLVM libc header directory from Linux
Summary:
The LLVM-libc stores its headers in the target-specific include
directory. This PR makes the Linux toolchain include this directory when
the environment is LLVM. This should allow the following to work
correctly when building the LLVM libc
```shell
$> clang --target=x86_64-unknown-linux-llvm -static foo.c
```
In the future we might want to consider making a separate toolchain, but
considering that the LLVM-libc intentionally shares a lot of
compatibility with GCC I thnk it's easier to just to do conditional
operations like this.
---
clang/lib/Driver/ToolChains/Linux.cpp | 7 +++++++
clang/test/Driver/Inputs/basic_llvm_linux_tree/bin/.keep | 0
.../Inputs/basic_llvm_linux_tree/include/c++/v1/.keep | 0
.../include/x86_64-unknown-linux-llvm/.keep | 0
.../include/x86_64-unknown-linux-llvm/c++/v1/.keep | 0
.../lib/x86_64-unknown-linux-llvm/.keep | 0
.../lib/x86_64-unknown-linux-llvm/crt0.o | 0
clang/test/Driver/linux-llvm-toolchain.c | 3 +++
8 files changed, 10 insertions(+)
create mode 100644 clang/test/Driver/Inputs/basic_llvm_linux_tree/bin/.keep
create mode 100644 clang/test/Driver/Inputs/basic_llvm_linux_tree/include/c++/v1/.keep
create mode 100644 clang/test/Driver/Inputs/basic_llvm_linux_tree/include/x86_64-unknown-linux-llvm/.keep
create mode 100644 clang/test/Driver/Inputs/basic_llvm_linux_tree/include/x86_64-unknown-linux-llvm/c++/v1/.keep
create mode 100644 clang/test/Driver/Inputs/basic_llvm_linux_tree/lib/x86_64-unknown-linux-llvm/.keep
create mode 100644 clang/test/Driver/Inputs/basic_llvm_linux_tree/lib/x86_64-unknown-linux-llvm/crt0.o
create mode 100644 clang/test/Driver/linux-llvm-toolchain.c
diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp
index cd2c4deb63d14..9e78f0acf6bed 100644
--- a/clang/lib/Driver/ToolChains/Linux.cpp
+++ b/clang/lib/Driver/ToolChains/Linux.cpp
@@ -760,6 +760,13 @@ void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
if (DriverArgs.hasArg(options::OPT_nostdlibinc))
return;
+ // The LLVM-libc environment stores its C headers in the Clang include
+ // directory.
+ if (getTriple().getEnvironment() == llvm::Triple::LLVM) {
+ if (std::optional<std::string> Path = getStdlibIncludePath())
+ addSystemInclude(DriverArgs, CC1Args, *Path);
+ }
+
// LOCAL_INCLUDE_DIR
addSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/local/include"));
// TOOL_INCLUDE_DIR
diff --git a/clang/test/Driver/Inputs/basic_llvm_linux_tree/bin/.keep b/clang/test/Driver/Inputs/basic_llvm_linux_tree/bin/.keep
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/Inputs/basic_llvm_linux_tree/include/c++/v1/.keep b/clang/test/Driver/Inputs/basic_llvm_linux_tree/include/c++/v1/.keep
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/Inputs/basic_llvm_linux_tree/include/x86_64-unknown-linux-llvm/.keep b/clang/test/Driver/Inputs/basic_llvm_linux_tree/include/x86_64-unknown-linux-llvm/.keep
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/Inputs/basic_llvm_linux_tree/include/x86_64-unknown-linux-llvm/c++/v1/.keep b/clang/test/Driver/Inputs/basic_llvm_linux_tree/include/x86_64-unknown-linux-llvm/c++/v1/.keep
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/Inputs/basic_llvm_linux_tree/lib/x86_64-unknown-linux-llvm/.keep b/clang/test/Driver/Inputs/basic_llvm_linux_tree/lib/x86_64-unknown-linux-llvm/.keep
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/Inputs/basic_llvm_linux_tree/lib/x86_64-unknown-linux-llvm/crt0.o b/clang/test/Driver/Inputs/basic_llvm_linux_tree/lib/x86_64-unknown-linux-llvm/crt0.o
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/clang/test/Driver/linux-llvm-toolchain.c b/clang/test/Driver/linux-llvm-toolchain.c
new file mode 100644
index 0000000000000..46632de779f25
--- /dev/null
+++ b/clang/test/Driver/linux-llvm-toolchain.c
@@ -0,0 +1,3 @@
+// RUN: %clang -### --target=x86_64-unknown-linux-llvm --sysroot=%S/Inputs/basic_llvm_linux_tree \
+// RUN: -ccc-install-dir %S/Inputs/basic_llvm_linux_tree/bin %s 2>&1 | FileCheck %s --check-prefix=CHECK-HEADERS
+// CHECK-HEADERS: "-cc1"{{.*}}"-isysroot"{{.*}}"-internal-isystem" "{{.*}}include/x86_64-unknown-linux-llvm"
>From a55e7a1e7b0105fe4dc87076caae5c5c5a39a088 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Tue, 27 Jan 2026 12:07:54 -0600
Subject: [PATCH 2/5] Redo to all Linux
---
clang/lib/Driver/ToolChains/Linux.cpp | 8 +++-----
.../usr}/bin/.keep | 0
.../usr/include/x86_64-unknown-linux-gnu}/.keep | 0
.../include/x86_64-unknown-linux-llvm/.keep | 0
.../include/x86_64-unknown-linux-llvm/c++/v1/.keep | 0
.../lib/x86_64-unknown-linux-llvm/.keep | 0
.../lib/x86_64-unknown-linux-llvm/crt0.o | 0
clang/test/Driver/linux-header-search.cpp | 13 +++++++++++++
clang/test/Driver/linux-llvm-toolchain.c | 3 ---
9 files changed, 16 insertions(+), 8 deletions(-)
rename clang/test/Driver/Inputs/{basic_llvm_linux_tree => basic_linux_tree/usr}/bin/.keep (100%)
rename clang/test/Driver/Inputs/{basic_llvm_linux_tree/include/c++/v1 => basic_linux_tree/usr/include/x86_64-unknown-linux-gnu}/.keep (100%)
delete mode 100644 clang/test/Driver/Inputs/basic_llvm_linux_tree/include/x86_64-unknown-linux-llvm/.keep
delete mode 100644 clang/test/Driver/Inputs/basic_llvm_linux_tree/include/x86_64-unknown-linux-llvm/c++/v1/.keep
delete mode 100644 clang/test/Driver/Inputs/basic_llvm_linux_tree/lib/x86_64-unknown-linux-llvm/.keep
delete mode 100644 clang/test/Driver/Inputs/basic_llvm_linux_tree/lib/x86_64-unknown-linux-llvm/crt0.o
delete mode 100644 clang/test/Driver/linux-llvm-toolchain.c
diff --git a/clang/lib/Driver/ToolChains/Linux.cpp b/clang/lib/Driver/ToolChains/Linux.cpp
index 9e78f0acf6bed..7631781310d60 100644
--- a/clang/lib/Driver/ToolChains/Linux.cpp
+++ b/clang/lib/Driver/ToolChains/Linux.cpp
@@ -760,12 +760,10 @@ void Linux::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
if (DriverArgs.hasArg(options::OPT_nostdlibinc))
return;
- // The LLVM-libc environment stores its C headers in the Clang include
+ // After the resource directory, we prioritize the standard clang include
// directory.
- if (getTriple().getEnvironment() == llvm::Triple::LLVM) {
- if (std::optional<std::string> Path = getStdlibIncludePath())
- addSystemInclude(DriverArgs, CC1Args, *Path);
- }
+ if (std::optional<std::string> Path = getStdlibIncludePath())
+ addSystemInclude(DriverArgs, CC1Args, *Path);
// LOCAL_INCLUDE_DIR
addSystemInclude(DriverArgs, CC1Args, concat(SysRoot, "/usr/local/include"));
diff --git a/clang/test/Driver/Inputs/basic_llvm_linux_tree/bin/.keep b/clang/test/Driver/Inputs/basic_linux_tree/usr/bin/.keep
similarity index 100%
rename from clang/test/Driver/Inputs/basic_llvm_linux_tree/bin/.keep
rename to clang/test/Driver/Inputs/basic_linux_tree/usr/bin/.keep
diff --git a/clang/test/Driver/Inputs/basic_llvm_linux_tree/include/c++/v1/.keep b/clang/test/Driver/Inputs/basic_linux_tree/usr/include/x86_64-unknown-linux-gnu/.keep
similarity index 100%
rename from clang/test/Driver/Inputs/basic_llvm_linux_tree/include/c++/v1/.keep
rename to clang/test/Driver/Inputs/basic_linux_tree/usr/include/x86_64-unknown-linux-gnu/.keep
diff --git a/clang/test/Driver/Inputs/basic_llvm_linux_tree/include/x86_64-unknown-linux-llvm/.keep b/clang/test/Driver/Inputs/basic_llvm_linux_tree/include/x86_64-unknown-linux-llvm/.keep
deleted file mode 100644
index e69de29bb2d1d..0000000000000
diff --git a/clang/test/Driver/Inputs/basic_llvm_linux_tree/include/x86_64-unknown-linux-llvm/c++/v1/.keep b/clang/test/Driver/Inputs/basic_llvm_linux_tree/include/x86_64-unknown-linux-llvm/c++/v1/.keep
deleted file mode 100644
index e69de29bb2d1d..0000000000000
diff --git a/clang/test/Driver/Inputs/basic_llvm_linux_tree/lib/x86_64-unknown-linux-llvm/.keep b/clang/test/Driver/Inputs/basic_llvm_linux_tree/lib/x86_64-unknown-linux-llvm/.keep
deleted file mode 100644
index e69de29bb2d1d..0000000000000
diff --git a/clang/test/Driver/Inputs/basic_llvm_linux_tree/lib/x86_64-unknown-linux-llvm/crt0.o b/clang/test/Driver/Inputs/basic_llvm_linux_tree/lib/x86_64-unknown-linux-llvm/crt0.o
deleted file mode 100644
index e69de29bb2d1d..0000000000000
diff --git a/clang/test/Driver/linux-header-search.cpp b/clang/test/Driver/linux-header-search.cpp
index 70a85deac89e4..d0e72607b36b2 100644
--- a/clang/test/Driver/linux-header-search.cpp
+++ b/clang/test/Driver/linux-header-search.cpp
@@ -1,6 +1,19 @@
// General tests that the header search paths detected by the driver and passed
// to CC1 are sane.
//
+// Test to see that the compiler include directory is present.
+// RUN: %clang -### %s -fsyntax-only 2>&1 \
+// RUN: --target=x86_64-unknown-linux-gnu \
+// RUN: -stdlib=libc++ \
+// RUN: -ccc-install-dir %S/Inputs/basic_linux_tree/usr/bin \
+// RUN: -resource-dir=%S/Inputs/resource_dir \
+// RUN: --sysroot=%S/Inputs/basic_linux_tree/ \
+// RUN: | FileCheck --check-prefix=CHECK-BASIC-CLANG-SYSROOT-SLASH %s
+// CHECK-BASIC-CLANG-SYSROOT-SLASH: "-cc1"
+// CHECK-BASIC-CLANG-SYSROOT-SLASH-SAME: "-isysroot" "[[SYSROOT:[^"]+/]]"
+// CHECK-BASIC-CLANG-SYSROOT-SLASH-SAME: "-internal-isystem" "[[SYSROOT]]usr[[SEP:/|\\\\]]bin[[SEP]]..[[SEP]]include[[SEP]]x86_64-unknown-linux-gnu"
+// CHECK-BASIC-CLANG-SYSROOT-SLASH-SAME: "-internal-isystem" "[[SYSROOT]]usr/local/include"
+//
// Test a simulated installation of libc++ on Linux, both through sysroot and
// the installation path of Clang.
// RUN: %clang -### %s -fsyntax-only 2>&1 \
diff --git a/clang/test/Driver/linux-llvm-toolchain.c b/clang/test/Driver/linux-llvm-toolchain.c
deleted file mode 100644
index 46632de779f25..0000000000000
--- a/clang/test/Driver/linux-llvm-toolchain.c
+++ /dev/null
@@ -1,3 +0,0 @@
-// RUN: %clang -### --target=x86_64-unknown-linux-llvm --sysroot=%S/Inputs/basic_llvm_linux_tree \
-// RUN: -ccc-install-dir %S/Inputs/basic_llvm_linux_tree/bin %s 2>&1 | FileCheck %s --check-prefix=CHECK-HEADERS
-// CHECK-HEADERS: "-cc1"{{.*}}"-isysroot"{{.*}}"-internal-isystem" "{{.*}}include/x86_64-unknown-linux-llvm"
>From 3be178e6600bc7de451862359e95441dc90a7e56 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Tue, 27 Jan 2026 15:27:25 -0600
Subject: [PATCH 3/5] fix test
---
clang/test/Driver/linux-cross.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/test/Driver/linux-cross.cpp b/clang/test/Driver/linux-cross.cpp
index 7f46211b6ed8b..419df5047b954 100644
--- a/clang/test/Driver/linux-cross.cpp
+++ b/clang/test/Driver/linux-cross.cpp
@@ -32,6 +32,7 @@
// DEBIAN_X86_64-SAME: {{^}} "-internal-isystem" "[[SYSROOT:[^"]+]]/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10"
// DEBIAN_X86_64-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward"
// DEBIAN_X86_64-SAME: {{^}} "-internal-isystem" "[[RESOURCE]]/include"
+// DEBIAN_X86_64-SAME: {{^}} "-internal-isystem" "[[BINROOT:[^"]+]]/usr/bin/../include/x86_64-unknown-linux-gnu"
// DEBIAN_X86_64-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/local/include"
// DEBIAN_X86_64-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include"
/// We set explicit -ccc-install-dir ensure that Clang does not pick up extra
@@ -61,6 +62,7 @@
// DEBIAN_X86_64_M32-SAME: {{^}} "-internal-isystem" "[[SYSROOT:[^"]+]]/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10/32"
// DEBIAN_X86_64_M32-SAME: {{^}} "-internal-isystem" "[[SYSROOT:[^"]+]]/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward"
// DEBIAN_X86_64_M32-SAME: {{^}} "-internal-isystem" "[[RESOURCE]]/include"
+// DEBIAN_X86_64-M32-SAME: {{^}} "-internal-isystem" "[[BINROOT:[^"]+]]/usr/bin/../include/x86_64-unknown-linux-gnu"
// DEBIAN_X86_64_M32-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/local/include"
// DEBIAN_X86_64_M32-SAME: {{^}} "-internal-isystem" "[[SYSROOT:[^"]+]]/usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include"
// DEBIAN_X86_64_M32: "-internal-externc-isystem"
@@ -112,6 +114,7 @@
// DEBIAN_I686_M64-SAME: {{^}} "-internal-isystem" "[[SYSROOT:[^"]+]]/usr/lib/gcc/i686-linux-gnu/10/../../../../include/i386-linux-gnu/c++/10/64"
// DEBIAN_I686_M64-SAME: {{^}} "-internal-isystem" "[[SYSROOT:[^"]+]]/usr/lib/gcc/i686-linux-gnu/10/../../../../include/c++/10/backward"
// DEBIAN_I686_M64-SAME: {{^}} "-internal-isystem" "[[RESOURCE]]/include"
+// DEBIAN_I686_M64-SAME: {{^}} "-internal-isystem" "[[BINROOT:[^"]+]]/usr/bin/../include/x86_64-unknown-linux-gnu"
// DEBIAN_I686_M64-SAME: {{^}} "-internal-isystem" "[[SYSROOT]]/usr/local/include"
// DEBIAN_I686_M64-SAME: {{^}} "-internal-isystem" "[[SYSROOT:[^"]+]]/usr/lib/gcc/i686-linux-gnu/10/../../../../i686-linux-gnu/include"
// DEBIAN_I686_M64: "-internal-externc-isystem"
>From 8198656f1966828a2e8e038d126a595a3e32e6be Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Wed, 28 Jan 2026 09:19:35 -0600
Subject: [PATCH 4/5] Try to fix Windows
---
clang/test/Driver/linux-header-search.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/Driver/linux-header-search.cpp b/clang/test/Driver/linux-header-search.cpp
index d0e72607b36b2..c50f3fbf53430 100644
--- a/clang/test/Driver/linux-header-search.cpp
+++ b/clang/test/Driver/linux-header-search.cpp
@@ -11,7 +11,7 @@
// RUN: | FileCheck --check-prefix=CHECK-BASIC-CLANG-SYSROOT-SLASH %s
// CHECK-BASIC-CLANG-SYSROOT-SLASH: "-cc1"
// CHECK-BASIC-CLANG-SYSROOT-SLASH-SAME: "-isysroot" "[[SYSROOT:[^"]+/]]"
-// CHECK-BASIC-CLANG-SYSROOT-SLASH-SAME: "-internal-isystem" "[[SYSROOT]]usr[[SEP:/|\\\\]]bin[[SEP]]..[[SEP]]include[[SEP]]x86_64-unknown-linux-gnu"
+// CHECK-BASIC-CLANG-SYSROOT-SLASH-SAME: "-internal-isystem" "[[BINROOT:[^"]+]]/usr/bin/../include/x86_64-unknown-linux-gnu"
// CHECK-BASIC-CLANG-SYSROOT-SLASH-SAME: "-internal-isystem" "[[SYSROOT]]usr/local/include"
//
// Test a simulated installation of libc++ on Linux, both through sysroot and
>From 3d9eb33e7fc941539fe5e6f584de531abcbd87fc Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Thu, 29 Jan 2026 08:33:17 -0600
Subject: [PATCH 5/5] Try to fix Windows again
---
clang/test/Driver/linux-header-search.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/Driver/linux-header-search.cpp b/clang/test/Driver/linux-header-search.cpp
index c50f3fbf53430..e15d8f1a3b934 100644
--- a/clang/test/Driver/linux-header-search.cpp
+++ b/clang/test/Driver/linux-header-search.cpp
@@ -11,7 +11,7 @@
// RUN: | FileCheck --check-prefix=CHECK-BASIC-CLANG-SYSROOT-SLASH %s
// CHECK-BASIC-CLANG-SYSROOT-SLASH: "-cc1"
// CHECK-BASIC-CLANG-SYSROOT-SLASH-SAME: "-isysroot" "[[SYSROOT:[^"]+/]]"
-// CHECK-BASIC-CLANG-SYSROOT-SLASH-SAME: "-internal-isystem" "[[BINROOT:[^"]+]]/usr/bin/../include/x86_64-unknown-linux-gnu"
+// CHECK-BASIC-CLANG-SYSROOT-SLASH-SAME: "-internal-isystem" "[[BINROOT:[^"]+]]/usr/bin[[SEP:/|\\\\]]..[[SEP]]include[[SEP]]x86_64-unknown-linux-gnu"
// CHECK-BASIC-CLANG-SYSROOT-SLASH-SAME: "-internal-isystem" "[[SYSROOT]]usr/local/include"
//
// Test a simulated installation of libc++ on Linux, both through sysroot and
More information about the cfe-commits
mailing list