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