summaryrefslogtreecommitdiffstats
path: root/pkgs/build-support/kernel/compress-firmware.nix
blob: 438b71ef32c70bfef05284ab13f2e27f3c09f280 (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
{
  runCommand,
  lib,
  type ? "zstd",
  zstd,
}:

firmware:

let
  compressor =
    {
      xz = {
        ext = "xz";
        nativeBuildInputs = [ ];
        cmd = file: target: ''xz -9c -T1 -C crc32 --lzma2=dict=2MiB "${file}" > "${target}"'';
      };
      zstd = {
        ext = "zst";
        nativeBuildInputs = [ zstd ];
        cmd = file: target: ''zstd -T1 -19 --long --check -f "${file}" -o "${target}"'';
      };
    }
    .${type} or (throw "Unsupported compressor type for firmware.");

  args = {
    outputChecks.out.allowedRequisites = [ "out" ];
    __structuredAttrs = true;
    inherit (compressor) nativeBuildInputs;
  }
  // lib.optionalAttrs (firmware ? meta) { inherit (firmware) meta; };
in

runCommand "${firmware.name}-${type}" args ''
  mkdir -p $out/lib
  (cd ${firmware} && find lib/firmware -type d -print0) |
      (cd $out && xargs -0 mkdir -v --)
  (cd ${firmware} && find lib/firmware -type f -print0) |
      (cd $out && xargs -0rtP "$NIX_BUILD_CORES" -n1 \
          sh -c '${compressor.cmd "${firmware}/$1" "$1.${compressor.ext}"}' --)
  (cd ${firmware} && find lib/firmware -type l) | while read link; do
      target="$(readlink "${firmware}/$link")"
      if [ -f "${firmware}/$link" ]; then
        ln -vs -- "''${target/^${firmware}/$out}.${compressor.ext}" "$out/$link.${compressor.ext}"
      else
        ln -vs -- "''${target/^${firmware}/$out}" "$out/$link"
      fi
  done

  echo "Checking for broken symlinks:"
  find -L $out -type l -print -execdir false -- '{}' '+'
''