Skip to content

Commit d63e649

Browse files
committed
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.
1 parent 69dfebb commit d63e649

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pkgs/development/compilers/llvm/common/clang/default.nix

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ stdenv.mkDerivation (
158158
(lib.cmakeFeature "LLVM_TABLEGEN_EXE" "${buildLlvmTools.tblgen}/bin/llvm-tblgen")
159159
(lib.cmakeFeature "CLANG_TABLEGEN" "${buildLlvmTools.tblgen}/bin/clang-tblgen")
160160
]
161+
++ lib.optionals (lib.versionAtLeast release_version "21") [
162+
(lib.cmakeFeature "CLANG_RESOURCE_DIR" "${placeholder "lib"}/lib/clang/${lib.versions.major release_version}")
163+
]
161164
++ lib.optionals (lib.versionAtLeast release_version "17") [
162165
(lib.cmakeBool "LLVM_INCLUDE_TESTS" false)
163166
]
@@ -190,6 +193,11 @@ stdenv.mkDerivation (
190193
--replace-fail 'StringRef P = llvm::sys::path::parent_path(D.Dir);' 'StringRef P = "${lib.getLib libllvm}";'
191194
(cd tools && ln -s ../../clang-tools-extra extra)
192195
''
196+
+ lib.optionalString (lib.versionAtLeast release_version "21") ''
197+
# This patch needs to be applied to the parent of the clang directory.
198+
chmod -R +w ../cmake
199+
(cd .. && patch -p1 < ${./resource-dir.diff})
200+
''
193201
+ lib.optionalString stdenv.hostPlatform.isMusl ''
194202
sed -i -e 's/lgcc_s/lgcc_eh/' lib/Driver/ToolChains/*.cpp
195203
'';
@@ -211,7 +219,7 @@ stdenv.mkDerivation (
211219
mkdir -p $lib/lib/clang
212220
mv $lib/lib/17 $lib/lib/clang/17
213221
'')
214-
+ (lib.optionalString (lib.versionAtLeast release_version "19") ''
222+
+ (lib.optionalString ((lib.versionAtLeast release_version "19") && !(lib.versionAtLeast release_version "21")) ''
215223
mv $out/lib/clang $lib/lib/clang
216224
'')
217225
+ ''
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/cmake/Modules/GetClangResourceDir.cmake b/cmake/Modules/GetClangResourceDir.cmake
2+
index def5bd60424e..9fd585729b0f 100644
3+
--- a/cmake/Modules/GetClangResourceDir.cmake
4+
+++ b/cmake/Modules/GetClangResourceDir.cmake
5+
@@ -10,7 +10,7 @@ function(get_clang_resource_dir out_var)
6+
cmake_parse_arguments(ARG "" "PREFIX;SUBDIR" "" ${ARGN})
7+
8+
if(DEFINED CLANG_RESOURCE_DIR AND NOT CLANG_RESOURCE_DIR STREQUAL "")
9+
- set(ret_dir bin/${CLANG_RESOURCE_DIR})
10+
+ cmake_path(APPEND bin ${CLANG_RESOURCE_DIR} OUTPUT_VARIABLE ret_dir)
11+
else()
12+
if (NOT CLANG_VERSION_MAJOR)
13+
string(REGEX MATCH "^[0-9]+" CLANG_VERSION_MAJOR ${PACKAGE_VERSION})

0 commit comments

Comments
 (0)