Skip to content

Commit

Permalink
[Rust] Remove blst to avoid double link issues, expose blst features (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
divagant-martian authored Apr 10, 2023
1 parent 8f43b2e commit fd24cf8
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 45 deletions.
40 changes: 0 additions & 40 deletions bindings/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion bindings/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,30 @@ links = "ckzg"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]

# BLST Compilation:
# By default, compile with ADX extension if the host supports it. Binary can be
# executed on systems similar to the host.
default = ["mainnet-spec"]
mainnet-spec = []
minimal-spec = []
# BLST Compilation:
# Compile in portable mode, without ISA extensions.
# Binary can be executed on all systems.
portable = []
# BLST Compilation:
# Enable ADX even if the host CPU doesn't support it.
# Binary can be executed on Broadwell+ and Ryzen+ systems.
force-adx = []
# BLST Compilation:
# Suppress multi-threading.
# Engaged on wasm32 target architecture automatically.
no-threads = []

[dependencies]
hex = "0.4.2"
libc = "0.2"
serde = { version = "1.0", features = ["derive"] }
blst = { path = "../../blst/bindings/rust" }

[dev-dependencies]
criterion = "0.4"
Expand All @@ -27,6 +42,9 @@ serde_yaml = "0.9.17"
bindgen = { git = "https://github.com/rust-lang/rust-bindgen" , rev = "0de11f0a521611ac8738b7b01d19dddaf3899e66" }
cc = "1.0"

[target.'cfg(target_env = "msvc")'.build-dependencies]
glob = "0.3"

[[bench]]
name = "kzg_benches"
harness = false
123 changes: 119 additions & 4 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,122 @@
use std::env;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

const MAINNET_FIELD_ELEMENTS_PER_BLOB: usize = 4096;
const MINIMAL_FIELD_ELEMENTS_PER_BLOB: usize = 4;

/// Compiles blst.
//
// NOTE: This code is taken from https://github.com/supranational/blst `build.rs` `main`. The crate
// is not used as a depedency to avoid double link issues on dependants.
fn compile_blst(blst_base_dir: PathBuf) {
// account for cross-compilation [by examining environment variables]
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();

if target_os.ne("none") && !env::var("BLST_TEST_NO_STD").is_ok() {
println!("cargo:rustc-cfg=feature=\"std\"");
if target_arch.eq("wasm32") {
println!("cargo:rustc-cfg=feature=\"no-threads\"");
}
}
println!("cargo:rerun-if-env-changed=BLST_TEST_NO_STD");

println!("Using blst source directory {}", blst_base_dir.display());

// Set CC environment variable to choose alternative C compiler.
// Optimization level depends on whether or not --release is passed
// or implied.

#[cfg(target_env = "msvc")]
if env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap().eq("32") && !env::var("CC").is_ok() {
match std::process::Command::new("clang-cl")
.arg("--version")
.output()
{
Ok(out) => {
if String::from_utf8(out.stdout)
.unwrap_or("unintelligible".to_string())
.contains("Target: i686-")
{
env::set_var("CC", "clang-cl");
}
}
Err(_) => { /* no clang-cl in sight, just ignore the error */ }
}
}

let mut cc = cc::Build::new();

let c_src_dir = blst_base_dir.join("src");
println!("cargo:rerun-if-changed={}", c_src_dir.display());
let mut file_vec = vec![c_src_dir.join("server.c")];

if target_arch.eq("x86_64") || target_arch.eq("aarch64") {
let asm_dir = blst_base_dir.join("build");
println!("cargo:rerun-if-changed={}", asm_dir.display());
blst_assembly(&mut file_vec, &asm_dir, &target_arch);
} else {
cc.define("__BLST_NO_ASM__", None);
}
match (cfg!(feature = "portable"), cfg!(feature = "force-adx")) {
(true, false) => {
println!("Compiling in portable mode without ISA extensions");
cc.define("__BLST_PORTABLE__", None);
}
(false, true) => {
if target_arch.eq("x86_64") {
println!("Enabling ADX support via `force-adx` feature");
cc.define("__ADX__", None);
} else {
println!("`force-adx` is ignored for non-x86_64 targets");
}
}
(false, false) =>
{
#[cfg(target_arch = "x86_64")]
if target_arch.eq("x86_64") && std::is_x86_feature_detected!("adx") {
println!("Enabling ADX because it was detected on the host");
cc.define("__ADX__", None);
}
}
(true, true) => panic!("Cannot compile with both `portable` and `force-adx` features"),
}
if env::var("CARGO_CFG_TARGET_ENV").unwrap().eq("msvc") {
cc.flag("-Zl");
}
cc.flag_if_supported("-mno-avx") // avoid costly transitions
.flag_if_supported("-fno-builtin")
.flag_if_supported("-Wno-unused-function")
.flag_if_supported("-Wno-unused-command-line-argument");
if target_arch.eq("wasm32") {
cc.flag_if_supported("-ffreestanding");
}
if !cfg!(debug_assertions) {
cc.opt_level(2);
}
cc.files(&file_vec).compile("blst");
}

/// Adds assembly files for blst compilation.
fn blst_assembly(file_vec: &mut Vec<PathBuf>, base_dir: &Path, _arch: &String) {
#[cfg(target_env = "msvc")]
if env::var("CARGO_CFG_TARGET_ENV").unwrap().eq("msvc") {
let sfx = match _arch.as_str() {
"x86_64" => "x86_64",
"aarch64" => "armv8",
_ => "unknown",
};
let files = glob::glob(&format!("{}/win64/*-{}.asm", base_dir.display(), sfx))
.expect("unable to collect assembly files");
for file in files {
file_vec.push(file.unwrap());
}
return;
}

file_vec.push(base_dir.join("assembly.S"));
}

fn main() {
let cargo_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let root_dir = cargo_dir
Expand All @@ -20,9 +133,11 @@ fn main() {

eprintln!("Using FIELD_ELEMENTS_PER_BLOB={}", field_elements_per_blob);

// Obtain the header files exposed by blst-bindings' crate.
let blst_headers_dir =
std::env::var_os("DEP_BLST_BINDINGS").expect("BLST exposes header files for bindings");
let blst_base_dir = root_dir.join("blst");
compile_blst(blst_base_dir.clone());

// Obtain the header files of blst
let blst_headers_dir = blst_base_dir.join("bindings");

let c_src_dir = root_dir.join("src");

Expand Down

0 comments on commit fd24cf8

Please sign in to comment.