From 86d05afdae1d7f788a750e095d386e303fda3c49 Mon Sep 17 00:00:00 2001 From: Yan Yue <1131531947@qq.com> Date: Fri, 23 Aug 2024 15:57:13 +0800 Subject: [PATCH 1/3] nix-support: we can build with nix environment --- .envrc | 1 + .gitignore | 3 ++- README.md | 27 +++++++++++++++++++ shell.nix | 60 +++++++++++++++++++++++++++++++++++++++++++ shell_no_jemalloc.nix | 41 +++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 .envrc create mode 100644 shell.nix create mode 100644 shell_no_jemalloc.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..1d953f4 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use nix diff --git a/.gitignore b/.gitignore index 41259a3..091c6a0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ run/ bin cpu2006_build/ Makefile.spec - +.cache/ +*/logs/* \ No newline at end of file diff --git a/README.md b/README.md index 337cc1e..db13b8c 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,33 @@ make ARCH=riscv64 \ build-all -j 29 ``` +# Use Nix to build +this repo can be built with nix, you do not need to warry about the dependencies. + +```shell +// install nix +sh <(curl -L https://nixos.org/nix/install) --no-daemon + +// prepare spec source +export SPEC=/spec2006_path +pushd $SPEC && source shrc && popd +make copy-all-src + +// enter nix shell +nix-shell + +// build +make build-all -j 29 +``` +shell.nix is prepared to build with jemalloc, if you do not want to use jemalloc, +you can use shell_no_jemalloc.nix + +```shell +nix-shell shell_no_jemalloc.nix +// build +make build-all -j 29 +``` + # Note for GCC >= 14 The old version of xerces-c in 483.xalancbmk [contains a bug](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111544) that will cause compile errors in GCC 14 and later. You may need to apply the following patch to SPEC CPU 2006 source code to get this fixed: diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..758be24 --- /dev/null +++ b/shell.nix @@ -0,0 +1,60 @@ +let + nixpkgs = fetchTarball { + url = "https://github.com/NixOS/nixpkgs/tarball/release-23.11"; + sha256 = "sha256:1f5d2g1p6nfwycpmrnnmc2xmcszp804adp16knjvdkj8nz36y1fg"; + }; + pkgs = import nixpkgs {}; + riscv64Pkgs = pkgs.pkgsCross.riscv64; + + # Custom jemalloc with static libraries + customJemalloc = riscv64Pkgs.jemalloc.overrideAttrs (oldAttrs: { + configureFlags = (oldAttrs.configureFlags or []) ++ [ + "--enable-static" + "--disable-shared" + ]; + preBuild = '' + # Add weak attribute to C++ operators, 作用和jemalloc_cpp.patch效果一样 + sed -i 's/void \*operator new(size_t)/void *operator new(size_t) __attribute__((weak))/g' src/jemalloc_cpp.cpp + sed -i 's/void operator delete(void \*)/void operator delete(void *) __attribute__((weak))/g' src/jemalloc_cpp.cpp + ''; + # Ensure static libraries are installed + postInstall = '' + ${oldAttrs.postInstall or ""} + cp -v lib/libjemalloc.a $out/lib/ + ''; + }); +in + +pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + pkg-config + file + riscv64Pkgs.buildPackages.gcc + riscv64Pkgs.buildPackages.binutils + ]; + + buildInputs = with riscv64Pkgs; [ + glibc + glibc.static + customJemalloc + ]; + + shellHook = '' + echo "Cross-compilation environment for RISC-V 64-bit (GNU) Linux with jemalloc ready" + echo "Use 'qemu-riscv64' to run the compiled program" + + export SPEC_LITE=$PWD + export ARCH=riscv64 + export CROSS_COMPILE=riscv64-unknown-linux-gnu- + export OPTIMIZE="-O3 -flto" + export SUBPROCESS_NUM=5 + + export CC=${riscv64Pkgs.stdenv.cc}/bin/riscv64-unknown-linux-gnu-gcc + export CXX=${riscv64Pkgs.stdenv.cc}/bin/riscv64-unknown-linux-gnu-g++ + export LD=${riscv64Pkgs.stdenv.cc}/bin/riscv64-unknown-linux-gnu-ld + + export CFLAGS="$CFLAGS -static -Wno-format-security -I${customJemalloc}/include " + export CXXFLAGS="$CXXFLAGS -static -Wno-format-security -I${customJemalloc}/include" + export LDFLAGS="$LDFLAGS -static -ljemalloc -L${customJemalloc}/lib" + ''; +} \ No newline at end of file diff --git a/shell_no_jemalloc.nix b/shell_no_jemalloc.nix new file mode 100644 index 0000000..9588962 --- /dev/null +++ b/shell_no_jemalloc.nix @@ -0,0 +1,41 @@ +let + nixpkgs = fetchTarball { + url = "https://github.com/NixOS/nixpkgs/tarball/release-23.11"; + sha256 = "sha256:1f5d2g1p6nfwycpmrnnmc2xmcszp804adp16knjvdkj8nz36y1fg"; + }; + pkgs = import nixpkgs {}; + riscv64Pkgs = pkgs.pkgsCross.riscv64; +in + +pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + pkg-config + file + riscv64Pkgs.buildPackages.gcc + riscv64Pkgs.buildPackages.binutils + ]; + + buildInputs = with riscv64Pkgs; [ + glibc + glibc.static + ]; + + shellHook = '' + echo "Cross-compilation environment for RISC-V 64-bit (GNU) Linux ready" + echo "Use 'qemu-riscv64' to run the compiled program" + + export SPEC_LITE=$PWD + export ARCH=riscv64 + export CROSS_COMPILE=riscv64-unknown-linux-gnu- + export OPTIMIZE="-O3 -flto" + export SUBPROCESS_NUM=5 + + export CC=${riscv64Pkgs.stdenv.cc}/bin/riscv64-unknown-linux-gnu-gcc + export CXX=${riscv64Pkgs.stdenv.cc}/bin/riscv64-unknown-linux-gnu-g++ + export LD=${riscv64Pkgs.stdenv.cc}/bin/riscv64-unknown-linux-gnu-ld + + export CFLAGS="$CFLAGS -static -Wno-format-security" + export CXXFLAGS="$CXXFLAGS -static -Wno-format-security" + export LDFLAGS="$LDFLAGS -static" + ''; +} \ No newline at end of file From 3347ac3492f59ea8bd6ed073f40d2af173fcb6b8 Mon Sep 17 00:00:00 2001 From: xieby1 Date: Mon, 26 Aug 2024 13:07:59 +0800 Subject: [PATCH 2/3] nix-support: add gfortran --- shell.nix | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/shell.nix b/shell.nix index 758be24..803389b 100644 --- a/shell.nix +++ b/shell.nix @@ -23,6 +23,21 @@ let cp -v lib/libjemalloc.a $out/lib/ ''; }); + riscv64Fortran = riscv64Pkgs.wrapCCWith { + cc = riscv64Pkgs.stdenv.cc.cc.override { + name = "gfortran"; + langFortran = true; + langCC = false; + langC = false; + profiledCompiler = false; + }; + # fixup wrapped prefix, which only appear if hostPlatform!=targetPlatform + # for more details see /pkgs/build-support/cc-wrapper/default.nix + stdenvNoCC = riscv64Pkgs.stdenvNoCC.override { + hostPlatform = pkgs.stdenv.hostPlatform; + }; + }; + in pkgs.mkShell { @@ -31,6 +46,7 @@ pkgs.mkShell { file riscv64Pkgs.buildPackages.gcc riscv64Pkgs.buildPackages.binutils + riscv64Fortran ]; buildInputs = with riscv64Pkgs; [ From eb2720459707649915640740592605113d615321 Mon Sep 17 00:00:00 2001 From: xieby1 Date: Mon, 26 Aug 2024 14:58:45 +0800 Subject: [PATCH 3/3] nix-support: patch $SPEC/bin make executables able to run in NixOS --- shell.nix | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/shell.nix b/shell.nix index 803389b..f496402 100644 --- a/shell.nix +++ b/shell.nix @@ -37,7 +37,20 @@ let hostPlatform = pkgs.stdenv.hostPlatform; }; }; + patchSPEC = let + rpath = pkgs.lib.makeLibraryPath [ + pkgs.libxcrypt-legacy + ]; + in pkgs.writeScriptBin "patchSPEC" '' + echo patching ''${SPEC:?env SPEC is not specified, fixupELFs aborted!}/bin + + for file in $(find $SPEC/bin -type f \( -perm /0111 -o -name \*.so\* \) ); do + patchelf --set-interpreter "$(cat ${pkgs.stdenv.cc}/nix-support/dynamic-linker)" "$file" || true + patchelf --set-rpath ${rpath} $file || true + done + echo patchSPEC: done! + ''; in pkgs.mkShell { @@ -47,6 +60,7 @@ pkgs.mkShell { riscv64Pkgs.buildPackages.gcc riscv64Pkgs.buildPackages.binutils riscv64Fortran + patchSPEC ]; buildInputs = with riscv64Pkgs; [