2022-09-20 19:54:27 +02:00
|
|
|
{ stdenv
|
|
|
|
, buildPackages
|
|
|
|
, runCommand
|
|
|
|
, writeText
|
|
|
|
, lib
|
|
|
|
|
|
|
|
, config
|
2022-10-19 18:34:22 +02:00
|
|
|
, src
|
2024-02-11 17:19:52 +01:00
|
|
|
, version ? "0"
|
2023-02-10 18:54:33 +01:00
|
|
|
, extraPatchPhase ? "echo"
|
2023-12-07 23:31:26 +01:00
|
|
|
, targets ? ["vmlinux"]
|
2022-09-20 19:54:27 +02:00
|
|
|
} :
|
2023-06-26 21:49:43 +02:00
|
|
|
let
|
|
|
|
writeConfig = import ./write-kconfig.nix { inherit lib writeText; };
|
|
|
|
kconfigFile = writeConfig "kconfig" config;
|
2023-10-12 19:59:45 +02:00
|
|
|
arch = stdenv.hostPlatform.linuxArch;
|
2024-01-02 20:40:57 +01:00
|
|
|
targetNames = map baseNameOf targets;
|
2023-06-26 21:49:43 +02:00
|
|
|
inherit lib; in
|
2022-09-20 19:54:27 +02:00
|
|
|
stdenv.mkDerivation rec {
|
2022-10-19 18:34:22 +02:00
|
|
|
name = "kernel";
|
|
|
|
inherit src extraPatchPhase;
|
2022-09-20 19:54:27 +02:00
|
|
|
hardeningDisable = ["all"];
|
|
|
|
nativeBuildInputs = [buildPackages.stdenv.cc] ++
|
2023-04-10 18:46:39 +02:00
|
|
|
(with buildPackages.pkgs; [
|
2023-09-21 13:25:35 +02:00
|
|
|
rsync bc bison flex pkg-config
|
2023-04-10 18:46:39 +02:00
|
|
|
openssl ncurses.all perl
|
|
|
|
]);
|
2022-09-20 19:54:27 +02:00
|
|
|
CC = "${stdenv.cc.bintools.targetPrefix}gcc";
|
2022-09-25 23:07:29 +02:00
|
|
|
HOSTCC = with buildPackages.pkgs;
|
|
|
|
"gcc -I${openssl}/include -I${ncurses}/include";
|
|
|
|
HOST_EXTRACFLAGS = with buildPackages.pkgs;
|
|
|
|
"-I${openssl.dev}/include -L${openssl.out}/lib -L${ncurses.out}/lib";
|
2022-09-20 19:54:27 +02:00
|
|
|
PKG_CONFIG_PATH = "./pkgconfig";
|
|
|
|
CROSS_COMPILE = stdenv.cc.bintools.targetPrefix;
|
2023-09-20 18:57:17 +02:00
|
|
|
ARCH = arch;
|
2022-10-19 18:34:22 +02:00
|
|
|
KBUILD_BUILD_HOST = "liminix.builder";
|
|
|
|
|
2022-09-20 19:54:27 +02:00
|
|
|
dontStrip = true;
|
|
|
|
dontPatchELF = true;
|
2024-01-02 20:40:57 +01:00
|
|
|
outputs = ["out" "headers" "modulesupport"] ++ targetNames;
|
2022-09-25 23:07:29 +02:00
|
|
|
phases = [
|
2022-10-19 18:34:22 +02:00
|
|
|
"unpackPhase"
|
2022-09-25 23:07:29 +02:00
|
|
|
"butcherPkgconfig"
|
2022-10-19 18:34:22 +02:00
|
|
|
"extraPatchPhase"
|
2023-03-23 22:50:44 +01:00
|
|
|
"patchPhase"
|
2022-10-19 18:34:22 +02:00
|
|
|
"patchScripts"
|
2022-09-25 23:07:29 +02:00
|
|
|
"configurePhase"
|
|
|
|
"checkConfigurationPhase"
|
|
|
|
"buildPhase"
|
|
|
|
"installPhase"
|
|
|
|
];
|
2022-09-20 19:54:27 +02:00
|
|
|
|
2023-03-23 22:50:44 +01:00
|
|
|
patches = [
|
|
|
|
./cmdline-cookie.patch
|
2024-01-23 13:23:07 +01:00
|
|
|
./phram-fix-missing-writebufsize.patch
|
2023-12-21 20:17:14 +01:00
|
|
|
./mips-malta-fdt-from-bootloader.patch
|
2024-02-11 17:19:52 +01:00
|
|
|
] ++ lib.optional (lib.versionOlder version "5.18.0")
|
|
|
|
./phram-allow-cached-mappings.patch;
|
2023-03-23 22:50:44 +01:00
|
|
|
|
2022-09-25 23:07:29 +02:00
|
|
|
# this is here to work around what I think is a bug in nixpkgs
|
|
|
|
# packaging of ncurses: it installs pkg-config data files which
|
|
|
|
# don't produce any -L options when queried with "pkg-config --lib
|
|
|
|
# ncurses". For a regular build you'll never even notice, this only
|
|
|
|
# becomes an issue if you do a nix-shell in this derivation and
|
|
|
|
# expect "make nconfig" to work.
|
2022-09-20 19:54:27 +02:00
|
|
|
butcherPkgconfig = ''
|
|
|
|
cp -r ${buildPackages.pkgs.ncurses.dev}/lib/pkgconfig .
|
|
|
|
chmod +w pkgconfig pkgconfig/*.pc
|
|
|
|
for i in pkgconfig/*.pc; do test -f $i && sed -i 's/^Libs:/Libs: -L''${libdir} /' $i;done
|
|
|
|
'';
|
|
|
|
|
2022-10-19 18:34:22 +02:00
|
|
|
patchScripts = ''
|
2023-03-19 10:49:32 +01:00
|
|
|
# Make kexec pass dtb in register when invoking new kernel. The
|
|
|
|
# code to do this is already present, but bracketed by UHI_BOOT
|
|
|
|
# which we can't enable.
|
|
|
|
sed -i arch/mips/kernel/machine_kexec.c -e 's/CONFIG_UHI_BOOT/CONFIG_MIPS/g'
|
|
|
|
|
2022-10-19 18:34:22 +02:00
|
|
|
patchShebangs scripts/
|
|
|
|
'';
|
|
|
|
|
2022-09-20 19:54:27 +02:00
|
|
|
configurePhase = ''
|
|
|
|
export KBUILD_OUTPUT=`pwd`
|
|
|
|
cp ${kconfigFile} .config
|
|
|
|
cp ${kconfigFile} .config.orig
|
2022-10-19 18:34:22 +02:00
|
|
|
make V=1 olddefconfig
|
2022-09-20 19:54:27 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
checkConfigurationPhase = ''
|
|
|
|
echo Checking required config items:
|
2022-10-19 23:09:38 +02:00
|
|
|
if comm -2 -3 <(grep 'CONFIG' ${kconfigFile} |sort) <(grep 'CONFIG' .config|sort) |grep '.' ; then
|
2022-09-20 19:54:27 +02:00
|
|
|
echo -e "^^^ Some configuration lost :-(\nPerhaps you have mutually incompatible settings, or have disabled options on which these depend.\n"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
echo "OK"
|
|
|
|
'';
|
|
|
|
|
|
|
|
buildPhase = ''
|
2024-01-02 20:40:57 +01:00
|
|
|
make ${lib.concatStringsSep " " targetNames} modules_prepare -j$NIX_BUILD_CORES
|
2022-09-20 19:54:27 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
installPhase = ''
|
|
|
|
${CROSS_COMPILE}strip -d vmlinux
|
2024-01-02 20:40:57 +01:00
|
|
|
${lib.concatStringsSep "\n" (map (f: "cp ${f} \$${baseNameOf f}") targets)}
|
|
|
|
cp vmlinux $out
|
2022-10-19 18:34:22 +02:00
|
|
|
mkdir -p $headers
|
|
|
|
cp -a include .config $headers/
|
2023-06-27 22:17:04 +02:00
|
|
|
mkdir -p $modulesupport
|
2024-02-12 00:47:11 +01:00
|
|
|
make modules
|
2023-02-22 19:33:17 +01:00
|
|
|
cp -a . $modulesupport
|
2022-09-20 19:54:27 +02:00
|
|
|
'';
|
|
|
|
}
|