Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1{ lib
2, stdenv
3, config
4, fetchurl
5, pkg-config
6, libGLSupported ? lib.elem stdenv.hostPlatform.system lib.platforms.mesaPlatforms
7, openglSupport ? libGLSupported
8, libGL
9, alsaSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
10, alsa-lib
11, x11Support ? !stdenv.targetPlatform.isWindows && !stdenv.hostPlatform.isAndroid
12, libX11
13, xorgproto
14, libICE
15, libXi
16, libXScrnSaver
17, libXcursor
18, libXinerama
19, libXext
20, libXxf86vm
21, libXrandr
22, waylandSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
23, wayland
24, wayland-protocols
25, wayland-scanner
26, drmSupport ? false
27, libdrm
28, mesa
29, libxkbcommon
30, dbusSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
31, dbus
32, udevSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
33, udev
34, ibusSupport ? false
35, ibus
36, libdecorSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
37, libdecor
38, pipewireSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid
39, pipewire # NOTE: must be built with SDL2 without pipewire support
40, pulseaudioSupport ? config.pulseaudio or stdenv.isLinux && !stdenv.hostPlatform.isAndroid
41, libpulseaudio
42, AudioUnit
43, Cocoa
44, CoreAudio
45, CoreServices
46, ForceFeedback
47, OpenGL
48, audiofile
49, libiconv
50, withStatic ? false
51}:
52
53# NOTE: When editing this expression see if the same change applies to
54# SDL expression too
55
56stdenv.mkDerivation rec {
57 pname = "SDL2";
58 version = "2.26.5";
59
60 src = fetchurl {
61 url = "https://www.libsdl.org/release/${pname}-${version}.tar.gz";
62 sha256 = "sha256-rY/qPaG+ZMg8RbHTY6a0uo/WD1veOyPsc4VXCexeq/c=";
63 };
64 dontDisableStatic = if withStatic then 1 else 0;
65 outputs = [ "out" "dev" ];
66 outputBin = "dev"; # sdl-config
67
68 patches = [
69 # `sdl2-config --cflags` from Nixpkgs returns include path to just SDL2.
70 # On a normal distro this is enough for includes from all SDL2* packages to work,
71 # but on NixOS they're spread across different paths.
72 # This patch + the setup-hook will ensure that `sdl2-config --cflags` works correctly.
73 ./find-headers.patch
74 ];
75
76 postPatch = ''
77 # Fix running wayland-scanner for the build platform when cross-compiling.
78 # See comment here: https://github.com/libsdl-org/SDL/issues/4860#issuecomment-1119003545
79 substituteInPlace configure \
80 --replace '$(WAYLAND_SCANNER)' 'wayland-scanner'
81 '';
82
83 strictDeps = true;
84
85 depsBuildBuild = [ pkg-config ];
86
87 nativeBuildInputs = [ pkg-config ] ++ lib.optionals waylandSupport [ wayland wayland-scanner ];
88
89 dlopenPropagatedBuildInputs = [ ]
90 # Propagated for #include <GLES/gl.h> in SDL_opengles.h.
91 ++ lib.optional openglSupport libGL
92 # Propagated for #include <X11/Xlib.h> and <X11/Xatom.h> in SDL_syswm.h.
93 ++ lib.optionals x11Support [ libX11 ];
94
95 propagatedBuildInputs = lib.optionals x11Support [ xorgproto ]
96 ++ dlopenPropagatedBuildInputs;
97
98 dlopenBuildInputs = lib.optionals alsaSupport [ alsa-lib audiofile ]
99 ++ lib.optional dbusSupport dbus
100 ++ lib.optional libdecorSupport libdecor
101 ++ lib.optional pipewireSupport pipewire
102 ++ lib.optional pulseaudioSupport libpulseaudio
103 ++ lib.optional udevSupport udev
104 ++ lib.optionals waylandSupport [ wayland libxkbcommon ]
105 ++ lib.optionals x11Support [ libICE libXi libXScrnSaver libXcursor libXinerama libXext libXrandr libXxf86vm ]
106 ++ lib.optionals drmSupport [ libdrm mesa ];
107
108 buildInputs = [ libiconv ]
109 ++ dlopenBuildInputs
110 ++ lib.optional ibusSupport ibus
111 ++ lib.optionals waylandSupport [ wayland-protocols ]
112 ++ lib.optionals stdenv.isDarwin [ AudioUnit Cocoa CoreAudio CoreServices ForceFeedback OpenGL ];
113
114 enableParallelBuilding = true;
115
116 configureFlags = [
117 "--disable-oss"
118 ] ++ lib.optional (!x11Support) "--without-x"
119 ++ lib.optional alsaSupport "--with-alsa-prefix=${alsa-lib.out}/lib"
120 ++ lib.optional stdenv.targetPlatform.isWindows "--disable-video-opengles"
121 ++ lib.optional stdenv.isDarwin "--disable-sdltest";
122
123 # We remove libtool .la files when static libs are requested,
124 # because they make the builds of downstream libs like `SDL_tff`
125 # fail with `cannot find -lXext, `-lXcursor` etc. linker errors
126 # because the `.la` files are not pruned if static libs exist
127 # (see https://github.com/NixOS/nixpkgs/commit/fd97db43bcb05e37f6bb77f363f1e1e239d9de53)
128 # and they also don't carry the necessary `-L` paths of their
129 # X11 dependencies.
130 # For static linking, it is better to rely on `pkg-config` `.pc`
131 # files.
132 postInstall = ''
133 if [ "$dontDisableStatic" -eq "1" ]; then
134 rm $out/lib/*.la
135 else
136 rm $out/lib/*.a
137 fi
138 moveToOutput bin/sdl2-config "$dev"
139 '';
140
141 # SDL is weird in that instead of just dynamically linking with
142 # libraries when you `--enable-*` (or when `configure` finds) them
143 # it `dlopen`s them at runtime. In principle, this means it can
144 # ignore any missing optional dependencies like alsa, pulseaudio,
145 # some x11 libs, wayland, etc if they are missing on the system
146 # and/or work with wide array of versions of said libraries. In
147 # nixpkgs, however, we don't need any of that. Moreover, since we
148 # don't have a global ld-cache we have to stuff all the propagated
149 # libraries into rpath by hand or else some applications that use
150 # SDL API that requires said libraries will fail to start.
151 #
152 # You can grep SDL sources with `grep -rE 'SDL_(NAME|.*_SYM)'` to
153 # list the symbols used in this way.
154 postFixup =
155 let
156 rpath = lib.makeLibraryPath (dlopenPropagatedBuildInputs ++ dlopenBuildInputs);
157 in
158 lib.optionalString (stdenv.hostPlatform.extensions.sharedLibrary == ".so") ''
159 for lib in $out/lib/*.so* ; do
160 if ! [[ -L "$lib" ]]; then
161 patchelf --set-rpath "$(patchelf --print-rpath $lib):${rpath}" "$lib"
162 fi
163 done
164 '';
165
166 setupHook = ./setup-hook.sh;
167
168 passthru = { inherit openglSupport; };
169
170 meta = with lib; {
171 description = "A cross-platform multimedia library";
172 homepage = "http://www.libsdl.org/";
173 changelog = "https://github.com/libsdl-org/SDL/releases/tag/release-${version}";
174 license = licenses.zlib;
175 platforms = platforms.all;
176 maintainers = with maintainers; [ cpages ];
177 };
178}