about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/libraries/vapoursynth/plugin-interface.nix
blob: f962634b131e7184b9600f9b361bae214428333c (plain) (blame)
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
109
110
111
112
113
114
{ lib, python3, buildEnv, writeText, runCommandCC, stdenv, runCommand
, vapoursynth, makeWrapper, withPlugins }:

plugins: let
  pythonEnvironment = python3.buildEnv.override {
    extraLibs = plugins;
  };

  getRecursivePropagatedBuildInputs = pkgs: lib.flatten
    (map
      (pkg: let cleanPropagatedBuildInputs = lib.filter lib.isDerivation pkg.propagatedBuildInputs;
        in cleanPropagatedBuildInputs ++ (getRecursivePropagatedBuildInputs cleanPropagatedBuildInputs))
      pkgs);

  deepPlugins = lib.unique (plugins ++ (getRecursivePropagatedBuildInputs plugins));

  pluginsEnv = buildEnv {
    name = "vapoursynth-plugins-env";
    pathsToLink = [ "/lib/vapoursynth" ];
    paths = deepPlugins;
  };

  pluginLoader = let
    source = writeText "vapoursynth-nix-plugins.c" ''
      void VSLoadPluginsNix(void (*load)(void *data, const char *path), void *data) {
      ${lib.concatMapStringsSep "" (path: "load(data, \"${path}/lib/vapoursynth\");") deepPlugins}
      }
    '';
  in
  runCommandCC "vapoursynth-plugin-loader" {
    executable = true;
    preferLocalBuild = true;
    allowSubstitutes = false;
  } ''
    mkdir -p $out/lib
    $CC -shared -fPIC ${source} -o "$out/lib/libvapoursynth-nix-plugins${ext}"
  '';

  ext = stdenv.hostPlatform.extensions.sharedLibrary;
in
runCommand "${vapoursynth.name}-with-plugins" {
  nativeBuildInputs = [ makeWrapper ];
  passthru = {
    inherit python3;
    inherit (vapoursynth) src version;
    withPlugins = plugins': withPlugins (plugins ++ plugins');
  };
} ''
  mkdir -p \
    $out/bin \
    $out/lib/pkgconfig \
    $out/lib/vapoursynth \
    $out/${python3.sitePackages}

  for textFile in \
      lib/pkgconfig/vapoursynth{,-script}.pc \
      lib/libvapoursynth.la \
      lib/libvapoursynth-script.la \
      ${python3.sitePackages}/vapoursynth.la
  do
      substitute ${vapoursynth}/$textFile $out/$textFile \
          --replace "${vapoursynth}" "$out"
  done

  for binaryPlugin in ${pluginsEnv}/lib/vapoursynth/*; do
      ln -s $binaryPlugin $out/''${binaryPlugin#"${pluginsEnv}/"}
  done

  for pythonPlugin in ${pythonEnvironment}/${python3.sitePackages}/*; do
      ln -s $pythonPlugin $out/''${pythonPlugin#"${pythonEnvironment}/"}
  done

  for binaryFile in \
      lib/libvapoursynth${ext} \
      lib/libvapoursynth-script${ext}.0.0.0
  do
    old_rpath=$(patchelf --print-rpath ${vapoursynth}/$binaryFile)
    new_rpath="$old_rpath:$out/lib"
    patchelf \
        --set-rpath "$new_rpath" \
        --output $out/$binaryFile \
        ${vapoursynth}/$binaryFile
    patchelf \
        --add-needed libvapoursynth-nix-plugins${ext} \
        $out/$binaryFile
  done

  for binaryFile in \
      ${python3.sitePackages}/vapoursynth${ext} \
      bin/.vspipe-wrapped
  do
      old_rpath=$(patchelf --print-rpath ${vapoursynth}/$binaryFile)
      new_rpath="''${old_rpath//"${vapoursynth}"/"$out"}"
      patchelf \
          --set-rpath "$new_rpath" \
          --output $out/$binaryFile \
          ${vapoursynth}/$binaryFile
  done

  ln -s \
      ${pluginLoader}/lib/libvapoursynth-nix-plugins${ext} \
      $out/lib/libvapoursynth-nix-plugins${ext}
  ln -s ${vapoursynth}/include $out/include
  ln -s ${vapoursynth}/lib/vapoursynth/* $out/lib/vapoursynth
  ln -s \
      libvapoursynth-script${ext}.0.0.0 \
      $out/lib/libvapoursynth-script${ext}
  ln -s \
      libvapoursynth-script${ext}.0.0.0 \
      $out/lib/libvapoursynth-script${ext}.0

  makeWrapper $out/bin/.vspipe-wrapped $out/bin/vspipe \
      --prefix PYTHONPATH : $out/${python3.sitePackages}
''