This is an archive of the discontinued LLVM Phabricator instance.

[clang][driver] Linking to just-built libc++.dylib when bootstrapping libc++ with clang
AcceptedPublic

Authored by fahadnayyar on Apr 13 2023, 1:06 PM.

Details

Summary

When libc++ is bootstrapped with clang using the cmake options
-DLLVM_ENABLE_PROJECTS="clang;llvm;lldb" and
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" then the resulting clang uses
the just-built libcxx headers from <build-directory>/bin/../include/c++/v1
. So the clang in this case should also use the just-built libc++.dylib
from <build-directory>/bin/../lib instead of using the system's libc++
libraries.

rdar://107060541

Diff Detail

Event Timeline

fahadnayyar created this revision.Apr 13 2023, 1:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2023, 1:06 PM
fahadnayyar requested review of this revision.Apr 13 2023, 1:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2023, 1:06 PM

Thanks for working on this! The idea looks good to me, but I am not familiar with this part of the codebase, so I'll defer the review to others

fahadnayyar edited the summary of this revision. (Show Details)Apr 14 2023, 10:45 AM
fahadnayyar added a reviewer: var-const.
ldionne requested changes to this revision.Apr 14 2023, 10:45 AM

Thanks for working on this! This generally LGTM, with a few comments. You should be able to add tests in a way similar to clang/test/Driver/darwin-header-search-libcxx.cpp. The idea is basically to create a dummy sysroot that contains a libc++.dylib file in the right location and trick the compiler into adding -L as you want.

clang/lib/Driver/ToolChains/Darwin.cpp
423
428

Otherwise you are forming the path <install>/bin/../lib/../lib/libc++.dylib, which is correct but weird.

This revision now requires changes to proceed.Apr 14 2023, 10:45 AM
ldionne added inline comments.Apr 14 2023, 10:52 AM
clang/lib/Driver/ToolChains/Darwin.cpp
420–421

I would reword this to:

// If the toolchain contains libc++.dylib (in <install>/bin/../lib/libc++.dylib), use that instead of the sysroot-provided one. This matches what we do for determining which libc++ headers to use.
435

Where do we set the usual <sysroot>/usr/lib search path? I think it might make sense to move the code you added to that place.

Added test case. Now checking the existence of libc++ headers in the toolchain when including the libc++.dylib form toolchain and vice versa. Also checking the absence of -nostdinc, -nostdinc++ or -nostdlib
arguments before linking with libc++.dylib from the toolchain.

fahadnayyar marked 3 inline comments as done.Apr 16 2023, 11:10 AM
ldionne added inline comments.
clang/lib/Driver/ToolChains/Darwin.cpp
435

What I mean: there's a place where we must be adding -L <sysroot>/usr/lib in the code, and it isn't in darwin::Linker::AddLinkArgs. Where is it? Would it make sense to move the code you added to that place instead?

438

The more I look at this, the more I think it seems kind of weird to me that a linker argument would be impacted by -nostdinc & friends, which control header search paths. IMO we should use the simplest behavior and only check for libc++.dylib here, and not check for libc++.dylib when we determine the header search paths.

@arphaman Do you have an opinion?

fahadnayyar added inline comments.Apr 17 2023, 7:49 AM
clang/lib/Driver/ToolChains/Darwin.cpp
435

<sysroot>/usr/local/include is added here in `DarwinClang::AddClangSystemIncludeArgs``: https://github.com/llvm/llvm-project/blob/main/clang/lib/Driver/ToolChains/Darwin.cpp#L2346 and <sysroot>/usr/include is added here: https://github.com/llvm/llvm-project/blob/main/clang/lib/Driver/ToolChains/Darwin.cpp#L2374 .. But since we want to add a new argument to the linker invocation, I feel `darwin::Linker::AddLinkArgs`` is the best place. But let me know if you think otherwise.

Fixed regression error in clang-tidy test.

Herald added a project: Restricted Project. · View Herald TranscriptApr 17 2023, 2:11 PM

Fixing some more regression test errors.

arphaman added inline comments.May 22 2023, 9:35 AM
clang/lib/Driver/ToolChains/Darwin.cpp
438

That makes sense to me. I'm not sure why specifically do we need to check if the dylib exists when determining the header search path. Is there a specific reason we couldn't mix the local headers and the system's dylib in that case?

ldionne added inline comments.May 25 2023, 1:30 PM
clang/lib/Driver/ToolChains/Darwin.cpp
438

We definitely don't want to mix the local headers and the system dylib. They can be configured differently (e.g. different ABI flags amongst other things). However it shouldn't really matter since when we build libc++, we build both the dylib and the headers so either both should be picked up or neither should be. I wouldn't bother checking. If we really wanted to check, we could actually error-out if one (but not both) are there, since that would point out to a mismatch.

Passing -L <toolchain-install>/bin/../lib/ unconditionally to the linker.

Rebasing to latest main.

ldionne requested changes to this revision.Jun 1 2023, 8:33 AM
ldionne added inline comments.
clang/test/Driver/darwin-header-search-libcxx.cpp
119

Is this change really needed anymore? Why?

clang/test/Driver/darwin-link-libcxx-from-toolchain.cpp
11

I don't see CHECK-TOOLCHAIN-LIBCXX-LINKING-2 defined anywhere?

13

Some of these test cases and comments don't make sense for this test -- you copy-pasted the test for header search paths and that's fine, but please go through it to make sure you make the necessary edits.

This revision now requires changes to proceed.Jun 1 2023, 8:33 AM

Simplifying the test case.

fahadnayyar marked 2 inline comments as done.Jun 1 2023, 11:07 AM
fahadnayyar added inline comments.
clang/test/Driver/darwin-header-search-libcxx.cpp
119

This is unrelated small bug in darwin-header-search-libcxx.cpp. I thought maybe we fix that small issue also in this patch. What you think?

ldionne added inline comments.Jun 1 2023, 11:35 AM
clang/test/Driver/darwin-header-search-libcxx.cpp
95
105
119

Ah, I see. I think I understand the bug. Yeah I think it makes sense to fix it here but I added a few more fixes in comments.

133
ldionne accepted this revision.Jun 1 2023, 11:36 AM

This LGTM w/ comments applied. Thanks!

This revision is now accepted and ready to land.Jun 1 2023, 11:36 AM
ldionne added inline comments.Jun 1 2023, 1:22 PM
clang/test/Driver/darwin-header-search-libcxx.cpp
105

Nevermind.

133

Nevermind.

Edits in comments.

Gentle ping. Are we waiting on anything to ship this?