From a67f064bd77274722f60f8d8279f2bc7657e9aa2 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Thu, 9 Jan 2025 18:50:03 +0100 Subject: [PATCH] feat: optimize size usage --- lib/rust/mkAttrs.nix | 70 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/lib/rust/mkAttrs.nix b/lib/rust/mkAttrs.nix index 5a6fa2c..a1fda33 100644 --- a/lib/rust/mkAttrs.nix +++ b/lib/rust/mkAttrs.nix @@ -238,9 +238,26 @@ let buildHostPackage = craneArgs: - trace' "buildHostPackage" { - inherit craneArgs; - } callHostCraneWithDeps craneArgs hostCraneLib.buildPackage; + trace' "buildHostPackage" + { + inherit craneArgs; + } + callHostCraneWithDeps + ( + craneArgs + // { + nativeBuildInputs = [ + final.removeReferencesTo + ]; + + postInstall = '' + find "$out" -type f -exec remove-references-to \ + -t ${hostRustToolchain} \ + '{}' + + ''; + } + ) + hostCraneLib.buildPackage; hostBin = buildHostPackage commonReleaseArgs; hostDebugBin = buildHostPackage commonDebugArgs; @@ -295,6 +312,7 @@ let craneArgs, craneLib, pkgsCross, + rustToolchain, target, }: let @@ -329,6 +347,10 @@ let ${final.zig}/bin/zig cc ${optionalString pkgsCross.stdenv.buildPlatform.isDarwin ''--sysroot="$SDKROOT" -I"$SDKROOT/usr/include" -L"$SDKROOT/usr/lib" -F"$SDKROOT/System/Library/Frameworks"''} $@ -target ${target'} ''; + nativeBuildInputs = [ + final.removeReferencesTo + ]; + targetArgs = { HOST_AR = "${final.stdenv.cc.targetPrefix}ar"; @@ -346,14 +368,31 @@ let // ( if pkgsCross.stdenv.hostPlatform.isDarwin then { + inherit + nativeBuildInputs + ; + depsBuildBuild = [ crossZigCC - ]; + ] ++ optional final.stdenv.hostPlatform.isDarwin pkgsCross.darwin.autoSignDarwinBinariesHook; disallowedReferences = [ + final.stdenv.cc + final.zig + pkgsCross.stdenv.cc + crossZigCC ]; + postInstall = '' + find "$out" -type f -exec remove-references-to \ + -t ${crossZigCC} \ + -t ${final.stdenv.cc} \ + -t ${final.zig} \ + -t ${rustToolchain} \ + '{}' + + ''; + preBuild = '' export HOME=$(mktemp -d) @@ -369,20 +408,34 @@ let else ( { - depsBuildBuild = [ + inherit + nativeBuildInputs + ; + + disallowedReferences = [ + final.stdenv.cc pkgsCross.stdenv.cc ] ++ optional pkgsCross.stdenv.hostPlatform.isWindows pkgsCross.windows.pthreads; - disallowedReferences = [ + depsBuildBuild = [ pkgsCross.stdenv.cc - ]; + ] ++ optional pkgsCross.stdenv.hostPlatform.isWindows pkgsCross.windows.pthreads; + + postInstall = '' + find "$out" -type f -exec remove-references-to \ + -t ${final.stdenv.cc} \ + -t ${pkgsCross.stdenv.cc} \ + -t ${rustToolchain} \ + ${optionalString pkgsCross.stdenv.hostPlatform.isWindows "-t ${pkgsCross.windows.pthreads}"} \ + '{}' + + ''; "AR_${target}" = "${pkgsCross.stdenv.cc.targetPrefix}ar"; "CC_${target}" = "${pkgsCross.stdenv.cc.targetPrefix}cc"; } # Use `mold` linker for Linux targets // optionalAttrs pkgsCross.stdenv.hostPlatform.isLinux { - nativeBuildInputs = [ + nativeBuildInputs = nativeBuildInputs ++ [ final.mold ]; @@ -701,6 +754,7 @@ let craneArgs craneLib pkgsCross + rustToolchain target ; };