From 67fb441dc31040fc1093c28c64d58b00f3344566 Mon Sep 17 00:00:00 2001 From: Avimitin Date: Mon, 22 Jul 2024 17:20:43 +0800 Subject: [PATCH] [nix] add static build for libspike and spike_interfaces --- difftest/default.nix | 3 +++ difftest/spike_interfaces/CMakeLists.txt | 2 +- difftest/spike_rs/build.rs | 18 ++++++++++++++++++ nix/overlay.nix | 1 - nix/pkgs/libspike.nix | 3 ++- 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 difftest/spike_rs/build.rs diff --git a/difftest/default.nix b/difftest/default.nix index 9e2c54769..69ceda196 100644 --- a/difftest/default.nix +++ b/difftest/default.nix @@ -1,4 +1,5 @@ { lib +, libspike , callPackage , elaborateConfig @@ -45,6 +46,8 @@ let env = { VERILATED_INC_DIR = "${verilated}/include"; VERILATED_LIB_DIR = "${verilated}/lib"; + SPIKE_LIB_DIR = "${libspike}/lib"; + SPIKE_INTERFACES_LIB_DIR = "${spike_interfaces}/lib"; DESIGN_VLEN = elaborateConfig.parameter.vLen; DESIGN_DLEN = elaborateConfig.parameter.dLen; }; diff --git a/difftest/spike_interfaces/CMakeLists.txt b/difftest/spike_interfaces/CMakeLists.txt index 6da21334c..fe5272891 100644 --- a/difftest/spike_interfaces/CMakeLists.txt +++ b/difftest/spike_interfaces/CMakeLists.txt @@ -4,7 +4,7 @@ set(CMAKE_CXX_STANDARD 17) find_package(libspike REQUIRED) -add_library(${CMAKE_PROJECT_NAME} SHARED spike_interfaces.cc) +add_library(${CMAKE_PROJECT_NAME} STATIC spike_interfaces.cc) target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC libspike) diff --git a/difftest/spike_rs/build.rs b/difftest/spike_rs/build.rs new file mode 100644 index 000000000..9399fdaf0 --- /dev/null +++ b/difftest/spike_rs/build.rs @@ -0,0 +1,18 @@ +use std::env; + +fn main() { + println!("cargo::rustc-link-search=native={}", env::var("SPIKE_LIB_DIR").expect("SPIKE_LIB_DIR should be set")); + println!("cargo::rustc-link-lib=static=riscv"); + println!("cargo::rustc-link-lib=static=softfloat"); + println!("cargo::rustc-link-lib=static=disasm"); + println!("cargo::rustc-link-lib=static=fesvr"); + println!("cargo::rustc-link-lib=static=fdt"); + + println!("cargo::rustc-link-search=native={}", env::var("SPIKE_INTERFACES_LIB_DIR").expect("SPIKE_INTERFACES_LIB_DIR should be set")); + println!("cargo::rustc-link-lib=static=spike_interfaces"); + + println!("cargo::rerun-if-env-changed=SPIKE_LIB_DIR"); + println!("cargo::rerun-if-env-changed=SPIKE_INTERFACES_LIB_DIR"); + + println!("cargo::rustc-link-lib=stdc++"); +} diff --git a/nix/overlay.nix b/nix/overlay.nix index d0ca888b5..4dae99039 100644 --- a/nix/overlay.nix +++ b/nix/overlay.nix @@ -18,7 +18,6 @@ rec { espresso = final.callPackage ./pkgs/espresso.nix { }; dramsim3 = final.callPackage ./pkgs/dramsim3.nix { }; libspike = final.callPackage ./pkgs/libspike.nix { }; - libspike_interfaces = final.callPackage ../difftest/libspike_interfaces { }; buddy-mlir = final.callPackage ./pkgs/buddy-mlir.nix { }; fetchMillDeps = final.callPackage ./pkgs/mill-builder.nix { }; circt-full = final.callPackage ./pkgs/circt-full.nix { }; diff --git a/nix/pkgs/libspike.nix b/nix/pkgs/libspike.nix index dc3a28fdc..1ecb1c317 100644 --- a/nix/pkgs/libspike.nix +++ b/nix/pkgs/libspike.nix @@ -7,7 +7,7 @@ stdenv.mkDerivation { env.cmakeConfig = '' add_library(libspike STATIC IMPORTED GLOBAL) set_target_properties(libspike PROPERTIES - IMPORTED_LOCATION "${placeholder "out"}/lib/libriscv.so") + IMPORTED_LOCATION "${placeholder "out"}/lib/libriscv.a") target_include_directories(libspike AFTER INTERFACE "${placeholder "out"}/include" "${placeholder "out"}/include/riscv" @@ -37,6 +37,7 @@ stdenv.mkDerivation { cp softfloat/*.h $out/include/softfloat cp config.h $out/include cp *.so $out/lib + cp *.a $out/lib echo "$cmakeConfig" > $out/lib/cmake/libspike/libspike-config.cmake runHook postInstall '';