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