From 98e1f635c8f63328e5b4c0922067c2e414fe1873 Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Wed, 15 May 2024 12:19:36 +0200 Subject: [PATCH] make it all work --- .gitignore | 1 + pkgs/hare.nix | 40 ++++++++++++++++++++++++++++++---------- pkgs/ld.rs | 9 +++++---- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 21a560c..5ef9adf 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ hello* +tmp diff --git a/pkgs/hare.nix b/pkgs/hare.nix index 7e801ba..3d4dad7 100644 --- a/pkgs/hare.nix +++ b/pkgs/hare.nix @@ -3,17 +3,14 @@ lib, makeWrapper, scdoc, - substituteAll, qbe, harec, binutils-unwrapped, tzdata, python3, - writeScript, rustc, }: let - platform = lib.toLower stdenv.hostPlatform.uname.system; arch = stdenv.hostPlatform.uname.processor; qbePlatform = { x86_64 = "amd64_apple"; @@ -21,7 +18,7 @@ let }.${arch}; hareAs = ./hare-as.py; hareLd = stdenv.mkDerivation (finalAttrs: { - pname = "rust-ld"; + pname = "hare-ld"; version = "1.0"; src = [ ./ld.rs ]; buildInputs = [ rustc ]; @@ -31,11 +28,33 @@ let done ''; buildPhase = '' - rustc -o rust-ld.bin $src + rustc -o hare-ld.bin ld.rs ''; installPhase = '' mkdir -p $out/bin - cp rust-ld.bin $out/bin/rust-ld + cp hare-ld.bin $out/bin/hare-ld + ''; + }); + hareCc = stdenv.mkDerivation (finalAttrs: { + pname = "hare-cc"; + version = "1.0"; + src = [ ./ld.rs ]; + buildInputs = [ rustc ]; + unpackPhase = '' + for srcFile in $src; do + cp $srcFile $(stripHash $srcFile) + done + ''; + buildPhase = '' + sed -i \ + -e 's/"ld"/"cc"/g' \ + -e '11 a let mut args = vec![];' \ + ld.rs + rustc -o hare-cc.bin ld.rs + ''; + installPhase = '' + mkdir -p $out/bin + cp hare-cc.bin $out/bin/hare-cc ''; }); in @@ -72,9 +91,9 @@ stdenv.mkDerivation (finalAttrs: { "ARCH=${arch}" "VERSION=${finalAttrs.version}-nixpkgs" "QBEFLAGS=-t${qbePlatform}" - "CC=${stdenv.cc.targetPrefix}cc" + "CC=${hareCc}/bin/hare-cc" "AS=${hareAs}" - "LD=${hareLd}/bin/rust-ld" + "LD=${hareLd}/bin/hare-ld" # Strip the variable of an empty $(SRCDIR)/hare/third-party, since nix does # not follow the FHS. "HAREPATH=$(SRCDIR)/hare/stdlib" @@ -90,8 +109,9 @@ stdenv.mkDerivation (finalAttrs: { postFixup = '' wrapProgram $out/bin/hare \ --prefix PATH : ${lib.makeBinPath [harec qbe]} \ - --set LD ${hareLd}/bin/rust-ld \ - --set AS ${hareAs} + --set CC ${hareCc}/bin/hare-cc \ + --set AS ${hareAs} \ + --set LD ${hareLd}/bin/hare-ld ''; setupHook = ./setup-hook.sh; diff --git a/pkgs/ld.rs b/pkgs/ld.rs index d5be1d3..a017d6b 100755 --- a/pkgs/ld.rs +++ b/pkgs/ld.rs @@ -18,10 +18,11 @@ fn main() { // Skip next one too _ = argv.remove(0); } - "--gc-sections" => { } - _ if arg.starts_with("-T") => { } - _ if arg.starts_with("-z") => { } - _ if arg.starts_with("--script=") => { } + "--gc-sections" => {} + "-Wl,--gc-sections" | "-Wl,--no-gc-sections" => {} + _ if arg.starts_with("-T") => {} + _ if arg.starts_with("-z") => {} + _ if arg.starts_with("--script=") => {} _ => { args.push(arg); }