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..f496402 --- /dev/null +++ b/shell.nix @@ -0,0 +1,90 @@ +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/ + ''; + }); + 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; + }; + }; + 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 { + nativeBuildInputs = with pkgs; [ + pkg-config + file + riscv64Pkgs.buildPackages.gcc + riscv64Pkgs.buildPackages.binutils + riscv64Fortran + patchSPEC + ]; + + 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