btrbk: add mainProgram (#356350)
[NixPkgs.git] / pkgs / by-name / va / vapoursynth / plugin-interface.nix
blob2716c27225cf15a132188981c08e20b7d7b5aa51
2   lib,
3   python3,
4   buildEnv,
5   runCommandCC,
6   stdenv,
7   runCommand,
8   vapoursynth,
9   makeWrapper,
10   withPlugins,
13 plugins:
14 let
15   pythonEnvironment = python3.buildEnv.override { extraLibs = plugins; };
17   getRecursivePropagatedBuildInputs =
18     pkgs:
19     lib.flatten (
20       map (
21         pkg:
22         let
23           cleanPropagatedBuildInputs = lib.filter lib.isDerivation pkg.propagatedBuildInputs;
24         in
25         cleanPropagatedBuildInputs ++ (getRecursivePropagatedBuildInputs cleanPropagatedBuildInputs)
26       ) pkgs
27     );
29   deepPlugins = lib.unique (plugins ++ (getRecursivePropagatedBuildInputs plugins));
31   pluginsEnv = buildEnv {
32     name = "vapoursynth-plugins-env";
33     pathsToLink = [ "/lib/vapoursynth" ];
34     paths = deepPlugins;
35   };
37   # Override default plugin path through nixPluginDir symbol
38   nixPlugins =
39     runCommandCC "libvapoursynth-nix-plugins${ext}"
40       {
41         executable = true;
42         preferLocalBuild = true;
43         allowSubstitutes = false;
44         src = ''
45           char const nixPluginDir[] = "${pluginsEnv}/lib/vapoursynth";
46         '';
47       }
48       ''
49         $CC -x c -shared -fPIC - -o "$out" <<<"$src"
50       '';
52   ext = stdenv.hostPlatform.extensions.sharedLibrary;
54 runCommand "${vapoursynth.name}-with-plugins"
55   {
56     nativeBuildInputs = [ makeWrapper ];
57     passthru = {
58       inherit python3;
59       inherit (vapoursynth) src version;
60       withPlugins = plugins': withPlugins (plugins ++ plugins');
61     };
62   }
63   ''
64     mkdir -p \
65       $out/bin \
66       $out/lib/pkgconfig \
67       $out/lib/vapoursynth \
68       $out/${python3.sitePackages}
70     for textFile in \
71         lib/pkgconfig/vapoursynth{,-script}.pc \
72         lib/libvapoursynth.la \
73         lib/libvapoursynth-script.la \
74         ${python3.sitePackages}/vapoursynth.la
75     do
76         substitute ${vapoursynth}/$textFile $out/$textFile \
77             --replace "${vapoursynth}" "$out"
78     done
80     for binaryPlugin in ${pluginsEnv}/lib/vapoursynth/*; do
81         ln -s $binaryPlugin $out/''${binaryPlugin#"${pluginsEnv}/"}
82     done
84     for pythonPlugin in ${pythonEnvironment}/${python3.sitePackages}/*; do
85         ln -s $pythonPlugin $out/''${pythonPlugin#"${pythonEnvironment}/"}
86     done
88     for binaryFile in \
89         lib/libvapoursynth${ext} \
90         lib/libvapoursynth-script${ext}.0.0.0
91     do
92       old_rpath=$(patchelf --print-rpath ${vapoursynth}/$binaryFile)
93       new_rpath="$old_rpath:$out/lib"
94       patchelf \
95           --set-rpath "$new_rpath" \
96           --output $out/$binaryFile \
97           ${vapoursynth}/$binaryFile
98       patchelf \
99           --add-needed libvapoursynth-nix-plugins${ext} \
100           $out/$binaryFile
101     done
103     for binaryFile in \
104         ${python3.sitePackages}/vapoursynth${ext} \
105         bin/.vspipe-wrapped
106     do
107         old_rpath=$(patchelf --print-rpath ${vapoursynth}/$binaryFile)
108         new_rpath="''${old_rpath//"${vapoursynth}"/"$out"}"
109         patchelf \
110             --set-rpath "$new_rpath" \
111             --output $out/$binaryFile \
112             ${vapoursynth}/$binaryFile
113     done
115     ln -s ${nixPlugins} $out/lib/libvapoursynth-nix-plugins${ext}
116     ln -s ${vapoursynth}/include $out/include
117     ln -s ${vapoursynth}/lib/vapoursynth/* $out/lib/vapoursynth
118     ln -s \
119         libvapoursynth-script${ext}.0.0.0 \
120         $out/lib/libvapoursynth-script${ext}
121     ln -s \
122         libvapoursynth-script${ext}.0.0.0 \
123         $out/lib/libvapoursynth-script${ext}.0
125     makeWrapper $out/bin/.vspipe-wrapped $out/bin/vspipe \
126         --prefix PYTHONPATH : $out/${python3.sitePackages}
127   ''