From e134f4888b19ac993bf1d9fc365cbf5c729ab630 Mon Sep 17 00:00:00 2001 From: David Mak Date: Thu, 13 Jun 2024 15:13:44 +0800 Subject: [PATCH] [meta] Add option to use mold as linker --- README.md | 13 +++++++++++++ flake.nix | 15 ++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 40b0d077..38fab1a8 100644 --- a/README.md +++ b/README.md @@ -60,3 +60,16 @@ Several things to note: - If `cargo fmt` or `cargo clippy` returns an error, the pre-commit hook will fail. You should fix all errors before trying to commit again. - If `cargo fmt` reformats some files, the pre-commit hook will also fail. You should review the changes and, if satisfied, try to commit again. + +### Use Mold Linker + +The [Mold linker](https://github.com/rui314/mold) is available when using the Nix Flakes system, and can be enabled by adding the following to `.cargo/config.toml`: + +``` +[target.x86_64-unknown-linux-gnu] +rustflags = ["-C", "link-arg=-fuse-ld=mold"] +``` + +Replace `x86_64-unknown-linux-gnu` with the target triple of your development platform. + +**Note: The use of Mold linker is experimental and should be used for development purposes only.** diff --git a/flake.nix b/flake.nix index a20e1f12..e833d8c8 100644 --- a/flake.nix +++ b/flake.nix @@ -41,9 +41,9 @@ lockFile = ./Cargo.lock; }; passthru.cargoLock = cargoLock; - nativeBuildInputs = [ pkgs.python3 (pkgs.wrapClangMulti pkgs.llvmPackages_14.clang) llvm-tools-irrt pkgs.llvmPackages_14.llvm.out llvm-nac3 ]; - buildInputs = [ pkgs.python3 llvm-nac3 ]; - checkInputs = [ (pkgs.python3.withPackages(ps: [ ps.numpy ps.scipy ])) ]; + nativeBuildInputs = [ pkgs.python3 pkgs.mold-wrapped (pkgs.wrapClangMulti pkgs.llvmPackages_14.clang) llvm-tools-irrt pkgs.llvmPackages_14.llvm.out llvm-nac3 ]; + buildInputs = [ pkgs.python3 pkgs.mold-wrapped llvm-nac3 ]; + checkInputs = [ (pkgs.python3.withPackages(ps: [ ps.numpy ps.scipy ])) pkgs.mold-wrapped ]; checkPhase = '' echo "Checking nac3standalone demos..." @@ -68,6 +68,7 @@ mkdir -p $standalone/bin cp target/x86_64-unknown-linux-gnu/release/nac3standalone $standalone/bin ''; + stdenv = [ pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv ]; } ); python3-mimalloc = pkgs.python3 // rec { @@ -86,7 +87,7 @@ src = self; inherit (nac3artiq) cargoLock; nativeBuildInputs = [ pkgs.python3 packages.x86_64-linux.llvm-tools-irrt llvm-nac3-instrumented ]; - buildInputs = [ pkgs.python3 llvm-nac3-instrumented ]; + buildInputs = [ pkgs.python3 pkgs.mold-wrapped llvm-nac3-instrumented ]; cargoBuildFlags = [ "--package" "nac3artiq" "--features" "init-llvm-profile" ]; doCheck = false; configurePhase = @@ -99,6 +100,7 @@ mkdir -p $TARGET_DIR cp target/x86_64-unknown-linux-gnu/release/libnac3artiq.so $TARGET_DIR/nac3artiq.so ''; + stdenv = [ pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv ]; } ); nac3artiq-profile = pkgs.stdenvNoCC.mkDerivation { @@ -148,7 +150,7 @@ src = self; inherit (nac3artiq) cargoLock; nativeBuildInputs = [ pkgs.python3 packages.x86_64-linux.llvm-tools-irrt llvm-nac3-pgo ]; - buildInputs = [ pkgs.python3 llvm-nac3-pgo ]; + buildInputs = [ pkgs.python3 pkgs.mold-wrapped llvm-nac3-pgo ]; cargoBuildFlags = [ "--package" "nac3artiq" ]; cargoTestFlags = [ "--package" "nac3ast" "--package" "nac3parser" "--package" "nac3core" "--package" "nac3artiq" ]; installPhase = @@ -157,6 +159,7 @@ mkdir -p $TARGET_DIR cp target/x86_64-unknown-linux-gnu/release/libnac3artiq.so $TARGET_DIR/nac3artiq.so ''; + stdenv = [ pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv ]; } ); }; @@ -172,6 +175,7 @@ packages.x86_64-linux.llvm-tools-irrt cargo rustc + mold-wrapped # runtime dependencies lld_14 # for running kernels on the host (packages.x86_64-linux.python3-mimalloc.withPackages(ps: [ ps.numpy ps.scipy ])) @@ -186,6 +190,7 @@ export DEMO_LINALG_STUB=${packages.x86_64-linux.demo-linalg-stub}/lib/liblinalg.a export DEMO_LINALG_STUB32=${packages.x86_64-linux.demo-linalg-stub32}/lib/liblinalg.a ''; + stdenv = [ pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv ]; }; devShells.x86_64-linux.msys2 = pkgs.mkShell { name = "nac3-dev-shell-msys2";