summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2020-11-03 10:28:57 +0100
committerpukkamustard <pukkamustard@posteo.net>2020-11-03 10:28:57 +0100
commit8e8d66d22f0f890f6235ed69205738861e2b7d1a (patch)
treedd51d89e5401b4755b03707b2057eeb4f4e8a418
parentd5b07f697fd1fc8d1040d3e0460e89658bc23c0f (diff)
Compile with rebar3.
-rw-r--r--.gitignore21
-rw-r--r--c_src/Makefile73
-rw-r--r--rebar.config8
-rw-r--r--src/monocypher.app.src13
-rw-r--r--src/monocypher.erl27
5 files changed, 137 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7565948
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,21 @@
+.rebar3
+_*
+.eunit
+*.o
+*.beam
+*.plt
+*.swp
+*.swo
+.erlang.cookie
+ebin
+log
+erl_crash.dump
+.rebar
+logs
+_build
+.idea
+*.iml
+rebar3.crashdump
+*~
+/priv/
+/rebar.lock
diff --git a/c_src/Makefile b/c_src/Makefile
new file mode 100644
index 0000000..3a73da1
--- /dev/null
+++ b/c_src/Makefile
@@ -0,0 +1,73 @@
+# Based on c_src.mk from erlang.mk by Loic Hoguin <essen@ninenines.eu>
+
+CURDIR := $(shell pwd)
+BASEDIR := $(abspath $(CURDIR)/..)
+
+PROJECT = monocypher
+
+ERTS_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~ts/erts-~ts/include/\", [code:root_dir(), erlang:system_info(version)]).")
+ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~ts\", [code:lib_dir(erl_interface, include)]).")
+ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~ts\", [code:lib_dir(erl_interface, lib)]).")
+
+C_SRC_DIR = $(CURDIR)
+C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so
+
+# System type and C compiler/flags.
+
+UNAME_SYS := $(shell uname -s)
+ifeq ($(UNAME_SYS), Darwin)
+ CC ?= cc
+ CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes
+ CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall
+ LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress
+else ifeq ($(UNAME_SYS), FreeBSD)
+ CC ?= cc
+ CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
+ CXXFLAGS ?= -O3 -finline-functions -Wall
+else ifeq ($(UNAME_SYS), Linux)
+ CC ?= gcc
+ CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
+ CXXFLAGS ?= -O3 -finline-functions -Wall
+endif
+
+CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
+CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
+
+LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei
+LDFLAGS += -shared
+
+# Verbosity.
+
+c_verbose_0 = @echo " C " $(?F);
+c_verbose = $(c_verbose_$(V))
+
+cpp_verbose_0 = @echo " CPP " $(?F);
+cpp_verbose = $(cpp_verbose_$(V))
+
+link_verbose_0 = @echo " LD " $(@F);
+link_verbose = $(link_verbose_$(V))
+
+SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \))
+OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))
+
+COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c
+COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c
+
+$(C_SRC_OUTPUT): $(OBJECTS)
+ @mkdir -p $(BASEDIR)/priv/
+ $(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) $(LDLIBS) -o $(C_SRC_OUTPUT)
+
+%.o: %.c
+ $(COMPILE_C) $(OUTPUT_OPTION) $<
+
+%.o: %.cc
+ $(COMPILE_CPP) $(OUTPUT_OPTION) $<
+
+%.o: %.C
+ $(COMPILE_CPP) $(OUTPUT_OPTION) $<
+
+%.o: %.cpp
+ $(COMPILE_CPP) $(OUTPUT_OPTION) $<
+
+clean:
+ @rm -f $(C_SRC_OUTPUT) $(OBJECTS)
diff --git a/rebar.config b/rebar.config
new file mode 100644
index 0000000..91ace11
--- /dev/null
+++ b/rebar.config
@@ -0,0 +1,8 @@
+{erl_opts, [debug_info]}.
+{pre_hooks,
+ [{"(linux|darwin|solaris)", compile, "make -C c_src"},
+ {"(freebsd)", compile, "gmake -C c_src"}]}.
+{post_hooks,
+ [{"(linux|darwin|solaris)", clean, "make -C c_src clean"},
+ {"(freebsd)", clean, "gmake -C c_src clean"}]}.
+{deps, []}.
diff --git a/src/monocypher.app.src b/src/monocypher.app.src
new file mode 100644
index 0000000..49d1a62
--- /dev/null
+++ b/src/monocypher.app.src
@@ -0,0 +1,13 @@
+{application, monocypher,
+ [{description, "Erlang bindings to the Monocypher cryptographic library."},
+ {vsn, "0.1.0"},
+ {registered, []},
+ {applications,
+ [kernel,
+ stdlib
+ ]},
+ {env,[]},
+ {modules, [monocypher]},
+ {licenses, ["Apache 2.0"]},
+ {links, []}
+ ]}.
diff --git a/src/monocypher.erl b/src/monocypher.erl
index dfceab9..d61bb79 100644
--- a/src/monocypher.erl
+++ b/src/monocypher.erl
@@ -3,11 +3,28 @@
-export([crypto_blake2b_general/3]).
-on_load(init/0).
-init() ->
- ok = erlang:load_nif("../c_src/monocypher_nif", 0).
+-define(APPNAME, monocypher).
+-define(LIBNAME, monocypher).
-crypto_blake2b(_X) ->
- exit(nif_library_not_loaded).
+crypto_blake2b(_) ->
+ not_loaded(?LINE).
crypto_blake2b_general(_,_,_) ->
- exit(nif_library_not_loaded).
+ not_loaded(?LINE).
+
+init() ->
+ SoName = case code:priv_dir(?APPNAME) of
+ {error, bad_name} ->
+ case filelib:is_dir(filename:join(["..", priv])) of
+ true ->
+ filename:join(["..", priv, ?LIBNAME]);
+ _ ->
+ filename:join([priv, ?LIBNAME])
+ end;
+ Dir ->
+ filename:join(Dir, ?LIBNAME)
+ end,
+ erlang:load_nif(SoName, 0).
+
+not_loaded(Line) ->
+ erlang:nif_error({not_loaded, [{module, ?MODULE}, {line, Line}]}).