makeBinaryWrapper: fix cross-compilation and add test

Fixes https://github.com/NixOS/nixpkgs/issues/175045
This commit is contained in:
Naïm Favier 2022-05-29 17:26:23 +02:00
parent 5db40e768d
commit eef2c762ce
No known key found for this signature in database
GPG key ID: 49B07322580B7EE2
3 changed files with 58 additions and 28 deletions

View file

@ -1,10 +1,11 @@
{ stdenv { stdenv
, targetPackages
, lib , lib
, makeSetupHook , makeSetupHook
, dieHook , dieHook
, writeShellScript , writeShellScript
, tests , tests
, cc ? stdenv.cc , cc ? targetPackages.stdenv.cc
, sanitizers ? [] , sanitizers ? []
}: }:
@ -14,7 +15,7 @@ makeSetupHook {
++ lib.optional (stdenv.isDarwin && stdenv.isAarch64) cc; ++ lib.optional (stdenv.isDarwin && stdenv.isAarch64) cc;
substitutions = { substitutions = {
cc = "${cc}/bin/cc ${lib.escapeShellArgs (map (s: "-fsanitize=${s}") sanitizers)}"; cc = "${cc}/bin/${cc.targetPrefix}cc ${lib.escapeShellArgs (map (s: "-fsanitize=${s}") sanitizers)}";
# Extract the function call used to create a binary wrapper from its embedded docstring # Extract the function call used to create a binary wrapper from its embedded docstring
passthru.extractCmd = writeShellScript "extract-binary-wrapper-cmd" '' passthru.extractCmd = writeShellScript "extract-binary-wrapper-cmd" ''

View file

@ -0,0 +1,23 @@
{ stdenv
, runCommand
, makeBinaryWrapper
, binutils
, expectedArch ? stdenv.hostPlatform.parsed.cpu.name
}:
runCommand "make-binary-wrapper-test-cross" {
nativeBuildInputs = [
makeBinaryWrapper
binutils
];
inherit expectedArch;
} ''
touch prog
chmod +x prog
makeWrapper prog $out
read -r _ arch < <($READELF --file-header $out | grep Machine:)
if [[ ''${arch,,} != *"''${expectedArch,,}"* ]]; then
echo "expected $expectedArch, got $arch"
exit 1
fi
''

View file

@ -1,12 +1,19 @@
{ lib, coreutils, python3, gcc, writeText, writeScript, runCommand, makeBinaryWrapper }: { lib
, stdenv
, pkgsCross
, makeBinaryWrapper
, writeText
, runCommand
, runCommandCC
}:
let let
env = { buildInputs = [ makeBinaryWrapper ]; }; env = { nativeBuildInputs = [ makeBinaryWrapper ]; };
envCheck = runCommand "envcheck" env '' envCheck = runCommandCC "envcheck" env ''
${gcc}/bin/cc -Wall -Werror -Wpedantic -o $out ${./envcheck.c} cc -Wall -Werror -Wpedantic -o $out ${./envcheck.c}
''; '';
makeGoldenTest = testname: runCommand "test-wrapper_${testname}" env '' makeGoldenTest = testname: runCommand "make-binary-wrapper-test-${testname}" env ''
mkdir -p ./tmp/foo mkdir -p tmp/foo # for the chdir test
params=$(<"${./.}/${testname}.cmdline") params=$(<"${./.}/${testname}.cmdline")
eval "makeCWrapper /send/me/flags $params" > wrapper.c eval "makeCWrapper /send/me/flags $params" > wrapper.c
@ -32,8 +39,7 @@ let
cp wrapper.c $out cp wrapper.c $out
''; '';
tests = let tests = lib.genAttrs [
names = [
"add-flags" "add-flags"
"argv0" "argv0"
"basic" "basic"
@ -42,14 +48,14 @@ let
"env" "env"
"inherit-argv0" "inherit-argv0"
"invalid-env" "invalid-env"
"overlength-strings"
"prefix" "prefix"
"suffix" "suffix"
"overlength-strings" ] makeGoldenTest // lib.optionalAttrs (! stdenv.isDarwin) {
]; cross = pkgsCross.aarch64-multiplatform.callPackage ./cross.nix { };
f = name: lib.nameValuePair name (makeGoldenTest name); };
in builtins.listToAttrs (builtins.map f names); in
in writeText "make-binary-wrapper-test" ''
${lib.concatStringsSep "\n" (lib.mapAttrsToList (_: test: '' writeText "make-binary-wrapper-tests" ''
"${test.name}" "${test}" ${lib.concatStringsSep "\n" (builtins.attrValues tests)}
'') tests)}
'' // tests '' // tests