Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added no_avx512 feature flag #19

Merged
merged 18 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tonlib-sys"
version = "2024.3.5"
version = "2024.3.6"
edition = "2021"
description = "Rust bindings for tonlibjson library"
license = "MIT"
Expand All @@ -13,6 +13,7 @@ include = [

[features]
default = ["cmake"]
no_avx512 = []
shared-tonlib = []

[dependencies]
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ Then, in your Rust code, you can import the library with:
use tonlib_sys;
```

## Cross-compilation
In order to cross-compile for specific cpu microachitecture set environment variable `TARGET_CPU_MARCH` to the required. Supported values are listen in https://gcc.gnu.org/onlinedocs/gcc/x86-Options.html


## Contributing

Expand Down
51 changes: 32 additions & 19 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::process::Command;
use std::thread::available_parallelism;
use std::{env, fs};

use cmake::Config;

fn main() {
build();
}
Expand All @@ -12,6 +14,8 @@ const TON_MONOREPO_DIR: &str = "./ton";

#[cfg(not(feature = "shared-tonlib"))]
fn build() {
#[cfg(feature = "no_avx512")]
disable_avx512_for_rustc();
env::set_var("TON_MONOREPO_REVISION", TON_MONOREPO_REVISION);
println!("cargo:rerun-if-env-changed=TON_MONOREPO_REVISION");
println!("cargo:rerun-if-changed=build.rs");
Expand Down Expand Up @@ -126,14 +130,12 @@ fn build() {
}

env::set_var("LD_LIBRARY_PATH", "lib/x86_64-linux-gnu");

let march = env::var("TARGET_CPU_MARCH").unwrap_or_default();
build_tonlibjson(march.as_str());
build_emulator(march.as_str());
build_tonlibjson();
build_emulator();
}

fn build_tonlibjson(march: &str) {
let mut cfg = cmake::Config::new(TON_MONOREPO_DIR);
fn build_tonlibjson() {
let mut cfg = Config::new(TON_MONOREPO_DIR);
let mut dst = cfg
.configure_arg("-DTON_ONLY_TONLIB=true")
.configure_arg("-DBUILD_SHARED_LIBS=false")
Expand All @@ -149,18 +151,16 @@ fn build_tonlibjson(march: &str) {
.always_configure(true)
.very_verbose(false);

#[cfg(feature = "no_avx512")]
disable_avx512_for_clang(dst);

if cfg!(target_os = "macos") {
let brew_prefix_output = Command::new("brew").arg("--prefix").output().unwrap();
let brew_prefix = String::from_utf8(brew_prefix_output.stdout).unwrap();
let lib_arg = format!("-DCMAKE_EXE_LINKER_FLAGS=-L{}/lib", brew_prefix.trim());
dst = dst.configure_arg(lib_arg)
}

if !march.is_empty() {
dst = dst
.configure_arg(format!("-DCMAKE_C_FLAGS=-march={}", march))
.configure_arg(format!("-DCMAKE_CXX_FLAGS=-march={}", march));
}
let dst = dst.build();

println!("cargo:rustc-link-search=native=/usr/lib/x86_64-linux-gnu");
Expand Down Expand Up @@ -270,9 +270,9 @@ fn build_tonlibjson(march: &str) {
println!("cargo:rustc-link-lib=static=tonlibjson_private");
}

fn build_emulator(march: &str) {
let mut cfg = cmake::Config::new(TON_MONOREPO_DIR);
let mut dst = cfg
fn build_emulator() {
let mut cfg = Config::new(TON_MONOREPO_DIR);
let dst = cfg
.configure_arg("-DTON_ONLY_TONLIB=true")
.configure_arg("-Wno-dev")
.configure_arg("-Wno-unused")
Expand All @@ -287,11 +287,9 @@ fn build_emulator(march: &str) {
.always_configure(true)
.very_verbose(false);

if !march.is_empty() {
dst = dst
.configure_arg(format!("-DCMAKE_C_FLAGS=-march={}", march))
.configure_arg(format!("-DCMAKE_CXX_FLAGS=-march={}", march));
}
#[cfg(feature = "no_avx512")]
disable_avx512_for_clang(dst);

let dst = dst.build();

println!("cargo:rustc-link-lib=dylib=sodium");
Expand All @@ -307,3 +305,18 @@ fn build_emulator(march: &str) {
fn build() {
println!("cargo:rustc-link-lib=tonlibjson.0.5");
}

#[cfg(feature = "no_avx512")]
fn disable_avx512_for_clang(dst: &mut Config) -> &mut Config {
dst
.define("CMAKE_C_FLAGS", "-mno-avx512f -mno-avx512dq -mno-avx512cd -mno-avx512bw -mno-avx512vl -mno-avx512ifma -mno-avx512vbmi -mno-vpclmulqdq")
.define("CMAKE_CXX_FLAGS", "-mno-avx512f -mno-avx512dq -mno-avx512cd -mno-avx512bw -mno-avx512vl -mno-avx512ifma -mno-avx512vbmi -mno-vpclmulqdq")
.define("CMAKE_C_FLAGS_RELEASE", "-mno-avx512f -mno-avx512dq -mno-avx512cd -mno-avx512bw -mno-avx512vl -mno-avx512ifma -mno-avx512vbmi -mno-vpclmulqdq")
.define("CMAKE_CXX_FLAGS_RELEASE", "-mno-avx512f -mno-avx512dq -mno-avx512cd -mno-avx512bw -mno-avx512vl -mno-avx512ifma -mno-avx512vbmi -mno-vpclmulqdq")
.asmflag("-mno-avx512f -mno-avx512dq -mno-avx512cd -mno-avx512bw -mno-avx512vl -mno-avx512ifma -mno-avx512vbmi -mno-vpclmulqdq")
}

#[cfg(feature = "no_avx512")]
fn disable_avx512_for_rustc() {
println!("cargo:rustc-env=RUSTFLAGS=-C target-feature=-avx512f,-avx512dq,-avx512cd,-avx512bw,-avx512vl,-avx512ifma,-avx512vbmi,-vpclmulqdq");
}
Loading