diff --git a/rust_bindings/.gitignore b/.gitignore similarity index 75% rename from rust_bindings/.gitignore rename to .gitignore index fa8d85a..1ab6a3f 100644 --- a/rust_bindings/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ Cargo.lock target +build diff --git a/rust_bindings/Cargo.toml b/Cargo.toml similarity index 55% rename from rust_bindings/Cargo.toml rename to Cargo.toml index 5a4a1f5..30d5f1c 100644 --- a/rust_bindings/Cargo.toml +++ b/Cargo.toml @@ -4,9 +4,13 @@ version = "0.1.0" authors = ["Potuz "] 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"] diff --git a/Makefile b/Makefile index 201dc05..b8e4150 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ libhashtree: clean: $(MAKE) -C src clean - cd rust_bindings && cargo clean + cargo clean test: $(MAKE) -C src test diff --git a/rust_bindings/build.rs b/build.rs similarity index 64% rename from rust_bindings/build.rs rename to build.rs index e25ded8..dcd26c0 100644 --- a/rust_bindings/build.rs +++ b/build.rs @@ -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"); } diff --git a/rust_bindings/examples/basic_usage.rs b/examples/basic_usage.rs similarity index 100% rename from rust_bindings/examples/basic_usage.rs rename to examples/basic_usage.rs diff --git a/rust_bindings/README.md b/rust_bindings/README.md deleted file mode 100644 index 2965eea..0000000 --- a/rust_bindings/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# Rust bindings for Hashstree - -At the top directory you can run - -``` -$ make rust_bindings -``` - -To run tests: - -``` -$ make rust_tests -``` - -See the `examples` directory for examples on how to use the library diff --git a/src/Makefile b/src/Makefile index d3ac075..894c8fb 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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 diff --git a/rust_bindings/src/lib.rs b/src/lib.rs similarity index 100% rename from rust_bindings/src/lib.rs rename to src/lib.rs