1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 cmake,
6 pkg-config,
7 libX11,
8 procps,
9 python3,
10 libdwarf,
11 qtbase,
12 wrapQtAppsHook,
13 libglvnd,
14 gtest,
15 brotli,
16 enableGui ? true,
17}:
18
19stdenv.mkDerivation rec {
20 pname = "apitrace";
21 version = "13.0";
22
23 src = fetchFromGitHub {
24 owner = "apitrace";
25 repo = "apitrace";
26 rev = version;
27 hash = "sha256-ZZ2RL9nvwvHBEuKSDr1tgRhxBeg+XJKPUvSiHz6g/cg=";
28 fetchSubmodules = true;
29 };
30
31 # LD_PRELOAD wrappers need to be statically linked to work against all kinds
32 # of games -- so it's fine to use e.g. bundled snappy.
33 buildInputs = [
34 libX11
35 procps
36 libdwarf
37 gtest
38 brotli
39 ]
40 ++ lib.optionals enableGui [
41 qtbase
42 ];
43
44 nativeBuildInputs = [
45 cmake
46 pkg-config
47 python3
48 ]
49 ++ lib.optionals enableGui [
50 wrapQtAppsHook
51 ];
52
53 cmakeFlags = [
54 (lib.cmakeBool "ENABLE_GUI" enableGui)
55 ];
56
57 # Don't automatically wrap all binaries, I prefer to explicitly only wrap
58 # `qapitrace`.
59 dontWrapQtApps = true;
60
61 postFixup = ''
62
63 # Since https://github.com/NixOS/nixpkgs/pull/60985, we add `/run-opengl-driver[-32]`
64 # to the `RUNPATH` of dispatcher libraries `dlopen()` ing OpenGL drivers.
65 # `RUNPATH` doesn't propagate throughout the whole application, but only
66 # from the module performing the `dlopen()`.
67 #
68 # Apitrace wraps programs by running them with `LD_PRELOAD` pointing to `.so`
69 # files in $out/lib/apitrace/wrappers.
70 #
71 # Theses wrappers effectively wrap the `dlopen()` calls from `libglvnd`
72 # and other dispatcher libraries, and run `dlopen()` by themselves.
73 #
74 # As `RUNPATH` doesn't propagate through the whole library, and they're now the
75 # library doing the real `dlopen()`, they also need to have
76 # `/run-opengl-driver[-32]` added to their `RUNPATH`.
77 #
78 # To stay simple, we add paths for 32 and 64 bits unconditionally.
79 # This doesn't have an impact on closure size, and if the 32 bit drivers
80 # are not available, that folder is ignored.
81 for i in $out/lib/apitrace/wrappers/*.so
82 do
83 echo "Patching OpenGL driver path for $i"
84 patchelf --set-rpath "/run/opengl-driver/lib:/run/opengl-driver-32/lib:$(patchelf --print-rpath $i)" $i
85 done
86
87 # Theses open the OpenGL driver at runtime, but it is not listed as NEEDED libraries. They need
88 # a reference to libglvnd.
89 for i in $out/bin/eglretrace $out/bin/glretrace
90 do
91 echo "Patching RPath for $i"
92 patchelf --set-rpath "${lib.makeLibraryPath [ libglvnd ]}:$(patchelf --print-rpath $i)" $i
93 done
94
95 ''
96 + lib.optionalString enableGui ''
97 wrapQtApp $out/bin/qapitrace
98 '';
99
100 meta = with lib; {
101 homepage = "https://apitrace.github.io";
102 description = "Tools to trace OpenGL, OpenGL ES, Direct3D, and DirectDraw APIs";
103 license = licenses.mit;
104 platforms = platforms.linux;
105 };
106}