cross work
Some checks failed
CI / checks (push) Failing after 3s

This commit is contained in:
Robin Appelman 2024-09-26 18:09:03 +02:00
commit f4380e5341
4 changed files with 180 additions and 1 deletions

38
crossArgs.nix Normal file
View file

@ -0,0 +1,38 @@
{ callPackage, pkgsCross, stdenv, lib }:
let
inherit (lib) mapAttrs hasInfix replaceStrings toUpper concatStrings recursiveUpdate;
isMusl = hasInfix "-musl";
crossOpts = callPackage ./crossOpts.nix { };
buildCrossArgs = target: { targetDeps ? [ ]
, rustFlags ? (if isMusl target then "-C target-feature=+crt-static" else "")
, cFlags ? ""
, targetStdenv
, ...
} @ args:
let
isHostTarget = targetStdenv.targetPlatform.config == stdenv.targetPlatform.config;
# don't use the pkgsCross cc if the target is the host platform
targetCc = if isHostTarget then stdenv.cc else targetStdenv.cc;
targetUnderscore = replaceStrings [ "-" ] [ "_" ] target;
targetUpperCase = toUpper targetUnderscore;
rest = removeAttrs args [ "rustFlags" "cc" "cFlags" "targetDeps" "targetStdenv" "nativeBuildInputs" ];
# by adding the dependency in the (target specific) linker args instead of buildInputs
# we can prevent it trying to link to it for host build dependencies
rustFlagsWithDeps = rustFlags + concatStrings (map (targetDep: " -Clink-arg=-L${targetDep}/lib") targetDeps);
in
(
{
nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ targetCc stdenv.cc ];
"CARGO_TARGET_${targetUpperCase}_RUSTFLAGS" = rustFlagsWithDeps;
"CARGO_TARGET_${targetUpperCase}_LINKER" = "${targetCc.targetPrefix}cc";
"AR_${targetUnderscore}" = "${targetCc.targetPrefix}ar";
"CC_${targetUnderscore}" = "${targetCc.targetPrefix}cc";
"CCX_${targetUnderscore}" = "${targetCc.targetPrefix}ccx";
"HOST_CC" = "${stdenv.cc.targetPrefix}cc";
"CFLAGS_${targetUnderscore}" = cFlags;
} //
rest
);
in
target: buildCrossArgs target crossOpts.${target}