hoarder: 0.21.0 -> 0.22.0
[NixPkgs.git] / pkgs / by-name / mu / mupdf / package.nix
blob93839bb440234b21350f5df8acd8806be032d6dd
2   stdenv,
3   lib,
4   fetchurl,
5   fetchFromGitHub,
6   copyDesktopItems,
7   makeDesktopItem,
8   desktopToDarwinBundle,
9   buildPackages,
10   pkg-config,
11   fixDarwinDylibNames,
12   freetype,
13   harfbuzz,
14   openjpeg,
15   jbig2dec,
16   libjpeg,
17   darwin,
18   gumbo,
19   enableX11 ? (!stdenv.hostPlatform.isDarwin),
20   libX11,
21   libXext,
22   libXi,
23   libXrandr,
24   enableCurl ? true,
25   curl,
26   openssl,
27   enableGL ? true,
28   freeglut,
29   libGLU,
30   enableOcr ? false,
31   leptonica,
32   tesseract,
33   enableCxx ? false,
34   python3,
35   enablePython ? false,
36   which,
37   swig,
38   xcbuild,
39   gitUpdater,
41   # for passthru.tests
42   cups-filters,
43   zathura,
44   mupdf,
47 assert enablePython -> enableCxx;
49 let
51   freeglut-mupdf = freeglut.overrideAttrs (old: rec {
52     pname = "freeglut-mupdf";
53     version = "3.0.0-r${src.rev}";
54     src = fetchFromGitHub {
55       owner = "ArtifexSoftware";
56       repo = "thirdparty-freeglut";
57       rev = "13ae6aa2c2f9a7b4266fc2e6116c876237f40477";
58       hash = "sha256-0fuE0lm9rlAaok2Qe0V1uUrgP4AjMWgp3eTbw8G6PMM=";
59     };
60   });
64 stdenv.mkDerivation rec {
65   version = "1.25.2";
66   pname = "mupdf";
68   src = fetchurl {
69     url = "https://mupdf.com/downloads/archive/${pname}-${version}-source.tar.gz";
70     hash = "sha256-Nsz2peaR4Yis+NtumNCL8F8nu0zjBDLcFfx20ympLU0=";
71   };
73   patches = [
74     # Upstream makefile does not work with system deps on macOS by default, so
75     # we reuse the Linux section instead.
76     ./fix-darwin-system-deps.patch
77     # Upstream C++ wrap script only defines fixed-sized integers on macOS but
78     # this is required on aarch64-linux too.
79     ./fix-cpp-build.patch
80   ];
82   postPatch = ''
83     substituteInPlace Makerules --replace "(shell pkg-config" "(shell $PKG_CONFIG"
85     patchShebangs scripts/mupdfwrap.py
87     # slip in makeFlags when building bindings
88     sed -i -e 's/^\( *make_args *=\)/\1 """ $(echo ''${makeFlagsArray[@]@Q})"""/' scripts/wrap/__main__.py
90     # fix libclang unnamed struct format
91     for wrapper in ./scripts/wrap/{cpp,state}.py; do
92       substituteInPlace "$wrapper" --replace 'struct (unnamed' '(unnamed struct'
93     done
94   '';
96   makeFlags =
97     [
98       "prefix=$(out)"
99       "shared=yes"
100       "USE_SYSTEM_LIBS=yes"
101       "PKG_CONFIG=${buildPackages.pkg-config}/bin/${buildPackages.pkg-config.targetPrefix}pkg-config"
102     ]
103     ++ lib.optionals (!enableX11) [ "HAVE_X11=no" ]
104     ++ lib.optionals (!enableGL) [ "HAVE_GLUT=no" ]
105     ++ lib.optionals (enableOcr) [ "USE_TESSERACT=yes" ];
107   nativeBuildInputs =
108     [ pkg-config ]
109     ++ lib.optional (enableGL || enableX11) copyDesktopItems
110     ++ lib.optional (stdenv.hostPlatform.isDarwin && (enableGL || enableX11)) desktopToDarwinBundle
111     ++ lib.optionals (enableCxx || enablePython) [
112       (python3.pythonOnBuildForHost.withPackages (ps: [
113         ps.setuptools
114         ps.libclang
115       ]))
116     ]
117     ++ lib.optionals (enablePython) [
118       which
119       swig
120     ]
121     ++ lib.optionals stdenv.hostPlatform.isDarwin [
122       fixDarwinDylibNames
123       xcbuild
124     ];
126   buildInputs =
127     [
128       freetype
129       harfbuzz
130       openjpeg
131       jbig2dec
132       libjpeg
133       gumbo
134     ]
135     ++ lib.optionals enableX11 [
136       libX11
137       libXext
138       libXi
139       libXrandr
140     ]
141     ++ lib.optionals enableCurl [
142       curl
143       openssl
144     ]
145     ++ lib.optionals enableGL (
146       if stdenv.hostPlatform.isDarwin then
147         with darwin.apple_sdk.frameworks;
148         [
149           GLUT
150           OpenGL
151         ]
152       else
153         [
154           freeglut-mupdf
155           libGLU
156         ]
157     )
158     ++ lib.optionals enableOcr [
159       leptonica
160       tesseract
161     ];
163   outputs = [
164     "bin"
165     "dev"
166     "out"
167     "man"
168     "doc"
169   ];
171   preConfigure = ''
172     # Don't remove mujs because upstream version is incompatible
173     rm -rf thirdparty/{curl,freetype,glfw,harfbuzz,jbig2dec,libjpeg,openjpeg,zlib}
174   '';
176   postBuild = lib.optionalString (enableCxx || enablePython) ''
177     for dir in build/*; do
178       ./scripts/mupdfwrap.py -d "$dir" -b ${lib.optionalString (enableCxx) "01"}${lib.optionalString (enablePython) "23"}
179     done
180   '';
182   desktopItems = lib.optionals (enableGL || enableX11) [
183     (makeDesktopItem {
184       name = pname;
185       desktopName = pname;
186       comment = meta.description;
187       icon = "mupdf";
188       exec = "${pname} %f";
189       terminal = false;
190       mimeTypes = [
191         "application/epub+zip"
192         "application/oxps"
193         "application/pdf"
194         "application/vnd.ms-xpsdocument"
195         "application/x-cbz"
196         "application/x-pdf"
197       ];
198       categories = [
199         "Graphics"
200         "Viewer"
201       ];
202       keywords = [
203         "mupdf"
204         "comic"
205         "document"
206         "ebook"
207         "viewer"
208         "cbz"
209         "epub"
210         "fb2"
211         "pdf"
212         "xps"
213       ];
214     })
215   ];
217   postInstall =
218     ''
219       mkdir -p "$out/lib/pkgconfig"
220       cat >"$out/lib/pkgconfig/mupdf.pc" <<EOF
221       prefix=$out
222       libdir=\''${prefix}/lib
223       includedir=\''${prefix}/include
225       Name: mupdf
226       Description: Library for rendering PDF documents
227       Version: ${version}
228       Libs: -L\''${libdir} -lmupdf
229       Cflags: -I\''${includedir}
230       EOF
232       moveToOutput "bin" "$bin"
233     ''
234     + (lib.optionalString (stdenv.hostPlatform.isDarwin) ''
235       for exe in $bin/bin/*; do
236         install_name_tool -change build/shared-release/libmupdf.dylib $out/lib/libmupdf.dylib "$exe"
237       done
238     '')
239     + (lib.optionalString (enableX11 || enableGL) ''
240       mkdir -p $bin/share/icons/hicolor/48x48/apps
241       cp docs/logo/mupdf-icon-48.png $bin/share/icons/hicolor/48x48/apps
242     '')
243     + (
244       if enableGL then
245         ''
246           ln -s "$bin/bin/mupdf-gl" "$bin/bin/mupdf"
247         ''
248       else
249         lib.optionalString (enableX11) ''
250           ln -s "$bin/bin/mupdf-x11" "$bin/bin/mupdf"
251         ''
252     )
253     + (lib.optionalString (enableCxx) ''
254       cp platform/c++/include/mupdf/*.h $out/include/mupdf
255       cp build/*/libmupdfcpp.so $out/lib
256     '')
257     + (lib.optionalString (enablePython) (
258       ''
259         mkdir -p $out/${python3.sitePackages}/mupdf
260         cp build/*/_mupdf.so $out/${python3.sitePackages}/mupdf
261         cp build/*/mupdf.py $out/${python3.sitePackages}/mupdf/__init__.py
262       ''
263       + lib.optionalString (stdenv.hostPlatform.isDarwin) ''
264         install_name_tool -add_rpath $out/lib $out/${python3.sitePackages}/mupdf/_mupdf.so
265       ''
266     ));
268   enableParallelBuilding = true;
270   env.USE_SONAME = "no";
272   passthru = {
273     tests = {
274       inherit cups-filters zathura;
275       inherit (python3.pkgs) pikepdf pymupdf;
276       mupdf-all = mupdf.override {
277         enableCurl = true;
278         enableGL = true;
279         enableOcr = true;
280         enableCxx = true;
281         enablePython = true;
282       };
283     };
285     updateScript = gitUpdater {
286       url = "https://git.ghostscript.com/mupdf.git";
287       ignoredVersions = ".rc.*";
288     };
289   };
291   meta = with lib; {
292     homepage = "https://mupdf.com";
293     description = "Lightweight PDF, XPS, and E-book viewer and toolkit written in portable C";
294     changelog = "https://git.ghostscript.com/?p=mupdf.git;a=blob_plain;f=CHANGES;hb=${version}";
295     license = licenses.agpl3Plus;
296     maintainers = with maintainers; [ fpletz ];
297     platforms = platforms.unix;
298     mainProgram = "mupdf";
299   };