llvmPackages.clang: embed resource directory in Clang libraries

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.
This commit is contained in:
Peter Collingbourne 2025-06-26 15:54:58 -07:00 committed by Alyssa Ross
parent c4b9e31ffa
commit 8dc663ea18

View File

@ -158,6 +158,9 @@ stdenv.mkDerivation (
(lib.cmakeFeature "LLVM_TABLEGEN_EXE" "${buildLlvmTools.tblgen}/bin/llvm-tblgen")
(lib.cmakeFeature "CLANG_TABLEGEN" "${buildLlvmTools.tblgen}/bin/clang-tblgen")
]
++ lib.optionals (lib.versionAtLeast release_version "21") [
(lib.cmakeFeature "CLANG_RESOURCE_DIR" "${placeholder "lib"}/lib/clang/${lib.versions.major release_version}")
]
++ lib.optionals (lib.versionAtLeast release_version "17") [
(lib.cmakeBool "LLVM_INCLUDE_TESTS" false)
]
@ -211,9 +214,12 @@ stdenv.mkDerivation (
mkdir -p $lib/lib/clang
mv $lib/lib/17 $lib/lib/clang/17
'')
+ (lib.optionalString (lib.versionAtLeast release_version "19") ''
mv $out/lib/clang $lib/lib/clang
'')
+ (lib.optionalString
((lib.versionAtLeast release_version "19") && !(lib.versionAtLeast release_version "21"))
''
mv $out/lib/clang $lib/lib/clang
''
)
+ ''
# Move libclang to 'lib' output