default.nix (3398B)
1 { stdenv 2 , buildPackages 3 , runCommand 4 , writeText 5 , lib 6 7 , config 8 , src 9 , extraPatchPhase ? "echo" 10 , targets ? ["vmlinux"] 11 } : 12 let 13 writeConfig = import ./write-kconfig.nix { inherit lib writeText; }; 14 kconfigFile = writeConfig "kconfig" config; 15 arch = stdenv.hostPlatform.linuxArch; 16 targetNames = map baseNameOf targets; 17 inherit lib; in 18 stdenv.mkDerivation rec { 19 name = "kernel"; 20 inherit src extraPatchPhase; 21 hardeningDisable = ["all"]; 22 nativeBuildInputs = [buildPackages.stdenv.cc] ++ 23 (with buildPackages.pkgs; [ 24 rsync bc bison flex pkg-config 25 openssl ncurses.all perl 26 ]); 27 CC = "${stdenv.cc.bintools.targetPrefix}gcc"; 28 HOSTCC = with buildPackages.pkgs; 29 "gcc -I${openssl}/include -I${ncurses}/include"; 30 HOST_EXTRACFLAGS = with buildPackages.pkgs; 31 "-I${openssl.dev}/include -L${openssl.out}/lib -L${ncurses.out}/lib"; 32 PKG_CONFIG_PATH = "./pkgconfig"; 33 CROSS_COMPILE = stdenv.cc.bintools.targetPrefix; 34 ARCH = arch; 35 KBUILD_BUILD_HOST = "liminix.builder"; 36 37 dontStrip = true; 38 dontPatchELF = true; 39 outputs = ["out" "headers" "modulesupport"] ++ targetNames; 40 phases = [ 41 "unpackPhase" 42 "butcherPkgconfig" 43 "extraPatchPhase" 44 "patchPhase" 45 "patchScripts" 46 "configurePhase" 47 "checkConfigurationPhase" 48 "buildPhase" 49 "installPhase" 50 ]; 51 52 patches = [ 53 ./cmdline-cookie.patch 54 ./phram-allow-cached-mappings.patch 55 ./mips-malta-fdt-from-bootloader.patch 56 ]; 57 58 # this is here to work around what I think is a bug in nixpkgs 59 # packaging of ncurses: it installs pkg-config data files which 60 # don't produce any -L options when queried with "pkg-config --lib 61 # ncurses". For a regular build you'll never even notice, this only 62 # becomes an issue if you do a nix-shell in this derivation and 63 # expect "make nconfig" to work. 64 butcherPkgconfig = '' 65 cp -r ${buildPackages.pkgs.ncurses.dev}/lib/pkgconfig . 66 chmod +w pkgconfig pkgconfig/*.pc 67 for i in pkgconfig/*.pc; do test -f $i && sed -i 's/^Libs:/Libs: -L''${libdir} /' $i;done 68 ''; 69 70 patchScripts = '' 71 # Make kexec pass dtb in register when invoking new kernel. The 72 # code to do this is already present, but bracketed by UHI_BOOT 73 # which we can't enable. 74 sed -i arch/mips/kernel/machine_kexec.c -e 's/CONFIG_UHI_BOOT/CONFIG_MIPS/g' 75 76 patchShebangs scripts/ 77 ''; 78 79 configurePhase = '' 80 export KBUILD_OUTPUT=`pwd` 81 cp ${kconfigFile} .config 82 cp ${kconfigFile} .config.orig 83 make V=1 olddefconfig 84 ''; 85 86 checkConfigurationPhase = '' 87 echo Checking required config items: 88 if comm -2 -3 <(grep 'CONFIG' ${kconfigFile} |sort) <(grep 'CONFIG' .config|sort) |grep '.' ; then 89 echo -e "^^^ Some configuration lost :-(\nPerhaps you have mutually incompatible settings, or have disabled options on which these depend.\n" 90 exit 0 91 fi 92 echo "OK" 93 ''; 94 95 buildPhase = '' 96 make ${lib.concatStringsSep " " targetNames} modules_prepare -j$NIX_BUILD_CORES 97 ''; 98 99 installPhase = '' 100 ${CROSS_COMPILE}strip -d vmlinux 101 ${lib.concatStringsSep "\n" (map (f: "cp ${f} \$${baseNameOf f}") targets)} 102 cp vmlinux $out 103 mkdir -p $headers 104 cp -a include .config $headers/ 105 mkdir -p $modulesupport 106 cp modules.* $modulesupport 107 make clean modules_prepare 108 cp -a . $modulesupport 109 ''; 110 }