1{ stdenv, substituteAll, fetchurl
2, zlib ? null, zlibSupport ? true, bzip2, pkgconfig, libffi
3, sqlite, openssl, ncurses, python, expat, tcl, tk, tix, xlibsWrapper, libX11
4, callPackage, self, gdbm, db, lzma
5, python-setup-hook
6# For the Python package set
7, packageOverrides ? (self: super: {})
8, sourceVersion
9, pythonVersion
10, sha256
11, passthruFun
12}:
13
14assert zlibSupport -> zlib != null;
15
16with stdenv.lib;
17
18let
19 isPy3k = substring 0 1 pythonVersion == "3";
20 passthru = passthruFun rec {
21 inherit self sourceVersion pythonVersion packageOverrides;
22 implementation = "pypy";
23 libPrefix = "pypy${pythonVersion}";
24 executable = "pypy${if isPy3k then "3" else ""}";
25 pythonForBuild = self; # No cross-compiling for now.
26 sitePackages = "site-packages";
27 };
28 pname = passthru.executable;
29 version = with sourceVersion; "${major}.${minor}.${patch}";
30 pythonForPypy = python.withPackages (ppkgs: [ ppkgs.pycparser ]);
31
32in with passthru; stdenv.mkDerivation rec {
33 inherit pname version;
34
35 src = fetchurl {
36 url = "https://bitbucket.org/pypy/pypy/get/release-pypy${pythonVersion}-v${version}.tar.bz2";
37 inherit sha256;
38 };
39
40 nativeBuildInputs = [ pkgconfig ];
41 buildInputs = [
42 bzip2 openssl pythonForPypy libffi ncurses expat sqlite tk tcl xlibsWrapper libX11 gdbm db
43 ] ++ optionals isPy3k [
44 lzma
45 ] ++ optionals (stdenv ? cc && stdenv.cc.libc != null) [
46 stdenv.cc.libc
47 ] ++ optionals zlibSupport [
48 zlib
49 ];
50
51 hardeningDisable = optional stdenv.isi686 "pic";
52
53 C_INCLUDE_PATH = makeSearchPathOutput "dev" "include" buildInputs;
54 LIBRARY_PATH = makeLibraryPath buildInputs;
55 LD_LIBRARY_PATH = makeLibraryPath (filter (x : x.outPath != stdenv.cc.libc.outPath or "") buildInputs);
56
57 patches = [
58 (substituteAll {
59 src = ./tk_tcl_paths.patch;
60 inherit tk tcl;
61 tk_dev = tk.dev;
62 tcl_dev = tcl;
63 tk_libprefix = tk.libPrefix;
64 tcl_libprefix = tcl.libPrefix;
65 })
66 ];
67
68 postPatch = ''
69 substituteInPlace "lib-python/${if isPy3k then "3/tkinter/tix.py" else "2.7/lib-tk/Tix.py"}" --replace "os.environ.get('TIX_LIBRARY')" "os.environ.get('TIX_LIBRARY') or '${tix}/lib'"
70
71 # hint pypy to find nix ncurses
72 substituteInPlace pypy/module/_minimal_curses/fficurses.py \
73 --replace "/usr/include/ncurses/curses.h" "${ncurses.dev}/include/curses.h" \
74 --replace "ncurses/curses.h" "${ncurses.dev}/include/curses.h" \
75 --replace "ncurses/term.h" "${ncurses.dev}/include/term.h" \
76 --replace "libraries=['curses']" "libraries=['ncurses']"
77
78 sed -i "s@libraries=\['sqlite3'\]\$@libraries=['sqlite3'], include_dirs=['${sqlite.dev}/include'], library_dirs=['${sqlite.out}/lib']@" lib_pypy/_sqlite3_build.py
79 '';
80
81 buildPhase = ''
82 ${pythonForPypy.interpreter} rpython/bin/rpython \
83 --make-jobs="$NIX_BUILD_CORES" \
84 -Ojit \
85 --batch pypy/goal/targetpypystandalone.py
86 '';
87
88 setupHook = python-setup-hook sitePackages;
89
90 doCheck = true;
91 checkPhase = let
92 disabledTests = [
93 # disable shutils because it assumes gid 0 exists
94 "test_shutil"
95 # disable socket because it has two actual network tests that fail
96 "test_socket"
97 ] ++ optionals (!isPy3k) [
98 # disable test_urllib2net, test_urllib2_localnet, and test_urllibnet because they require networking (example.com)
99 "test_urllib2net"
100 "test_urllibnet"
101 "test_urllib2_localnet"
102 ] ++ optionals isPy3k [
103 # disable asyncio due to https://github.com/NixOS/nix/issues/1238
104 "test_asyncio"
105 # disable os due to https://github.com/NixOS/nixpkgs/issues/10496
106 "test_os"
107 # disable pathlib due to https://bitbucket.org/pypy/pypy/pull-requests/594
108 "test_pathlib"
109 # disable tarfile because it assumes gid 0 exists
110 "test_tarfile"
111 ];
112 in ''
113 export TERMINFO="${ncurses.out}/share/terminfo/";
114 export TERM="xterm";
115 export HOME="$TMPDIR";
116
117 ${pythonForPypy.interpreter} ./pypy/test_all.py --pypy=./${executable}-c -k 'not (${concatStringsSep " or " disabledTests})' lib-python
118 '';
119
120 installPhase = ''
121 mkdir -p $out/{bin,include,lib,${executable}-c}
122
123 cp -R {include,lib_pypy,lib-python,${executable}-c} $out/${executable}-c
124 cp lib${executable}-c.so $out/lib/
125 ln -s $out/${executable}-c/${executable}-c $out/bin/${executable}
126
127 # other packages expect to find stuff according to libPrefix
128 ln -s $out/${executable}/include $out/include/${libPrefix}
129 ln -s $out/${executable}-c/lib-python/${if isPy3k then "3" else pythonVersion} $out/lib/${libPrefix}
130
131 # verify cffi modules
132 $out/bin/${executable} -c ${if isPy3k then "'import tkinter;import sqlite3;import curses;import lzma'" else "'import Tkinter;import sqlite3;import curses'"}
133
134 # Python on Nix is not manylinux1 compatible. https://github.com/NixOS/nixpkgs/issues/18484
135 echo "manylinux1_compatible=False" >> $out/lib/${libPrefix}/_manylinux.py
136 '';
137
138 inherit passthru;
139 enableParallelBuilding = true; # almost no parallelization without STM
140
141 meta = with stdenv.lib; {
142 homepage = http://pypy.org/;
143 description = "Fast, compliant alternative implementation of the Python language (${pythonVersion})";
144 license = licenses.mit;
145 platforms = [ "i686-linux" "x86_64-linux" ];
146 maintainers = with maintainers; [ andersk ];
147 };
148}