summaryrefslogtreecommitdiff
path: root/pkgs/development/compilers/llvm/common/lld/default.nix
blob: ea5fe0e4e9cfb8fcd06e292c1dd8c848647537ea (about) (plain)
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
{
  lib,
  stdenv,
  llvm_meta,
  release_version,
  buildLlvmTools,
  monorepoSrc ? null,
  src ? null,
  runCommand,
  cmake,
  ninja,
  libxml2,
  libllvm,
  version,
  devExtraCmakeFlags ? [ ],
  getVersionFile,
  fetchpatch,
}:
stdenv.mkDerivation (finalAttrs: {
  pname = "lld";
  inherit version;

  src =
    if monorepoSrc != null then
      runCommand "lld-src-${version}" { inherit (monorepoSrc) passthru; } (
        ''
          mkdir -p "$out"
        ''
        + lib.optionalString (lib.versionAtLeast release_version "14") ''
          cp -r ${monorepoSrc}/cmake "$out"
        ''
        + ''
          cp -r ${monorepoSrc}/lld "$out"
          mkdir -p "$out/libunwind"
          cp -r ${monorepoSrc}/libunwind/include "$out/libunwind"
          mkdir -p "$out/llvm"
        ''
      )
    else
      src;

  sourceRoot = "${finalAttrs.src.name}/lld";

  patches =
    [ (getVersionFile "lld/gnu-install-dirs.patch") ]
    ++ lib.optional (lib.versions.major release_version == "14") (
      getVersionFile "lld/fix-root-src-dir.patch"
    )
    ++ lib.optional (lib.versionAtLeast release_version "16" && lib.versionOlder release_version "18") (
      getVersionFile "lld/add-table-base.patch"
    )
    ++ lib.optional (lib.versions.major release_version == "18") (
      # https://github.com/llvm/llvm-project/pull/97122
      fetchpatch {
        name = "more-openbsd-program-headers.patch";
        url = "https://github.com/llvm/llvm-project/commit/d7fd8b19e560fbb613159625acd8046d0df75115.patch";
        stripLen = 1;
        hash = "sha256-7wTy7XDTx0+fhWQpW1KEuz7xJvpl42qMTUfd20KGOfA=";
      }
    );

  nativeBuildInputs = [ cmake ] ++ lib.optional (lib.versionAtLeast release_version "15") ninja;
  buildInputs = [
    libllvm
    libxml2
  ];

  cmakeFlags =
    lib.optionals (lib.versionOlder release_version "14") [
      (lib.cmakeFeature "LLVM_CONFIG_PATH" "${libllvm.dev}/bin/llvm-config${
        lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) "-native"
      }")
    ]
    ++ lib.optionals (lib.versionAtLeast release_version "15") [
      (lib.cmakeFeature "LLD_INSTALL_PACKAGE_DIR" "${placeholder "dev"}/lib/cmake/lld")
    ]
    ++ [
      (lib.cmakeFeature "LLVM_TABLEGEN_EXE" "${buildLlvmTools.tblgen}/bin/llvm-tblgen")
    ]
    ++ devExtraCmakeFlags;

  postPatch = lib.optionalString (lib.versionOlder release_version "14") ''
    substituteInPlace MachO/CMakeLists.txt --replace-fail \
      '(''${LLVM_MAIN_SRC_DIR}/' '(../'
  '';

  # Musl's default stack size is too small for lld to be able to link Firefox.
  LDFLAGS = lib.optionalString stdenv.hostPlatform.isMusl "-Wl,-z,stack-size=2097152";

  outputs = [
    "out"
    "lib"
    "dev"
  ];

  meta = llvm_meta // {
    homepage = "https://lld.llvm.org/";
    description = "LLVM linker (unwrapped)";
    longDescription = ''
      LLD is a linker from the LLVM project that is a drop-in replacement for
      system linkers and runs much faster than them. It also provides features
      that are useful for toolchain developers.
      The linker supports ELF (Unix), PE/COFF (Windows), Mach-O (macOS), and
      WebAssembly in descending order of completeness. Internally, LLD consists
      of several different linkers.
    '';
  };
})