1{ lib, stdenv, fetchurl, bash, flex, bison, valgrind }:
2
3stdenv.mkDerivation rec {
4 pname = "lockdep";
5
6 # it would be nice to be able to pick a kernel version in sync with something
7 # else we already ship, but it seems userspace lockdep isn't very well maintained
8 # and appears broken in many kernel releases
9 version = "5.0.21";
10 fullver = "5.0.21";
11 src = fetchurl {
12 url = "mirror://kernel/linux/kernel/v5.x/linux-${version}.tar.xz";
13 sha256 = "1my2m9hvnvdrvzcg0fgqgaga59y2cd5zlpv7xrfj2nn98sjhglwq";
14 };
15
16 # ensure *this* kernel's userspace-headers are picked up before we
17 # fall back to those in glibc, as they will be from a mismatched
18 # kernel version
19 postPatch = ''
20 substituteInPlace tools/lib/lockdep/Makefile \
21 --replace 'CONFIG_INCLUDES =' $'CONFIG_INCLUDES = -I../../../usr/include\n#'
22 '';
23
24 nativeBuildInputs = [ flex bison ];
25
26 # Workaround build failure on -fno-common toolchains like upstream
27 # gcc-10. Otherwise build fails as:
28 # ld: lockdep.o:/build/linux-5.0.21/tools/lib/lockdep/../../include/linux/rcu.h:5: multiple definition of
29 # `rcu_scheduler_active'; common.o:/build/linux-5.0.21/tools/lib/lockdep/../../include/linux/rcu.h:5: first defined here
30 env.NIX_CFLAGS_COMPILE = "-fcommon";
31
32 buildPhase = ''
33 make defconfig
34 make headers_install
35 cd tools/lib/lockdep
36 make
37 '';
38
39 doCheck = true;
40 nativeCheckInputs = [ valgrind ];
41 checkPhase = ''
42 # there are more /bin/bash references than just shebangs
43 for f in lockdep run_tests.sh tests/*.sh; do
44 substituteInPlace $f \
45 --replace '/bin/bash' '${bash}/bin/bash'
46 done
47
48 ./run_tests.sh
49 '';
50
51 installPhase = ''
52 mkdir -p $out/bin $out/lib $out/include
53
54 cp -R include/liblockdep $out/include
55 make install DESTDIR=$out prefix=""
56
57 substituteInPlace $out/bin/lockdep --replace "./liblockdep.so" "$out/lib/liblockdep.so.$fullver"
58 '';
59
60 meta = {
61 description = "Userspace locking validation tool built on the Linux kernel";
62 mainProgram = "lockdep";
63 homepage = "https://kernel.org/";
64 license = lib.licenses.gpl2Only;
65 platforms = lib.platforms.linux;
66 maintainers = [ lib.maintainers.thoughtpolice ];
67 };
68}