Build(flake): Add rustc cranelift codegen to development shell

This is executed by running `cargo clif` instead of `cargo`.
It _should_ result in decreased compile times (around 30%).
This commit is contained in:
Benedikt Peetz 2023-07-24 17:11:17 +02:00
parent 33d7062e9c
commit a79479c1fa
Signed by: bpeetz
GPG Key ID: A5E94010C3A642AD
1 changed files with 269 additions and 38 deletions

307
flake.nix
View File

@ -29,6 +29,29 @@
flake-utils.follows = "flake-utils"; flake-utils.follows = "flake-utils";
}; };
}; };
# cranelift
rustc_cranelift_backend = {
url = "github:bjorn3/rustc_codegen_cranelift/dev";
flake = false;
};
rustc_cranelift_backend_src = {
url = "https://github.com/bjorn3/rustc_codegen_cranelift/releases/download/dev/cg_clif-x86_64-unknown-linux-gnu.tar.xz";
flake = false;
};
# # dependencies
# rust_regex ={
# url = "git+https://github.com/rust-lang/regex.git";
# flake = false;
# };
# rust_portable_simd = {
# url = "git+https://github.com/rust-lang/portable-simd.git";
# flake = false;
# };
# rust_rand = {
# url = "git+https://github.com/rust-random/rand.git";
# flake = false;
# };
}; };
outputs = { outputs = {
@ -37,56 +60,264 @@
crane, crane,
flake-utils, flake-utils,
rust-overlay, rust-overlay,
rustc_cranelift_backend,
rustc_cranelift_backend_src,
# rust_regex,
# rust_portable_simd,
# rust_rand,
... ...
}: }:
flake-utils.lib.eachDefaultSystem (system: let flake-utils.lib.eachDefaultSystem (system: let
pkgs = import nixpkgs { rcb_deps = let
inherit system; c_rust = pkgs.rust-bin.fromRustupToolchainFile "${rustc_cranelift_backend}/rust-toolchain";
overlays = [(import rust-overlay)]; in
}; pkgs.stdenv.mkDerivation {
pname = "rustc_cranelift_backend_dependencies";
version = "1.0";
nativeBuildInputs = with pkgs; [git curl cacert c_rust];
src = "${rustc_cranelift_backend}";
# old implementation {{{
#"${rust_regex}" "${rust_portable_simd}" "${rust_rand}"
#];
#sourceRoot = "source";
# preUnpack = ''
# #set -x;
# unpackCmdHooks=(_defaultUnpack);
# checkForHashToName() {
# case "$1" in
# "regex")
# echo source/download/regex
# ;;
# "portable-simd")
# echo source/download/portable-simd
# ;;
# "rand")
# echo source/download/rand
# ;;
# "rustc_codegen_cranelift")
# echo source
# ;;
# *)
# echo "Warning: Unable to turn hash ($1) to name!" 1>&2
# echo "Using another method" 1>&2
# echo null;
# ;;
# esac
# }
# fail() {
# echo "$@";
# exit 1;
# }
# _defaultUnpack(){
# local fn="$1"
# local destination
# local finalDestination
#
# # extrem hacky solution to turn the nix hash path to useful names:
# destination="$(sed -n '2p' "$fn/Cargo.toml" | sed 's|name = "||' | sed 's|"||')"
#
# finalDestination="$(checkForHashToName "$destination")"
# if [ "$finalDestination" = "null" ]; then
# if grep "Portable SIMD" "$fn"/README.md > /dev/null 2>&1; then
# destination="portable-simd";
# else
# echo "Can not grep readme directly"
# fi
# fi
#
# finalDestination="$(checkForHashToName "$destination")"
# if [ "$finalDestination" = "null" ]; then
# fail "No idea how to turn your hash to a name, sorry!";
# fi
#
# echo "Final destination is '$finalDestination'"
# destination="$finalDestination"
# echo "Copying '$fn' to '$destination'"
#
# mkdir --parents "$destination";
# # We can't preserve hardlinks because they may have been
# # introduced by store optimization, which might break things
# # in the build.
# cp -pr --reflink=auto -- "$fn"/. "$destination"
# chmod -R u+w "$destination"
# }
# '';
# }}}
buildPhase = ''
bash ./y.sh prepare
'';
installPhase = ''
install -d $out/
cp -r ./. $out/
'';
fixupPhase = ''
set -x
''; # skip it
CARGO = "${c_rust}/bin/cargo";
RUSTC = "${c_rust}/bin/rustc";
RUSTDOC = "${c_rust}/bin/rustdoc";
CARGO_HOME = "/build/cargo";
#rust-nightly = pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default); outputHashAlgo = "sha512";
rust-stable = pkgs.rust-bin.stable.latest.default; outputHashMode = "recursive";
outputHash = "sha512-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==";
};
rcb = let
c_rust = pkgs.rust-bin.fromRustupToolchainFile "${rustc_cranelift_backend}/rust-toolchain";
in
pkgs.stdenv.mkDerivation {
pname = "rustc_cranelift_backend";
version = "1.0";
nativeBuildInputs = [c_rust];
src = "${rcb_deps}";
buildPhase = ''
bash ./y.sh build
'';
installPhase = ''
install -d $out/
cp -r ./dist/. $out/
'';
CARGO = "${c_rust}/bin/cargo";
RUSTC = "${c_rust}/bin/rustc";
RUSTDOC = "${c_rust}/bin/rustdoc";
CARGO_HOME = "/build/cargo";
};
c_rust = pkgs.rust-bin.fromRustupToolchainFile "${rustc_cranelift_backend}/rust-toolchain";
rcb_better = pkgs.stdenv.mkDerivation {
pname = "rustc_cranelift_backend";
version = "1.0";
buildInputs = [c_rust];
nativeBuildInputs = with pkgs; [gawk fd];
srcs = ["${rustc_cranelift_backend_src}" "${c_rust}"];
sourceRoot = ".";
postUnpack = ''
rust_dir="$(fd . --max-depth 1 | awk '!/source/ && !/env-vars/')"
craneLib = (crane.mkLib pkgs).overrideToolchain rust-stable; # remove unneeded stuff
rm -r "$rust_dir"/{bin,nix-support,share};
rm -r "$rust_dir"/lib/rustlib/{etc,src,rustc-src};
nativeBuildInputs = with pkgs; [ cp -r "$rust_dir"/. source/extra_dependencies
pkg-config rm -r "$rust_dir"
mold cd source
]; '';
buildInputs = with pkgs; [ postPatch = ''
openssl # patch bins
]; for file in $(fd . --type file);do
file="$(file --mime "$file" | awk 'BEGIN{FS=":"}/application\/x-pie-executable/{print $1}')";
if [ "$file" ]; then
echo "file: '$file' matches";
correct_interpreter_path="$(ldd "$file" | tail -n1 | awk 'BEGIN{FS="=> "} {print $2}' | awk 'BEGIN{FS=" "}{print $1}')"
echo "correct interpreter path is: '$correct_interpreter_path'"
patchelf --set-interpreter "$correct_interpreter_path" "$file"
craneBuild = craneLib.buildPackage { if [ "$(patchelf --print-interpreter "$file")" = "$correct_interpreter_path" ];then
src = craneLib.cleanCargoSource ./.; echo "Set interpreter";
else
echo "Failed to set interprter, the interpreter still is $(patchelf --print-interpreter )";
exit 1
fi
fi
done
doCheck = true; # patch libs
inherit nativeBuildInputs buildInputs; all_files=$(mktemp);
}; for file in $(fd .);do
in { canonical_path="$(readlink -f "$file")"
packages.default = craneBuild; 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)];
};
app.default = { nightly = true;
type = "app"; rust =
program = "${self.packages.${system}.default}/bin/trinitix"; if nightly
}; then pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default)
else pkgs.rust-bin.stable.latest.default;
devShells.default = pkgs.mkShell { craneLib = (crane.mkLib pkgs).overrideToolchain rust;
packages = with pkgs; [
nil
alejandra
statix
ltex-ls
rust-stable nativeBuildInputs = with pkgs; [
rust-analyzer pkg-config
cargo-edit mold
cargo-expand rcb_better
#self.packages."${system}".cranelift-deps
]; ];
inherit nativeBuildInputs buildInputs; buildInputs = with pkgs; [
}; openssl
}); ];
craneBuild = craneLib.buildPackage {
src = craneLib.cleanCargoSource ./.;
doCheck = true;
inherit nativeBuildInputs buildInputs;
};
in {
packages = {
default = craneBuild;
cranelift = rcb_better;
cranelift-deps = c_rust;
};
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