Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Driver] Fix detection of libc++ with empty sysroot. #66947

Merged
merged 3 commits into from
Sep 22, 2023

Conversation

sam-mccall
Copy link
Collaborator

b1e3cd1 dropped the leading slash here,
presumably unintentionally.

(I don't understand Driver well enough to know how/where this is supposed
to be tested, but it broke clangd on any project that uses the
system-installed -stdlib=libc++ on a standard debian install)

b1e3cd1 dropped the leading slash here,
presumably unintentionally.

(I don't understand Driver well enough to know how/where this is supposed
to be tested, but it broke clangd on any project that uses the
system-installed -stdlib=libc++ on a standard debian install)
@sam-mccall sam-mccall requested a review from smeenai September 20, 2023 19:28
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Sep 20, 2023
@llvmbot
Copy link
Member

llvmbot commented Sep 20, 2023

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Changes

b1e3cd1 dropped the leading slash here,
presumably unintentionally.

(I don't understand Driver well enough to know how/where this is supposed
to be tested, but it broke clangd on any project that uses the
system-installed -stdlib=libc++ on a standard debian install)


Full diff: https://github.com/llvm/llvm-project/pull/66947.diff

1 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Gnu.cpp (+2-2)
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index e909549d20708fe..f2fd85021a777b4 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -3139,11 +3139,11 @@ Generic_GCC::addLibCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
   // not be found at ../include/c++ but it likely to be found at
   // one of the following two locations:
   SmallString<128> UsrLocalIncludeDir(SysRoot);
-  llvm::sys::path::append(UsrLocalIncludeDir, "usr", "local", "include");
+  llvm::sys::path::append(UsrLocalIncludeDir, "/usr", "local", "include");
   if (AddIncludePath(UsrLocalIncludeDir))
     return;
   SmallString<128> UsrIncludeDir(SysRoot);
-  llvm::sys::path::append(UsrIncludeDir, "usr", "include");
+  llvm::sys::path::append(UsrIncludeDir, "/usr", "include");
   if (AddIncludePath(UsrIncludeDir))
     return;
 }

@smeenai
Copy link
Collaborator

smeenai commented Sep 20, 2023

Sorry about the breakage.

The sys::path::append behavior here is surprising. I think it'd be better to change the computation of SysRoot in the function above (line 3102, which I can't comment because of GitHub, sigh) to default to llvm::sys::path::get_separator() if it's empty, to handle Windows slashes as well as be less confusing to read.

This is tricky to test because it requires no sysroot to be passed, but we might be able to do so with a VFS overlay. I'm okay adding the test afterwards though so we get the breakage fixed first, and I can also look into the test myself if you'd like.

@sam-mccall
Copy link
Collaborator Author

Thanks for the quick response!

The sys::path::append behavior here is surprising. I think it'd be better to change the computation of SysRoot in the function above (line 3102, which I can't comment because of GitHub, sigh) to default to llvm::sys::path::get_separator() if it's empty, to handle Windows slashes as well as be less confusing to read.

That sounds sensible to me in principle, but not trivial: presumably the overrides of Toolchain::getSysroot() should have a consistent contract. There are 6 overrides, where some explicitly return the empty string, and 25 callers some of which do things like computeSysRoot() + "/include/c++/" (RISCVToolchain).
I feel it's likely I'll break something on a platform I don't understand before getting back to a good state :-)

This is tricky to test because it requires no sysroot to be passed, but we might be able to do so with a VFS overlay. I'm okay adding the test afterwards though so we get the breakage fixed first, and I can also look into the test myself if you'd like.

I can try to add something to the unittest. Looks like in the current form I also need to change the lit test back to hard-coding / for this one separator.
(I'm out of time tonight but will pick it up tomorrow)

@smeenai
Copy link
Collaborator

smeenai commented Sep 20, 2023

Thanks for the quick response!

The sys::path::append behavior here is surprising. I think it'd be better to change the computation of SysRoot in the function above (line 3102, which I can't comment because of GitHub, sigh) to default to llvm::sys::path::get_separator() if it's empty, to handle Windows slashes as well as be less confusing to read.

That sounds sensible to me in principle, but not trivial: presumably the overrides of Toolchain::getSysroot() should have a consistent contract. There are 6 overrides, where some explicitly return the empty string, and 25 callers some of which do things like computeSysRoot() + "/include/c++/" (RISCVToolchain). I feel it's likely I'll break something on a platform I don't understand before getting back to a good state :-)

I just meant to change

std::string SysRoot = computeSysRoot();
to something like (untested):

std::string SysRoot = computeSysRoot();
if (SysRoot.empty())
  SysRoot = llvm::sys::path::get_separator();

It's a local fix, but all the code here is pretty inconsistent anyway, so I don't feel too bad about it.

@sam-mccall
Copy link
Collaborator Author

I just meant to change [...] It's a local fix, but all the code here is pretty inconsistent anyway, so I don't feel too bad about it.

Oops, of course. Done & I worked out the testing.

@smeenai
Copy link
Collaborator

smeenai commented Sep 22, 2023

LGTM, thanks!

@sam-mccall sam-mccall merged commit 7ca8c21 into llvm:main Sep 22, 2023
@vvereschaka
Copy link
Contributor

@sam-mccall ,
your 7ca8c21 commit breaks Clang-Unit :: Driver/./ClangDriverTests.exe/ToolChainTest/VFSGnuLibcxxPathNoSysroot (Clang-Unit::67) test on the windows toolchain builders

The problem remains for more than 3 days already. Would you fix it as soon as it possible or revert your changes?

@sam-mccall
Copy link
Collaborator Author

Sorry, there were no notifications, I guess the bot was already red.

I'm not really sure what to do other than disable the test on these bots - I don't know why it would fail on win/arm (it seems to pass on win/x86), and I don't have access to such a machine.
This behavior isn't particularly relevant on windows but is critical on linux. This isn't a regression, there was just previously no testing of it.

@sam-mccall
Copy link
Collaborator Author

sam-mccall commented Sep 27, 2023

... and as soon as I said that, I found something: looks like these are maybe building with DEFAULT_SYSROOT=C:/buildbot/.arm-ubuntu, which breaks the no-sysroot behavior we're testing here.

I'll see if I can override with --sysroot=

edit: hopefully good after 5aa3338

@asl
Copy link
Collaborator

asl commented Sep 28, 2023

@sam-mccall As far as I can see, the bot was green before this change: https://lab.llvm.org/buildbot/#/builders/119/builds/15229

And your commit was the only one in the set of 19 commits that were build that was relevant to this testcase.

Have you not received email from the buildbot?

@vvereschaka
Copy link
Contributor

edit: hopefully good after 5aa3338

the test has been passed locally on the builder with this commit. Thank you for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants