1{ stdenvNoCC, lib, buildPackages, fetchurl, perl, elf-header
2, bison ? null, flex ? null, python ? null, rsync ? null
3}:
4
5assert stdenvNoCC.hostPlatform.isAndroid ->
6 (flex != null && bison != null && python != null && rsync != null);
7
8let
9 makeLinuxHeaders = { src, version, patches ? [] }: stdenvNoCC.mkDerivation {
10 inherit src;
11
12 pname = "linux-headers";
13 inherit version;
14
15 ARCH = stdenvNoCC.hostPlatform.linuxArch;
16
17 # It may look odd that we use `stdenvNoCC`, and yet explicit depend on a cc.
18 # We do this so we have a build->build, not build->host, C compiler.
19 depsBuildBuild = [ buildPackages.stdenv.cc ];
20 # `elf-header` is null when libc provides `elf.h`.
21 nativeBuildInputs = [
22 perl elf-header
23 ] ++ lib.optionals stdenvNoCC.hostPlatform.isAndroid [
24 flex bison python rsync
25 ];
26
27 extraIncludeDirs = lib.optional stdenvNoCC.hostPlatform.isPowerPC ["ppc"];
28
29 inherit patches;
30
31 hardeningDisable = lib.optional stdenvNoCC.buildPlatform.isDarwin "format";
32
33 makeFlags = [
34 "SHELL=bash"
35 # Avoid use of runtime build->host compilers for checks. These
36 # checks only cared to work around bugs in very old compilers, so
37 # these changes should be safe.
38 "cc-version:=9999"
39 "cc-fullversion:=999999"
40 # `$(..)` expanded by make alone
41 "HOSTCC:=$(CC_FOR_BUILD)"
42 "HOSTCXX:=$(CXX_FOR_BUILD)"
43 ];
44
45 # Skip clean on darwin, case-sensitivity issues.
46 buildPhase = lib.optionalString (!stdenvNoCC.buildPlatform.isDarwin) ''
47 make mrproper $makeFlags
48 '' + (if stdenvNoCC.hostPlatform.isAndroid then ''
49 make defconfig
50 make headers_install
51 '' else ''
52 make headers $makeFlags
53 '');
54
55 checkPhase = ''
56 make headers_check $makeFlags
57 '';
58
59 # The following command requires rsync:
60 # make headers_install INSTALL_HDR_PATH=$out $makeFlags
61 # but rsync depends on popt which does not compile on aarch64 without
62 # updateAutotoolsGnuConfigScriptsHook which is not enabled in stage2,
63 # so we replicate it with cp. This also reduces bootstrap closure size.
64 installPhase = ''
65 mkdir -p $out
66 cp -r usr/include $out
67 find $out -type f ! -name '*.h' -delete
68 ''
69 # Some builds (e.g. KVM) want a kernel.release.
70 + ''
71 mkdir -p $out/include/config
72 echo "${version}-default" > $out/include/config/kernel.release
73 '';
74
75 meta = with lib; {
76 description = "Header files and scripts for Linux kernel";
77 license = licenses.gpl2;
78 platforms = platforms.linux;
79 };
80 };
81in {
82 inherit makeLinuxHeaders;
83
84 linuxHeaders = let version = "5.14"; in
85 makeLinuxHeaders {
86 inherit version;
87 src = fetchurl {
88 url = "mirror://kernel/linux/kernel/v5.x/linux-${version}.tar.xz";
89 sha256 = "sha256-fgaLXg0mpisQ5TILJdzldYjLvG94HAkEQhOMnJwycbI=";
90 };
91 patches = [
92 ./no-relocs.patch # for building x86 kernel headers on non-ELF platforms
93 ];
94 };
95}