diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..05dcca3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +c_src/libsodium-f553bb4bf22a6a8e4fa5f69cfbf91ce95327b790 diff --git a/build_libsodium.sh b/build_libsodium.sh new file mode 100755 index 0000000..e6e06af --- /dev/null +++ b/build_libsodium.sh @@ -0,0 +1,63 @@ +#!/bin/sh + +CORE_TOP=`pwd` + +LIBSODIUM_VER="f553bb4bf22a6a8e4fa5f69cfbf91ce95327b790" +LIBSODIUM_ZIP="libsodium-$LIBSODIUM_VER.zip" +LIBSODIUM_DIR="$CORE_TOP/c_src/libsodium-$LIBSODIUM_VER" + + +UNZIP=`which unzip` +if ! test -n "UNZIP"; then + display_error "Error: unzip is required. Add it to 'PATH'" + exit 1 +fi + +clean() { + echo "==> clean libsodium" + if test -f $LIBSODIUM_DIR/Makefile; then + cd $LIBSODIUM_DIR && make clean + fi +} + + +build() { + echo "==> build libsodium" + # unzip + if ! test -d $LIBSODIUM_DIR; then + cd $CORE_TOP/c_src && $UNZIP $LIBSODIUM_ZIP + fi + + cd $LIBSODIUM_DIR + + # configure + if ! test -f $LIBSODIUM_DIR/Makefile; then + ./autogen.sh + ./configure --disable-pie \ + --disable-ssp \ + --disable-shared \ + --enable-static + fi + + make +} + +if [ "x$1" = "x" ]; then + build + exit 0 +fi + +case "$1" in + build) + shift 1 + build + ;; + clean) + shift 1 + clean + ;; + *) + echo "badarg" + exit 1; + ;; +esac diff --git a/c_src/libsodium-f553bb4bf22a6a8e4fa5f69cfbf91ce95327b790.zip b/c_src/libsodium-f553bb4bf22a6a8e4fa5f69cfbf91ce95327b790.zip new file mode 100644 index 0000000..2b15056 Binary files /dev/null and b/c_src/libsodium-f553bb4bf22a6a8e4fa5f69cfbf91ce95327b790.zip differ diff --git a/rebar.config.script b/rebar.config.script index b57dee2..65ee3bc 100644 --- a/rebar.config.script +++ b/rebar.config.script @@ -1,3 +1,6 @@ +%-*-Mode:erlang;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*- +% ex: set ft=erlang fenc=utf-8 sts=4 ts=4 sw=4 et: +% %%% %%% Reconcile NaCl's idea of architecture name with Erlang's, set compiler %%% flags and paths for NIF compilation. @@ -5,12 +8,19 @@ Arch = erlang:system_info(system_architecture), +LIBSODIUM_VER="f553bb4bf22a6a8e4fa5f69cfbf91ce95327b790", +LIBSODIUM_CFLAGS="-Ic_src/libsodium-" ++ LIBSODIUM_VER ++"/src/libsodium/include/sodium", +LIBSODIUM_LDLAGS="c_src/libsodium-" ++ LIBSODIUM_VER ++ "/src/libsodium/.libs/libsodium.a", + %% Augment configuration from rebar.config with NIF settings. lists:keymerge(1, - lists:keysort(1, [ - {port_env, [{"DRV_CFLAGS", "$DRV_CFLAGS -Wall -Werror -I/usr/local/include/sodium"}, - {"DRV_LDFLAGS", "$DRV_LDFLAGS -L/usr/local/lib -Wl,-R/usr/local/lib -lsodium"}]}, - {port_specs, [{filename:join(["priv", Arch, "salt_nif.so"]), ["c_src/salt_nif.c"]}]}, - {pre_hooks, [{clean, "rm -fr ebin erl_crash.dump salt_test.beam c_src/salt_nif.o priv/" ++ Arch}]} - ]), - lists:keysort(1, CONFIG)). +lists:keysort(1, [ +{port_env, [{"DRV_CFLAGS", "$DRV_CFLAGS -Wall -Werror " ++ LIBSODIUM_CFLAGS}, + {"DRV_LDFLAGS", "$DRV_LDFLAGS " ++ LIBSODIUM_LDLAGS}]}, + {port_specs, [{filename:join(["priv", Arch, "salt_nif.so"]), + ["c_src/salt_nif.c"]}]}, + {pre_hooks, [{compile, "./build_libsodium.sh"}, + {clean, "rm -fr ebin erl_crash.dump salt_test.beam c_src/salt_nif.o priv/" ++ Arch}, + {clean, "./build_libsodium.sh clean"}]} +]), +lists:keysort(1, CONFIG)). diff --git a/src/salt_nif.erl b/src/salt_nif.erl index 954a28c..c8eac29 100644 --- a/src/salt_nif.erl +++ b/src/salt_nif.erl @@ -45,7 +45,19 @@ %%% load() -> - Path = filename:join([code:priv_dir(salt), erlang:system_info(system_architecture), "salt_nif"]), + %% get local priv dir if we test from the shell + PrivDir = case code:priv_dir(salt) of + {error, _} -> + EbinDir = filename:dirname(code:which(?MODULE)), + AppPath = filename:dirname(EbinDir), + filename:join(AppPath, "priv"); + Dir -> + Dir + end, + + Path = filename:join([PrivDir, + erlang:system_info(system_architecture), + "salt_nif"]), erlang:load_nif(Path, 0). %%% Exported from salt_nif.c.