about summary refs log tree commit diff
path: root/pkgs/development/compilers/llvm/common/default.nix
blob: 7e8e65b51c1e040f2509d1e881ca7b0ee65d54a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
{
  lowPrio,
  newScope,
  pkgs,
  lib,
  stdenv,
  preLibcCrossHeaders,
  substitute,
  substituteAll,
  fetchFromGitHub,
  fetchpatch,
  overrideCC,
  wrapCCWith,
  wrapBintoolsWith,
  buildLlvmTools, # tools, but from the previous stage, for cross
  targetLlvmLibraries, # libraries, but from the next stage, for cross
  targetLlvm,
  # This is the default binutils, but with *this* version of LLD rather
  # than the default LLVM version's, if LLD is the choice. We use these for
  # the `useLLVM` bootstrapping below.
  bootBintoolsNoLibc ? if stdenv.targetPlatform.linker == "lld" then null else pkgs.bintoolsNoLibc,
  bootBintools ? if stdenv.targetPlatform.linker == "lld" then null else pkgs.bintools,
  darwin,
  gitRelease ? null,
  officialRelease ? null,
  monorepoSrc ? null,
  version ? null,
  ...
}@args:

assert lib.assertMsg (lib.xor (gitRelease != null) (officialRelease != null)) (
  "must specify `gitRelease` or `officialRelease`"
  + (lib.optionalString (gitRelease != null) " — not both")
);

let
  monorepoSrc' = monorepoSrc;

  metadata = rec {
    # Import releaseInfo separately to avoid infinite recursion
    inherit
      (import ./common-let.nix {
        inherit (args)
          lib
          gitRelease
          officialRelease
          version
          ;
      })
      releaseInfo
      ;
    inherit (releaseInfo) release_version version;
    inherit
      (import ./common-let.nix {
        inherit
          lib
          fetchFromGitHub
          release_version
          gitRelease
          officialRelease
          monorepoSrc'
          version
          ;
      })
      llvm_meta
      monorepoSrc
      ;
    src = monorepoSrc;
    versionDir =
      (builtins.toString ../.)
      + "/${if (gitRelease != null) then "git" else lib.versions.major release_version}";
    getVersionFile =
      p:
      builtins.path {
        name = builtins.baseNameOf p;
        path = "${metadata.versionDir}/${p}";
      };
  };

  lldbPlugins = lib.makeExtensible (
    lldbPlugins:
    let
      callPackage = newScope (
        lldbPlugins
        // {
          inherit stdenv;
          inherit (tools) lldb;
        }
      );
    in
    {
      llef = callPackage ./lldb-plugins/llef.nix { };
    }
  );

  tools = lib.makeExtensible (
    tools:
    let
      callPackage = newScope (
        tools
        // args
        // metadata
        # Previously monorepoSrc was erroneously not being passed through.
        // lib.optionalAttrs (lib.versionOlder metadata.release_version "14") { monorepoSrc = null; } # Preserve a bug during #307211, TODO: remove; causes llvm 13 rebuild.
      );
      clangVersion =
        if (lib.versionOlder metadata.release_version "16") then
          metadata.release_version
        else
          lib.versions.major metadata.release_version;
      mkExtraBuildCommands0 = cc: ''
        rsrc="$out/resource-root"
        mkdir "$rsrc"
        ln -s "${cc.lib}/lib/clang/${clangVersion}/include" "$rsrc"
        echo "-resource-dir=$rsrc" >> $out/nix-support/cc-cflags
      '';
      mkExtraBuildCommands =
        cc:
        mkExtraBuildCommands0 cc
        + ''
          ln -s "${targetLlvmLibraries.compiler-rt.out}/lib" "$rsrc/lib"
          ln -s "${targetLlvmLibraries.compiler-rt.out}/share" "$rsrc/share"
        '';

      bintoolsNoLibc' = if bootBintoolsNoLibc == null then tools.bintoolsNoLibc else bootBintoolsNoLibc;
      bintools' = if bootBintools == null then tools.bintools else bootBintools;
    in
    {
      libllvm = callPackage ./llvm {
        patches =
          lib.optional (lib.versionOlder metadata.release_version "14") ./llvm/llvm-config-link-static.patch
          ++ [ (metadata.getVersionFile "llvm/gnu-install-dirs.patch") ]
          ++ lib.optionals (lib.versionAtLeast metadata.release_version "15") [
            # Running the tests involves invoking binaries (like `opt`) that depend on
            # the LLVM dylibs and reference them by absolute install path (i.e. their
            # nix store path).
            #
            # Because we have not yet run the install phase (we're running these tests
            # as part of `checkPhase` instead of `installCheckPhase`) these absolute
            # paths do not exist yet; to work around this we point the loader (`ld` on
            # unix, `dyld` on macOS) at the `lib` directory which will later become this
            # package's `lib` output.
            #
            # Previously we would just set `LD_LIBRARY_PATH` to include the build `lib`
            # dir but:
            #   - this doesn't generalize well to other platforms; `lit` doesn't forward
            #     `DYLD_LIBRARY_PATH` (macOS):
            #     + https://github.com/llvm/llvm-project/blob/0d89963df354ee309c15f67dc47c8ab3cb5d0fb2/llvm/utils/lit/lit/TestingConfig.py#L26
            #   - even if `lit` forwarded this env var, we actually cannot set
            #     `DYLD_LIBRARY_PATH` in the child processes `lit` launches because
            #     `DYLD_LIBRARY_PATH` (and `DYLD_FALLBACK_LIBRARY_PATH`) is cleared for
            #     "protected processes" (i.e. the python interpreter that runs `lit`):
            #     https://stackoverflow.com/a/35570229
            #   - other LLVM subprojects deal with this issue by having their `lit`
            #     configuration set these env vars for us; it makes sense to do the same
            #     for LLVM:
            #     + https://github.com/llvm/llvm-project/blob/4c106cfdf7cf7eec861ad3983a3dd9a9e8f3a8ae/clang-tools-extra/test/Unit/lit.cfg.py#L22-L31
            #
            # !!! TODO: look into upstreaming this patch
            (metadata.getVersionFile "llvm/llvm-lit-cfg-add-libs-to-dylib-path.patch")

            # `lit` has a mode where it executes run lines as a shell script which is
            # constructs; this is problematic for macOS because it means that there's
            # another process in between `lit` and the binaries being tested. As noted
            # above, this means that `DYLD_LIBRARY_PATH` is cleared which means that our
            # tests fail with dyld errors.
            #
            # To get around this we patch `lit` to reintroduce `DYLD_LIBRARY_PATH`, when
            # present in the test configuration.
            #
            # It's not clear to me why this isn't an issue for LLVM developers running
            # on macOS (nothing about this _seems_ nix specific)..
            (metadata.getVersionFile "llvm/lit-shell-script-runner-set-dyld-library-path.patch")
          ]
          ++
            lib.optional (lib.versions.major metadata.release_version == "13")
              # Fix random compiler crashes: https://bugs.llvm.org/show_bug.cgi?id=50611
              (
                fetchpatch {
                  url = "https://raw.githubusercontent.com/archlinux/svntogit-packages/4764a4f8c920912a2bfd8b0eea57273acfe0d8a8/trunk/no-strict-aliasing-DwarfCompileUnit.patch";
                  sha256 = "18l6mrvm2vmwm77ckcnbjvh6ybvn72rhrb799d4qzwac4x2ifl7g";
                  stripLen = 1;
                }
              )
          ++
            lib.optional (lib.versionOlder metadata.release_version "16")
              # Fix musl build.
              (
                fetchpatch {
                  url = "https://github.com/llvm/llvm-project/commit/5cd554303ead0f8891eee3cd6d25cb07f5a7bf67.patch";
                  relative = "llvm";
                  hash = "sha256-XPbvNJ45SzjMGlNUgt/IgEvM2dHQpDOe6woUJY+nUYA=";
                }
              )
          ++ lib.optionals (lib.versions.major metadata.release_version == "13") [
            # Backport gcc-13 fixes with missing includes.
            (fetchpatch {
              name = "signals-gcc-13.patch";
              url = "https://github.com/llvm/llvm-project/commit/ff1681ddb303223973653f7f5f3f3435b48a1983.patch";
              hash = "sha256-CXwYxQezTq5vdmc8Yn88BUAEly6YZ5VEIA6X3y5NNOs=";
              stripLen = 1;
            })
            (fetchpatch {
              name = "base64-gcc-13.patch";
              url = "https://github.com/llvm/llvm-project/commit/5e9be93566f39ee6cecd579401e453eccfbe81e5.patch";
              hash = "sha256-PAwrVrvffPd7tphpwCkYiz+67szPRzRB2TXBvKfzQ7U=";
              stripLen = 1;
            })
          ]
          ++ lib.optionals (lib.versions.major metadata.release_version == "14") [
            # fix RuntimeDyld usage on aarch64-linux (e.g. python312Packages.numba tests)
            (fetchpatch {
              url = "https://github.com/llvm/llvm-project/commit/2e1b838a889f9793d4bcd5dbfe10db9796b77143.patch";
              relative = "llvm";
              hash = "sha256-Ot45P/iwaR4hkcM3xtLwfryQNgHI6pv6ADjv98tgdZA=";
            })
          ]
          ++
            lib.optional (lib.versions.major metadata.release_version == "17")
              # resolves https://github.com/llvm/llvm-project/issues/75168
              (
                fetchpatch {
                  name = "fix-fzero-call-used-regs.patch";
                  url = "https://github.com/llvm/llvm-project/commit/f800c1f3b207e7bcdc8b4c7192928d9a078242a0.patch";
                  stripLen = 1;
                  hash = "sha256-e8YKrMy2rGcSJGC6er2V66cOnAnI+u1/yImkvsRsmg8=";
                }
              );
        pollyPatches =
          [ (metadata.getVersionFile "llvm/gnu-install-dirs-polly.patch") ]
          ++ lib.optional (lib.versionAtLeast metadata.release_version "15")
            # Just like the `llvm-lit-cfg` patch, but for `polly`.
            (metadata.getVersionFile "llvm/polly-lit-cfg-add-libs-to-dylib-path.patch");
      };

      # `llvm` historically had the binaries.  When choosing an output explicitly,
      # we need to reintroduce `outputSpecified` to get the expected behavior e.g. of lib.get*
      llvm = tools.libllvm;

      libclang = callPackage ./clang {
        patches =
          [
            (metadata.getVersionFile "clang/purity.patch")
            # https://reviews.llvm.org/D51899
            (metadata.getVersionFile "clang/gnu-install-dirs.patch")
          ]
          ++ lib.optional (lib.versions.major metadata.release_version == "13")
            # Revert of https://reviews.llvm.org/D100879
            # The malloc alignment assumption is incorrect for jemalloc and causes
            # mis-compilation in firefox.
            # See: https://bugzilla.mozilla.org/show_bug.cgi?id=1741454
            (metadata.getVersionFile "clang/revert-malloc-alignment-assumption.patch")
          ++ [
            ./clang/add-nostdlibinc-flag.patch
            (substituteAll {
              src =
                if (lib.versionOlder metadata.release_version "16") then
                  ./clang/clang-11-15-LLVMgold-path.patch
                else
                  ./clang/clang-at-least-16-LLVMgold-path.patch;
              libllvmLibdir = "${tools.libllvm.lib}/lib";
            })
          ]
          ++ lib.optional (lib.versions.major metadata.release_version == "18") (fetchpatch {
            name = "tweak-tryCaptureVariable-for-unevaluated-lambdas.patch";
            url = "https://github.com/llvm/llvm-project/commit/3d361b225fe89ce1d8c93639f27d689082bd8dad.patch";
            # TreeTransform.h is not affected in LLVM 18.
            excludes = [
              "docs/ReleaseNotes.rst"
              "lib/Sema/TreeTransform.h"
            ];
            stripLen = 1;
            hash = "sha256-1NKej08R9SPlbDY/5b0OKUsHjX07i9brR84yXiPwi7E=";
          });
      };

      clang-unwrapped = tools.libclang;

      llvm-manpages = lowPrio (
        tools.libllvm.override {
          enableManpages = true;
          python3 = pkgs.python3; # don't use python-boot
        }
      );

      clang-manpages = lowPrio (
        tools.libclang.override {
          enableManpages = true;
          python3 = pkgs.python3; # don't use python-boot
        }
      );

      # Wrapper for standalone command line utilities
      clang-tools = callPackage ./clang-tools { };

      # pick clang appropriate for package set we are targeting
      clang =
        if stdenv.targetPlatform.libc == null then
          tools.clangNoLibc
        else if stdenv.targetPlatform.useLLVM or false then
          tools.clangUseLLVM
        else if (pkgs.targetPackages.stdenv or args.stdenv).cc.isGNU then
          tools.libstdcxxClang
        else
          tools.libcxxClang;

      libstdcxxClang = wrapCCWith rec {
        cc = tools.clang-unwrapped;
        # libstdcxx is taken from gcc in an ad-hoc way in cc-wrapper.
        libcxx = null;
        extraPackages = [ targetLlvmLibraries.compiler-rt ];
        extraBuildCommands = mkExtraBuildCommands cc;
      };

      libcxxClang = wrapCCWith rec {
        cc = tools.clang-unwrapped;
        libcxx = targetLlvmLibraries.libcxx;
        extraPackages = [ targetLlvmLibraries.compiler-rt ];
        extraBuildCommands = mkExtraBuildCommands cc;
      };

      lld = callPackage ./lld {
        patches =
          [ (metadata.getVersionFile "lld/gnu-install-dirs.patch") ]
          ++ lib.optional (lib.versions.major metadata.release_version == "14") (
            metadata.getVersionFile "lld/fix-root-src-dir.patch"
          )
          ++ lib.optional (
            lib.versionAtLeast metadata.release_version "16" && lib.versionOlder metadata.release_version "18"
          ) (metadata.getVersionFile "lld/add-table-base.patch");
      };

      lldb = callPackage ./lldb.nix (
        {
          patches =
            let
              resourceDirPatch = callPackage (
                { substituteAll, libclang }:
                (substituteAll {
                  src = metadata.getVersionFile "lldb/resource-dir.patch";
                  clangLibDir = "${libclang.lib}/lib";
                }).overrideAttrs
                  (_: _: { name = "resource-dir.patch"; })
              ) { };
            in
            lib.optional (lib.versionOlder metadata.release_version "16")
              # FIXME: do we need this after 15?
              (metadata.getVersionFile "lldb/procfs.patch")
            ++ lib.optional (lib.versionOlder metadata.release_version "17") resourceDirPatch
            ++ lib.optional (lib.versionOlder metadata.release_version "14") (
              metadata.getVersionFile "lldb/gnu-install-dirs.patch"
            )
            ++ lib.optional (lib.versionAtLeast metadata.release_version "14") ./lldb/gnu-install-dirs.patch
            # This is a stopgap solution if/until the macOS SDK used for x86_64 is
            # updated.
            #
            # The older 10.12 SDK used on x86_64 as of this writing has a `mach/machine.h`
            # header that does not define `CPU_SUBTYPE_ARM64E` so we replace the one use
            # of this preprocessor symbol in `lldb` with its expansion.
            #
            # See here for some context:
            # https://github.com/NixOS/nixpkgs/pull/194634#issuecomment-1272129132
            ++ lib.optional (
              stdenv.targetPlatform.isDarwin
              && lib.versionOlder stdenv.targetPlatform.darwinSdkVersion "11.0"
            ) (metadata.getVersionFile "lldb/cpu_subtype_arm64e_replacement.patch");
        }
        // lib.optionalAttrs (lib.versions.major metadata.release_version == "16") {
          src = callPackage (
            { runCommand }:
            runCommand "lldb-src-${metadata.version}" { } ''
              mkdir -p "$out"
              cp -r ${monorepoSrc}/cmake "$out"
              cp -r ${monorepoSrc}/lldb "$out"
            ''
          ) { };
        }
      );

      # Below, is the LLVM bootstrapping logic. It handles building a
      # fully LLVM toolchain from scratch. No GCC toolchain should be
      # pulled in. As a consequence, it is very quick to build different
      # targets provided by LLVM and we can also build for what GCC
      # doesn’t support like LLVM. Probably we should move to some other
      # file.

      bintools-unwrapped = callPackage ./bintools.nix { };

      bintoolsNoLibc = wrapBintoolsWith {
        bintools = tools.bintools-unwrapped;
        libc = preLibcCrossHeaders;
      };

      bintools = wrapBintoolsWith { bintools = tools.bintools-unwrapped; };

      clangUseLLVM = wrapCCWith (
        rec {
          cc = tools.clang-unwrapped;
          libcxx = targetLlvmLibraries.libcxx;
          bintools = bintools';
          extraPackages =
            [ targetLlvmLibraries.compiler-rt ]
            ++ lib.optionals (!stdenv.targetPlatform.isWasm && !stdenv.targetPlatform.isFreeBSD) [
              targetLlvmLibraries.libunwind
            ];
          extraBuildCommands =
            lib.optionalString (lib.versions.major metadata.release_version == "13") (
              ''
                echo "-rtlib=compiler-rt -Wno-unused-command-line-argument" >> $out/nix-support/cc-cflags
                echo "-B${targetLlvmLibraries.compiler-rt}/lib" >> $out/nix-support/cc-cflags
              ''
              + lib.optionalString (!stdenv.targetPlatform.isWasm) ''
                echo "--unwindlib=libunwind" >> $out/nix-support/cc-cflags
                echo "-L${targetLlvmLibraries.libunwind}/lib" >> $out/nix-support/cc-ldflags
              ''
              + lib.optionalString (!stdenv.targetPlatform.isWasm && stdenv.targetPlatform.useLLVM or false) ''
                echo "-lunwind" >> $out/nix-support/cc-ldflags
              ''
              + lib.optionalString stdenv.targetPlatform.isWasm ''
                echo "-fno-exceptions" >> $out/nix-support/cc-cflags
              ''
            )
            + mkExtraBuildCommands cc;
        }
        // lib.optionalAttrs (lib.versionAtLeast metadata.release_version "14") {
          nixSupport.cc-cflags =
            [
              "-rtlib=compiler-rt"
              "-Wno-unused-command-line-argument"
              "-B${targetLlvmLibraries.compiler-rt}/lib"
            ]
            ++ lib.optional (
              !stdenv.targetPlatform.isWasm && !stdenv.targetPlatform.isFreeBSD
            ) "--unwindlib=libunwind"
            ++ lib.optional (
              !stdenv.targetPlatform.isWasm
              && !stdenv.targetPlatform.isFreeBSD
              && stdenv.targetPlatform.useLLVM or false
            ) "-lunwind"
            ++ lib.optional stdenv.targetPlatform.isWasm "-fno-exceptions";
          nixSupport.cc-ldflags = lib.optionals (
            !stdenv.targetPlatform.isWasm && !stdenv.targetPlatform.isFreeBSD
          ) [ "-L${targetLlvmLibraries.libunwind}/lib" ];
        }
      );

      clangNoLibcxx = wrapCCWith (
        rec {
          cc = tools.clang-unwrapped;
          libcxx = null;
          bintools = bintools';
          extraPackages = [ targetLlvmLibraries.compiler-rt ];
          extraBuildCommands =
            lib.optionalString (lib.versions.major metadata.release_version == "13") ''
              echo "-rtlib=compiler-rt" >> $out/nix-support/cc-cflags
              echo "-B${targetLlvmLibraries.compiler-rt}/lib" >> $out/nix-support/cc-cflags
              echo "-nostdlib++" >> $out/nix-support/cc-cflags
            ''
            + mkExtraBuildCommands cc;
        }
        // lib.optionalAttrs (lib.versionAtLeast metadata.release_version "14") {
          nixSupport.cc-cflags =
            [
              "-rtlib=compiler-rt"
              "-B${targetLlvmLibraries.compiler-rt}/lib"
              "-nostdlib++"
            ]
            ++ lib.optional (
              lib.versionAtLeast metadata.release_version "15" && stdenv.targetPlatform.isWasm
            ) "-fno-exceptions";
        }
      );

      clangNoLibc = wrapCCWith (
        rec {
          cc = tools.clang-unwrapped;
          libcxx = null;
          bintools = bintoolsNoLibc';
          extraPackages = [ targetLlvmLibraries.compiler-rt ];
          extraBuildCommands =
            lib.optionalString (lib.versions.major metadata.release_version == "13") ''
              echo "-rtlib=compiler-rt" >> $out/nix-support/cc-cflags
              echo "-B${targetLlvmLibraries.compiler-rt}/lib" >> $out/nix-support/cc-cflags
            ''
            + mkExtraBuildCommands cc;
        }
        // lib.optionalAttrs (lib.versionAtLeast metadata.release_version "14") {
          nixSupport.cc-cflags =
            [
              "-rtlib=compiler-rt"
              "-B${targetLlvmLibraries.compiler-rt}/lib"
            ]
            ++ lib.optional (
              lib.versionAtLeast metadata.release_version "15" && stdenv.targetPlatform.isWasm
            ) "-fno-exceptions";
        }
      );

      clangNoCompilerRt = wrapCCWith (
        rec {
          cc = tools.clang-unwrapped;
          libcxx = null;
          bintools = bintoolsNoLibc';
          extraPackages = [ ];
          extraBuildCommands =
            lib.optionalString (lib.versions.major metadata.release_version == "13") ''
              echo "-nostartfiles" >> $out/nix-support/cc-cflags
            ''
            + mkExtraBuildCommands0 cc;
        }
        // lib.optionalAttrs (lib.versionAtLeast metadata.release_version "14") {
          nixSupport.cc-cflags =
            [ "-nostartfiles" ]
            ++ lib.optional (
              lib.versionAtLeast metadata.release_version "15" && stdenv.targetPlatform.isWasm
            ) "-fno-exceptions";
        }
      );

      clangNoCompilerRtWithLibc =
        wrapCCWith rec {
          cc = tools.clang-unwrapped;
          libcxx = null;
          bintools = bintools';
          extraPackages = [ ];
          extraBuildCommands = mkExtraBuildCommands0 cc;
        }
        // lib.optionalAttrs (
          lib.versionAtLeast metadata.release_version "15" && stdenv.targetPlatform.isWasm
        ) { nixSupport.cc-cflags = [ "-fno-exceptions" ]; };
    }
    // lib.optionalAttrs (lib.versionAtLeast metadata.release_version "15") {
      # TODO: pre-15: lldb/docs/index.rst:155:toctree contains reference to nonexisting document 'design/structureddataplugins'
      lldb-manpages = lowPrio (
        tools.lldb.override {
          enableManpages = true;
          python3 = pkgs.python3; # don't use python-boot
        }
      );
    }
    // lib.optionalAttrs (lib.versionAtLeast metadata.release_version "16") {
      mlir = callPackage ./mlir { };
      libclc = callPackage ./libclc.nix { };
    }
  );

  libraries = lib.makeExtensible (
    libraries:
    let
      callPackage = newScope (
        libraries
        // buildLlvmTools
        // args
        // metadata
        # Previously monorepoSrc was erroneously not being passed through.
        // lib.optionalAttrs (lib.versionOlder metadata.release_version "14") { monorepoSrc = null; } # Preserve a bug during #307211, TODO: remove; causes llvm 13 rebuild.
      );

      compiler-rtPatches =
        lib.optional (lib.versionOlder metadata.release_version "15") (
          metadata.getVersionFile "compiler-rt/codesign.patch"
        ) # Revert compiler-rt commit that makes codesign mandatory
        ++ [
          (metadata.getVersionFile "compiler-rt/X86-support-extension.patch") # Add support for i486 i586 i686 by reusing i386 config
        ]
        ++ lib.optional (
          lib.versionAtLeast metadata.release_version "14" && lib.versionOlder metadata.release_version "18"
        ) (metadata.getVersionFile "compiler-rt/gnu-install-dirs.patch")
        ++ [
          # ld-wrapper dislikes `-rpath-link //nix/store`, so we normalize away the
          # extra `/`.
          (metadata.getVersionFile "compiler-rt/normalize-var.patch")
        ]
        ++
          lib.optional (lib.versionOlder metadata.release_version "18")
            # Prevent a compilation error on darwin
            (metadata.getVersionFile "compiler-rt/darwin-targetconditionals.patch")
        ++
          lib.optional (lib.versionAtLeast metadata.release_version "15")
            # See: https://github.com/NixOS/nixpkgs/pull/186575
            ./compiler-rt/darwin-plistbuddy-workaround.patch
        ++
          lib.optional (lib.versions.major metadata.release_version == "15")
            # See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
            ./compiler-rt/armv7l-15.patch
        ++ lib.optionals (lib.versionOlder metadata.release_version "15") [
          ./compiler-rt/darwin-plistbuddy-workaround.patch
          (metadata.getVersionFile "compiler-rt/armv7l.patch")
          # Fix build on armv6l
          ./compiler-rt/armv6-mcr-dmb.patch
          ./compiler-rt/armv6-sync-ops-no-thumb.patch
          ./compiler-rt/armv6-no-ldrexd-strexd.patch
          ./compiler-rt/armv6-scudo-no-yield.patch
          ./compiler-rt/armv6-scudo-libatomic.patch
        ];
    in
    {
      compiler-rt-libc = callPackage ./compiler-rt {
        patches = compiler-rtPatches;
        stdenv =
          if
            stdenv.hostPlatform.useLLVM or false
            || (
              lib.versionAtLeast metadata.release_version "16"
              && stdenv.hostPlatform.isDarwin
              && stdenv.hostPlatform.isStatic
            )
          then
            overrideCC stdenv buildLlvmTools.clangNoCompilerRtWithLibc
          else
            args.stdenv;
      };

      compiler-rt-no-libc = callPackage ./compiler-rt {
        patches = compiler-rtPatches;
        stdenv =
          if stdenv.hostPlatform.useLLVM or false then
            overrideCC stdenv buildLlvmTools.clangNoCompilerRt
          else
            stdenv;
      };

      # N.B. condition is safe because without useLLVM both are the same.
      compiler-rt =
        if
          stdenv.hostPlatform.isAndroid
          || (lib.versionAtLeast metadata.release_version "16" && stdenv.hostPlatform.isDarwin)
        then
          libraries.compiler-rt-libc
        else
          libraries.compiler-rt-no-libc;

      stdenv = overrideCC stdenv buildLlvmTools.clang;

      libcxxStdenv = overrideCC stdenv buildLlvmTools.libcxxClang;

      libcxx = callPackage ./libcxx (
        {
          patches =
            lib.optionals (lib.versionOlder metadata.release_version "16") (
              lib.optional (lib.versions.major metadata.release_version == "15")
                # See:
                #   - https://reviews.llvm.org/D133566
                #   - https://github.com/NixOS/nixpkgs/issues/214524#issuecomment-1429146432
                # !!! Drop in LLVM 16+
                (
                  fetchpatch {
                    url = "https://github.com/llvm/llvm-project/commit/57c7bb3ec89565c68f858d316504668f9d214d59.patch";
                    hash = "sha256-B07vHmSjy5BhhkGSj3e1E0XmMv5/9+mvC/k70Z29VwY=";
                  }
                )
              ++ [
                (substitute {
                  src = ./libcxxabi/wasm.patch;
                  substitutions = [
                    "--replace-fail"
                    "/cmake/"
                    "/llvm/cmake/"
                  ];
                })
              ]
              ++ lib.optional stdenv.hostPlatform.isMusl (substitute {
                src = ./libcxx/libcxx-0001-musl-hacks.patch;
                substitutions = [
                  "--replace-fail"
                  "/include/"
                  "/libcxx/include/"
                ];
              })
            )
            ++
              lib.optional
                (
                  lib.versions.major metadata.release_version == "17"
                  && stdenv.isDarwin
                  && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13"
                )
                # https://github.com/llvm/llvm-project/issues/64226
                (
                  fetchpatch {
                    name = "0042-mbstate_t-not-defined.patch";
                    url = "https://github.com/macports/macports-ports/raw/acd8acb171f1658596ed1cf25da48d5b932e2d19/lang/llvm-17/files/0042-mbstate_t-not-defined.patch";
                    hash = "sha256-jo+DYA6zuSv9OH3A0bYwY5TlkWprup4OKQ7rfK1WHBI=";
                  }
                )
            ++
              lib.optional
                (
                  lib.versionAtLeast metadata.release_version "18"
                  && stdenv.isDarwin
                  && lib.versionOlder stdenv.hostPlatform.darwinMinVersion "10.13"
                )
                # https://github.com/llvm/llvm-project/issues/64226
                (metadata.getVersionFile "libcxx/0001-darwin-10.12-mbstate_t-fix.patch");
          stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
        }
        // lib.optionalAttrs (lib.versionOlder metadata.release_version "14") {
          # TODO: remove this, causes LLVM 13 packages rebuild.
          inherit (metadata) monorepoSrc; # Preserve bug during #307211 refactor.
        }
      );

      libunwind = callPackage ./libunwind {
        patches = lib.optional (lib.versionOlder metadata.release_version "17") (
          metadata.getVersionFile "libunwind/gnu-install-dirs.patch"
        );
        stdenv = overrideCC stdenv buildLlvmTools.clangNoLibcxx;
      };

      openmp = callPackage ./openmp {
        patches =
          lib.optional (lib.versionAtLeast metadata.release_version "15") (
            metadata.getVersionFile "openmp/fix-find-tool.patch"
          )
          ++ lib.optional (
            lib.versionAtLeast metadata.release_version "14" && lib.versionOlder metadata.release_version "18"
          ) (metadata.getVersionFile "openmp/gnu-install-dirs.patch")
          ++ lib.optional (lib.versionAtLeast metadata.release_version "14") (
            metadata.getVersionFile "openmp/run-lit-directly.patch"
          )
          ++
            lib.optional (lib.versionOlder metadata.release_version "14")
              # Fix cross.
              (
                fetchpatch {
                  url = "https://github.com/llvm/llvm-project/commit/5e2358c781b85a18d1463fd924d2741d4ae5e42e.patch";
                  hash = "sha256-UxIlAifXnexF/MaraPW0Ut6q+sf3e7y1fMdEv1q103A=";
                }
              );
      };
    }
  );

  noExtend = extensible: lib.attrsets.removeAttrs extensible [ "extend" ];
in
{
  inherit tools libraries lldbPlugins;
  inherit (metadata) release_version;
}
// (noExtend libraries)
// (noExtend tools)