1{ lowPrio, newScope, pkgs, lib, stdenv, cmake, ninja
2, preLibcCrossHeaders
3, libxml2, python3, fetchFromGitHub, substituteAll, overrideCC, wrapCCWith, wrapBintoolsWith
4, buildLlvmTools # tools, but from the previous stage, for cross
5, targetLlvmLibraries # libraries, but from the next stage, for cross
6, targetLlvm
7# This is the default binutils, but with *this* version of LLD rather
8# than the default LLVM verion's, if LLD is the choice. We use these for
9# the `useLLVM` bootstrapping below.
10, bootBintoolsNoLibc ?
11 if stdenv.targetPlatform.linker == "lld"
12 then null
13 else pkgs.bintoolsNoLibc
14, bootBintools ?
15 if stdenv.targetPlatform.linker == "lld"
16 then null
17 else pkgs.bintools
18, darwin
19# LLVM release information; specify one of these but not both:
20, gitRelease ? {
21 version = "19.0.0-git";
22 rev = "cebf77fb936a7270c7e3fa5c4a7e76216321d385";
23 rev-version = "19.0.0-unstable-2024-04-07";
24 sha256 = "sha256-616tscgsiFgHQcXW4KzK5srrudYizQFnJVM6K0qRf+I=";
25}
26 # i.e.:
27 # {
28 # version = /* i.e. "15.0.0" */;
29 # rev = /* commit SHA */;
30 # rev-version = /* human readable version; i.e. "15.0.0-unstable-2022-07-26" */;
31 # sha256 = /* checksum for this release, can omit if specifying your own `monorepoSrc` */;
32 # }
33, officialRelease ? null
34 # i.e.:
35 # {
36 # version = /* i.e. "15.0.0" */;
37 # candidate = /* optional; if specified, should be: "rcN" */
38 # sha256 = /* checksum for this release, can omit if specifying your own `monorepoSrc` */;
39 # }
40# By default, we'll try to fetch a release from `github:llvm/llvm-project`
41# corresponding to the `gitRelease` or `officialRelease` specified.
42#
43# You can provide your own LLVM source by specifying this arg but then it's up
44# to you to make sure that the LLVM repo given matches the release configuration
45# specified.
46, monorepoSrc ? null
47}:
48
49assert
50 lib.assertMsg
51 (lib.xor
52 (gitRelease != null)
53 (officialRelease != null))
54 ("must specify `gitRelease` or `officialRelease`" +
55 (lib.optionalString (gitRelease != null) " — not both"));
56let
57 monorepoSrc' = monorepoSrc;
58in let
59 inherit (import ../common/common-let.nix { inherit lib gitRelease officialRelease; }) releaseInfo;
60
61 inherit (releaseInfo) release_version version;
62
63 inherit (import ../common/common-let.nix { inherit lib fetchFromGitHub release_version gitRelease officialRelease monorepoSrc'; }) llvm_meta monorepoSrc;
64
65 tools = lib.makeExtensible (tools: let
66 callPackage = newScope (tools // { inherit stdenv cmake ninja libxml2 python3 release_version version monorepoSrc buildLlvmTools; });
67 major = lib.versions.major release_version;
68 mkExtraBuildCommands0 = cc: ''
69 rsrc="$out/resource-root"
70 mkdir "$rsrc"
71 ln -s "${cc.lib}/lib/clang/${major}/include" "$rsrc"
72 echo "-resource-dir=$rsrc" >> $out/nix-support/cc-cflags
73 '';
74 mkExtraBuildCommands = cc: mkExtraBuildCommands0 cc + ''
75 ln -s "${targetLlvmLibraries.compiler-rt.out}/lib" "$rsrc/lib"
76 ln -s "${targetLlvmLibraries.compiler-rt.out}/share" "$rsrc/share"
77 '';
78
79 bintoolsNoLibc' =
80 if bootBintoolsNoLibc == null
81 then tools.bintoolsNoLibc
82 else bootBintoolsNoLibc;
83 bintools' =
84 if bootBintools == null
85 then tools.bintools
86 else bootBintools;
87
88 in {
89
90 libllvm = callPackage ../common/llvm {
91 patches = [
92 ./llvm/gnu-install-dirs.patch
93
94 # Running the tests involves invoking binaries (like `opt`) that depend on
95 # the LLVM dylibs and reference them by absolute install path (i.e. their
96 # nix store path).
97 #
98 # Because we have not yet run the install phase (we're running these tests
99 # as part of `checkPhase` instead of `installCheckPhase`) these absolute
100 # paths do not exist yet; to work around this we point the loader (`ld` on
101 # unix, `dyld` on macOS) at the `lib` directory which will later become this
102 # package's `lib` output.
103 #
104 # Previously we would just set `LD_LIBRARY_PATH` to include the build `lib`
105 # dir but:
106 # - this doesn't generalize well to other platforms; `lit` doesn't forward
107 # `DYLD_LIBRARY_PATH` (macOS):
108 # + https://github.com/llvm/llvm-project/blob/0d89963df354ee309c15f67dc47c8ab3cb5d0fb2/llvm/utils/lit/lit/TestingConfig.py#L26
109 # - even if `lit` forwarded this env var, we actually cannot set
110 # `DYLD_LIBRARY_PATH` in the child processes `lit` launches because
111 # `DYLD_LIBRARY_PATH` (and `DYLD_FALLBACK_LIBRARY_PATH`) is cleared for
112 # "protected processes" (i.e. the python interpreter that runs `lit`):
113 # https://stackoverflow.com/a/35570229
114 # - other LLVM subprojects deal with this issue by having their `lit`
115 # configuration set these env vars for us; it makes sense to do the same
116 # for LLVM:
117 # + https://github.com/llvm/llvm-project/blob/4c106cfdf7cf7eec861ad3983a3dd9a9e8f3a8ae/clang-tools-extra/test/Unit/lit.cfg.py#L22-L31
118 #
119 # !!! TODO: look into upstreaming this patch
120 ./llvm/llvm-lit-cfg-add-libs-to-dylib-path.patch
121
122 # `lit` has a mode where it executes run lines as a shell script which is
123 # constructs; this is problematic for macOS because it means that there's
124 # another process in between `lit` and the binaries being tested. As noted
125 # above, this means that `DYLD_LIBRARY_PATH` is cleared which means that our
126 # tests fail with dyld errors.
127 #
128 # To get around this we patch `lit` to reintroduce `DYLD_LIBRARY_PATH`, when
129 # present in the test configuration.
130 #
131 # It's not clear to me why this isn't an issue for LLVM developers running
132 # on macOS (nothing about this _seems_ nix specific)..
133 ./llvm/lit-shell-script-runner-set-dyld-library-path.patch
134 ];
135 pollyPatches = [
136 ./llvm/gnu-install-dirs-polly.patch
137
138 # Just like the `llvm-lit-cfg` patch, but for `polly`.
139 ./llvm/polly-lit-cfg-add-libs-to-dylib-path.patch
140 ];
141 inherit llvm_meta;
142 };
143
144 # `llvm` historically had the binaries. When choosing an output explicitly,
145 # we need to reintroduce `outputSpecified` to get the expected behavior e.g. of lib.get*
146 llvm = tools.libllvm;
147
148 libclang = callPackage ../common/clang {
149 patches = [
150 ./clang/purity.patch
151 # https://reviews.llvm.org/D51899
152 ./clang/gnu-install-dirs.patch
153 ../common/clang/add-nostdlibinc-flag.patch
154 (substituteAll {
155 src = ../common/clang/clang-at-least-16-LLVMgold-path.patch;
156 libllvmLibdir = "${tools.libllvm.lib}/lib";
157 })
158 ];
159 inherit llvm_meta;
160 };
161
162 clang-unwrapped = tools.libclang;
163
164 llvm-manpages = lowPrio (tools.libllvm.override {
165 enableManpages = true;
166 python3 = pkgs.python3; # don't use python-boot
167 });
168
169 clang-manpages = lowPrio (tools.libclang.override {
170 enableManpages = true;
171 python3 = pkgs.python3; # don't use python-boot
172 });
173
174 lldb-manpages = lowPrio (tools.lldb.override {
175 enableManpages = true;
176 python3 = pkgs.python3; # don't use python-boot
177 });
178
179 # pick clang appropriate for package set we are targeting
180 clang =
181 /**/ if stdenv.targetPlatform.useLLVM or false then tools.clangUseLLVM
182 else if (pkgs.targetPackages.stdenv or stdenv).cc.isGNU then tools.libstdcxxClang
183 else tools.libcxxClang;
184
185 libstdcxxClang = wrapCCWith rec {
186 cc = tools.clang-unwrapped;
187 # libstdcxx is taken from gcc in an ad-hoc way in cc-wrapper.
188 libcxx = null;
189 extraPackages = [
190 targetLlvmLibraries.compiler-rt
191 ];
192 extraBuildCommands = mkExtraBuildCommands cc;
193 };
194
195 libcxxClang = wrapCCWith rec {
196 cc = tools.clang-unwrapped;
197 libcxx = targetLlvmLibraries.libcxx;
198 extraPackages = [
199 targetLlvmLibraries.compiler-rt
200 ];
201 extraBuildCommands = mkExtraBuildCommands cc;
202 };
203
204 lld = callPackage ../common/lld {
205 patches = [
206 ./lld/gnu-install-dirs.patch
207 ];
208 inherit llvm_meta;
209 };
210
211 mlir = callPackage ../common/mlir {
212 inherit llvm_meta;
213 };
214
215 lldb = callPackage ../common/lldb.nix {
216 src = callPackage ({ runCommand }: runCommand "lldb-src-${version}" {} ''
217 mkdir -p "$out"
218 cp -r ${monorepoSrc}/cmake "$out"
219 cp -r ${monorepoSrc}/lldb "$out"
220 '') { };
221 patches =
222 [
223 # FIXME: do we need this? ./procfs.patch
224 ../common/lldb/gnu-install-dirs.patch
225 ]
226 # This is a stopgap solution if/until the macOS SDK used for x86_64 is
227 # updated.
228 #
229 # The older 10.12 SDK used on x86_64 as of this writing has a `mach/machine.h`
230 # header that does not define `CPU_SUBTYPE_ARM64E` so we replace the one use
231 # of this preprocessor symbol in `lldb` with its expansion.
232 #
233 # See here for some context:
234 # https://github.com/NixOS/nixpkgs/pull/194634#issuecomment-1272129132
235 ++ lib.optional (
236 stdenv.targetPlatform.isDarwin
237 && !stdenv.targetPlatform.isAarch64
238 && (lib.versionOlder darwin.apple_sdk.sdk.version "11.0")
239 ) ./lldb/cpu_subtype_arm64e_replacement.patch;
240 inherit llvm_meta;
241 };
242
243 # Below, is the LLVM bootstrapping logic. It handles building a
244 # fully LLVM toolchain from scratch. No GCC toolchain should be
245 # pulled in. As a consequence, it is very quick to build different
246 # targets provided by LLVM and we can also build for what GCC
247 # doesn’t support like LLVM. Probably we should move to some other
248 # file.
249
250 bintools-unwrapped = callPackage ../common/bintools.nix { };
251
252 bintoolsNoLibc = wrapBintoolsWith {
253 bintools = tools.bintools-unwrapped;
254 libc = preLibcCrossHeaders;
255 };
256
257 bintools = wrapBintoolsWith {
258 bintools = tools.bintools-unwrapped;
259 };
260
261 clangUseLLVM = wrapCCWith rec {
262 cc = tools.clang-unwrapped;
263 libcxx = targetLlvmLibraries.libcxx;
264 bintools = bintools';
265 extraPackages = [
266 targetLlvmLibraries.compiler-rt
267 ] ++ lib.optionals (!stdenv.targetPlatform.isWasm) [
268 targetLlvmLibraries.libunwind
269 ];
270 extraBuildCommands = mkExtraBuildCommands cc;
271 nixSupport.cc-cflags =
272 [ "-rtlib=compiler-rt"
273 "-Wno-unused-command-line-argument"
274 "-B${targetLlvmLibraries.compiler-rt}/lib"
275 ]
276 ++ lib.optional (!stdenv.targetPlatform.isWasm) "--unwindlib=libunwind"
277 ++ lib.optional
278 (!stdenv.targetPlatform.isWasm && stdenv.targetPlatform.useLLVM or false)
279 "-lunwind"
280 ++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
281 nixSupport.cc-ldflags = lib.optionals (!stdenv.targetPlatform.isWasm) [ "-L${targetLlvmLibraries.libunwind}/lib" ];
282 };
283
284 clangNoLibcxx = wrapCCWith rec {
285 cc = tools.clang-unwrapped;
286 libcxx = null;
287 bintools = bintools';
288 extraPackages = [
289 targetLlvmLibraries.compiler-rt
290 ];
291 extraBuildCommands = mkExtraBuildCommands cc;
292 nixSupport.cc-cflags =
293 [
294 "-rtlib=compiler-rt"
295 "-B${targetLlvmLibraries.compiler-rt}/lib"
296 "-nostdlib++"
297 ]
298 ++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
299 };
300
301 clangNoLibc = wrapCCWith rec {
302 cc = tools.clang-unwrapped;
303 libcxx = null;
304 bintools = bintoolsNoLibc';
305 extraPackages = [
306 targetLlvmLibraries.compiler-rt
307 ];
308 extraBuildCommands = mkExtraBuildCommands cc;
309 nixSupport.cc-cflags =
310 [
311 "-rtlib=compiler-rt"
312 "-B${targetLlvmLibraries.compiler-rt}/lib"
313 ]
314 ++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
315 };
316
317 clangNoCompilerRt = wrapCCWith rec {
318 cc = tools.clang-unwrapped;
319 libcxx = null;
320 bintools = bintoolsNoLibc';
321 extraPackages = [ ];
322 extraBuildCommands = mkExtraBuildCommands0 cc;
323 nixSupport.cc-cflags =
324 [
325 "-nostartfiles"
326 ]
327 ++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
328 };
329
330 clangNoCompilerRtWithLibc = wrapCCWith (rec {
331 cc = tools.clang-unwrapped;
332 libcxx = null;
333 bintools = bintools';
334 extraPackages = [ ];
335 extraBuildCommands = mkExtraBuildCommands0 cc;
336 } // lib.optionalAttrs stdenv.targetPlatform.isWasm {
337 nixSupport.cc-cflags = [ "-fno-exceptions" ];
338 });
339
340 # Has to be in tools despite mostly being a library,
341 # because we use a native helper executable from a
342 # non-cross build in cross builds.
343 libclc = callPackage ../common/libclc.nix {
344 inherit buildLlvmTools;
345 };
346 });
347
348 libraries = lib.makeExtensible (libraries: let
349 callPackage = newScope (libraries // buildLlvmTools // { inherit stdenv cmake ninja libxml2 python3 release_version version monorepoSrc; });
350 in {
351
352 compiler-rt-libc = callPackage ../common/compiler-rt {
353 patches = [
354 ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
355 # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
356 # extra `/`.
357 ./compiler-rt/normalize-var.patch
358 # See: https://github.com/NixOS/nixpkgs/pull/186575
359 ../common/compiler-rt/darwin-plistbuddy-workaround.patch
360 # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
361 # ../common/compiler-rt/armv7l-15.patch
362 ];
363 inherit llvm_meta;
364 stdenv = if stdenv.hostPlatform.useLLVM or false || (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isStatic)
365 then overrideCC stdenv buildLlvmTools.clangNoCompilerRtWithLibc
366 else stdenv;
367 };
368
369 compiler-rt-no-libc = callPackage ../common/compiler-rt {
370 patches = [
371 ./compiler-rt/X86-support-extension.patch # Add support for i486 i586 i686 by reusing i386 config
372 # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
373 # extra `/`.
374 ./compiler-rt/normalize-var.patch
375 # See: https://github.com/NixOS/nixpkgs/pull/186575
376 ../common/compiler-rt/darwin-plistbuddy-workaround.patch
377 # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
378 # ../common/compiler-rt/armv7l-15.patch
379 ];
380 inherit llvm_meta;
381 stdenv = if stdenv.hostPlatform.useLLVM or false
382 then overrideCC stdenv buildLlvmTools.clangNoCompilerRt
383 else stdenv;
384 };
385
386 # N.B. condition is safe because without useLLVM both are the same.
387 compiler-rt = if stdenv.hostPlatform.isAndroid || stdenv.hostPlatform.isDarwin
388 then libraries.compiler-rt-libc
389 else libraries.compiler-rt-no-libc;
390
391 stdenv = overrideCC stdenv buildLlvmTools.clang;
392
393 libcxxStdenv = overrideCC stdenv buildLlvmTools.libcxxClang;
394
395 # `libcxx` requires a fairly modern C++ compiler,
396 # so: we use the clang from this LLVM package set instead of the regular
397 # stdenv's compiler.
398 libcxx = callPackage ../common/libcxx {
399 patches = lib.optionals (stdenv.isDarwin && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13") [
400 # https://github.com/llvm/llvm-project/issues/64226
401 ./libcxx/0001-darwin-10.12-mbstate_t-fix.patch
402 ];
403 inherit llvm_meta;
404 stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
405 };
406
407 libunwind = callPackage ../common/libunwind {
408 inherit llvm_meta;
409 stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
410 };
411
412 openmp = callPackage ../common/openmp {
413 patches = [
414 ./openmp/fix-find-tool.patch
415 ./openmp/run-lit-directly.patch
416 ];
417 inherit llvm_meta targetLlvm;
418 };
419 });
420 noExtend = extensible: lib.attrsets.removeAttrs extensible [ "extend" ];
421
422in { inherit tools libraries release_version; } // (noExtend libraries) // (noExtend tools)