1{ config
2, stdenv
3, lib
4, fetchurl
5, pkg-config
6, zlib
7, expat
8, openssl
9, autoconf
10, libjpeg
11, libpng
12, libtiff
13, freetype
14, fontconfig
15, libpaper
16, jbig2dec
17, libiconv
18, ijs
19, lcms2
20, callPackage
21, bash
22, buildPackages
23, openjpeg
24, cupsSupport ? config.ghostscript.cups or (!stdenv.isDarwin)
25, cups
26, x11Support ? cupsSupport
27, xorg # with CUPS, X11 only adds very little
28, dynamicDrivers ? true
29
30# for passthru.tests
31, graphicsmagick
32, imagemagick
33, libspectre
34, lilypond
35, pstoedit
36, python3
37}:
38
39let
40 fonts = stdenv.mkDerivation {
41 name = "ghostscript-fonts";
42
43 srcs = [
44 (fetchurl {
45 url = "mirror://sourceforge/gs-fonts/ghostscript-fonts-std-8.11.tar.gz";
46 sha256 = "00f4l10xd826kak51wsmaz69szzm2wp8a41jasr4jblz25bg7dhf";
47 })
48 (fetchurl {
49 url = "mirror://gnu/ghostscript/gnu-gs-fonts-other-6.0.tar.gz";
50 sha256 = "1cxaah3r52qq152bbkiyj2f7dx1rf38vsihlhjmrvzlr8v6cqil1";
51 })
52 # ... add other fonts here
53 ];
54
55 installPhase = ''
56 mkdir "$out"
57 mv -v * "$out/"
58 '';
59 };
60
61in
62stdenv.mkDerivation rec {
63 pname = "ghostscript${lib.optionalString x11Support "-with-X"}";
64 version = "10.01.1";
65
66 src = fetchurl {
67 url = "https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs${lib.replaceStrings ["."] [""] version}/ghostscript-${version}.tar.xz";
68 hash = "sha512-2US+norvaNEXbWTEDbb6htVdDJ4wBH8hR8AoBqthz+msLLANTlshj/PFHMbtR87/4brE3Z1MwXYLeXTzDGwnNQ==";
69 };
70
71 patches = [
72 ./urw-font-files.patch
73 ./doc-no-ref.diff
74 ];
75
76 outputs = [ "out" "man" "doc" ];
77
78 enableParallelBuilding = true;
79
80 depsBuildBuild = [
81 buildPackages.stdenv.cc
82 ];
83
84 nativeBuildInputs = [ pkg-config autoconf zlib ]
85 ++ lib.optional cupsSupport cups;
86
87 buildInputs = [
88 zlib expat openssl
89 libjpeg libpng libtiff freetype fontconfig libpaper jbig2dec
90 libiconv ijs lcms2 bash openjpeg
91 ]
92 ++ lib.optionals x11Support [ xorg.libICE xorg.libX11 xorg.libXext xorg.libXt ]
93 ++ lib.optional cupsSupport cups
94 ;
95
96 preConfigure = ''
97 # https://ghostscript.com/doc/current/Make.htm
98 export CCAUX=$CC_FOR_BUILD
99 ${lib.optionalString cupsSupport ''export CUPSCONFIG="${cups.dev}/bin/cups-config"''}
100
101 rm -rf jpeg libpng zlib jasper expat tiff lcms2mt jbig2dec freetype cups/libs ijs openjpeg
102
103 sed "s@if ( test -f \$(INCLUDE)[^ ]* )@if ( true )@; s@INCLUDE=/usr/include@INCLUDE=/no-such-path@" -i base/unix-aux.mak
104 sed "s@^ZLIBDIR=.*@ZLIBDIR=${zlib.dev}/include@" -i configure.ac
105
106 autoconf
107 '';
108
109 configureFlags = [
110 "--with-system-libtiff"
111 "--without-tesseract"
112 ] ++ lib.optionals dynamicDrivers [
113 "--enable-dynamic"
114 "--disable-hidden-visibility"
115 ] ++ lib.optionals x11Support [
116 "--with-x"
117 ] ++ lib.optionals cupsSupport [
118 "--enable-cups"
119 ];
120
121 # make check does nothing useful
122 doCheck = false;
123
124 # don't build/install statically linked bin/gs
125 buildFlags = [ "so" ];
126 installTargets = [ "soinstall" ];
127
128 postInstall = ''
129 ln -s gsc "$out"/bin/gs
130
131 cp -r Resource "$out/share/ghostscript/${version}"
132
133 ln -s "${fonts}" "$out/share/ghostscript/fonts"
134 '' + lib.optionalString stdenv.isDarwin ''
135 for file in $out/lib/*.dylib* ; do
136 install_name_tool -id "$file" $file
137 done
138 '';
139
140 # dynamic library name only contains maj.min, eg. '9.53'
141 dylib_version = lib.versions.majorMinor version;
142 preFixup = lib.optionalString stdenv.isDarwin ''
143 install_name_tool -change libgs.dylib.$dylib_version $out/lib/libgs.dylib.$dylib_version $out/bin/gs
144 '';
145
146 # validate dynamic linkage
147 doInstallCheck = true;
148 installCheckPhase = ''
149 runHook preInstallCheck
150
151 $out/bin/gs --version
152 pushd examples
153 for f in *.{ps,eps,pdf}; do
154 echo "Rendering $f"
155 $out/bin/gs \
156 -dNOPAUSE \
157 -dBATCH \
158 -sDEVICE=bitcmyk \
159 -sOutputFile=/dev/null \
160 -r600 \
161 -dBufferSpace=100000 \
162 $f
163 done
164 popd # examples
165
166 runHook postInstallCheck
167 '';
168
169 passthru.tests = {
170 test-corpus-render = callPackage ./test-corpus-render.nix {};
171 inherit graphicsmagick imagemagick libspectre lilypond pstoedit;
172 inherit (python3.pkgs) matplotlib;
173 };
174
175 meta = {
176 homepage = "https://www.ghostscript.com/";
177 description = "PostScript interpreter (mainline version)";
178 longDescription = ''
179 Ghostscript is the name of a set of tools that provides (i) an
180 interpreter for the PostScript language and the PDF file format,
181 (ii) a set of C procedures (the Ghostscript library) that
182 implement the graphics capabilities that appear as primitive
183 operations in the PostScript language, and (iii) a wide variety
184 of output drivers for various file formats and printers.
185 '';
186 license = lib.licenses.agpl3;
187 platforms = lib.platforms.all;
188 maintainers = [ lib.maintainers.viric ];
189 mainProgram = "gs";
190 };
191}