1{ lib
2, stdenv
3, fetchurl
4, python-setup-hook
5, self
6, which
7# Dependencies
8, bzip2
9, zlib
10, openssl_1_0_2
11, expat
12, ncurses6
13, tcl-8_5
14, tk-8_5
15# For the Python package set
16, packageOverrides ? (self: super: {})
17, sourceVersion
18, pythonVersion
19, sha256
20, passthruFun
21}:
22
23# This version of PyPy is primarily added to speed-up translation of
24# our PyPy source build when developing that expression.
25
26with lib;
27
28let
29 isPy3k = majorVersion == "3";
30 passthru = passthruFun {
31 inherit self sourceVersion pythonVersion packageOverrides;
32 implementation = "pypy";
33 libPrefix = "pypy${pythonVersion}";
34 executable = "pypy${if isPy3k then "3" else ""}";
35 sitePackages = "site-packages";
36 hasDistutilsCxxPatch = false;
37
38 # Not possible to cross-compile with.
39 pythonOnBuildForBuild = throw "${pname} does not support cross compilation";
40 pythonOnBuildForHost = self;
41 pythonOnBuildForTarget = throw "${pname} does not support cross compilation";
42 pythonOnHostForHost = throw "${pname} does not support cross compilation";
43 pythonOnTargetForTarget = throw "${pname} does not support cross compilation";
44 };
45 pname = "${passthru.executable}_prebuilt";
46 version = with sourceVersion; "${major}.${minor}.${patch}";
47
48 majorVersion = substring 0 1 pythonVersion;
49
50 deps = [
51 bzip2
52 zlib
53 openssl_1_0_2
54 expat
55 ncurses6
56 tcl-8_5
57 tk-8_5
58 ];
59
60in with passthru; stdenv.mkDerivation {
61 inherit pname version;
62
63 src = fetchurl {
64 url = "https://downloads.python.org/pypy/pypy${pythonVersion}-v${version}-linux64.tar.bz2";
65 inherit sha256;
66 };
67
68 buildInputs = [ which ];
69
70 installPhase = ''
71 mkdir -p $out/lib
72 echo "Moving files to $out"
73 mv -t $out bin include lib-python lib_pypy site-packages
74 mv lib/libffi.so.6* $out/lib/
75
76 mv $out/bin/libpypy*-c.so $out/lib/
77
78 rm $out/bin/*.debug
79
80 echo "Patching binaries"
81 interpreter=$(patchelf --print-interpreter $(readlink -f $(which patchelf)))
82 patchelf --set-interpreter $interpreter \
83 --set-rpath $out/lib \
84 $out/bin/pypy*
85
86 pushd $out
87 find {lib,lib_pypy*} -name "*.so" -exec patchelf --remove-needed libncursesw.so.6 --replace-needed libtinfow.so.6 libncursesw.so.6 {} \;
88 find {lib,lib_pypy*} -name "*.so" -exec patchelf --set-rpath ${lib.makeLibraryPath deps}:$out/lib {} \;
89
90 echo "Removing bytecode"
91 find . -name "__pycache__" -type d -depth -exec rm -rf {} \;
92 popd
93
94 # Include a sitecustomize.py file
95 cp ${../sitecustomize.py} $out/${sitePackages}/sitecustomize.py
96
97 '';
98
99 doInstallCheck = true;
100
101 # Check whether importing of (extension) modules functions
102 installCheckPhase = let
103 modules = [
104 "ssl"
105 "sys"
106 "curses"
107 ] ++ optionals (!isPy3k) [
108 "Tkinter"
109 ] ++ optionals isPy3k [
110 "tkinter"
111 ];
112 imports = concatMapStringsSep "; " (x: "import ${x}") modules;
113 in ''
114 echo "Testing whether we can import modules"
115 $out/bin/${executable} -c '${imports}'
116 '';
117
118 setupHook = python-setup-hook sitePackages;
119
120 donPatchElf = true;
121 dontStrip = true;
122
123 inherit passthru;
124
125 meta = with lib; {
126 homepage = "http://pypy.org/";
127 description = "Fast, compliant alternative implementation of the Python language (${pythonVersion})";
128 license = licenses.mit;
129 platforms = [ "x86_64-linux" ];
130 };
131
132}