[clang] [clang][driver] Add \<executable\>/../include/c++/v1 to include path on Darwin (PR #70817)
Liviu Ionescu via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 13 05:36:30 PST 2023
https://github.com/ilg-ul updated https://github.com/llvm/llvm-project/pull/70817
>From 7fbc229ee7316d826517480ee7896c91dad941f3 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Tue, 31 Oct 2023 17:09:04 +0200
Subject: [PATCH 01/13] Add \<executable\>/../include/c++/v1 to include path
On macOS, when clang is invoked via a symlink, since the InstalledDir is
where the link is located, the C++ headers are not identified and the
default system headers are used.
This fix adds a second check using the folder where the executable is
located.
---
clang/lib/Driver/ToolChains/Darwin.cpp | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index f28e08d81bf29b4..de55307385966cf 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2494,6 +2494,19 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
<< "\"\n";
}
+ // Check for the folder where the executable is located, if different.
+ if (getDriver().getInstalledDir() != getDriver().Dir) {
+ InstallBin = llvm::StringRef(getDriver().Dir.c_str());
+ llvm::sys::path::append(InstallBin, "..", "include", "c++", "v1");
+ if (getVFS().exists(InstallBin)) {
+ addSystemInclude(DriverArgs, CC1Args, InstallBin);
+ return;
+ } else if (DriverArgs.hasArg(options::OPT_v)) {
+ llvm::errs() << "ignoring nonexistent directory \"" << InstallBin
+ << "\"\n";
+ }
+ }
+
// Otherwise, check for (2)
llvm::SmallString<128> SysrootUsr = Sysroot;
llvm::sys::path::append(SysrootUsr, "usr", "include", "c++", "v1");
>From b2f287c6a53697ac9bdce3eaa1ce55d345d734b1 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Fri, 10 Nov 2023 12:05:50 +0200
Subject: [PATCH 02/13] Darwin.cpp: update comments
---
clang/lib/Driver/ToolChains/Darwin.cpp | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index de55307385966cf..e6bcf0227d7dc65 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2470,14 +2470,19 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
switch (GetCXXStdlibType(DriverArgs)) {
case ToolChain::CST_Libcxx: {
- // On Darwin, libc++ can be installed in one of the following two places:
+ // On Darwin, libc++ can be installed in one of the following places:
// 1. Alongside the compiler in <install>/include/c++/v1
- // 2. In a SDK (or a custom sysroot) in <sysroot>/usr/include/c++/v1
+ // 2. Alongside the compiler in <clang-executable-location>/../include/c++/v1
+ // 3. In a SDK (or a custom sysroot) in <sysroot>/usr/include/c++/v1
//
- // The precendence of paths is as listed above, i.e. we take the first path
- // that exists. Also note that we never include libc++ twice -- we take the
- // first path that exists and don't send the other paths to CC1 (otherwise
+ // The precedence of paths is as listed above, i.e. we take the first path
+ // that exists. Note that we never include libc++ twice -- we take the first
+ // path that exists and don't send the other paths to CC1 (otherwise
// include_next could break).
+ //
+ // Also note that in most cases, (1) and (2) are exactly the same path.
+ // Those two paths will differ only when the `clang` program being run
+ // is actually a symlink to the real executable.
// Check for (1)
// Get from '<install>/bin' to '<install>/include/c++/v1'.
@@ -2494,7 +2499,7 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
<< "\"\n";
}
- // Check for the folder where the executable is located, if different.
+ // (2) Check for the folder where the executable is located, if different.
if (getDriver().getInstalledDir() != getDriver().Dir) {
InstallBin = llvm::StringRef(getDriver().Dir.c_str());
llvm::sys::path::append(InstallBin, "..", "include", "c++", "v1");
>From 31b7482ffabda61c005a4cd90a07bcfec65c232e Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Fri, 10 Nov 2023 12:08:51 +0200
Subject: [PATCH 03/13] Darwin.cpp: construct StringRef from string
---
clang/lib/Driver/ToolChains/Darwin.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index e6bcf0227d7dc65..d89b6d60f1852e4 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2501,7 +2501,7 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
// (2) Check for the folder where the executable is located, if different.
if (getDriver().getInstalledDir() != getDriver().Dir) {
- InstallBin = llvm::StringRef(getDriver().Dir.c_str());
+ InstallBin = llvm::StringRef(getDriver().Dir);
llvm::sys::path::append(InstallBin, "..", "include", "c++", "v1");
if (getVFS().exists(InstallBin)) {
addSystemInclude(DriverArgs, CC1Args, InstallBin);
>From 6db76fecae69764ad6f66e2b30e3b093d9a91f40 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Fri, 10 Nov 2023 12:39:31 +0200
Subject: [PATCH 04/13] Darwin.cpp: reformat comments
---
clang/lib/Driver/ToolChains/Darwin.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index d89b6d60f1852e4..1f61bb02c6ae226 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2471,8 +2471,8 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
switch (GetCXXStdlibType(DriverArgs)) {
case ToolChain::CST_Libcxx: {
// On Darwin, libc++ can be installed in one of the following places:
- // 1. Alongside the compiler in <install>/include/c++/v1
- // 2. Alongside the compiler in <clang-executable-location>/../include/c++/v1
+ // 1. Alongside the compiler in <install>/include/c++/v1
+ // 2. Alongside the compiler in <clang-executable-folder>/../include/c++/v1
// 3. In a SDK (or a custom sysroot) in <sysroot>/usr/include/c++/v1
//
// The precedence of paths is as listed above, i.e. we take the first path
@@ -2512,7 +2512,7 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
}
}
- // Otherwise, check for (2)
+ // Otherwise, check for (3)
llvm::SmallString<128> SysrootUsr = Sysroot;
llvm::sys::path::append(SysrootUsr, "usr", "include", "c++", "v1");
if (getVFS().exists(SysrootUsr)) {
>From 04b69133a1482c17e927306c1a77075d51d87315 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Sun, 12 Nov 2023 23:51:30 +0200
Subject: [PATCH 05/13] add test
---
clang/test/Driver/darwin-header-search-libcxx.cpp | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/clang/test/Driver/darwin-header-search-libcxx.cpp b/clang/test/Driver/darwin-header-search-libcxx.cpp
index cc8ec9ceb89b3ab..e31c6ee182e65d0 100644
--- a/clang/test/Driver/darwin-header-search-libcxx.cpp
+++ b/clang/test/Driver/darwin-header-search-libcxx.cpp
@@ -172,3 +172,14 @@
// RUN: --check-prefix=CHECK-LIBCXX-STDLIB-UNSPECIFIED %s
// CHECK-LIBCXX-STDLIB-UNSPECIFIED: "-cc1"
// CHECK-LIBCXX-STDLIB-UNSPECIFIED: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
+
+// Make sure the target folder has no toolchain structure.
+// RUN: rm -rf %t/xpacks
+// RUN: mkdir -pv %t/xpacks/.bin
+// RUN: ln -s %clang++ %t/xpacks/.bin/clang++
+
+// RUN: %t/xpacks/.bin/clang++ -### %s -fsyntax-only 2>&1 \
+// RUN: --target=x86_64-apple-darwin \
+// RUN: | FileCheck -DRESOURCE=%S/Inputs/resource_dir \
+// RUN: --check-prefix=CHECK-SAME %s
+// CHECK-SAME: "-internal-isystem" "[[RESOURCE]]/bin/../include/c++/v1"
>From fc1108ed4d721f7070152cde131abccf46cf3b41 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Mon, 13 Nov 2023 00:37:51 +0200
Subject: [PATCH 06/13] fix test
---
clang/test/Driver/darwin-header-search-libcxx.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/clang/test/Driver/darwin-header-search-libcxx.cpp b/clang/test/Driver/darwin-header-search-libcxx.cpp
index e31c6ee182e65d0..8b63362a96ded99 100644
--- a/clang/test/Driver/darwin-header-search-libcxx.cpp
+++ b/clang/test/Driver/darwin-header-search-libcxx.cpp
@@ -176,10 +176,11 @@
// Make sure the target folder has no toolchain structure.
// RUN: rm -rf %t/xpacks
// RUN: mkdir -pv %t/xpacks/.bin
-// RUN: ln -s %clang++ %t/xpacks/.bin/clang++
+// RUN: ln -s %clang_cpp %t/xpacks/.bin/clang++
// RUN: %t/xpacks/.bin/clang++ -### %s -fsyntax-only 2>&1 \
// RUN: --target=x86_64-apple-darwin \
// RUN: | FileCheck -DRESOURCE=%S/Inputs/resource_dir \
-// RUN: --check-prefix=CHECK-SAME %s
-// CHECK-SAME: "-internal-isystem" "[[RESOURCE]]/bin/../include/c++/v1"
+// RUN: --check-prefix=CHECK-LIBCXX-TOOLCHAIN-3 %s
+// CHECK-LIBCXX-TOOLCHAIN-3: "-internal-isystem" "{{.+}}/bin/../include/c++/v1"
+// CHECK-LIBCXX-TOOLCHAIN-3-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
>From e62ac5e574ea58a3a30aa9bbf8fb843f59a9c18b Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Mon, 13 Nov 2023 00:54:03 +0200
Subject: [PATCH 07/13] fix test
---
clang/test/Driver/darwin-header-search-libcxx.cpp | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/clang/test/Driver/darwin-header-search-libcxx.cpp b/clang/test/Driver/darwin-header-search-libcxx.cpp
index 8b63362a96ded99..1676e5a660046c3 100644
--- a/clang/test/Driver/darwin-header-search-libcxx.cpp
+++ b/clang/test/Driver/darwin-header-search-libcxx.cpp
@@ -180,7 +180,6 @@
// RUN: %t/xpacks/.bin/clang++ -### %s -fsyntax-only 2>&1 \
// RUN: --target=x86_64-apple-darwin \
-// RUN: | FileCheck -DRESOURCE=%S/Inputs/resource_dir \
-// RUN: --check-prefix=CHECK-LIBCXX-TOOLCHAIN-3 %s
-// CHECK-LIBCXX-TOOLCHAIN-3: "-internal-isystem" "{{.+}}/bin/../include/c++/v1"
-// CHECK-LIBCXX-TOOLCHAIN-3-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
+// RUN: | FileCheck --check-prefix=CHECK-TOOLCHAIN-INCLUDE-CXX-V1 %s
+// CHECK-TOOLCHAIN-INCLUDE-CXX-V1: "-internal-isystem" "{{.*}}/bin/../include/c++/v1"
+// CHECK-TOOLCHAIN-INCLUDE-CXX-V1-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
>From e12726fa9c19143244899fcd814eced6edaf8e6d Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Mon, 13 Nov 2023 01:06:37 +0200
Subject: [PATCH 08/13] fix test
---
clang/test/Driver/darwin-header-search-libcxx.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/Driver/darwin-header-search-libcxx.cpp b/clang/test/Driver/darwin-header-search-libcxx.cpp
index 1676e5a660046c3..ce4f2a45f5cf526 100644
--- a/clang/test/Driver/darwin-header-search-libcxx.cpp
+++ b/clang/test/Driver/darwin-header-search-libcxx.cpp
@@ -182,4 +182,4 @@
// RUN: --target=x86_64-apple-darwin \
// RUN: | FileCheck --check-prefix=CHECK-TOOLCHAIN-INCLUDE-CXX-V1 %s
// CHECK-TOOLCHAIN-INCLUDE-CXX-V1: "-internal-isystem" "{{.*}}/bin/../include/c++/v1"
-// CHECK-TOOLCHAIN-INCLUDE-CXX-V1-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
+// CHECK-TOOLCHAIN-INCLUDE-CXX-V1-NOT: "-internal-isystem" "{{.*}}/SDKs/MacOSX.sdk/usr/include/c++/v1"
>From ec4b0586810301a39ec1fcb069b28aae84bca3e5 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Mon, 13 Nov 2023 01:22:08 +0200
Subject: [PATCH 09/13] fix test
---
clang/test/Driver/darwin-header-search-libcxx.cpp | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/test/Driver/darwin-header-search-libcxx.cpp b/clang/test/Driver/darwin-header-search-libcxx.cpp
index ce4f2a45f5cf526..ef62ff1f35a78b8 100644
--- a/clang/test/Driver/darwin-header-search-libcxx.cpp
+++ b/clang/test/Driver/darwin-header-search-libcxx.cpp
@@ -180,6 +180,8 @@
// RUN: %t/xpacks/.bin/clang++ -### %s -fsyntax-only 2>&1 \
// RUN: --target=x86_64-apple-darwin \
-// RUN: | FileCheck --check-prefix=CHECK-TOOLCHAIN-INCLUDE-CXX-V1 %s
+// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
+// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
+// RUN: --check-prefix=CHECK-TOOLCHAIN-INCLUDE-CXX-V1 %s
// CHECK-TOOLCHAIN-INCLUDE-CXX-V1: "-internal-isystem" "{{.*}}/bin/../include/c++/v1"
-// CHECK-TOOLCHAIN-INCLUDE-CXX-V1-NOT: "-internal-isystem" "{{.*}}/SDKs/MacOSX.sdk/usr/include/c++/v1"
+// CHECK-TOOLCHAIN-INCLUDE-CXX-V1-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
>From 8b5eb524c9dd9dab7032f1b6b1e44efcfe883fb0 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Mon, 13 Nov 2023 09:13:56 +0200
Subject: [PATCH 10/13] fix test
---
clang/test/Driver/darwin-header-search-libcxx.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/test/Driver/darwin-header-search-libcxx.cpp b/clang/test/Driver/darwin-header-search-libcxx.cpp
index ef62ff1f35a78b8..27567499a7b9335 100644
--- a/clang/test/Driver/darwin-header-search-libcxx.cpp
+++ b/clang/test/Driver/darwin-header-search-libcxx.cpp
@@ -176,9 +176,9 @@
// Make sure the target folder has no toolchain structure.
// RUN: rm -rf %t/xpacks
// RUN: mkdir -pv %t/xpacks/.bin
-// RUN: ln -s %clang_cpp %t/xpacks/.bin/clang++
+// RUN: ln -s %clang %t/xpacks/.bin/clang
-// RUN: %t/xpacks/.bin/clang++ -### %s -fsyntax-only 2>&1 \
+// RUN: %t/xpacks/.bin/clang -### %s -fsyntax-only 2>&1 \
// RUN: --target=x86_64-apple-darwin \
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
>From 7da33d4914d31753b82a5b08a4999c4a12e61219 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Mon, 13 Nov 2023 10:29:25 +0200
Subject: [PATCH 11/13] test -v
---
clang/test/Driver/darwin-header-search-libcxx.cpp | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/clang/test/Driver/darwin-header-search-libcxx.cpp b/clang/test/Driver/darwin-header-search-libcxx.cpp
index 27567499a7b9335..84a4d9e023b175f 100644
--- a/clang/test/Driver/darwin-header-search-libcxx.cpp
+++ b/clang/test/Driver/darwin-header-search-libcxx.cpp
@@ -178,6 +178,14 @@
// RUN: mkdir -pv %t/xpacks/.bin
// RUN: ln -s %clang %t/xpacks/.bin/clang
+// RUN: %t/xpacks/.bin/clang -### %s -fsyntax-only 2>&1 \
+// RUN: --target=x86_64-apple-darwin \
+// RUN: -v
+
+// RUN: %t/xpacks/.bin/clang -### %s -fsyntax-only 2>&1 \
+// RUN: --target=x86_64-apple-darwin \
+// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 -v
+
// RUN: %t/xpacks/.bin/clang -### %s -fsyntax-only 2>&1 \
// RUN: --target=x86_64-apple-darwin \
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
>From 231bb4916be2a865e435b9e0a1bb0a3bae98b4c9 Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Mon, 13 Nov 2023 11:44:00 +0200
Subject: [PATCH 12/13] more debug
---
clang/lib/Driver/ToolChains/Darwin.cpp | 2 ++
clang/test/Driver/darwin-header-search-libcxx.cpp | 4 ++++
2 files changed, 6 insertions(+)
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index 1f61bb02c6ae226..ec75f5a6cd19712 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -2499,6 +2499,8 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
<< "\"\n";
}
+ llvm::outs() << "DEBUG: getInstalledDir() " << getDriver().getInstalledDir() << "\"\n";
+ llvm::outs() << "DEBUG: Dir " << getDriver().Dir << "\"\n";
// (2) Check for the folder where the executable is located, if different.
if (getDriver().getInstalledDir() != getDriver().Dir) {
InstallBin = llvm::StringRef(getDriver().Dir);
diff --git a/clang/test/Driver/darwin-header-search-libcxx.cpp b/clang/test/Driver/darwin-header-search-libcxx.cpp
index 84a4d9e023b175f..d92bb93cb8f3cbd 100644
--- a/clang/test/Driver/darwin-header-search-libcxx.cpp
+++ b/clang/test/Driver/darwin-header-search-libcxx.cpp
@@ -178,6 +178,10 @@
// RUN: mkdir -pv %t/xpacks/.bin
// RUN: ln -s %clang %t/xpacks/.bin/clang
+// RUN: %clang -### %s -fsyntax-only 2>&1 \
+// RUN: --target=x86_64-apple-darwin \
+// RUN: -v
+
// RUN: %t/xpacks/.bin/clang -### %s -fsyntax-only 2>&1 \
// RUN: --target=x86_64-apple-darwin \
// RUN: -v
>From ea5c34c42ac6bd364795cb7cc2a16828b3e2e53a Mon Sep 17 00:00:00 2001
From: Liviu Ionescu <ilg at livius.net>
Date: Mon, 13 Nov 2023 15:36:11 +0200
Subject: [PATCH 13/13] test tweaks
---
clang/test/Driver/darwin-header-search-libcxx.cpp | 10 ++--------
1 file changed, 2 insertions(+), 8 deletions(-)
diff --git a/clang/test/Driver/darwin-header-search-libcxx.cpp b/clang/test/Driver/darwin-header-search-libcxx.cpp
index d92bb93cb8f3cbd..43377dd9612ecd3 100644
--- a/clang/test/Driver/darwin-header-search-libcxx.cpp
+++ b/clang/test/Driver/darwin-header-search-libcxx.cpp
@@ -178,20 +178,14 @@
// RUN: mkdir -pv %t/xpacks/.bin
// RUN: ln -s %clang %t/xpacks/.bin/clang
-// RUN: %clang -### %s -fsyntax-only 2>&1 \
-// RUN: --target=x86_64-apple-darwin \
-// RUN: -v
-
-// RUN: %t/xpacks/.bin/clang -### %s -fsyntax-only 2>&1 \
-// RUN: --target=x86_64-apple-darwin \
-// RUN: -v
-
// RUN: %t/xpacks/.bin/clang -### %s -fsyntax-only 2>&1 \
// RUN: --target=x86_64-apple-darwin \
+// RUN: -stdlib=libc++ \
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 -v
// RUN: %t/xpacks/.bin/clang -### %s -fsyntax-only 2>&1 \
// RUN: --target=x86_64-apple-darwin \
+// RUN: -stdlib=libc++ \
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
// RUN: --check-prefix=CHECK-TOOLCHAIN-INCLUDE-CXX-V1 %s
More information about the cfe-commits
mailing list