Unverified Commit a1071d00 authored by Michele Guerini Rocco's avatar Michele Guerini Rocco Committed by GitHub
Browse files

vapoursynth: Simplify plugin loading (#332800)

parents 1dd9d10b 1a66c5bf
Loading
Loading
Loading
Loading
+0 −28
Original line number Diff line number Diff line
diff --git a/src/core/vscore.cpp b/src/core/vscore.cpp
index 73e2eafc..66a01326 100644
--- a/src/core/vscore.cpp
+++ b/src/core/vscore.cpp
@@ -1779,6 +1779,12 @@ void VSCore::isPortableInit() {
 }
 #endif
 
+void __attribute__((weak)) VSLoadPluginsNix(void (*load)(VSCore *core, const std::filesystem::path &), VSCore *);
+
+static void VSLoadPluginsNixCallback(VSCore *core, const std::filesystem::path &path) {
+    core->loadAllPluginsInPath(path);
+}
+
 VSCore::VSCore(int flags) :
     numFilterInstances(1),
     numFunctionInstances(0),
@@ -1890,6 +1896,10 @@ VSCore::VSCore(int flags) :
 #endif
     }
 
+    if (VSLoadPluginsNix != nullptr) {
+        VSLoadPluginsNix(VSLoadPluginsNixCallback, this);
+    };
+
     VSMap *settings = readSettings(configFile);
     const char *error = vs_internal_vsapi.mapGetError(settings);
     if (error) {
+21 −4
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@
  runCommandCC,
  runCommand,
  vapoursynth,
  writeText,
  buildEnv,
  zimg,
  libass,
@@ -29,8 +28,6 @@ stdenv.mkDerivation rec {
    hash = "sha256-T2bCVNH0dLM9lFYChXzvD6AJM3xEtOVCb2tI10tIXJs=";
  };

  patches = [ ./nix-plugin-loader.patch ];

  nativeBuildInputs = [
    pkg-config
    autoreconfHook
@@ -53,6 +50,7 @@ stdenv.mkDerivation rec {
    ];

  enableParallelBuilding = true;
  doInstallCheck = true;

  passthru = rec {
    # If vapoursynth is added to the build inputs of mpv and then
@@ -66,7 +64,6 @@ stdenv.mkDerivation rec {
        lib
        python3
        buildEnv
        writeText
        runCommandCC
        stdenv
        runCommand
@@ -83,6 +80,14 @@ stdenv.mkDerivation rec {
    };
  };

  postPatch = ''
    # Export weak symbol nixPluginDir to permit override of default plugin path
    sed -E -i \
      -e 's/(VS_PATH_PLUGINDIR)/(nixPluginDir ? nixPluginDir : \1)/g' \
      -e '1i\extern char const __attribute__((weak)) nixPluginDir[];' \
      src/core/vscore.cpp
  '';

  postInstall = ''
    wrapProgram $out/bin/vspipe \
        --prefix PYTHONPATH : $out/${python3.sitePackages}
@@ -92,6 +97,18 @@ stdenv.mkDerivation rec {
    mkdir $out/lib/vapoursynth
  '';

  installCheckPhase = ''
    runHook preInstallCheck

    libv="$out/lib/libvapoursynth${stdenv.hostPlatform.extensions.sharedLibrary}"
    if ! $NM -g -P "$libv" | grep -q '^nixPluginDir w'; then
      echo "Weak symbol nixPluginDir is missing from $libv." >&2
      exit 1
    fi

    runHook postInstallCheck
  '';

  meta = with lib; {
    broken = stdenv.hostPlatform.isDarwin; # see https://github.com/NixOS/nixpkgs/pull/189446 for partial fix
    description = "Video processing framework with the future in mind";
+8 −21
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@
  lib,
  python3,
  buildEnv,
  writeText,
  runCommandCC,
  stdenv,
  runCommand,
@@ -35,29 +34,19 @@ let
    paths = deepPlugins;
  };

  pluginLoader =
    let
      source = writeText "vapoursynth-nix-plugins.cpp" ''
        #include <filesystem>

        struct VSCore;

        void VSLoadPluginsNix(void (*load)(VSCore *, const std::filesystem::path &), VSCore *core) {
        ${lib.concatMapStrings (
          path: ''load(core, std::filesystem::u8path("${path}/lib/vapoursynth"));''
        ) deepPlugins}
        }
      '';
    in
    runCommandCC "vapoursynth-plugin-loader"
  # Override default plugin path through nixPluginDir symbol
  nixPlugins =
    runCommandCC "libvapoursynth-nix-plugins${ext}"
      {
        executable = true;
        preferLocalBuild = true;
        allowSubstitutes = false;
        src = ''
          char const nixPluginDir[] = "${pluginsEnv}/lib/vapoursynth";
        '';
      }
      ''
        mkdir -p $out/lib
        $CXX -std=c++17 -shared -fPIC ${source} -o "$out/lib/libvapoursynth-nix-plugins${ext}"
        $CC -x c -shared -fPIC - -o "$out" <<<"$src"
      '';

  ext = stdenv.hostPlatform.extensions.sharedLibrary;
@@ -123,9 +112,7 @@ runCommand "${vapoursynth.name}-with-plugins"
            ${vapoursynth}/$binaryFile
    done

    ln -s \
        ${pluginLoader}/lib/libvapoursynth-nix-plugins${ext} \
        $out/lib/libvapoursynth-nix-plugins${ext}
    ln -s ${nixPlugins} $out/lib/libvapoursynth-nix-plugins${ext}
    ln -s ${vapoursynth}/include $out/include
    ln -s ${vapoursynth}/lib/vapoursynth/* $out/lib/vapoursynth
    ln -s \