Add win32 support

Replace Make-based compilation of the nif to using rebar's port
compiler.
This commit is contained in:
Tino Breddin 2018-11-23 15:16:41 +01:00
parent 1121321573
commit 26180f42c0
4 changed files with 39 additions and 82 deletions

4
.gitignore vendored
View File

@ -12,3 +12,7 @@ doc/*.png
doc/*.css doc/*.css
_build _build
/.eqc-info /.eqc-info
priv/enacl_nif.dll
priv/enacl_nif.exp
priv/enacl_nif.lib
c_src/enacl_nif.d

View File

@ -1,75 +0,0 @@
# Based on c_src.mk from erlang.mk by Loïc Hoguin <essen@ninenines.eu>
PROJECT ?= enacl_nif
ERTS_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s/erts-~s/include/\", [code:root_dir(), erlang:system_info(version)]).")
ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, include)]).")
ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, lib)]).")
C_SRC_DIR = $(CURDIR)
C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so
# System type and C compiler/flags.
MACHINE_SYS := $(shell uname -m)
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 -I /usr/local/include
CXXFLAGS ?= -O3 -finline-functions -Wall
LDFLAGS ?= -fPIC -L /usr/local/lib
else ifeq ($(UNAME_SYS), Linux)
CC ?= gcc
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
CXXFLAGS ?= -O3 -finline-functions -Wall
else ifeq ($(UNAME_SYS), SunOS)
CC = gcc
CFLAGS ?= -m64 -I/opt/local/include -O2 -std=c99 -finline-functions -Wall -Wmissing-prototypes
CXXFALGS ?= -O2 -finline-function -Wall
LDFLAGS ?= -m64 -fPIC -L /opt/local/lib
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 -lsodium
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)
$(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)

View File

@ -1515,7 +1515,7 @@ static
crypto_generichash_state *align64(void *ptr){ crypto_generichash_state *align64(void *ptr){
if((unsigned long)ptr % 64 == 0) if((unsigned long)ptr % 64 == 0)
return ptr; return ptr;
return ptr + (64 - ((unsigned long)ptr % 64)); return (unsigned long)ptr + (64 - ((unsigned long)ptr % 64));
} }
static static

View File

@ -1,9 +1,37 @@
{erl_opts, [debug_info]}. {erl_opts, [debug_info]}.
{pre_hooks, [{"freebsd", compile, "gmake -C c_src"}, {plugins, [pc]}.
{"freebsd", clean, "gmake -C c_src clean"},
{"netbsd", compile, "gmake -C c_src"}, {provider_hooks, [
{"netbsd", clean, "gmake -C c_src clean"}, {pre, [
{"(linux|darwin|solaris)", compile, "make -C c_src"}, {compile, {pc, compile}},
{"(linux|darwin|solaris)", clean, "make -C c_src clean"} {clean, {pc, clean}}
]}
]}.
{port_specs, [
{"priv/enacl_nif.so", [
"c_src/*.c"
]}
]}.
{port_env, [
{"darwin", "CFLAGS", "$CFLAGS -fPIC -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes"},
{"darwin", "CXXFLAGS", "$CXXFLAGS -fPIC -O3 -arch x86_64 -finline-functions -Wall"},
{"darwin", "LDFLAGS", "$LDFLAGS -arch x86_64 -flat_namespace -undefined suppress -lsodium"},
{"linux", "CFLAGS", "$CFLAGS -fPIC -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes"},
{"linux", "CXXFLAGS", "$CXXFLAGS -fPIC -O3 -finline-functions -Wall"},
{"linux", "LDFLAGS", "$LDFLAGS -lsodium"},
{"freebsd", "CFLAGS", "$CFLAGS -fPIC -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes -I /usr/local/include"},
{"freebsd", "CXXFLAGS", "$CXXFLAGS -fPIC -O3 -finline-functions -Wall"},
{"freebsd", "LDFLAGS", "$LDFLAGS -fPIC -L /usr/local/lib -lsodium"},
{"solaris", "CFLAGS", "$CFLAGS -fPIC -m64 -I/opt/local/include -O2 -std=c99 -finline-functions -Wall -Wmissing-prototypes"},
{"solaris", "CXXFLAGS", "$CXXFLAGS -fPIC -O2 -finline-function -Wall"},
{"solaris", "LDFLAGS", "$LDFLAGS -m64 -fPIC -L /opt/local/lib -lsodium"},
{"win32", "CFLAGS", "$CFLAGS /LD /O2 /DNDEBUG"},
{"win32", "LDFLAGS", "$LDFLAGS libsodium.dll.a"}
]}. ]}.