Skip to content

Commit

Permalink
feat: optimize size usage
Browse files Browse the repository at this point in the history
  • Loading branch information
rvolosatovs committed Jan 9, 2025
1 parent c6bad1f commit 3024f0b
Showing 1 changed file with 52 additions and 20 deletions.
72 changes: 52 additions & 20 deletions lib/rust/mkAttrs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,26 @@ let

buildHostPackage =
craneArgs:
trace' "buildHostPackage" {
inherit craneArgs;
} callHostCraneWithDeps craneArgs hostCraneLib.buildPackage;
trace' "buildHostPackage"
{
inherit craneArgs;
}
callHostCraneWithDeps
(
craneArgs
// {
nativeBuildInputs = [
final.removeReferencesTo
] ++ optional final.stdenv.hostPlatform.isDarwin final.darwin.autoSignDarwinBinariesHook;

postInstall = ''
find "$out" -type f -exec remove-references-to \
-t ${hostRustToolchain} \
'{}' +
'';
}
)
hostCraneLib.buildPackage;

hostBin = buildHostPackage commonReleaseArgs;
hostDebugBin = buildHostPackage commonDebugArgs;
Expand Down Expand Up @@ -295,6 +312,7 @@ let
craneArgs,
craneLib,
pkgsCross,
rustToolchain,
target,
}:
let
Expand Down Expand Up @@ -336,31 +354,31 @@ let

CARGO_BUILD_TARGET = target;
}
// optionalAttrs pkgsCross.stdenv.hostPlatform.isDarwin {
# Removing vendor references here:
# - invalidates the signature, which is required on aarch64-darwin
# - fails on Darwin dylibs starting with Rust 1.79
doNotRemoveReferencesToVendorDir = true;
}
# Use `rust-lld` linker and Zig C compiler for Darwin targets
// (
if pkgsCross.stdenv.hostPlatform.isDarwin then
if
pkgsCross.stdenv.hostPlatform.isDarwin
# Use `rust-lld` linker and Zig C compiler for Darwin targets
then
{
# Removing vendor references here:
# - invalidates the signature, which is required on aarch64-darwin
# - fails on Darwin dylibs starting with Rust 1.79
doNotRemoveReferencesToVendorDir = true;

depsBuildBuild = [
crossZigCC
];

disallowedReferences = [
final.zig

crossZigCC
];

preBuild =
''
export HOME=$(mktemp -d)
''
+ optionalString pkgsCross.stdenv.hostPlatform.isDarwin ''
export SDKROOT="${macos-sdk}"
'';
preBuild = ''
export HOME=$(mktemp -d)
export SDKROOT="${macos-sdk}"
'';

"CC_${target}" = "${target}-zigcc";

Expand All @@ -369,21 +387,34 @@ let
else
(
{
depsBuildBuild = [
disallowedReferences = [
pkgsCross.stdenv.cc
] ++ optional pkgsCross.stdenv.hostPlatform.isWindows pkgsCross.windows.pthreads;

disallowedReferences = [
depsBuildBuild = [
pkgsCross.stdenv.cc
] ++ optional pkgsCross.stdenv.hostPlatform.isWindows pkgsCross.windows.pthreads;

nativeBuildInputs = [
final.removeReferencesTo
];

postInstall = ''
find "$out" -type f -exec remove-references-to \
-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 = [
final.mold
final.removeReferencesTo
];

"CARGO_TARGET_${toUpper (kebab2snake target)}_RUSTFLAGS" = "-Clink-arg=-fuse-ld=mold";
Expand Down Expand Up @@ -701,6 +732,7 @@ let
craneArgs
craneLib
pkgsCross
rustToolchain
target
;
};
Expand Down

0 comments on commit 3024f0b

Please sign in to comment.