1{
2 stdenv,
3 lib,
4 fetchurl,
5 fetchpatch,
6 texliveSmall,
7 bison,
8 flex,
9 lapack,
10 blas,
11 autoreconfHook,
12 gmp,
13 mpfr,
14 pari,
15 ntl,
16 gsl,
17 mpfi,
18 ecm,
19 glpk,
20 nauty,
21 buildPackages,
22 readline,
23 gettext,
24 libpng,
25 libao,
26 gfortran,
27 perl,
28 enableGUI ? false,
29 libGL,
30 libGLU,
31 xorg,
32 fltk,
33 enableMicroPy ? false,
34 python3,
35}:
36
37assert (!blas.isILP64) && (!lapack.isILP64);
38
39stdenv.mkDerivation rec {
40 pname = "giac${lib.optionalString enableGUI "-with-xcas"}";
41 version = "1.9.0-993"; # TODO try to remove preCheck phase on upgrade
42
43 src = fetchurl {
44 url = "https://www-fourier.ujf-grenoble.fr/~parisse/debian/dists/stable/main/source/giac_${version}.tar.gz";
45 sha256 = "sha256-pqytFWrSWfEwQqRdRbaigGCq68s8mdgj2j8M+kclslE=";
46 };
47
48 patches = [
49 ./remove-old-functional-patterns.patch
50 ./fix-fltk-guard.patch
51
52 (fetchpatch {
53 name = "pari_2_15.patch";
54 url = "https://raw.githubusercontent.com/sagemath/sage/07a2afd65fb4b0a1c9cbc43ede7d4a18c921a000/build/pkgs/giac/patches/pari_2_15.patch";
55 sha256 = "sha256-Q3xBFED7XEAyNz6AHjzt63XtospmdGAIdS6iPq1C2UE=";
56 })
57
58 (fetchpatch {
59 name = "infinity.patch";
60 url = "https://github.com/geogebra/giac/commit/851c2cd91e879c79d6652f8a5d5bed03b65c6d39.patch";
61 sha256 = "sha256-WJRT2b8I9kgAkRuIugMiXoF4hT7yR7qyad8A6IspNTM=";
62 stripLen = 5;
63 extraPrefix = "/src/";
64 excludes = [ "src/kdisplay.cc" ];
65 })
66
67 # giac calls scanf/printf with non-constant first arguments, which
68 # the compiler rightfully warns about (with an error nowadays).
69 (fetchpatch {
70 name = "fix-string-compiler-error.patch";
71 url = "https://salsa.debian.org/science-team/giac/-/raw/9ca8dbf4bb16d9d96948aa4024326d32485d7917/debian/patches/fix-string-compiler-error.patch";
72 sha256 = "sha256-r+M+9MRPRqhHcdhYWI6inxyNvWbXUbBcPCeDY7aulvk=";
73 })
74
75 # issue with include path precedence
76 (fetchpatch {
77 name = "fix_implicit_declaration.patch";
78 url = "https://salsa.debian.org/science-team/giac/-/raw/c05ae9b9e74d3c6ee6411d391071989426a76201/debian/patches/fix_implicit_declaration.patch";
79 sha256 = "sha256-ompUceYJLiL0ftfjBkIMcYvX1YqG2/XA7e1yDyFY0IY=";
80 })
81 ]
82 ++ lib.optionals (!enableGUI) [
83 # when enableGui is false, giac is compiled without fltk. That
84 # means some outputs differ in the make check. Patch around this:
85 (fetchpatch {
86 name = "nofltk-check.patch";
87 url = "https://raw.githubusercontent.com/sagemath/sage/7553a3c8dfa7bcec07241a07e6a4e7dcf5bb4f26/build/pkgs/giac/patches/nofltk-check.patch";
88 sha256 = "sha256-nAl5q3ufLjK3X9s0qMlGNowdRRf3EaC24eVtJABzdXY=";
89 })
90 ];
91
92 # 1.9.0-5's tarball contains a binary (src/mkjs) which is executed
93 # at build time. we will delete and rebuild it.
94 depsBuildBuild = [ buildPackages.stdenv.cc ];
95
96 postPatch = ''
97 for i in doc/*/Makefile* micropython*/xcas/Makefile*; do
98 substituteInPlace "$i" --replace "/bin/cp" "cp";
99 done;
100 rm src/mkjs
101 substituteInPlace src/Makefile.am --replace "g++ mkjs.cc" \
102 "${buildPackages.stdenv.cc.targetPrefix}c++ mkjs.cc"
103
104 # to open help
105 substituteInPlace src/global.cc --replace 'browser="mozilla"' 'browser="xdg-open"'
106 '';
107
108 nativeBuildInputs = [
109 autoreconfHook
110 texliveSmall
111 bison
112 flex
113 ];
114
115 # perl is only needed for patchShebangs fixup.
116 buildInputs = [
117 gmp
118 mpfr
119 pari
120 ntl
121 gsl
122 blas
123 mpfi
124 glpk
125 nauty
126 readline
127 gettext
128 libpng
129 libao
130 perl
131 ecm
132 # gfortran.cc default output contains static libraries compiled without -fPIC
133 # we want libgfortran.so.3 instead
134 (lib.getLib gfortran.cc)
135 lapack
136 blas
137 ]
138 ++ lib.optionals enableGUI [
139 libGL
140 libGLU
141 fltk
142 xorg.libX11
143 ]
144 ++ lib.optional enableMicroPy python3;
145
146 # xcas Phys and Turtle menus are broken with split outputs
147 # and interactive use is likely to need docs
148 outputs = [ "out" ] ++ lib.optional (!enableGUI) "doc";
149
150 doCheck = true;
151 preCheck =
152 lib.optionalString (!enableGUI) ''
153 # even with the nofltk patch, some changes in src/misc.cc (grep
154 # for HAVE_LIBFLTK) made it so that giac behaves differently
155 # when fltk is disabled. disable these tests for now.
156 echo > check/chk_fhan2
157 echo > check/chk_fhan9
158 ''
159 + lib.optionalString (stdenv.hostPlatform.isDarwin) ''
160 # these cover a known regression in giac, likely due to how pari state
161 # is shared between multiple giac instances (see pari.cc.old).
162 # see https://github.com/NixOS/nixpkgs/pull/264126 for more information
163 echo > check/chk_fhan4
164 echo > check/chk_fhan6
165 '';
166
167 enableParallelBuilding = true;
168
169 configureFlags = [
170 "--enable-gc"
171 "--enable-png"
172 "--enable-gsl"
173 "--enable-lapack"
174 "--enable-pari"
175 "--enable-ntl"
176 "--enable-gmpxx" # "--enable-cocoa"
177 "--enable-ao"
178 "--enable-ecm"
179 "--enable-glpk"
180 ]
181 ++ lib.optionals enableGUI [
182 "--enable-gui"
183 "--with-x"
184 ]
185 ++ lib.optionals stdenv.hostPlatform.isDarwin [
186 "--disable-nls"
187 ]
188 ++ lib.optionals (!enableGUI) [
189 "--disable-fltk"
190 ]
191 ++ lib.optionals (!enableMicroPy) [
192 "--disable-micropy"
193 ];
194
195 postInstall = ''
196 # example Makefiles contain the full path to some commands
197 # notably texlive, and we don't want texlive to become a runtime
198 # dependency
199 for file in $(find $out -name Makefile) ; do
200 sed -i "s@${builtins.storeDir}/[^/]*/bin/@@" "$file" ;
201 done;
202
203 # reference cycle
204 rm "$out/share/giac/doc/el/"{casinter,tutoriel}/Makefile
205
206 if [ -n "$doc" ]; then
207 mkdir -p "$doc/share/giac"
208 # $out/share/giac/doc/aide_cas is a symlink to ../aide_cas
209 mv "$out/share/giac/doc" "$doc/share/giac"
210 ln -sf "$out/share/giac/aide_cas" "$doc/share/giac/doc/aide_cas"
211 mv "$out/share/giac/examples" "$doc/share/giac"
212 fi
213 ''
214 + lib.optionalString (!enableGUI) ''
215 for i in pixmaps application-registry applications icons; do
216 rm -r "$out/share/$i";
217 done;
218 '';
219
220 meta = with lib; {
221 description = "Free computer algebra system (CAS)";
222 homepage = "https://www-fourier.ujf-grenoble.fr/~parisse/giac.html";
223 license = licenses.gpl3Plus;
224 platforms = platforms.linux ++ (optionals (!enableGUI) platforms.darwin);
225 maintainers = [ maintainers.symphorien ];
226 };
227}