This is an archive of the discontinued LLVM Phabricator instance.

[driver] Conditionally include installed libc++ headers for Android
ClosedPublic

Authored by smeenai on Aug 31 2023, 9:17 AM.

Details

Summary

https://reviews.llvm.org/D71154 prevented Clang from search for libc++
headers installed alongside the driver when targeting Android. The
motivation was the NDK's use of a different libc++ inline namespace
(__ndk1 instead of the standard __1), which made regular libc++
headers incompatible with the NDK's libc++ library.

Since then, libc++ has gained the ability to install its __config_site
header (which controls the inline namespace, among other things) to a
per-target include directory, which enables per-target customizations.
If this directory is present, the user has expressly built libc++ for
Android, and we should use those headers.

The motivation is that, with the current setup, if a user builds their
own libc++ for Android, they'll use the library they built themselves
but the NDK's headers instead of their own, which is surprising at best
and can cause all sorts of problems (e.g. if you built your own libc++
with a different ABI configuration). It's important to match the headers
and libraries in that scenario, and checking for an Android per-target
include directory lets us do so without regressing the original scenario
which https://reviews.llvm.org/D71154 was addressing.

While I'm here, switch to using sys::path::append instead of slashes
directly, to get system path separators on Windows, which is consistent
with how library paths are constructed (and that consistency will be
important in a follow-up, where we use a common search function for the
include and library path construction).

(As an aside, one of the motivations for https://reviews.llvm.org/D71154
was to support targeting both Android and Apple platforms, which
expected libc++ headers to be provided by the toolcain at the time.
Apple has since switched to including libc++ headers in the platform SDK
instead of in the toolchain, so that specific motivation no longer
applies either.)

Diff Detail

Event Timeline

smeenai created this revision.Aug 31 2023, 9:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 31 2023, 9:17 AM
Herald added a subscriber: danielkiss. · View Herald Transcript
smeenai requested review of this revision.Aug 31 2023, 9:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 31 2023, 9:17 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

This change looks like an improvement to me. The NDK currently puts the libc++ headers into the sysroot, but putting it in the usual toolchain include location seems better.

clang/lib/Driver/ToolChains/Gnu.cpp
3108

Will the Target here have an Android API level suffix?

smeenai added inline comments.Aug 31 2023, 1:22 PM
clang/lib/Driver/ToolChains/Gnu.cpp
3108

Yup, it will. D159293 and D158476 are the follow-ups to address that. The first one makes this call-site use the current fallback logic in the driver, and the second one extends that fallback logic to look for the next newest available API level if you don't have headers/libraries for the current API level.

glandium resigned from this revision.Aug 31 2023, 3:09 PM

This + D159293 works great for me. Thanks a bunch. (I'm not a reviewer, though)

smeenai updated this revision to Diff 555246.Aug 31 2023, 9:24 PM

Rebase to avoid pre-existing CI failure

This change looks like an improvement to me. The NDK currently puts the libc++ headers into the sysroot, but putting it in the usual toolchain include location seems better.

Are you comfortable accepting this, or should I ping other reviewers? :)

Ping.

I asked a couple of other people at Google to take a look at the patches. Someone should get to it next week I think?

Ping.

I asked a couple of other people at Google to take a look at the patches. Someone should get to it next week I think?

Thank you!

phosek accepted this revision.Sep 18 2023, 1:15 AM

LGTM

This revision is now accepted and ready to land.Sep 18 2023, 1:15 AM
clang/test/Driver/android-ndk-standalone.cpp