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
# The Bluetooth source as ChromeOS builds it: sources.bluetooth with the
# patches of the floss ebuild applied, in the ebuild's order (REL_PATCHES,
# applied with `eapply -F0`, i.e. without fuzz). Both halves build from this,
# so the C++ and the Rust side always see the same source. Our own patches
# (../patches) come last.
{ pkgs, lib, floss, ... }:

let
  overlay = "${floss.sources.chromiumos-overlay-floss}/net-wireless/floss";
  ebuild = "${overlay}/floss-0.0.2-r8348.ebuild";
  # Patches written for ChromeOS's own libchrome (R158 ships d64569d4), not
  # for the older one floss/build/dpkg builds and we use (0519670b):
  #   1005  includes base/logging/logging_settings.h, a split of
  #         base/logging.h that our libchrome does not have yet; base/logging.h
  #         still declares everything mmc/main.cc uses
  skip = [ "1005-UPSTREAM-floss-Update-logging-include-files.patch" ];
  # ours, applied after ChromeOS's (see each patch's description)
  ours = lib.sort lib.lessThan (lib.filter (lib.hasSuffix ".patch")
    (lib.attrNames (builtins.readDir ../patches)));
in
pkgs.runCommandLocal "Bluetooth-chromeos-r158" { } ''
  cp -r ${floss.sources.bluetooth} $out
  chmod -R u+w $out
  # the patch list between "REL_PATCHES=(" and its closing paren
  sed --quiet '/^REL_PATCHES=(/,/^)/p' ${ebuild} \
    | grep --only-matching 'patches/[^ ]*\.patch' > patches
  [ -s patches ] || { echo "no REL_PATCHES in ${ebuild}"; exit 1; }
  while read -r p; do
    case " ${toString skip} " in
      *" ''${p#patches/} "*) echo "skipping $p (see default.nix)"; continue ;;
    esac
    echo "applying $p"
    patch --strip=1 --fuzz=0 --forward --directory=$out < ${overlay}/files/$p
  done < patches
  # then ours, from ../patches, in name order
  for p in ${lib.concatMapStringsSep " " (p: "${../patches}/${p}") ours}; do
    echo "applying ours: $(basename $p)"
    patch --strip=1 --fuzz=0 --forward --directory=$out < $p
  done
''