libredirect: build fat library for x86_64, arm64, arm64e on darwin (#153441)

macOS's dyld can be rather picky as to what dylib it accepts. This
even changes across macOS versions. Therefore we now build a fat
dylib with all three architectures (x86_64, arm64, arm64e). This
should then be compatible with pretty much any macOS's dyld.
This commit is contained in:
Moritz Angermann 2022-01-08 07:32:21 +08:00 committed by GitHub
parent ecd989e7ee
commit 38377ab026
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,4 @@
{ stdenv, lib, coreutils }: { lib, stdenv, bintools-unwrapped, llvmPackages_13, coreutils }:
stdenv.mkDerivation rec { stdenv.mkDerivation rec {
pname = "libredirect"; pname = "libredirect";
version = "0"; version = "0";
@ -9,17 +8,36 @@ stdenv.mkDerivation rec {
cp ${./test.c} test.c cp ${./test.c} test.c
''; '';
libName = "libredirect" + stdenv.targetPlatform.extensions.sharedLibrary;
outputs = ["out" "hook"]; outputs = ["out" "hook"];
libName = "libredirect" + stdenv.targetPlatform.extensions.sharedLibrary;
buildPhase = '' buildPhase = ''
runHook preBuild runHook preBuild
$CC -Wall -std=c99 -O3 -fPIC -ldl -shared \ ${if stdenv.isDarwin && stdenv.isAarch64 then ''
${lib.optionalString stdenv.isDarwin "-Wl,-install_name,$out/lib/$libName"} \ # We need the unwrapped binutils and clang:
-o "$libName" \ # We also want to build a fat library with x86_64, arm64, arm64e in there.
libredirect.c # Because we use the unwrapped tools, we need to provide -isystem for headers
# and the library search directory for libdl.
# We can't build this on x86_64, because the libSystem we point to doesn't
# like arm64(e).
PATH=${bintools-unwrapped}/bin:${llvmPackages_13.clang-unwrapped}/bin:$PATH \
clang -arch x86_64 -arch arm64 -arch arm64e \
-isystem ${llvmPackages_13.clang.libc}/include \
-isystem ${llvmPackages_13.libclang.lib}/lib/clang/*/include \
-L${llvmPackages_13.clang.libc}/lib \
-Wl,-install_name,$out/lib/$libName \
-Wall -std=c99 -O3 -fPIC libredirect.c \
-ldl -shared -o "$libName"
'' else if stdenv.isDarwin then ''
$CC -Wall -std=c99 -O3 -fPIC libredirect.c \
-Wl,-install_name,$out/lib/$libName \
-ldl -shared -o "$libName"
'' else ''
$CC -Wall -std=c99 -O3 -fPIC libredirect.c \
-ldl -shared -o "$libName"
''}
if [ -n "$doInstallCheck" ]; then if [ -n "$doInstallCheck" ]; then
$CC -Wall -std=c99 -O3 test.c -o test $CC -Wall -std=c99 -O3 test.c -o test