1{
2 lib,
3 stdenv,
4 buildPythonPackage,
5 fetchPypi,
6 unzip,
7 pythonOlder,
8 libGL,
9 libGLU,
10 xorg,
11 pytestCheckHook,
12 glibc,
13 gtk2-x11,
14 gdk-pixbuf,
15 fontconfig,
16 freetype,
17 ffmpeg-full,
18 openal,
19 libpulseaudio,
20 harfbuzz,
21 apple-sdk,
22}:
23
24buildPythonPackage rec {
25 version = "2.0.10";
26 format = "setuptools";
27 pname = "pyglet";
28 disabled = pythonOlder "3.6";
29
30 src = fetchPypi {
31 inherit pname version;
32 hash = "sha256-JCvrGzvWfFvr3+W6EexWtpathrUMbn8qMX+NeDJWuck=";
33 extension = "zip";
34 };
35
36 # find_library doesn't reliably work with nix (https://github.com/NixOS/nixpkgs/issues/7307).
37 # Even naively searching `LD_LIBRARY_PATH` won't work since `libc.so` is a linker script and
38 # ctypes.cdll.LoadLibrary cannot deal with those. Therefore, just hardcode the paths to the
39 # necessary libraries.
40 postPatch =
41 let
42 ext = stdenv.hostPlatform.extensions.sharedLibrary;
43 in
44 lib.optionalString stdenv.isLinux ''
45 cat > pyglet/lib.py <<EOF
46 import ctypes
47 def load_library(*names, **kwargs):
48 for name in names:
49 path = None
50 if name == 'GL':
51 path = '${libGL}/lib/libGL${ext}'
52 elif name == 'EGL':
53 path = '${libGL}/lib/libEGL${ext}'
54 elif name == 'GLU':
55 path = '${libGLU}/lib/libGLU${ext}'
56 elif name == 'c':
57 path = '${glibc}/lib/libc${ext}.6'
58 elif name == 'X11':
59 path = '${xorg.libX11}/lib/libX11${ext}'
60 elif name == 'gdk-x11-2.0':
61 path = '${gtk2-x11}/lib/libgdk-x11-2.0${ext}'
62 elif name == 'gdk_pixbuf-2.0':
63 path = '${gdk-pixbuf}/lib/libgdk_pixbuf-2.0${ext}'
64 elif name == 'Xext':
65 path = '${xorg.libXext}/lib/libXext${ext}'
66 elif name == 'fontconfig':
67 path = '${fontconfig.lib}/lib/libfontconfig${ext}'
68 elif name == 'freetype':
69 path = '${freetype}/lib/libfreetype${ext}'
70 elif name[0:2] == 'av' or name[0:2] == 'sw':
71 path = '${lib.getLib ffmpeg-full}/lib/lib' + name + '${ext}'
72 elif name == 'openal':
73 path = '${openal}/lib/libopenal${ext}'
74 elif name == 'pulse':
75 path = '${libpulseaudio}/lib/libpulse${ext}'
76 elif name == 'Xi':
77 path = '${xorg.libXi}/lib/libXi${ext}'
78 elif name == 'Xinerama':
79 path = '${xorg.libXinerama}/lib/libXinerama${ext}'
80 elif name == 'Xxf86vm':
81 path = '${xorg.libXxf86vm}/lib/libXxf86vm${ext}'
82 elif name == 'harfbuzz':
83 path = '${harfbuzz}/lib/libharfbuzz${ext}'
84 if path is not None:
85 return ctypes.cdll.LoadLibrary(path)
86 raise Exception("Could not load library {}".format(names))
87 EOF
88 ''
89 + lib.optionalString stdenv.isDarwin ''
90 cat > pyglet/lib.py <<EOF
91 import os
92 import ctypes
93 def load_library(*names, **kwargs):
94 path = None
95 framework = kwargs.get('framework')
96 if framework is not None:
97 path = '${apple-sdk}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/{framework}.framework/{framework}'.format(framework=framework)
98 else:
99 names = kwargs.get('darwin', names)
100 if not isinstance(names, tuple):
101 names = (names,)
102 for name in names:
103 if name == "libharfbuzz.0.dylib":
104 path = '${harfbuzz}/lib/%s' % name
105 break
106 elif name.startswith('avutil'):
107 path = '${lib.getLib ffmpeg-full}/lib/lib%s.dylib' % name
108 if not os.path.exists(path):
109 path = None
110 else:
111 break
112 if path is not None:
113 return ctypes.cdll.LoadLibrary(path)
114 raise ImportError("Could not load library {}".format(names))
115 EOF
116 '';
117
118 nativeBuildInputs = [ unzip ];
119
120 # needs GL set up which isn't really possible in a build environment even in headless mode.
121 # tests do run and pass in nix-shell, however.
122 doCheck = false;
123
124 nativeCheckInputs = [ pytestCheckHook ];
125
126 preCheck = # libEGL only available on Linux (despite meta.platforms on libGL)
127 lib.optionalString stdenv.isLinux ''
128 export PYGLET_HEADLESS=True
129 '';
130
131 # test list taken from .travis.yml
132 disabledTestPaths = [
133 "tests/base"
134 "tests/interactive"
135 "tests/integration"
136 "tests/unit/text/test_layout.py"
137 ];
138
139 pythonImportsCheck = [ "pyglet" ];
140
141 meta = {
142 homepage = "http://www.pyglet.org/";
143 description = "Cross-platform windowing and multimedia library";
144 license = lib.licenses.bsd3;
145 # The patch needs adjusting for other platforms.
146 platforms = with lib.platforms; linux ++ darwin;
147 };
148}