Skip to content

Commit

Permalink
fix use new Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
Ljzn committed Sep 23, 2022
1 parent 11371fb commit 3f748f1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ _build
.idea
*.iml
rebar3.crashdump
.*#
.*#
*.so
85 changes: 63 additions & 22 deletions c_src/Makefile
Original file line number Diff line number Diff line change
@@ -1,33 +1,74 @@
OTPROOT=/usr/lib/erlang
INCLUDES = -I/usr/lib/erlang/usr/include
# Based on c_src.mk from erlang.mk by Loic Hoguin <[email protected]>

# OS X flags.
#GCCFLAGS = -O3 -fPIC -bundle -flat_namespace -undefined suppress -fno-common -Wall
CURDIR := $(shell pwd)
BASEDIR := $(abspath $(CURDIR)/..)

# Linux Flags
GCCFLAGS = -O3 -fPIC -shared -fno-common -Wall
PROJECT ?= $(notdir $(BASEDIR))
PROJECT := $(strip $(PROJECT))

CFLAGS = $(GCCFLAGS) $(INCLUDES)
CFLAGS_32=-m32
LDFLAGS = $(CFLAGS) $(LIBS)
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)]).")

#OBJECTS = curve25519-donna.o curve25519-donna-c64.o curve25519_donna_nif.c
C_SRC_DIR = $(CURDIR)
C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT).so

DRIVER = curve25519_donna_nif.so curve25519_donna_c64_nif.so
BEAM = curve25519.beam
# System type and C compiler/flags.

all: $(DRIVER) $(BEAM)
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

clean:
rm -f *.o *.beam *.so
CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)

curve25519_donna_nif.so: curve25519-donna.c curve25519_donna_nif.c
gcc -o $@ $^ $(LDFLAGS) $(CFLAGS_32)
LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lei
LDFLAGS += -shared

curve25519_donna_c64_nif.so: curve25519-donna-c64.c curve25519_donna_nif.c
gcc -o $@ $^ $(LDFLAGS)
# Verbosity.

$(BEAM): curve25519.erl
erlc $^
c_verbose_0 = @echo " C " $(?F);
c_verbose = $(c_verbose_$(V))

# EOF
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)
Binary file removed priv/curve25519_drv.so
Binary file not shown.

0 comments on commit 3f748f1

Please sign in to comment.