runInLinuxVM: fix simple build (#378580)
[NixPkgs.git] / pkgs / by-name / sd / sdl3 / package.nix
blob004dee8aa8d120db072a69b62cce219560082034
2   lib,
3   stdenv,
4   config,
5   alsa-lib,
6   apple-sdk_11,
7   cmake,
8   darwinMinVersionHook,
9   dbus,
10   fcitx5,
11   fetchFromGitHub,
12   ibus,
13   installShellFiles,
14   libGL,
15   libayatana-appindicator,
16   libdecor,
17   libdrm,
18   libjack2,
19   libpulseaudio,
20   libusb1,
21   libxkbcommon,
22   libgbm,
23   ninja,
24   nix-update-script,
25   nixosTests,
26   pipewire,
27   sndio,
28   systemdLibs,
29   testers,
30   validatePkgConfig,
31   vulkan-headers,
32   vulkan-loader,
33   wayland,
34   wayland-scanner,
35   xorg,
36   zenity,
37   alsaSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
38   dbusSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
39   drmSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
40   ibusSupport ? stdenv.hostPlatform.isUnix && !stdenv.hostPlatform.isDarwin,
41   jackSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
42   libdecorSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
43   openglSupport ? lib.meta.availableOn stdenv.hostPlatform libGL,
44   pipewireSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
45   pulseaudioSupport ?
46     config.pulseaudio or stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
47   libudevSupport ? stdenv.hostPlatform.isLinux && !stdenv.hostPlatform.isAndroid,
48   sndioSupport ? false,
49   testSupport ? true,
50   waylandSupport ? stdenv.isLinux && !stdenv.hostPlatform.isAndroid,
51   x11Support ? !stdenv.hostPlatform.isAndroid && !stdenv.hostPlatform.isWindows,
54 stdenv.mkDerivation (finalAttrs: {
55   pname = "sdl3";
56   version = "3.2.2";
58   outputs = [
59     "lib"
60     "dev"
61     "out"
62   ];
64   src = fetchFromGitHub {
65     owner = "libsdl-org";
66     repo = "SDL";
67     tag = "release-${finalAttrs.version}";
68     hash = "sha256-AbEwpIxgYl3g4calnWca8Vz/Tw1DnYfZrD1Vl5cSMpk=";
69   };
71   postPatch =
72     # Tests timeout on Darwin
73     lib.optionalString testSupport ''
74       substituteInPlace test/CMakeLists.txt \
75         --replace-fail 'set(noninteractive_timeout 10)' 'set(noninteractive_timeout 30)'
76     ''
77     + lib.optionalString waylandSupport ''
78       substituteInPlace src/video/wayland/SDL_waylandmessagebox.c \
79         --replace-fail '"zenity"' '"${lib.getExe zenity}"'
80     '';
82   strictDeps = true;
84   nativeBuildInputs = [
85     cmake
86     ninja
87     validatePkgConfig
88   ] ++ lib.optional waylandSupport wayland-scanner;
90   buildInputs =
91     finalAttrs.dlopenBuildInputs
92     ++ lib.optionals stdenv.hostPlatform.isDarwin [
93       # error: 'MTLPixelFormatASTC_4x4_LDR' is unavailable: not available on macOS
94       (darwinMinVersionHook "11.0")
96       apple-sdk_11
97     ]
98     ++ lib.optionals ibusSupport [
99       fcitx5
100       ibus
101     ]
102     ++ lib.optional waylandSupport zenity;
104   dlopenBuildInputs =
105     lib.optionals stdenv.hostPlatform.isLinux [
106       libusb1
107     ]
108     ++ lib.optional (
109       stdenv.hostPlatform.isUnix && !stdenv.hostPlatform.isDarwin
110     ) libayatana-appindicator
111     ++ lib.optional alsaSupport alsa-lib
112     ++ lib.optional dbusSupport dbus
113     ++ lib.optionals drmSupport [
114       libdrm
115       libgbm
116     ]
117     ++ lib.optional jackSupport libjack2
118     ++ lib.optional libdecorSupport libdecor
119     ++ lib.optional libudevSupport systemdLibs
120     ++ lib.optional openglSupport libGL
121     ++ lib.optional pipewireSupport pipewire
122     ++ lib.optional pulseaudioSupport libpulseaudio
123     ++ lib.optional sndioSupport sndio
124     ++ lib.optionals waylandSupport [
125       libxkbcommon
126       wayland
127     ]
128     ++ lib.optionals x11Support [
129       xorg.libX11
130       xorg.libXScrnSaver
131       xorg.libXcursor
132       xorg.libXext
133       xorg.libXfixes
134       xorg.libXi
135       xorg.libXrandr
136     ];
138   propagatedBuildInputs = finalAttrs.dlopenPropagatedBuildInputs;
140   dlopenPropagatedBuildInputs =
141     [
142       vulkan-headers
143       vulkan-loader
144     ]
145     ++ lib.optional (openglSupport && !stdenv.hostPlatform.isDarwin) libGL
146     ++ lib.optional x11Support xorg.libX11;
148   cmakeFlags = [
149     (lib.cmakeBool "SDL_ALSA" alsaSupport)
150     (lib.cmakeBool "SDL_DBUS" dbusSupport)
151     (lib.cmakeBool "SDL_IBUS" ibusSupport)
152     (lib.cmakeBool "SDL_JACK" jackSupport)
153     (lib.cmakeBool "SDL_KMSDRM" drmSupport)
154     (lib.cmakeBool "SDL_LIBUDEV" libudevSupport)
155     (lib.cmakeBool "SDL_OPENGL" openglSupport)
156     (lib.cmakeBool "SDL_PIPEWIRE" pipewireSupport)
157     (lib.cmakeBool "SDL_PULSEAUDIO" pulseaudioSupport)
158     (lib.cmakeBool "SDL_SNDIO" sndioSupport)
159     (lib.cmakeBool "SDL_TEST_LIBRARY" testSupport)
160     (lib.cmakeBool "SDL_WAYLAND" waylandSupport)
161     (lib.cmakeBool "SDL_WAYLAND_LIBDECOR" libdecorSupport)
162     (lib.cmakeBool "SDL_X11" x11Support)
164     (lib.cmakeBool "SDL_TESTS" finalAttrs.finalPackage.doCheck)
165   ];
167   doCheck = testSupport && stdenv.buildPlatform.canExecute stdenv.hostPlatform;
169   # See comment below. We actually *do* need these RPATH entries
170   dontPatchELF = true;
172   env = {
173     # Many dependencies are not directly linked to, but dlopen()'d at runtime. Adding them to the RPATH
174     # helps them be found
175     NIX_LDFLAGS =
176       lib.optionalString (stdenv.hostPlatform.extensions.sharedLibrary == ".so")
177         "-rpath ${
178           lib.makeLibraryPath (finalAttrs.dlopenBuildInputs ++ finalAttrs.dlopenPropagatedBuildInputs)
179         }";
180   };
182   passthru = {
183     # Building this in its own derivation to make sure the rpath hack above propagate to users
184     debug-text-example = stdenv.mkDerivation (finalAttrs': {
185       pname = "sdl3-debug-text-example";
186       inherit (finalAttrs) version src;
188       sourceRoot = "${finalAttrs'.src.name}/examples/renderer/18-debug-text";
190       nativeBuildInputs = [
191         installShellFiles
192       ];
194       buildInputs = [ finalAttrs.finalPackage ];
196       postBuild = ''
197         $CC -lSDL3 -o debug-text{,.c}
198       '';
200       postInstall = ''
201         installBin debug-text
202       '';
204       meta = {
205         inherit (finalAttrs.meta) maintainers platforms;
206         mainProgram = "debug-text";
207       };
208     });
210     tests =
211       {
212         pkg-config = testers.hasPkgConfigModules { package = finalAttrs.finalPackage; };
213         inherit (finalAttrs.passthru) debug-text-example;
214       }
215       // lib.optionalAttrs stdenv.hostPlatform.isLinux {
216         nixosTest = nixosTests.sdl3;
217       };
219     updateScript = nix-update-script {
220       extraArgs = [
221         "--version-regex"
222         "'release-(.*)'"
223       ];
224     };
225   };
227   meta = {
228     description = "Cross-platform development library";
229     homepage = "https://libsdl.org";
230     changelog = "https://github.com/libsdl-org/SDL/releases/tag/${finalAttrs.src.tag}";
231     license = lib.licenses.zlib;
232     maintainers = with lib.maintainers; [ getchoo ];
233     platforms = lib.platforms.unix;
234     pkgConfigModules = [ "sdl3" ];
235   };