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