Skip to content

cmake: Allow CLANG_RESOURCE_DIR to be absolute. #145996

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

Merged
merged 2 commits into from
Jun 27, 2025

Conversation

pcc
Copy link
Contributor

@pcc pcc commented Jun 26, 2025

Currently an absolute CLANG_RESOURCE_DIR is treated as being relative
to bin. Fix that by using cmake_path(APPEND) to append the path to bin.

The prepending of PREFIX is left as-is because callers passing PREFIX
are usually forming a path within the build directory and would not
want build products to be written to an absolute resource directory
that is likely to be an installation prefix. One exception is the caller
in lldb/cmake/modules/LLDBStandalone.cmake; for now it is not possible
to build LLDB with an absolute resource directory until the users are
disambiguated.

Created using spr 1.3.6-beta.1
@llvmbot llvmbot added the cmake Build system in general and CMake in particular label Jun 26, 2025
@pcc pcc requested a review from petrhosek June 27, 2025 00:09
pcc added a commit to pcc/nixpkgs that referenced this pull request Jun 27, 2025
When Clang is statically linked against other programs they are unable to
find the headers in Clang's resource directory. Typically the resource
directory is found by searching a path relative to argv[0] but this
would only really work for Clang itself due to each binary having a
separate prefix (and not in Nix because of the full resource directory
being split between multiple derivations and assembled in the wrapper).

Because users of Clang as a library typically only need include in order
for parsing to succeed, let's set that as the resource directory. The LLVM
patch to make this work was sent upstream in llvm/llvm-project#145996,
I intend to land it upstream and drop it from this PR.
@@ -10,7 +10,7 @@ function(get_clang_resource_dir out_var)
cmake_parse_arguments(ARG "" "PREFIX;SUBDIR" "" ${ARGN})

if(DEFINED CLANG_RESOURCE_DIR AND NOT CLANG_RESOURCE_DIR STREQUAL "")
set(ret_dir bin/${CLANG_RESOURCE_DIR})
cmake_path(APPEND bin ${CLANG_RESOURCE_DIR} OUTPUT_VARIABLE ret_dir)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmake_path(APPEND <path-var> [<input>...] [OUTPUT_VARIABLE <out-var>])
IIUC here bin should be replaced by a variable name:

Suggested change
cmake_path(APPEND bin ${CLANG_RESOURCE_DIR} OUTPUT_VARIABLE ret_dir)
set(ret_dir bin)
cmake_path(APPEND ret_dir ${CLANG_RESOURCE_DIR})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, fixed.

Created using spr 1.3.6-beta.1
@pcc pcc merged commit 71d4c9c into main Jun 27, 2025
19 of 20 checks passed
@pcc pcc deleted the users/pcc/spr/cmake-allow-clang_resource_dir-to-be-absolute branch June 27, 2025 23:04
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jun 27, 2025
Currently an absolute CLANG_RESOURCE_DIR is treated as being relative
to bin. Fix that by using cmake_path(APPEND) to append the path to bin.

The prepending of PREFIX is left as-is because callers passing PREFIX
are usually forming a path within the build directory and would not
want build products to be written to an absolute resource directory
that is likely to be an installation prefix. One exception is the caller
in lldb/cmake/modules/LLDBStandalone.cmake; for now it is not possible
to build LLDB with an absolute resource directory until the users are
disambiguated.

Reviewers: petrhosek

Reviewed By: petrhosek

Pull Request: llvm/llvm-project#145996
pcc added a commit to pcc/nixpkgs that referenced this pull request Jul 1, 2025
When Clang is statically linked against other programs they are unable to
find the headers in Clang's resource directory. Typically the resource
directory is found by searching a path relative to argv[0] but this
would only really work for Clang itself due to each binary having a
separate prefix (and not in Nix because of the full resource directory
being split between multiple derivations and assembled in the wrapper).

Because users of Clang as a library typically only need include in order
for parsing to succeed, let's set that as the resource directory. The LLVM
patch to make this work was sent upstream in llvm/llvm-project#145996,
I intend to land it upstream and drop it from this PR.
rlavaee pushed a commit to rlavaee/llvm-project that referenced this pull request Jul 1, 2025
Currently an absolute CLANG_RESOURCE_DIR is treated as being relative
to bin. Fix that by using cmake_path(APPEND) to append the path to bin.

The prepending of PREFIX is left as-is because callers passing PREFIX
are usually forming a path within the build directory and would not
want build products to be written to an absolute resource directory
that is likely to be an installation prefix. One exception is the caller
in lldb/cmake/modules/LLDBStandalone.cmake; for now it is not possible
to build LLDB with an absolute resource directory until the users are
disambiguated.

Reviewers: petrhosek

Reviewed By: petrhosek

Pull Request: llvm#145996
pcc added a commit that referenced this pull request Jul 2, 2025
After #145996 CLANG_RESOURCE_DIR can be an absolute path so we need to
handle it correctly in the driver.

llvm::sys::path::append does not append absolute paths in the way
that I expected (or consistent with other similar APIs such as C++17
std::filesystem::path::append or Python os.path.join); instead, it
effectively discards the leading / and appends the resulting relative path
(e.g. append(P, "/bar") with P = "/foo" sets P to "/foo/bar").

Many tests start failing if I try to align llvm::sys::path::append with
the other APIs because of callers that expect the existing behavior,
so for now let's add a special case here for absolute resource paths,
and document the behavior in Path.h.

Reviewers: MaskRay

Reviewed By: MaskRay

Pull Request: #146449
alyssais pushed a commit to NixOS/nixpkgs that referenced this pull request Jul 2, 2025
When Clang is statically linked against other programs they are unable to
find the headers in Clang's resource directory. Typically the resource
directory is found by searching a path relative to argv[0] but this
would only really work for Clang itself due to each binary having a
separate prefix (and not in Nix because of the full resource directory
being split between multiple derivations and assembled in the wrapper).

Because users of Clang as a library typically only need include in order
for parsing to succeed, let's set that as the resource directory. The LLVM
patch to make this work was sent upstream in llvm/llvm-project#145996,
I intend to land it upstream and drop it from this PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmake Build system in general and CMake in particular
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants