From 4b6ad98de7b8ebd41597bc418ccf49a4e0b440c2 Mon Sep 17 00:00:00 2001 From: Preston Evans Date: Wed, 15 Jun 2022 09:02:37 -0500 Subject: [PATCH 1/4] Modify to cross compile for riscv32im * Update C compiler flags for riscv32im * Detect target. enable flags only for rv32 --- secp256k1-sys/build.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/secp256k1-sys/build.rs b/secp256k1-sys/build.rs index 2d9f8441b..e7a04cb24 100644 --- a/secp256k1-sys/build.rs +++ b/secp256k1-sys/build.rs @@ -37,6 +37,24 @@ fn main() { .define("ENABLE_MODULE_SCHNORRSIG", Some("1")) .define("ENABLE_MODULE_EXTRAKEYS", Some("1")); + if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "riscv32" { + base_config.compiler("/usr/local/opt/llvm/bin/clang") + .flag("--sysroot=/opt/riscv/riscv32-unknown-elf") // https://github.com/riscv-collab/riscv-gnu-toolchain has been built and stored in /opt/riscv + .flag("--gcc-toolchain=/opt/riscv") // https://github.com/riscv-collab/riscv-gnu-toolchain has been built and stored in /opt/riscv + .no_default_flags(true) + .flag("-O3") + .flag("--target=riscv32-unknown-none-elf") + .flag("-mabi=ilp32") + .flag("-mcmodel=medany") + .flag("-Os") + .flag("-fdata-sections") + .flag("-ffunction-sections") + .flag("-dead_strip") + .flag("-flto") + .flag("-march=rv32im") + .target("riscv32-unknown-none-elf"); + } + if cfg!(feature = "lowmemory") { base_config.define("ECMULT_WINDOW_SIZE", Some("4")); // A low-enough value to consume negligible memory base_config.define("ECMULT_GEN_PREC_BITS", Some("2")); @@ -68,4 +86,3 @@ fn main() { base_config.compile("libsecp256k1.a"); } } - From 22feb9696bb3cf2767bfbc868a86116c8256d384 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Tue, 19 Mar 2024 13:55:53 +0100 Subject: [PATCH 2/4] Make it compile with risc0 --- secp256k1-sys/build.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/secp256k1-sys/build.rs b/secp256k1-sys/build.rs index e7a04cb24..060da154c 100644 --- a/secp256k1-sys/build.rs +++ b/secp256k1-sys/build.rs @@ -38,9 +38,9 @@ fn main() { .define("ENABLE_MODULE_EXTRAKEYS", Some("1")); if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "riscv32" { - base_config.compiler("/usr/local/opt/llvm/bin/clang") - .flag("--sysroot=/opt/riscv/riscv32-unknown-elf") // https://github.com/riscv-collab/riscv-gnu-toolchain has been built and stored in /opt/riscv - .flag("--gcc-toolchain=/opt/riscv") // https://github.com/riscv-collab/riscv-gnu-toolchain has been built and stored in /opt/riscv + base_config.compiler("/usr/bin/clang") + .flag("--sysroot=/tmp/riscv32im-osx-arm64/riscv32-unknown-elf") // https://github.com/riscv-collab/riscv-gnu-toolchain has been built and stored in /opt/riscv + .flag("--gcc-toolchain=/tmp/riscv32im-osx-arm64") // https://github.com/riscv-collab/riscv-gnu-toolchain has been built and stored in /opt/riscv .no_default_flags(true) .flag("-O3") .flag("--target=riscv32-unknown-none-elf") From af351efadb995d56f500be031995fb1e2cbc0444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Zwoli=C5=84ski?= Date: Fri, 19 Apr 2024 17:07:53 +0200 Subject: [PATCH 3/4] Allow custom riscv toolchain paths --- secp256k1-sys/build.rs | 44 ++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/secp256k1-sys/build.rs b/secp256k1-sys/build.rs index 060da154c..251a3c98c 100644 --- a/secp256k1-sys/build.rs +++ b/secp256k1-sys/build.rs @@ -38,21 +38,35 @@ fn main() { .define("ENABLE_MODULE_EXTRAKEYS", Some("1")); if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "riscv32" { - base_config.compiler("/usr/bin/clang") - .flag("--sysroot=/tmp/riscv32im-osx-arm64/riscv32-unknown-elf") // https://github.com/riscv-collab/riscv-gnu-toolchain has been built and stored in /opt/riscv - .flag("--gcc-toolchain=/tmp/riscv32im-osx-arm64") // https://github.com/riscv-collab/riscv-gnu-toolchain has been built and stored in /opt/riscv - .no_default_flags(true) - .flag("-O3") - .flag("--target=riscv32-unknown-none-elf") - .flag("-mabi=ilp32") - .flag("-mcmodel=medany") - .flag("-Os") - .flag("-fdata-sections") - .flag("-ffunction-sections") - .flag("-dead_strip") - .flag("-flto") - .flag("-march=rv32im") - .target("riscv32-unknown-none-elf"); + const DEFAULT_RISCV_GNU_TOOLCHAIN: &str = "/opt/riscv"; + println!("cargo:rerun-if-env-changed=RISCV_GNU_TOOLCHAIN"); + + let riscv_gnu_toolchain_path = env::var("RISCV_GNU_TOOLCHAIN").unwrap_or_else(|_| { + println!("cargo:warning=Variable RISCV_GNU_TOOLCHAIN unset. Assuming '{DEFAULT_RISCV_GNU_TOOLCHAIN}'"); + println!("cargo:warning=Please make sure to build riscv toolchain:"); + println!("cargo:warning= git clone https://github.com/riscv-collab/riscv-gnu-toolchain && cd riscv-gnu-toolchain"); + println!("cargo:warning= export RISCV_GNU_TOOLCHAIN={DEFAULT_RISCV_GNU_TOOLCHAIN}"); + println!("cargo:warning= configure --prefix=\"$RISCV_GNU_TOOLCHAIN\" --with-arch=rv32im --with-abi=ilp32"); + println!("cargo:warning= make -j$(nproc)"); + + // if unset, try the default and fail eventually + DEFAULT_RISCV_GNU_TOOLCHAIN.into() + }); + + base_config + .compiler("clang") + .no_default_flags(true) + .flag(&format!("--sysroot={riscv_gnu_toolchain_path}/riscv32-unknown-elf")) + .flag(&format!("--gcc-toolchain={riscv_gnu_toolchain_path}")) + .flag("--target=riscv32-unknown-none-elf") + .flag("-march=rv32im") + .flag("-mabi=ilp32") + .flag("-mcmodel=medany") + .flag("-Os") + .flag("-fdata-sections") + .flag("-ffunction-sections") + .flag("-flto") + .target("riscv32-unknown-none-elf"); } if cfg!(feature = "lowmemory") { From 7a87e4f1d16df50186acff2f45170bfc69d1face Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Zwoli=C5=84ski?= Date: Thu, 25 Apr 2024 15:41:04 +0200 Subject: [PATCH 4/4] Allow custom riscv toolchain paths --- secp256k1-sys/build.rs | 44 ++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/secp256k1-sys/build.rs b/secp256k1-sys/build.rs index 060da154c..251a3c98c 100644 --- a/secp256k1-sys/build.rs +++ b/secp256k1-sys/build.rs @@ -38,21 +38,35 @@ fn main() { .define("ENABLE_MODULE_EXTRAKEYS", Some("1")); if env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "riscv32" { - base_config.compiler("/usr/bin/clang") - .flag("--sysroot=/tmp/riscv32im-osx-arm64/riscv32-unknown-elf") // https://github.com/riscv-collab/riscv-gnu-toolchain has been built and stored in /opt/riscv - .flag("--gcc-toolchain=/tmp/riscv32im-osx-arm64") // https://github.com/riscv-collab/riscv-gnu-toolchain has been built and stored in /opt/riscv - .no_default_flags(true) - .flag("-O3") - .flag("--target=riscv32-unknown-none-elf") - .flag("-mabi=ilp32") - .flag("-mcmodel=medany") - .flag("-Os") - .flag("-fdata-sections") - .flag("-ffunction-sections") - .flag("-dead_strip") - .flag("-flto") - .flag("-march=rv32im") - .target("riscv32-unknown-none-elf"); + const DEFAULT_RISCV_GNU_TOOLCHAIN: &str = "/opt/riscv"; + println!("cargo:rerun-if-env-changed=RISCV_GNU_TOOLCHAIN"); + + let riscv_gnu_toolchain_path = env::var("RISCV_GNU_TOOLCHAIN").unwrap_or_else(|_| { + println!("cargo:warning=Variable RISCV_GNU_TOOLCHAIN unset. Assuming '{DEFAULT_RISCV_GNU_TOOLCHAIN}'"); + println!("cargo:warning=Please make sure to build riscv toolchain:"); + println!("cargo:warning= git clone https://github.com/riscv-collab/riscv-gnu-toolchain && cd riscv-gnu-toolchain"); + println!("cargo:warning= export RISCV_GNU_TOOLCHAIN={DEFAULT_RISCV_GNU_TOOLCHAIN}"); + println!("cargo:warning= configure --prefix=\"$RISCV_GNU_TOOLCHAIN\" --with-arch=rv32im --with-abi=ilp32"); + println!("cargo:warning= make -j$(nproc)"); + + // if unset, try the default and fail eventually + DEFAULT_RISCV_GNU_TOOLCHAIN.into() + }); + + base_config + .compiler("clang") + .no_default_flags(true) + .flag(&format!("--sysroot={riscv_gnu_toolchain_path}/riscv32-unknown-elf")) + .flag(&format!("--gcc-toolchain={riscv_gnu_toolchain_path}")) + .flag("--target=riscv32-unknown-none-elf") + .flag("-march=rv32im") + .flag("-mabi=ilp32") + .flag("-mcmodel=medany") + .flag("-Os") + .flag("-fdata-sections") + .flag("-ffunction-sections") + .flag("-flto") + .target("riscv32-unknown-none-elf"); } if cfg!(feature = "lowmemory") {