1{ lib, stdenv, substituteAll, fetchurl
2, zlib ? null, zlibSupport ? true, bzip2, pkg-config, libffi, libunwind, Security
3, sqlite, openssl, ncurses, python, expat, tcl, tk, tix, xlibsWrapper, libX11
4, self, gdbm, db, xz
5, python-setup-hook
6# For the Python package set
7, packageOverrides ? (self: super: {})
8, pkgsBuildBuild
9, pkgsBuildHost
10, pkgsBuildTarget
11, pkgsHostHost
12, pkgsTargetTarget
13, sourceVersion
14, pythonVersion
15, sha256
16, passthruFun
17, pythonAttr ? "pypy${lib.substring 0 1 pythonVersion}${lib.substring 2 3 pythonVersion}"
18}:
19
20assert zlibSupport -> zlib != null;
21
22with lib;
23
24let
25 isPy3k = substring 0 1 pythonVersion == "3";
26 passthru = passthruFun {
27 inherit self sourceVersion pythonVersion packageOverrides;
28 implementation = "pypy";
29 libPrefix = "pypy${pythonVersion}";
30 executable = "pypy${if isPy3k then "3" else ""}";
31 sitePackages = "site-packages";
32 hasDistutilsCxxPatch = false;
33
34 pythonOnBuildForBuild = pkgsBuildBuild.${pythonAttr};
35 pythonOnBuildForHost = pkgsBuildHost.${pythonAttr};
36 pythonOnBuildForTarget = pkgsBuildTarget.${pythonAttr};
37 pythonOnHostForHost = pkgsHostHost.${pythonAttr};
38 pythonOnTargetForTarget = pkgsTargetTarget.${pythonAttr} or {};
39 };
40 pname = passthru.executable;
41 version = with sourceVersion; "${major}.${minor}.${patch}";
42 pythonForPypy = python.withPackages (ppkgs: [ ppkgs.pycparser ]);
43
44in with passthru; stdenv.mkDerivation rec {
45 inherit pname version;
46
47 src = fetchurl {
48 url = "https://downloads.python.org/pypy/pypy${pythonVersion}-v${version}-src.tar.bz2";
49 inherit sha256;
50 };
51
52 nativeBuildInputs = [ pkg-config ];
53 buildInputs = [
54 bzip2 openssl pythonForPypy libffi ncurses expat sqlite tk tcl xlibsWrapper libX11 gdbm db
55 ] ++ optionals isPy3k [
56 xz
57 ] ++ optionals (stdenv ? cc && stdenv.cc.libc != null) [
58 stdenv.cc.libc
59 ] ++ optionals zlibSupport [
60 zlib
61 ] ++ optionals stdenv.isDarwin [
62 libunwind Security
63 ];
64
65 hardeningDisable = optional stdenv.isi686 "pic";
66
67 # Remove bootstrap python from closure
68 dontPatchShebangs = true;
69 disallowedReferences = [ python ];
70
71 C_INCLUDE_PATH = makeSearchPathOutput "dev" "include" buildInputs;
72 LIBRARY_PATH = makeLibraryPath buildInputs;
73 LD_LIBRARY_PATH = makeLibraryPath (filter (x : x.outPath != stdenv.cc.libc.outPath or "") buildInputs);
74
75 patches = [
76 ./dont_fetch_vendored_deps.patch
77
78 (substituteAll {
79 src = ./tk_tcl_paths.patch;
80 inherit tk tcl;
81 tk_dev = tk.dev;
82 tcl_dev = tcl;
83 tk_libprefix = tk.libPrefix;
84 tcl_libprefix = tcl.libPrefix;
85 })
86
87 (substituteAll {
88 src = ./sqlite_paths.patch;
89 inherit (sqlite) out dev;
90 })
91 ];
92
93 postPatch = ''
94 substituteInPlace lib_pypy/pypy_tools/build_cffi_imports.py \
95 --replace "multiprocessing.cpu_count()" "$NIX_BUILD_CORES"
96
97 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'"
98 '';
99
100 buildPhase = ''
101 ${pythonForPypy.interpreter} rpython/bin/rpython \
102 --make-jobs="$NIX_BUILD_CORES" \
103 -Ojit \
104 --batch pypy/goal/targetpypystandalone.py
105 '';
106
107 setupHook = python-setup-hook sitePackages;
108
109 # TODO: A bunch of tests are failing as of 7.1.1, please feel free to
110 # fix and re-enable if you have the patience and tenacity.
111 doCheck = false;
112 checkPhase = let
113 disabledTests = [
114 # disable shutils because it assumes gid 0 exists
115 "test_shutil"
116 # disable socket because it has two actual network tests that fail
117 "test_socket"
118 ] ++ optionals (!isPy3k) [
119 # disable test_urllib2net, test_urllib2_localnet, and test_urllibnet because they require networking (example.com)
120 "test_urllib2net"
121 "test_urllibnet"
122 "test_urllib2_localnet"
123 ] ++ optionals isPy3k [
124 # disable asyncio due to https://github.com/NixOS/nix/issues/1238
125 "test_asyncio"
126 # disable os due to https://github.com/NixOS/nixpkgs/issues/10496
127 "test_os"
128 # disable pathlib due to https://bitbucket.org/pypy/pypy/pull-requests/594
129 "test_pathlib"
130 # disable tarfile because it assumes gid 0 exists
131 "test_tarfile"
132 # disable __all__ because of spurious imp/importlib warning and
133 # warning-to-error test policy
134 "test___all__"
135 ];
136 in ''
137 export TERMINFO="${ncurses.out}/share/terminfo/";
138 export TERM="xterm";
139 export HOME="$TMPDIR";
140
141 ${pythonForPypy.interpreter} ./pypy/test_all.py --pypy=./${executable}-c -k 'not (${concatStringsSep " or " disabledTests})' lib-python
142 '';
143
144 installPhase = ''
145 mkdir -p $out/{bin,include,lib,${executable}-c}
146
147 cp -R {include,lib_pypy,lib-python,${executable}-c} $out/${executable}-c
148 cp lib${executable}-c${stdenv.hostPlatform.extensions.sharedLibrary} $out/lib/
149 ln -s $out/${executable}-c/${executable}-c $out/bin/${executable}
150
151 # other packages expect to find stuff according to libPrefix
152 ln -s $out/${executable}-c/include $out/include/${libPrefix}
153 ln -s $out/${executable}-c/lib-python/${if isPy3k then "3" else pythonVersion} $out/lib/${libPrefix}
154
155 ${lib.optionalString stdenv.isDarwin ''
156 install_name_tool -change @rpath/libpypy${optionalString isPy3k "3"}-c.dylib $out/lib/libpypy${optionalString isPy3k "3"}-c.dylib $out/bin/${executable}
157 ''}
158
159 # verify cffi modules
160 $out/bin/${executable} -c ${if isPy3k then "'import tkinter;import sqlite3;import curses;import lzma'" else "'import Tkinter;import sqlite3;import curses'"}
161
162 # Include a sitecustomize.py file
163 cp ${../sitecustomize.py} $out/lib/${libPrefix}/${sitePackages}/sitecustomize.py
164 '';
165
166 inherit passthru;
167 enableParallelBuilding = true; # almost no parallelization without STM
168
169 meta = with lib; {
170 homepage = "http://pypy.org/";
171 description = "Fast, compliant alternative implementation of the Python language (${pythonVersion})";
172 license = licenses.mit;
173 platforms = [ "aarch64-linux" "i686-linux" "x86_64-linux" "x86_64-darwin" ];
174 maintainers = with maintainers; [ andersk ];
175 };
176}