#
# scopehal apps, including glscopeclient and optionally ngscopeclient
#
# vim: et:ts=2:sw=2:
#
{ deprekages, pkgs, lib, cmake, pkg-config, glib, ... }:
pkgs.stdenv.mkDerivation rec {
  pname = "scopehal-sigrok-bridge";
  version = "git";

  src = pkgs.fetchgit {
    url = "https://github.com/glscopeclient/scopehal-sigrok-bridge.git";
    rev = "cdd4d31e8c362561592b7551617d681a2ffd1c05";
    sha256 = "sha256-9dz5poVUSyDMCQ3BwJsAerVudPuxpEGfK1clPqeoqGc=";
    fetchSubmodules = true;
  };

  nativeBuildInputs = [
    cmake
    pkg-config
    pkgs.git
  ];

  buildInputs = with pkgs; [
    libusb1
    glib
    glib.dev
    deprekages.libsigrok4DSL
  ];

  cmakeFlags = [ "-DCMAKE_BUILD_TYPE=Release" ];

  # Currently, this fails to build because it's passing __PRETTY_FUNCTION__ to printf.
  # __PRETTY_FUNCTION__ resolves to a function, rather than a string, which triggers -Wformat-security.
  # We know we control that, so we'll just disable the error.
  patches = [
    # Build specifically libsigrok4DSL, not the rest of dsview.
    ./sigrok-bridge_noformat.patch
  ];

  # This cmake recipe has no install step; so we'll just splork it in place.
  installPhase = ''
    mkdir -p $out/bin
    cp scopehal-sigrok-bridge $out/bin/
  '';


  meta = {
    description = "bridge from libsigrok (with DreamSourceLabs devices) to libscopehal/glscopeclient";

    longDescription =
      '' Bridge that allows glscopeclient -- or other libscopehal apps -- to access devices provided by libsigrok.
         Can be connected to using the _twinlan_ endpoint.
      '';

    homepage = "https://www.antikernel.net/temp/glscopeclient-manual.pdf";
    license = pkgs.lib.licenses.bsd3;
  };
}
