kernel: Enable cross compiling

This commit is contained in:
Ben Gamari 2017-10-28 15:09:54 -04:00 committed by Ben Wolsieffer
parent 5d8b359db7
commit 7a9b6ac39a
15 changed files with 53 additions and 94 deletions

View File

@ -51,7 +51,7 @@ with stdenv.lib;
# Bump the maximum number of CPUs to support systems like EC2 x1.* # Bump the maximum number of CPUs to support systems like EC2 x1.*
# instances and Xeon Phi. # instances and Xeon Phi.
${optionalString (stdenv.system == "x86_64-linux" || stdenv.system == "aarch64-linux") '' ${optionalString (stdenv.hostPlatform.system == "x86_64-linux" || stdenv.hostPlatform.system == "aarch64-linux") ''
NR_CPUS 384 NR_CPUS 384
''} ''}
@ -347,11 +347,12 @@ with stdenv.lib;
SECURITY_SELINUX_BOOTPARAM_VALUE 0 # Disable SELinux by default SECURITY_SELINUX_BOOTPARAM_VALUE 0 # Disable SELinux by default
SECURITY_YAMA? y # Prevent processes from ptracing non-children processes SECURITY_YAMA? y # Prevent processes from ptracing non-children processes
DEVKMEM n # Disable /dev/kmem DEVKMEM n # Disable /dev/kmem
${if versionOlder version "3.14" then '' ${optionalString (! stdenv.hostPlatform.isArm)
CC_STACKPROTECTOR? y # Detect buffer overflows on the stack (if versionOlder version "3.14" then ''
'' else '' CC_STACKPROTECTOR? y # Detect buffer overflows on the stack
CC_STACKPROTECTOR_REGULAR? y '' else ''
''} CC_STACKPROTECTOR_REGULAR? y
'')}
${optionalString (versionAtLeast version "3.12") '' ${optionalString (versionAtLeast version "3.12") ''
USER_NS y # Support for user namespaces USER_NS y # Support for user namespaces
''} ''}

View File

@ -1,4 +1,4 @@
{ stdenv, perl, buildLinux { stdenv, buildPackages, perl, buildLinux
, # The kernel source tarball. , # The kernel source tarball.
src src
@ -23,7 +23,7 @@
# symbolic name and `patch' is the actual patch. The patch may # symbolic name and `patch' is the actual patch. The patch may
# optionally be compressed with gzip or bzip2. # optionally be compressed with gzip or bzip2.
kernelPatches ? [] kernelPatches ? []
, ignoreConfigErrors ? stdenv.platform.name != "pc" , ignoreConfigErrors ? hostPlatform.platform.name != "pc"
, extraMeta ? {} , extraMeta ? {}
, hostPlatform , hostPlatform
, ... , ...
@ -58,37 +58,29 @@ let
in lib.concatStringsSep "\n" ([baseConfig] ++ configFromPatches); in lib.concatStringsSep "\n" ([baseConfig] ++ configFromPatches);
configfile = stdenv.mkDerivation { configfile = stdenv.mkDerivation {
inherit ignoreConfigErrors; #inherit ignoreConfigErrors;
name = "linux-config-${version}"; name = "linux-config-${version}";
generateConfig = ./generate-config.pl; generateConfig = ./generate-config.pl;
kernelConfig = kernelConfigFun config; kernelConfig = kernelConfigFun config;
nativeBuildInputs = [ perl ]; nativeBuildInputs = [ buildPackages.stdenv.cc perl ];
platformName = stdenv.platform.name; platformName = hostPlatform.platform.name;
kernelBaseConfig = stdenv.platform.kernelBaseConfig; kernelBaseConfig = hostPlatform.platform.kernelBaseConfig;
kernelTarget = stdenv.platform.kernelTarget; kernelTarget = hostPlatform.platform.kernelTarget;
autoModules = stdenv.platform.kernelAutoModules; autoModules = hostPlatform.platform.kernelAutoModules;
preferBuiltin = stdenv.platform.kernelPreferBuiltin or false; preferBuiltin = hostPlatform.platform.kernelPreferBuiltin or false;
arch = stdenv.platform.kernelArch; arch = hostPlatform.platform.kernelArch;
# TODO(@Ericson2314): No null next hash break
ignoreConfigErrors = if stdenv.hostPlatform == stdenv.buildPlatform then null else true;
crossAttrs = let crossAttrs = let
cp = hostPlatform.platform; cp = hostPlatform.platform;
in { in {
arch = cp.kernelArch;
platformName = cp.name;
kernelBaseConfig = cp.kernelBaseConfig;
kernelTarget = cp.kernelTarget;
autoModules = cp.kernelAutoModules;
# Just ignore all options that don't apply (We are lazy).
ignoreConfigErrors = true; ignoreConfigErrors = true;
kernelConfig = kernelConfigFun configCross;
inherit (kernel.crossDrv) src patches preUnpack;
}; };
prePatch = kernel.prePatch + '' prePatch = kernel.prePatch + ''
@ -103,7 +95,7 @@ let
cd $buildRoot cd $buildRoot
# Get a basic config file for later refinement with $generateConfig. # Get a basic config file for later refinement with $generateConfig.
make -C ../$sourceRoot O=$PWD $kernelBaseConfig ARCH=$arch make HOSTCC=${buildPackages.stdenv.cc.targetPrefix}gcc -C ../$sourceRoot O=$PWD $kernelBaseConfig ARCH=$arch
# Create the config file. # Create the config file.
echo "generating kernel configuration..." echo "generating kernel configuration..."
@ -122,11 +114,7 @@ let
configfile = configfile.nativeDrv or configfile; configfile = configfile.nativeDrv or configfile;
crossConfigfile = configfile.crossDrv or configfile;
config = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; }; config = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; };
crossConfig = { CONFIG_MODULES = "y"; CONFIG_FW_LOADER = "m"; };
}; };
passthru = { passthru = {
@ -134,12 +122,4 @@ let
passthru = kernel.passthru // (removeAttrs passthru [ "passthru" ]); passthru = kernel.passthru // (removeAttrs passthru [ "passthru" ]);
}; };
addPassthru' = lib.extendDerivation true passthru; in lib.extendDerivation true passthru kernel
nativeDrv = addPassthru' kernel.nativeDrv;
crossDrv = addPassthru' kernel.crossDrv;
in if kernel ? crossDrv
then nativeDrv // { inherit nativeDrv crossDrv; }
else addPassthru' kernel

View File

@ -1,4 +1,4 @@
{ stdenv, hostPlatform, fetchurl, perl, buildLinux, ... } @ args: { stdenv, buildPackages, hostPlatform, fetchurl, perl, buildLinux, ... } @ args:
import ./generic.nix (args // rec { import ./generic.nix (args // rec {
version = "4.13.16"; version = "4.13.16";

View File

@ -1,4 +1,4 @@
{ stdenv, hostPlatform, fetchurl, perl, buildLinux, ... } @ args: { stdenv, buildPackages, hostPlatform, fetchurl, perl, buildLinux, ... } @ args:
with stdenv.lib; with stdenv.lib;

View File

@ -1,4 +1,4 @@
{ stdenv, hostPlatform, fetchurl, perl, buildLinux, ... } @ args: { stdenv, buildPackages, hostPlatform, fetchurl, perl, buildLinux, ... } @ args:
import ./generic.nix (args // rec { import ./generic.nix (args // rec {
version = "4.4.111"; version = "4.4.111";

View File

@ -1,4 +1,4 @@
{ stdenv, hostPlatform, fetchurl, perl, buildLinux, ... } @ args: { stdenv, buildPackages, hostPlatform, fetchurl, perl, buildLinux, ... } @ args:
import ./generic.nix (args // rec { import ./generic.nix (args // rec {
version = "4.9.76"; version = "4.9.76";

View File

@ -1,4 +1,4 @@
{ stdenv, hostPlatform, fetchFromGitHub, perl, buildLinux, ... } @ args: { stdenv, buildPackages, hostPlatform, fetchFromGitHub, perl, buildLinux, ... } @ args:
let let
modDirVersion = "4.9.61"; modDirVersion = "4.9.61";

View File

@ -1,4 +1,4 @@
{ stdenv, hostPlatform, fetchFromGitHub, perl, buildLinux, ... } @ args: { stdenv, buildPackages, hostPlatform, fetchFromGitHub, perl, buildLinux, ... } @ args:
with stdenv.lib; with stdenv.lib;

View File

@ -1,4 +1,4 @@
{ stdenv, hostPlatform, fetchFromGitHub, perl, buildLinux, ... } @ args: { stdenv, buildPackages, hostPlatform, fetchFromGitHub, perl, buildLinux, ... } @ args:
import ./generic.nix (rec { import ./generic.nix (rec {
mptcpVersion = "0.93"; mptcpVersion = "0.93";

View File

@ -1,4 +1,4 @@
{ stdenv, hostPlatform, fetchFromGitHub, perl, buildLinux, ... } @ args: { stdenv, buildPackages, hostPlatform, fetchFromGitHub, perl, buildLinux, ... } @ args:
let let
modDirVersion = "4.9.59"; modDirVersion = "4.9.59";

View File

@ -1,4 +1,4 @@
{ stdenv, hostPlatform, fetchFromGitHub, perl, buildLinux, ncurses, ... } @ args: { stdenv, buildPackages, hostPlatform, fetchFromGitHub, perl, buildLinux, ncurses, ... } @ args:
import ./generic.nix (args // rec { import ./generic.nix (args // rec {
version = "4.12.2"; version = "4.12.2";

View File

@ -1,4 +1,4 @@
{ stdenv, hostPlatform, fetchgit, perl, buildLinux, ... } @ args: { stdenv, buildPackages, hostPlatform, fetchgit, perl, buildLinux, ... } @ args:
import ./generic.nix (args // rec { import ./generic.nix (args // rec {
version = "4.11.2017.08.23"; version = "4.11.2017.08.23";

View File

@ -1,4 +1,4 @@
{ stdenv, hostPlatform, fetchurl, perl, buildLinux, libelf, utillinux, ... } @ args: { stdenv, buildPackages, hostPlatform, fetchurl, perl, buildLinux, libelf, utillinux, ... } @ args:
import ./generic.nix (args // rec { import ./generic.nix (args // rec {
version = "4.15-rc8"; version = "4.15-rc8";

View File

@ -1,6 +1,6 @@
{ runCommand, nettools, bc, perl, gmp, libmpc, mpfr, kmod, openssl { buildPackages, runCommand, nettools, bc, perl, gmp, libmpc, mpfr, kmod, openssl
, libelf ? null , libelf
, utillinux ? null , utillinux
, writeTextFile, ubootTools , writeTextFile, ubootTools
, hostPlatform , hostPlatform
}: }:
@ -26,19 +26,11 @@ in {
src, src,
# Any patches # Any patches
kernelPatches ? [], kernelPatches ? [],
# Patches for native compiling only # The kernel .config file
nativeKernelPatches ? [],
# Patches for cross compiling only
crossKernelPatches ? [],
# The native kernel .config file
configfile, configfile,
# The cross kernel .config file
crossConfigfile ? configfile,
# Manually specified nixexpr representing the config # Manually specified nixexpr representing the config
# If unspecified, this will be autodetected from the .config # If unspecified, this will be autodetected from the .config
config ? stdenv.lib.optionalAttrs allowImportFromDerivation (readConfig configfile), config ? stdenv.lib.optionalAttrs allowImportFromDerivation (readConfig configfile),
# Cross-compiling config
crossConfig ? if allowImportFromDerivation then (readConfig crossConfigfile) else config,
# Use defaultMeta // extraMeta # Use defaultMeta // extraMeta
extraMeta ? {}, extraMeta ? {},
# Whether to utilize the controversial import-from-derivation feature to parse the config # Whether to utilize the controversial import-from-derivation feature to parse the config
@ -61,8 +53,8 @@ let
commonMakeFlags = [ commonMakeFlags = [
"O=$(buildRoot)" "O=$(buildRoot)"
] ++ stdenv.lib.optionals (stdenv.platform ? kernelMakeFlags) ] ++ stdenv.lib.optionals (hostPlatform.platform ? kernelMakeFlags)
stdenv.platform.kernelMakeFlags; hostPlatform.platform.kernelMakeFlags;
drvAttrs = config_: platform: kernelPatches: configfile: drvAttrs = config_: platform: kernelPatches: configfile:
let let
@ -233,13 +225,13 @@ let
maintainers.thoughtpolice maintainers.thoughtpolice
]; ];
platforms = platforms.linux; platforms = platforms.linux;
} // extraMeta; };
}; } // extraMeta;
in in
assert stdenv.lib.versionAtLeast version "4.14" -> libelf != null; assert stdenv.lib.versionAtLeast version "4.14" -> libelf != null;
assert stdenv.lib.versionAtLeast version "4.15" -> utillinux != null; assert stdenv.lib.versionAtLeast version "4.15" -> utillinux != null;
stdenv.mkDerivation ((drvAttrs config stdenv.platform (kernelPatches ++ nativeKernelPatches) configfile) // { stdenv.mkDerivation ((drvAttrs config hostPlatform.platform kernelPatches configfile) // {
name = "linux-${version}"; name = "linux-${version}";
enableParallelBuilding = true; enableParallelBuilding = true;
@ -253,20 +245,11 @@ stdenv.mkDerivation ((drvAttrs config stdenv.platform (kernelPatches ++ nativeKe
hardeningDisable = [ "bindnow" "format" "fortify" "stackprotector" "pic" ]; hardeningDisable = [ "bindnow" "format" "fortify" "stackprotector" "pic" ];
makeFlags = commonMakeFlags ++ [ makeFlags = commonMakeFlags ++ [
"ARCH=${stdenv.platform.kernelArch}" "HOSTCC=${buildPackages.stdenv.cc.targetPrefix}gcc"
"ARCH=${stdenv.hostPlatform.platform.kernelArch}"
] ++ stdenv.lib.optional (stdenv.hostPlatform != stdenv.buildPlatform) [
"CROSS_COMPILE=${stdenv.cc.targetPrefix}"
]; ];
karch = stdenv.platform.kernelArch; karch = hostPlatform.platform.kernelArch;
crossAttrs = let cp = hostPlatform.platform; in
(drvAttrs crossConfig cp (kernelPatches ++ crossKernelPatches) crossConfigfile) // {
makeFlags = commonMakeFlags ++ [
"ARCH=${cp.kernelArch}"
"CROSS_COMPILE=$(crossConfig)-"
];
karch = cp.kernelArch;
nativeBuildInputs = optional (cp.kernelTarget == "uImage") ubootTools;
};
}) })

View File

@ -23,8 +23,8 @@ stdenv.mkDerivation {
# perf refers both to newt and slang # perf refers both to newt and slang
# binutils is required for libbfd. # binutils is required for libbfd.
nativeBuildInputs = [ asciidoc xmlto docbook_xsl docbook_xml_dtd_45 libxslt nativeBuildInputs = [ asciidoc xmlto docbook_xsl docbook_xml_dtd_45 libxslt
flex bison libiberty libaudit makeWrapper pkgconfig ]; flex bison libiberty libaudit makeWrapper pkgconfig python perl ];
buildInputs = [ elfutils python perl newt slang libunwind binutils zlib ] ++ buildInputs = [ elfutils newt slang libunwind binutils zlib ] ++
stdenv.lib.optional withGtk gtk2; stdenv.lib.optional withGtk gtk2;
# Note: we don't add elfutils to buildInputs, since it provides a # Note: we don't add elfutils to buildInputs, since it provides a
@ -40,6 +40,10 @@ stdenv.mkDerivation {
"-Wno-error=unused-const-variable" "-Wno-error=misleading-indentation" "-Wno-error=unused-const-variable" "-Wno-error=misleading-indentation"
]; ];
makeFlags = if stdenv.hostPlatform == stdenv.buildPlatform
then null
else "CROSS_COMPILE=${stdenv.cc.prefix}";
installFlags = "install install-man ASCIIDOC8=1"; installFlags = "install install-man ASCIIDOC8=1";
preFixup = '' preFixup = ''
@ -47,15 +51,6 @@ stdenv.mkDerivation {
--prefix PATH : "${binutils}/bin" --prefix PATH : "${binutils}/bin"
''; '';
crossAttrs = {
/* I don't want cross-python or cross-perl -
I don't know if cross-python even works */
propagatedBuildInputs = [ elfutils.crossDrv newt.crossDrv ];
makeFlags = "CROSS_COMPILE=${stdenv.cc.targetPrefix}";
elfutils = elfutils.crossDrv;
inherit (kernel.crossDrv) src patches;
};
meta = { meta = {
homepage = https://perf.wiki.kernel.org/; homepage = https://perf.wiki.kernel.org/;
description = "Linux tools to profile with performance counters"; description = "Linux tools to profile with performance counters";