Skip to content

Commit

Permalink
linux: simplify toolchain selection
Browse files Browse the repository at this point in the history
When we set CROSS_COMPILE to set the toolchain prefix, everything
works out of the box without trying to drop absolute paths everywhere.
This removes a lot of duplication from the kernel derivations.
  • Loading branch information
blitz committed Oct 21, 2024
1 parent a2f6525 commit 89da132
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 37 deletions.
3 changes: 1 addition & 2 deletions pkgs/os-specific/linux/kernel/generate-config.pl
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ sub runConfig {
# required to get clang LTO working, among other things.
my $pid = open2(\*IN, \*OUT,
"make -C $ENV{SRC} O=$buildRoot config"
. " SHELL=bash ARCH=$ENV{ARCH} CC=$ENV{CC} HOSTCC=$ENV{HOSTCC} HOSTCXX=$ENV{HOSTCXX}"
. " LD=$ENV{LD} NM=$ENV{NM} AR=$ENV{AR} OBJCOPY=$ENV{OBJCOPY}"
. " SHELL=bash ARCH=$ENV{ARCH} CROSS_COMPILE=$ENV{CROSS_COMPILE}"
. " $makeFlags");

# Parse the output, look for questions and then send an
Expand Down
22 changes: 3 additions & 19 deletions pkgs/os-specific/linux/kernel/generic.nix
Original file line number Diff line number Diff line change
Expand Up @@ -166,34 +166,18 @@ let

buildPhase = ''
export buildRoot="''${buildRoot:-build}"
export HOSTCC=$CC_FOR_BUILD
export HOSTCXX=$CXX_FOR_BUILD
export HOSTAR=$AR_FOR_BUILD
export HOSTLD=$LD_FOR_BUILD
# Absolute paths for tools avoid any PATH-clobbering issues.
export CC=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc
export LD=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}ld
export AR=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}ar
export NM=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}nm
export STRIP=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}strip
export OBJCOPY=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}objcopy
export OBJDUMP=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}objdump
export READELF=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}readelf
# Get a basic config file for later refinement with $generateConfig.
make $makeFlags \
-C . O="$buildRoot" $kernelBaseConfig \
ARCH=$kernelArch \
HOSTCC=$HOSTCC HOSTCXX=$HOSTCXX HOSTAR=$HOSTAR HOSTLD=$HOSTLD \
CC=$CC OBJCOPY=$OBJCOPY OBJDUMP=$OBJDUMP READELF=$READELF \
LD=$LD AR=$AR NM=$NM STRIP=$STRIP \
ARCH=$kernelArch CROSS_COMPILE=${stdenv.cc.targetPrefix} \
$makeFlags
# Create the config file.
echo "generating kernel configuration..."
ln -s "$kernelConfigPath" "$buildRoot/kernel-config"
DEBUG=1 ARCH=$kernelArch KERNEL_CONFIG="$buildRoot/kernel-config" AUTO_MODULES=$autoModules \
DEBUG=1 ARCH=$kernelArch CROSS_COMPILE=${stdenv.cc.targetPrefix} \
KERNEL_CONFIG="$buildRoot/kernel-config" AUTO_MODULES=$autoModules \
PREFER_BUILTIN=$preferBuiltin BUILD_ROOT="$buildRoot" SRC=. MAKE_FLAGS="$makeFlags" \
perl -w $generateConfig
'';
Expand Down
22 changes: 6 additions & 16 deletions pkgs/os-specific/linux/kernel/manual-config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -426,24 +426,14 @@ stdenv.mkDerivation ((drvAttrs config stdenv.hostPlatform.linux-kernel kernelPat
# Absolute paths for compilers avoid any PATH-clobbering issues.
makeFlags = [
"O=$(buildRoot)"
"CC=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc"
"LD=${if stdenv.isx86_64 && stdenv.cc.bintools.isLLVM
then
# The wrapper for ld.lld breaks linking the kernel. We use the unwrapped linker as workaround. See:
# https://github.com/NixOS/nixpkgs/issues/321667
stdenv.cc.bintools.bintools
else stdenv.cc}/bin/${stdenv.cc.targetPrefix}ld"
"AR=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}ar"
"NM=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}nm"
"STRIP=${stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}strip"
"OBJCOPY=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}objcopy"
"OBJDUMP=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}objdump"
"READELF=${stdenv.cc}/bin/${stdenv.cc.targetPrefix}readelf"
"HOSTCC=${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}cc"
"HOSTLD=${buildPackages.stdenv.cc.bintools}/bin/${buildPackages.stdenv.cc.targetPrefix}ld"
"ARCH=${stdenv.hostPlatform.linuxArch}"
] ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
"CROSS_COMPILE=${stdenv.cc.targetPrefix}"
] ++ lib.optionals (stdenv.isx86_64 && stdenv.cc.bintools.isLLVM) [
# The wrapper for ld.lld breaks linking the kernel. We use the
# unwrapped linker as workaround. See:
#
# https://github.com/NixOS/nixpkgs/issues/321667
"LD=${stdenv.cc.bintools.bintools}/bin/${stdenv.cc.targetPrefix}ld"
] ++ (stdenv.hostPlatform.linux-kernel.makeFlags or [])
++ extraMakeFlags;

Expand Down

0 comments on commit 89da132

Please sign in to comment.