From 8d3fa96235387bb4bdf7105aa702a7d47cdb7be0 Mon Sep 17 00:00:00 2001 From: Dmitrii Baranov Date: Fri, 19 Apr 2024 09:51:39 +0000 Subject: [PATCH 01/12] Ni: crosscompilation --- .gitlab-ci.yml | 11 ++++++----- README.md | 4 ++++ build.rs | 41 ++++++++++++++++++++++++++++++----------- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a8e52e9..3ff35e3 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,7 +16,8 @@ variables: FF_USE_FASTZIP: 1 CACHE_COMPRESSION_LEVEL: "fastest" CARGO_HOME: "${CI_PROJECT_DIR}/.cargo" - RUSTFLAGS: "-D warnings" + RUSTFLAGS: "-D warnings -C target-cpu=znver2" + TARGET_CPU_MARCH: "znver2" cache: key: shared-cache @@ -33,7 +34,7 @@ test-branch: - g++ -v - rm -rf ton - cargo fmt --check - - cargo rustc -- -D warnings + - cargo rustc -- $RUSTFLAGS - cargo test --lib tags: - ston @@ -51,7 +52,7 @@ test-mr: - g++ -v - rm -rf ton - cargo fmt --check - - cargo rustc -- -D warnings + - cargo rustc -- $RUSTFLAGS - cargo test --lib -- --test-threads=1 rules: - if: $CI_PIPELINE_SOURCE == 'merge_request_event' @@ -62,7 +63,7 @@ test-master: stage: test script: - cargo fmt --check - - cargo rustc -- -D warnings + - cargo rustc -- $RUSTFLAGS - cargo test --lib -- --test-threads=1 rules: - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH @@ -81,7 +82,7 @@ release-master: script: - git checkout $CI_BUILD_REF_NAME - cargo fmt --check - - cargo rustc -- -D warnings + - cargo rustc -- $RUSTFLAGS - cargo test --lib -- --test-threads=1 - cargo release release --execute --no-publish --no-confirm - export VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0]["version"]') diff --git a/README.md b/README.md index 92e5275..ebb9252 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ 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 If you want to contribute to this library, please feel free to open a pull request on GitHub. diff --git a/build.rs b/build.rs index 0dacdf2..db9f4d2 100644 --- a/build.rs +++ b/build.rs @@ -1,10 +1,12 @@ +use std::env; + fn main() { build(); } #[cfg(not(feature = "shared-tonlib"))] fn build() { - use std::{env, process::Command}; + use std::process::Command; if !std::path::Path::new("ton/tonlib").is_dir() { let clone_status = std::process::Command::new("git") @@ -114,12 +116,14 @@ fn build() { env::set_var("LD_LIBRARY_PATH", "lib/x86_64-linux-gnu"); - build_tonlibjson(); - build_emulator(); + let march = env::var("TARGET_CPU_MARCH").unwrap_or_default(); + build_tonlibjson(march.as_str()); + build_emulator(march.as_str()); } -fn build_tonlibjson() { - let dst = cmake::Config::new("ton") +fn build_tonlibjson(march: &str) { + let mut cfg = cmake::Config::new("ton"); + let mut dst = cfg .configure_arg("-DTON_ONLY_TONLIB=true") .configure_arg("-DBUILD_SHARED_LIBS=false") .configure_arg("-DCMAKE_EXE_LINKER_FLAGS=-L/opt/homebrew/lib") @@ -127,11 +131,18 @@ fn build_tonlibjson() { .define("BUILD_SHARED_LIBS", "OFF") .define("PORTABLE", "1") .define("CMAKE_BUILD_TYPE", "Release") + //.args(&rust_cmake_args) .configure_arg("-Wno-dev") .build_target("tonlibjson") .always_configure(true) - .very_verbose(false) - .build(); + .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)); + } + let dst = dst.build(); println!("cargo:rustc-link-search=native=/usr/lib/x86_64-linux-gnu"); println!("cargo:rustc-link-search=native=/usr/include"); @@ -242,8 +253,9 @@ fn build_tonlibjson() { println!("cargo:rustc-link-lib=static=tonlibjson_private"); } -fn build_emulator() { - let dst = cmake::Config::new("ton") +fn build_emulator(march: &str) { + let mut cfg = cmake::Config::new("ton"); + let mut dst = cfg .configure_arg("-DTON_ONLY_TONLIB=true") .configure_arg("-Wno-dev") .configure_arg("-Wno-unused") @@ -252,9 +264,16 @@ fn build_emulator() { .define("PORTABLE", "1") .define("CMAKE_BUILD_TYPE", "Release") .build_target("emulator") + //.configure_arg() .always_configure(true) - .very_verbose(false) - .build(); + .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)); + } + let dst = dst.build(); println!("cargo:rustc-link-lib=dylib=sodium"); println!("cargo:rustc-link-lib=dylib=secp256k1"); From 43d83e2ad2c8339da3d5472c65cb429b3bc62878 Mon Sep 17 00:00:00 2001 From: Dmitrii Korchagin Date: Tue, 23 Apr 2024 00:47:33 +0200 Subject: [PATCH 02/12] NI: rebuild tonlyb-sys only if ton revision or sources are changed --- build.rs | 86 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/build.rs b/build.rs index db9f4d2..940c0fa 100644 --- a/build.rs +++ b/build.rs @@ -1,54 +1,64 @@ -use std::env; +use std::path::Path; +use std::{env, fs}; fn main() { build(); } +const TONLIB_REVISION: &str = "25f61dff161b9c76dce0fc62dc51da911a208b68"; +const TON_DIR: &str = "./ton"; + #[cfg(not(feature = "shared-tonlib"))] fn build() { + env::set_var("TONLIB_REVISION", TONLIB_REVISION); + println!("cargo:rerun-if-env-changed=TONLIB_REVISION"); + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=src"); + use std::process::Command; - if !std::path::Path::new("ton/tonlib").is_dir() { - let clone_status = std::process::Command::new("git") - .args([ - "clone", - "--recurse-submodules", - "https://github.com/ton-blockchain/ton", - "--branch", - "testnet", - ]) + // cleanup tonlib after previous build + if Path::new(TON_DIR).exists() { + let _ = fs::remove_dir_all(TON_DIR); + } + + let clone_status = Command::new("git") + .args([ + "clone", + "--recurse-submodules", + "https://github.com/ton-blockchain/ton", + "--branch", + "testnet", + TON_DIR, + ]) + .status() + .unwrap(); + if clone_status.success() { + let checkout_status = Command::new("git") + .current_dir(TON_DIR) + .args(["checkout", TONLIB_REVISION]) .status() .unwrap(); - if clone_status.success() { - let checkout_status = std::process::Command::new("git") - .current_dir("ton") - .args(["checkout", "25f61dff161b9c76dce0fc62dc51da911a208b68"]) - .status() - .unwrap(); - - if checkout_status.success() { - println!("Cloned and checked out specific commit successfully!"); - } else { - println!("Failed to checkout specific commit!"); - } + + if checkout_status.success() { + println!("Cloned and checked out specific commit successfully!"); } else { - println!("Failed to clone repository!"); - } - if !clone_status.success() { - panic!("Git clone TON repo fail"); - } - let update_submodules_status = std::process::Command::new("git") - .current_dir("./ton") - .args(["submodule", "update", "--init", "--recursive"]) - .status() - .unwrap(); - if !update_submodules_status.success() { - panic!("Git update submodules for TON repo fail"); + println!("Failed to checkout specific commit!"); } + } else { + println!("Failed to clone repository!"); + } + if !clone_status.success() { + panic!("Git clone TON repo fail"); + } + let update_submodules_status = Command::new("git") + .current_dir("./ton") + .args(["submodule", "update", "--init", "--recursive"]) + .status() + .unwrap(); + if !update_submodules_status.success() { + panic!("Git update submodules for TON repo fail"); } - - println!("cargo:rerun-if-changed=ton/CMakeLists.txt"); - println!("cargo:rerun-if-changed=build.rs"); if cfg!(target_os = "macos") { env::set_var("NUM_JOBS", num_cpus::get().to_string()); @@ -240,14 +250,12 @@ fn build_tonlibjson(march: &str) { "cargo:rustc-link-search=native={}/build/emulator", dst.display() ); - println!("cargo:rerun-if-changed={}/build/emulator", dst.display()); println!("cargo:rustc-link-lib=static=emulator_static"); println!( "cargo:rustc-link-search=native={}/build/tonlib", dst.display() ); - println!("cargo:rerun-if-changed={}/build/tonlib", dst.display()); println!("cargo:rustc-link-lib=static=tonlibjson"); println!("cargo:rustc-link-lib=static=tonlib"); println!("cargo:rustc-link-lib=static=tonlibjson_private"); From d42e18a40b65cc96156a63d001e2c6ba61172c21 Mon Sep 17 00:00:00 2001 From: Dmitrii Korchagin Date: Tue, 23 Apr 2024 01:18:41 +0200 Subject: [PATCH 03/12] NI: use --depth=1 for clone: ~35sec speed-up --- build.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 940c0fa..1430530 100644 --- a/build.rs +++ b/build.rs @@ -25,10 +25,12 @@ fn build() { let clone_status = Command::new("git") .args([ "clone", - "--recurse-submodules", - "https://github.com/ton-blockchain/ton", "--branch", "testnet", + "--depth", + "1", // get only the latest commit + "--recurse-submodules", // clone submodules as well + "https://github.com/ton-blockchain/ton", TON_DIR, ]) .status() From 44a429108188c32539b129a97e37be2e21403a31 Mon Sep 17 00:00:00 2001 From: Dmitrii Korchagin Date: Tue, 23 Apr 2024 01:19:36 +0200 Subject: [PATCH 04/12] NI: use --shallow-submodules for clone: ~40sec speed up --- build.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/build.rs b/build.rs index 1430530..001266c 100644 --- a/build.rs +++ b/build.rs @@ -30,6 +30,7 @@ fn build() { "--depth", "1", // get only the latest commit "--recurse-submodules", // clone submodules as well + "--shallow-submodules", // get only the latest commit of submodules "https://github.com/ton-blockchain/ton", TON_DIR, ]) From 5dfa352c789e7cf43c3552524bcb0ee01066d410 Mon Sep 17 00:00:00 2001 From: Dmitrii Korchagin Date: Tue, 23 Apr 2024 01:37:14 +0200 Subject: [PATCH 05/12] NI: use -j {cpu_num} for tonlibjsob: ~110sec speed up --- build.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 001266c..0f18d87 100644 --- a/build.rs +++ b/build.rs @@ -144,7 +144,8 @@ fn build_tonlibjson(march: &str) { .define("BUILD_SHARED_LIBS", "OFF") .define("PORTABLE", "1") .define("CMAKE_BUILD_TYPE", "Release") - //.args(&rust_cmake_args) + .build_arg("-j") + .build_arg(num_cpus::get().to_string()) .configure_arg("-Wno-dev") .build_target("tonlibjson") .always_configure(true) From 96bd49f6385f8b7aa1de13b91cb02d0b9580e9f2 Mon Sep 17 00:00:00 2001 From: Dmitrii Korchagin Date: Tue, 23 Apr 2024 01:39:34 +0200 Subject: [PATCH 06/12] NI: use -j {cpu_num} for tonlibjsob: ~4sec speed up --- build.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.rs b/build.rs index 0f18d87..7284e92 100644 --- a/build.rs +++ b/build.rs @@ -275,6 +275,8 @@ fn build_emulator(march: &str) { .configure_arg("-Wno-deprecated-declarations") .define("PORTABLE", "1") .define("CMAKE_BUILD_TYPE", "Release") + .build_arg("-j") + .build_arg(num_cpus::get().to_string()) .build_target("emulator") //.configure_arg() .always_configure(true) From 81ed423ba0c80a10c4b603caaf2d3f18bc1d734c Mon Sep 17 00:00:00 2001 From: Dmitrii Korchagin Date: Tue, 23 Apr 2024 10:19:25 +0200 Subject: [PATCH 07/12] NI: use available_parallelism() instead of num_cpus --- Cargo.toml | 3 +-- build.rs | 7 +++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index acb9ee3..0eb195b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,11 +12,10 @@ include = [ ] [features] -default = ["cmake", "num_cpus"] +default = ["cmake"] shared-tonlib = [] [dependencies] [build-dependencies] cmake = { version = "0.1", optional = true } -num_cpus = { version = "1", optional = true } diff --git a/build.rs b/build.rs index 7284e92..af93116 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,5 @@ use std::path::Path; +use std::thread::available_parallelism; use std::{env, fs}; fn main() { @@ -64,8 +65,6 @@ fn build() { } if cfg!(target_os = "macos") { - env::set_var("NUM_JOBS", num_cpus::get().to_string()); - // OpenSSL let openssl_installed = Command::new("brew") .args(["--prefix", "openssl@3"]) @@ -145,7 +144,7 @@ fn build_tonlibjson(march: &str) { .define("PORTABLE", "1") .define("CMAKE_BUILD_TYPE", "Release") .build_arg("-j") - .build_arg(num_cpus::get().to_string()) + .build_arg(available_parallelism().unwrap().get().to_string()) .configure_arg("-Wno-dev") .build_target("tonlibjson") .always_configure(true) @@ -276,7 +275,7 @@ fn build_emulator(march: &str) { .define("PORTABLE", "1") .define("CMAKE_BUILD_TYPE", "Release") .build_arg("-j") - .build_arg(num_cpus::get().to_string()) + .build_arg(available_parallelism().unwrap().get().to_string()) .build_target("emulator") //.configure_arg() .always_configure(true) From 2c5e9f7359cbdd83f8b58244ecf98fd7800d450c Mon Sep 17 00:00:00 2001 From: Dmitrii Korchagin Date: Tue, 23 Apr 2024 10:43:27 +0200 Subject: [PATCH 08/12] NI: use TON_DIR everywhere --- build.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index af93116..8c24d8c 100644 --- a/build.rs +++ b/build.rs @@ -56,7 +56,7 @@ fn build() { panic!("Git clone TON repo fail"); } let update_submodules_status = Command::new("git") - .current_dir("./ton") + .current_dir(TON_DIR) .args(["submodule", "update", "--init", "--recursive"]) .status() .unwrap(); @@ -134,7 +134,7 @@ fn build() { } fn build_tonlibjson(march: &str) { - let mut cfg = cmake::Config::new("ton"); + let mut cfg = cmake::Config::new(TON_DIR); let mut dst = cfg .configure_arg("-DTON_ONLY_TONLIB=true") .configure_arg("-DBUILD_SHARED_LIBS=false") @@ -265,7 +265,7 @@ fn build_tonlibjson(march: &str) { } fn build_emulator(march: &str) { - let mut cfg = cmake::Config::new("ton"); + let mut cfg = cmake::Config::new(TON_DIR); let mut dst = cfg .configure_arg("-DTON_ONLY_TONLIB=true") .configure_arg("-Wno-dev") From 075cf0fb21876eb34e7d7dc4b2d58ee1b5cb1abc Mon Sep 17 00:00:00 2001 From: Dmitrii Korchagin Date: Tue, 23 Apr 2024 11:49:52 +0200 Subject: [PATCH 09/12] NI: review fixes --- build.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/build.rs b/build.rs index 8c24d8c..4236f3d 100644 --- a/build.rs +++ b/build.rs @@ -6,21 +6,21 @@ fn main() { build(); } -const TONLIB_REVISION: &str = "25f61dff161b9c76dce0fc62dc51da911a208b68"; -const TON_DIR: &str = "./ton"; +const TON_MONOREPO_REVISION: &str = "25f61dff161b9c76dce0fc62dc51da911a208b68"; +const TON_MONOREPO_DIR: &str = "./ton"; #[cfg(not(feature = "shared-tonlib"))] fn build() { - env::set_var("TONLIB_REVISION", TONLIB_REVISION); - println!("cargo:rerun-if-env-changed=TONLIB_REVISION"); + 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"); println!("cargo:rerun-if-changed=src"); use std::process::Command; // cleanup tonlib after previous build - if Path::new(TON_DIR).exists() { - let _ = fs::remove_dir_all(TON_DIR); + if Path::new(TON_MONOREPO_DIR).exists() { + let _ = fs::remove_dir_all(TON_MONOREPO_DIR); } let clone_status = Command::new("git") @@ -33,14 +33,14 @@ fn build() { "--recurse-submodules", // clone submodules as well "--shallow-submodules", // get only the latest commit of submodules "https://github.com/ton-blockchain/ton", - TON_DIR, + TON_MONOREPO_DIR, ]) .status() .unwrap(); if clone_status.success() { let checkout_status = Command::new("git") - .current_dir(TON_DIR) - .args(["checkout", TONLIB_REVISION]) + .current_dir(TON_MONOREPO_DIR) + .args(["checkout", TON_MONOREPO_DIR]) .status() .unwrap(); @@ -56,7 +56,7 @@ fn build() { panic!("Git clone TON repo fail"); } let update_submodules_status = Command::new("git") - .current_dir(TON_DIR) + .current_dir(TON_MONOREPO_DIR) .args(["submodule", "update", "--init", "--recursive"]) .status() .unwrap(); @@ -134,7 +134,7 @@ fn build() { } fn build_tonlibjson(march: &str) { - let mut cfg = cmake::Config::new(TON_DIR); + let mut cfg = cmake::Config::new(TON_MONOREPO_DIR); let mut dst = cfg .configure_arg("-DTON_ONLY_TONLIB=true") .configure_arg("-DBUILD_SHARED_LIBS=false") @@ -143,6 +143,7 @@ fn build_tonlibjson(march: &str) { .define("BUILD_SHARED_LIBS", "OFF") .define("PORTABLE", "1") .define("CMAKE_BUILD_TYPE", "Release") + // multi-thread build used to fail compilation. Please try comment out next 2 lines if you have build errors .build_arg("-j") .build_arg(available_parallelism().unwrap().get().to_string()) .configure_arg("-Wno-dev") @@ -265,7 +266,7 @@ fn build_tonlibjson(march: &str) { } fn build_emulator(march: &str) { - let mut cfg = cmake::Config::new(TON_DIR); + let mut cfg = cmake::Config::new(TON_MONOREPO_DIR); let mut dst = cfg .configure_arg("-DTON_ONLY_TONLIB=true") .configure_arg("-Wno-dev") @@ -274,6 +275,7 @@ fn build_emulator(march: &str) { .configure_arg("-Wno-deprecated-declarations") .define("PORTABLE", "1") .define("CMAKE_BUILD_TYPE", "Release") + // multi-thread build used to fail compilation. Please try comment out next 2 lines if you have build errors .build_arg("-j") .build_arg(available_parallelism().unwrap().get().to_string()) .build_target("emulator") From 1a97f0d4b36f2b567bc522a7c69ceff4cd2016d2 Mon Sep 17 00:00:00 2001 From: Dmitrii Baranov Date: Wed, 1 May 2024 16:40:54 +0000 Subject: [PATCH 10/12] Upstream 2024.3.3 (#16) --- Cargo.toml | 2 +- build.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0eb195b..af86e71 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tonlib-sys" -version = "2024.3.2-dev" +version = "2024.3.5-dev" edition = "2021" description = "Rust bindings for tonlibjson library" license = "MIT" diff --git a/build.rs b/build.rs index 4236f3d..69eb92a 100644 --- a/build.rs +++ b/build.rs @@ -279,7 +279,6 @@ fn build_emulator(march: &str) { .build_arg("-j") .build_arg(available_parallelism().unwrap().get().to_string()) .build_target("emulator") - //.configure_arg() .always_configure(true) .very_verbose(false); From 56daf3e38b004aad885571fffd345ada474ce31c Mon Sep 17 00:00:00 2001 From: Andrey Vasiliev Date: Mon, 6 May 2024 10:04:39 +0000 Subject: [PATCH 11/12] NI: repair old mac build --- build.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index 69eb92a..c6546ed 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,5 @@ use std::path::Path; +use std::process::Command; use std::thread::available_parallelism; use std::{env, fs}; @@ -16,8 +17,6 @@ fn build() { println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=src"); - use std::process::Command; - // cleanup tonlib after previous build if Path::new(TON_MONOREPO_DIR).exists() { let _ = fs::remove_dir_all(TON_MONOREPO_DIR); @@ -138,7 +137,6 @@ fn build_tonlibjson(march: &str) { let mut dst = cfg .configure_arg("-DTON_ONLY_TONLIB=true") .configure_arg("-DBUILD_SHARED_LIBS=false") - .configure_arg("-DCMAKE_EXE_LINKER_FLAGS=-L/opt/homebrew/lib") .define("TON_ONLY_TONLIB", "ON") .define("BUILD_SHARED_LIBS", "OFF") .define("PORTABLE", "1") @@ -151,6 +149,13 @@ fn build_tonlibjson(march: &str) { .always_configure(true) .very_verbose(false); + 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)) From ad9c8dec60de102f15731f1732702cb364d101d6 Mon Sep 17 00:00:00 2001 From: Dmitrii Baranov Date: Wed, 8 May 2024 14:34:00 +0000 Subject: [PATCH 12/12] Impl NI: no_avx512 flag added --- Cargo.toml | 1 + README.md | 3 --- build.rs | 51 ++++++++++++++++++++++++++++++++------------------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index af86e71..2251e8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ include = [ [features] default = ["cmake"] +no_avx512 = [] shared-tonlib = [] [dependencies] diff --git a/README.md b/README.md index ebb9252..fc060e7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build.rs b/build.rs index c6546ed..07e1e95 100644 --- a/build.rs +++ b/build.rs @@ -3,6 +3,8 @@ use std::process::Command; use std::thread::available_parallelism; use std::{env, fs}; +use cmake::Config; + fn main() { build(); } @@ -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"); @@ -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") @@ -149,6 +151,9 @@ 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(); @@ -156,11 +161,6 @@ fn build_tonlibjson(march: &str) { 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"); @@ -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") @@ -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"); @@ -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"); +}