From b3fa59a0612d28a484f70605453421107aeef97b Mon Sep 17 00:00:00 2001 From: Tino Breddin Date: Mon, 9 Dec 2019 14:03:17 +0100 Subject: [PATCH] Change NIF build order to prefer local builds --- Makefile | 2 ++ README.md | 24 +++++++++++++++++++----- src/ecrecover.erl | 34 +++++++++++++++++++++++----------- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 855842b..7704403 100644 --- a/Makefile +++ b/Makefile @@ -20,8 +20,10 @@ compile: ./rebar3 compile priv/$(nif_lib): src/lib.rs +ifneq ($(ECRECOVER_DISABLE_NIF_BUILD), true) cargo build --release cp target/release/$(nif_lib_src) $@ +endif clean: rm -f priv/$(nif_lib) target/release/$(nif_lib_src) diff --git a/README.md b/README.md index 3a5c971..4a12097 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,29 @@ # ecrecover FFI (NIF) export of Ethereum's ecrecover for use from Erlang. -### prerequisite: -- a checked out copy of my fork of parity-ethereum (https://github.com/johnsnewby/parity-ethereum) checked out into this directory this is (i.e. it will be referenced as `./parity-ethereum`) +### Prerequisites +The NIF is written in Rust, therefore the following additional build +dependencies are needed: -### to compile: -`cargo build` +- Rust +- Cargo +- Cmake + +### Build +Execute: +``` +make +``` + +To disable the local build of the NIF library, e.g. to use a prebuilt binary, +use the following command: +``` +ECRECOVER_DISABLE_NIF_BUILD=true make +``` ## Erlang integration -The shared library uses NIF. Use the erlang file `src/ecrecover.erl` to use this: +The shared library uses NIF. Use the Erlang file `src/ecrecover.erl` to use this: ``` c("src/ecrecover"). diff --git a/src/ecrecover.erl b/src/ecrecover.erl index d181b11..f968f91 100644 --- a/src/ecrecover.erl +++ b/src/ecrecover.erl @@ -11,8 +11,14 @@ %% NIF API load() -> - SoName = filename:join(priv(), atom_to_list(?MODULE)), - ok = erlang:load_nif(SoName, 0). + % Prefer locally built NIF (good for testing and exotic platforms) over + % prebuilt binaries. + case load_local_nif() of + ok -> + ok; + {error, _} -> + load_prebuilt_nif() + end. not_loaded(Line) -> erlang:nif_error({error, {not_loaded, [{module, ?MODULE}, {line, Line}]}}). @@ -35,15 +41,21 @@ recover(<<_:32/binary>> = Hash, <<_:65/binary>> = Sig) -> %%============================================================================= %% Internal Functions -priv()-> - case code:priv_dir(ecrecoverprebuilt) of - {error, _} -> - EbinDir = filename:dirname(code:which(?MODULE)), - AppPath = filename:dirname(EbinDir), - filename:join(AppPath, "priv"); - Path -> - Path - end. +load_local_nif() -> + EbinDir = filename:dirname(code:which(?MODULE)), + AppDir = filename:dirname(EbinDir), + PrivDir = filename:join(AppDir, "priv"), + SoName = filename:join(PrivDir, atom_to_list(?MODULE)), + erlang:load_nif(SoName, 0). + +load_prebuilt_nif() -> + case code:priv_dir(ecrecoverprebuilt) of + {error, _} -> + {error, prebuilt_priv_dir_not_found}; + PrivDir -> + SoName = filename:join(PrivDir, atom_to_list(?MODULE)), + erlang:load_nif(SoName, 0) + end. recover_(_Input) -> not_loaded(?LINE).