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