1
0
Fork 0

build(flake): Wrap mold so that it works with the nix supplied libraries

This commit is contained in:
Benedikt Peetz 2023-09-03 22:11:41 +02:00
parent a8112d554e
commit eb63cb6247
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
1 changed files with 147 additions and 125 deletions

272
flake.nix
View File

@ -52,139 +52,161 @@
... ...
}: }:
flake-utils.lib.eachDefaultSystem (system: let flake-utils.lib.eachDefaultSystem (system: let
c_rust = pkgs.rust-bin.fromRustupToolchainFile "${rustc_cranelift_backend}/rust-toolchain"; inherit (pkgs) lib;
rcb = pkgs.stdenv.mkDerivation { bintools-wrapper = "${nixpkgs}/pkgs/build-support/bintools-wrapper";
pname = "rustc_cranelift_backend"; mold' = pkgs.symlinkJoin {
version = "1.0"; name = "mold";
buildInputs = [c_rust]; paths = [pkgs.mold];
nativeBuildInputs = with pkgs; [gawk fd]; nativeBuildInputs = [pkgs.makeWrapper];
srcs = ["${rustc_cranelift_backend_src}" "${c_rust}"]; suffixSalt = lib.replaceStrings ["-" "."] ["_" "_"] pkgs.targetPlatform.config;
sourceRoot = "."; postBuild = ''
postUnpack = '' for bin in ${pkgs.mold}/bin/*; do
rust_dir="$(fd . --max-depth 1 | awk '!/source/ && !/env-vars/')" rm $out/bin/"$(basename "$bin")"
# remove unneeded stuff export prog="$bin"
rm -r "$rust_dir"/{bin,nix-support,share}; substituteAll "${bintools-wrapper}/ld-wrapper.sh" $out/bin/"$(basename "$bin")"
rm -r "$rust_dir"/lib/rustlib/{etc,src,rustc-src}; chmod +x $out/bin/"$(basename "$bin")"
cp -r "$rust_dir"/. source/extra_dependencies mkdir -p $out/nix-support
rm -r "$rust_dir" substituteAll "${bintools-wrapper}/add-flags.sh" $out/nix-support/add-flags.sh
cd source substituteAll "${bintools-wrapper}/add-hardening.sh" $out/nix-support/add-hardening.sh
''; substituteAll "${bintools-wrapper}/../wrapper-common/utils.bash" $out/nix-support/utils.bash
postPatch = '' done
# patch bins '';
for file in $(fd . --type file);do };
file="$(file --mime "$file" | awk 'BEGIN{FS=":"}/application\/x-pie-executable/{print $1}')"; c_rust = pkgs.rust-bin.fromRustupToolchainFile "${rustc_cranelift_backend}/rust-toolchain";
if [ "$file" ]; then rcb = pkgs.stdenv.mkDerivation {
echo "file: '$file' matches"; pname = "rustc_cranelift_backend";
correct_interpreter_path="$(ldd "$file" | tail -n1 | awk 'BEGIN{FS="=> "} {print $2}' | awk 'BEGIN{FS=" "}{print $1}')" version = "1.0";
echo "correct interpreter path is: '$correct_interpreter_path'" buildInputs = [c_rust];
patchelf --set-interpreter "$correct_interpreter_path" "$file" nativeBuildInputs = with pkgs; [gawk fd];
srcs = ["${rustc_cranelift_backend_src}" "${c_rust}"];
sourceRoot = ".";
postUnpack = ''
rust_dir="$(fd . --max-depth 1 | awk '!/source/ && !/env-vars/')"
if [ "$(patchelf --print-interpreter "$file")" = "$correct_interpreter_path" ];then # remove unneeded stuff
echo "Set interpreter"; rm -r "$rust_dir"/{bin,nix-support,share};
else rm -r "$rust_dir"/lib/rustlib/{etc,src,rustc-src};
echo "Failed to set interprter, the interpreter still is $(patchelf --print-interpreter )";
exit 1
fi
fi
done
# patch libs cp -r "$rust_dir"/. source/extra_dependencies
all_files=$(mktemp); rm -r "$rust_dir"
for file in $(fd .);do cd source
canonical_path="$(readlink -f "$file")" '';
file="$(file --mime "$canonical_path" | awk 'BEGIN{FS=":"}/application\/x-sharedlib/{print $1}')"; postPatch = ''
if [ "$file" ]; then # patch bins
echo "$file" >> $all_files; for file in $(fd . --type file);do
fi file="$(file --mime "$file" | awk 'BEGIN{FS=":"}/application\/x-pie-executable/{print $1}')";
done if [ "$file" ]; then
while read -r file; do echo "file: '$file' matches";
echo "___________________"; correct_interpreter_path="$(ldd "$file" | tail -n1 | awk 'BEGIN{FS="=> "} {print $2}' | awk 'BEGIN{FS=" "}{print $1}')"
echo "Checking file: '$file'"; echo "correct interpreter path is: '$correct_interpreter_path'"
is_missing="$(ldd "$file" | awk 'BEGIN{FS="=>"}{if (/not found/){print $1}}')" patchelf --set-interpreter "$correct_interpreter_path" "$file"
if [ "$is_missing" ];then
echo "Warning: The following things are missing:";
for line in $is_missing; do
echo " $line";
done
echo
for line in $is_missing; do
echo "Searching for a substitute for '$line'"
substitute="$(grep "$line" "$all_files" | awk 'BEGIN{FS=" "}{print $1}' | tail -n1)";
if [ "$substitute" ]; then
echo "Found '$substitute', which can substitute '$line'"
echo "Patching.."
patchelf --replace-needed "$line" "$(readlink -f "$substitute")" "$file";
else
echo "Error: Failed to find a substitute"
fi
done
else
echo "This file is not missing anything"
fi
done < $all_files
rm $all_files
'';
installPhase = ''
install -d $out/
cp -r ./. $out/
'';
};
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};
nightly = true; if [ "$(patchelf --print-interpreter "$file")" = "$correct_interpreter_path" ];then
rust = echo "Set interpreter";
if nightly else
then pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default) echo "Failed to set interprter, the interpreter still is $(patchelf --print-interpreter )";
else pkgs.rust-bin.stable.latest.default; exit 1
fi
fi
done
craneLib = (crane.mkLib pkgs).overrideToolchain rust; # patch libs
all_files=$(mktemp);
for file in $(fd .);do
canonical_path="$(readlink -f "$file")"
file="$(file --mime "$canonical_path" | awk 'BEGIN{FS=":"}/application\/x-sharedlib/{print $1}')";
if [ "$file" ]; then
echo "$file" >> $all_files;
fi
done
while read -r file; do
echo "___________________";
echo "Checking file: '$file'";
is_missing="$(ldd "$file" | awk 'BEGIN{FS="=>"}{if (/not found/){print $1}}')"
if [ "$is_missing" ];then
echo "Warning: The following things are missing:";
for line in $is_missing; do
echo " $line";
done
echo
for line in $is_missing; do
echo "Searching for a substitute for '$line'"
substitute="$(grep "$line" "$all_files" | awk 'BEGIN{FS=" "}{print $1}' | tail -n1)";
if [ "$substitute" ]; then
echo "Found '$substitute', which can substitute '$line'"
echo "Patching.."
patchelf --replace-needed "$line" "$(readlink -f "$substitute")" "$file";
else
echo "Error: Failed to find a substitute"
fi
done
else
echo "This file is not missing anything"
fi
done < $all_files
rm $all_files
'';
installPhase = ''
install -d $out/
cp -r ./. $out/
'';
};
pkgs = import nixpkgs {
inherit system;
overlays = [(import rust-overlay)];
};
nativeBuildInputs = with pkgs; [ nightly = true;
pkg-config rust =
mold if nightly
rcb then pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default)
else pkgs.rust-bin.stable.latest.default;
craneLib = (crane.mkLib pkgs).overrideToolchain rust;
nativeBuildInputs = with pkgs; [
pkg-config
mold'
rcb
];
buildInputs = with pkgs; [
openssl
lua54Packages.lua
];
craneBuild = craneLib.buildPackage {
src = craneLib.cleanCargoSource ./.;
doCheck = true;
inherit nativeBuildInputs buildInputs;
};
in {
packages = {
default = craneBuild;
};
app.default = {
type = "app";
program = "${self.packages.${system}.default}/bin/trinitix";
};
devShells.default = pkgs.mkShell {
packages = with pkgs; [
nil
alejandra
statix
ltex-ls
rust
rust-analyzer
cargo-edit
cargo-expand
]; ];
buildInputs = with pkgs; [ inherit nativeBuildInputs buildInputs;
openssl };
lua54Packages.lua });
];
craneBuild = craneLib.buildPackage {
src = craneLib.cleanCargoSource ./.;
doCheck = true;
inherit nativeBuildInputs buildInputs;
};
in {
packages = {
default = craneBuild;
};
app.default = {
type = "app";
program = "${self.packages.${system}.default}/bin/trinitix";
};
devShells.default = pkgs.mkShell {
packages = with pkgs; [
nil
alejandra
statix
ltex-ls
rust
rust-analyzer
cargo-edit
cargo-expand
];
inherit nativeBuildInputs buildInputs;
};
});
} }
# vim: ts=2 # vim: ts=2