This is an archive of the discontinued LLVM Phabricator instance.

libclang: Pass Clang install directory to driver via argv[0].
ClosedPublic

Authored by pcc on Mar 20 2023, 8:14 PM.

Details

Summary

Various driver features, such as the sysroot path detection for Android
targets, rely on being able to find the Clang install directory (look
for callers of getDriver().getInstalledDir()). However, the install
directory isn't currently being plumbed through to the driver, which is
conventionally done via the argv[0] passed to the Driver constructor.

It looks like D14695 attempted to fix this by adding another API that
allows specifying the argv[0]. However, rather than requiring every
user of libclang to switch to this API for correct behavior, let's have
the other existing APIs work by default, by using the existing logic in
libclang for finding the install directory.

Diff Detail

Event Timeline

pcc created this revision.Mar 20 2023, 8:14 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 20 2023, 8:14 PM
pcc requested review of this revision.Mar 20 2023, 8:14 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 20 2023, 8:14 PM
aaron.ballman accepted this revision.Mar 21 2023, 11:08 AM

LGTM with a nit and a question, but can you add a release note when you land to let users know about the changes?

clang/tools/libclang/CIndex.cpp
4017–4019

Just for visual separation.

4022

I suspect this doesn't matter *too* much, but... on Windows, wouldn't this be clang.exe instead? (This isn't new to your patch, so mostly wondering if there's a separate issue we should track.)

This revision is now accepted and ready to land.Mar 21 2023, 11:08 AM
pcc added inline comments.Mar 22 2023, 3:00 PM
clang/tools/libclang/CIndex.cpp
4022

I don't think it matters; the Driver class strips off the bin/clang part before storing it:
https://github.com/llvm/llvm-project/blob/4d18d97b594ccaa3cbd79beb4afef45e4156dc8d/clang/lib/Driver/Driver.cpp#L211

Aside from that, the only user I could find that's reachable from this function is a rather obscure codegen feature:
https://github.com/llvm/llvm-project/blob/fe27495be2040007c7b20844a9371b06156ab405/clang/lib/Frontend/CompilerInvocation.cpp#L4464
and this API doesn't do codegen.

This revision was automatically updated to reflect the committed changes.

FYI this commit breaks clang/test/Index/index-file.cu for me:

warning: CUDA version 11.8 is only partially supported [-Wunknown-cuda-version]
warning: CUDA version 11.8 is only partially supported [-Wunknown-cuda-version]
/home/jhahnfel/LLVM/src/clang/test/Index/index-file.cu:8:20: error: CHECK-HOST-NOT: excluded string found in input
// CHECK-HOST-NOT: macro definition=__CUDA_ARCH__
                   ^
<stdin>:6218:48: note: found here
// CHECK: __clang_cuda_runtime_wrapper.h:70:9: macro definition=__CUDA_ARCH__ Extent=[70:9 - 70:27]
                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It could be related to the warning about my locally installed CUDA version, but __CUDA_ARCH__ should really not be defined when compiling for the host. If you have any idea what's going on here, please let me know - I'm a bit puzzled right now...

FYI this commit breaks clang/test/Index/index-file.cu for me:

warning: CUDA version 11.8 is only partially supported [-Wunknown-cuda-version]
warning: CUDA version 11.8 is only partially supported [-Wunknown-cuda-version]
/home/jhahnfel/LLVM/src/clang/test/Index/index-file.cu:8:20: error: CHECK-HOST-NOT: excluded string found in input
// CHECK-HOST-NOT: macro definition=__CUDA_ARCH__
                   ^
<stdin>:6218:48: note: found here
// CHECK: __clang_cuda_runtime_wrapper.h:70:9: macro definition=__CUDA_ARCH__ Extent=[70:9 - 70:27]
                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It could be related to the warning about my locally installed CUDA version, but __CUDA_ARCH__ should really not be defined when compiling for the host. If you have any idea what's going on here, please let me know - I'm a bit puzzled right now...

This is confirmed on at least this build bot: https://lab.llvm.org/buildbot/#/builders/91/builds/15271

I'll investigate locally and revert if it's not something I can fix forward.

FYI this commit breaks clang/test/Index/index-file.cu for me:

warning: CUDA version 11.8 is only partially supported [-Wunknown-cuda-version]
warning: CUDA version 11.8 is only partially supported [-Wunknown-cuda-version]
/home/jhahnfel/LLVM/src/clang/test/Index/index-file.cu:8:20: error: CHECK-HOST-NOT: excluded string found in input
// CHECK-HOST-NOT: macro definition=__CUDA_ARCH__
                   ^
<stdin>:6218:48: note: found here
// CHECK: __clang_cuda_runtime_wrapper.h:70:9: macro definition=__CUDA_ARCH__ Extent=[70:9 - 70:27]
                                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It could be related to the warning about my locally installed CUDA version, but __CUDA_ARCH__ should really not be defined when compiling for the host. If you have any idea what's going on here, please let me know - I'm a bit puzzled right now...

This is confirmed on at least this build bot: https://lab.llvm.org/buildbot/#/builders/91/builds/15271

I'll investigate locally and revert if it's not something I can fix forward.

The issue doesn't reproduce for me locally on my Windows machine, so it may be something specific to the VE setup. It looks like the macro is being printed on the host side when it's expected to not be present, which doesn't seem like an expected outcome from this change, so I reverted in 18d56880a89ad7d58f8543d148facebd079cef19 to hopefully bring the bot back to green (if the bot stays red for the same reason, I'll reapply the changes).

The issue doesn't reproduce for me locally on my Windows machine, so it may be something specific to the VE setup.

FWIW I'm not doing anything specific for VE, just

set(CMAKE_C_COMPILER "clang" CACHE STRING "")
set(CMAKE_CXX_COMPILER "clang++" CACHE STRING "")
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "")
set(LLVM_APPEND_VC_REV OFF CACHE STRING "")
set(LLVM_ENABLE_ASSERTIONS ON CACHE BOOL "")
set(LLVM_ENABLE_PROJECTS "clang" CACHE STRING "")

set(LLVM_ENABLE_LLD ON CACHE BOOL "")
set(LLVM_LINK_LLVM_DYLIB ON CACHE BOOL "")

in the file I load as the initial-cache (cmake -C).

The issue doesn't reproduce for me locally on my Windows machine, so it may be something specific to the VE setup.

FWIW I'm not doing anything specific for VE, just

set(CMAKE_C_COMPILER "clang" CACHE STRING "")
set(CMAKE_CXX_COMPILER "clang++" CACHE STRING "")
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "")
set(LLVM_APPEND_VC_REV OFF CACHE STRING "")
set(LLVM_ENABLE_ASSERTIONS ON CACHE BOOL "")
set(LLVM_ENABLE_PROJECTS "clang" CACHE STRING "")

set(LLVM_ENABLE_LLD ON CACHE BOOL "")
set(LLVM_LINK_LLVM_DYLIB ON CACHE BOOL "")

in the file I load as the initial-cache (cmake -C).

Interesting, and I was testing with llvm-lit.py. The revert did get the bot back to green in https://lab.llvm.org/buildbot/#/builders/91/builds/15275