Skip to content

Commit

Permalink
Modify rust bindings to create a crate
Browse files Browse the repository at this point in the history
  • Loading branch information
potuz committed May 2, 2024
1 parent 616c243 commit 6aee1fa
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 47 deletions.
1 change: 1 addition & 0 deletions rust_bindings/.gitignore → .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Cargo.lock
target
build
4 changes: 4 additions & 0 deletions rust_bindings/Cargo.toml → Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ version = "0.1.0"
authors = ["Potuz <potuz@prysmaticlabs.com>"]
edition = "2021"
description = "Rust bindings for the hashtree library"
documentation = "https://github.com/prysmaticlabs/hashtree"
repository = "https://github.com/prysmaticlabs/hashtree"
homepage = "https://github.com/prysmaticlabs/hashtree"
license = "MIT"
build = "build.rs"
keywords = ["hash", "crypto", "sha256", "merkle"]
include = ["build.rs", "Cargo.toml", "src/lib.rs", "src/*.c", "src/*.h", "src/*.S", "Makefile", "src/Makefile"]

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ libhashtree:

clean:
$(MAKE) -C src clean
cd rust_bindings && cargo clean
cargo clean

test:
$(MAKE) -C src test
Expand Down
27 changes: 17 additions & 10 deletions rust_bindings/build.rs → build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,29 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/


use std::env;
use std::path::PathBuf;
use std::process::Command;

fn main() {
let root_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("..");
let out_dir = env::var("OUT_DIR").unwrap_or_else(|_| {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
format!("{}/build", manifest_dir)
});

let lib_dir = PathBuf::from(&out_dir).join("lib");
let src_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());

let status = Command::new("make")
.current_dir(&src_dir)
.env("OUT_DIR", &out_dir) // Pass OUT_DIR to makefile if needed
.status()
.expect("Failed to execute make command");

Command::new("make")
.current_dir(&root_path)
.output()
.expect("Failed to build hashtree");
if !status.success() {
panic!("Failed to build the C library");
}

println!(
"cargo:rustc-link-search=native={}",
root_path.join("src").display()
);
println!("cargo:rustc-link-search=native={}", lib_dir.display());
println!("cargo:rustc-link-lib=static=hashtree");
}
File renamed without changes.
15 changes: 0 additions & 15 deletions rust_bindings/README.md

This file was deleted.

56 changes: 35 additions & 21 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,46 +37,60 @@ benchlibs += -lcrypto
testlibs += -lcrypto
endif

objarm = sha256_armv8_neon_x4.o\
sha256_armv8_neon_x1.o\
sha256_armv8_crypto.o\
hashtree.o\

objx86 = sha256_shani.o\
sha256_avx_x16.o\
sha256_avx_x8.o\
sha256_avx_x4.o\
sha256_avx_x1.o\
sha256_sse_x1.o\
hashtree.o\
OUT_DIR ?= $(CURDIR)/../build
OBJ_DIR := $(OUT_DIR)/obj
LIB_DIR := $(OUT_DIR)/lib

# Ensure these directories exist
$(shell mkdir -p $(OBJ_DIR))
$(shell mkdir -p $(LIB_DIR))

objarm = $(OBJ_DIR)/sha256_armv8_neon_x4.o\
$(OBJ_DIR)/sha256_armv8_neon_x1.o\
$(OBJ_DIR)/sha256_armv8_crypto.o\
$(OBJ_DIR)/hashtree.o

objx86 = $(OBJ_DIR)/sha256_shani.o\
$(OBJ_DIR)/sha256_avx_x16.o\
$(OBJ_DIR)/sha256_avx_x8.o\
$(OBJ_DIR)/sha256_avx_x4.o\
$(OBJ_DIR)/sha256_avx_x1.o\
$(OBJ_DIR)/sha256_sse_x1.o\
$(OBJ_DIR)/hashtree.o

.PHONY : clean .FORCE
.FORCE:

$(OBJ_DIR)/%.o: %.S
$(CC) $(ASFLAGS) -c $< -o $@

$(OBJ_DIR)/%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

ifeq ($(WIN),1)
libname = libhashtree.lib
libname = $(LIB_DIR)/libhashtree.lib
else
libname = libhashtree.a
libname = $(LIB_DIR)/libhashtree.a
endif

ifeq ($(ARM),1)
libhashtree.a: $(objarm) hashtree.pc
$(AR) rcs libhashtree.a $(objarm)
$(LIB_DIR)/libhashtree.a: $(objarm)
$(AR) rcs $@ $(objarm)
else
$(libname): $(objx86) hashtree.pc
$(AR) rcs $(libname) $(objx86)
$(libname): $(objx86)
$(AR) rcs $@ $(objx86)
endif

all: $(libname) test bench

test: hashtree.h acutest.h test.c $(libname)
$(CC) $(CFLAGS) $(LDFLAGS) -o test test.c $(testlibs)
$(CC) $(CFLAGS) $(LDFLAGS) -L$(LIB_DIR) -o $(OUT_DIR)/test test.c $(testlibs)

bench: hashtree.h ubench.h bench.c $(libname)
$(CC) $(CFLAGS) $(LDFLAGS) -o bench bench.c $(benchlibs)
$(CC) $(CFLAGS) $(LDFLAGS) -L$(LIB_DIR) -o $(OUT_DIR)/bench bench.c $(benchlibs)

clean:
-rm -f $(objarm) $(objx86) libhashtree.a libhashtree.lib test test.exe bench hashtree.pc
-rm -f $(objarm) $(objx86) $(LIB_DIR)/libhashtree.a $(LIB_DIR)/libhashtree.lib $(OUT_DIR)/test $(OUT_DIR)/test.exe $(OUT_DIR)/bench hashtree.pc

ifeq ($(PREFIX),)
PREFIX := /usr
Expand Down
File renamed without changes.

0 comments on commit 6aee1fa

Please sign in to comment.