Skip to content

Commit c2f47a4

Browse files
author
Silviu Caragea
committed
Compatible with rebar3
1 parent 790aa30 commit c2f47a4

File tree

7 files changed

+103
-17
lines changed

7 files changed

+103
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/epqueue_nif.xcodeproj/
55
/priv/
66
*.iml
7+
/_build/

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@ This can be changed if it's required by moving the encoding/decoding or terms in
2727

2828
Compile:
2929

30-
```
30+
```sh
3131
rebar compile
3232
```
3333

34+
or
35+
36+
```sh
37+
rebar3 compile
38+
```
39+
3440
Simple usage:
3541

3642
```erlang

c_src/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.d
22
*.o
3+
/env.mk

c_src/Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
PROJECT_NIF_NAME=epqueue_nif
3+
4+
include nif.mk
5+
6+
CXXFLAGS+= -g -Wextra -Werror -Wno-missing-field-initializers -fno-rtti -fno-exceptions
7+
LDFLAGS += -lstdc++

c_src/nif.mk

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Based on c_src.mk from erlang.mk by Loic Hoguin <[email protected]>
2+
# https://github.com/ninenines/erlang.mk/blob/master/plugins/c_src.mk
3+
4+
CURDIR := $(shell pwd)
5+
BASEDIR := $(abspath $(CURDIR)/..)
6+
7+
C_SRC_DIR = $(CURDIR)
8+
C_SRC_ENV ?= $(C_SRC_DIR)/env.mk
9+
C_SRC_OUTPUT ?= $(CURDIR)/../priv/$(PROJECT_NIF_NAME).so
10+
11+
#regenerate all the time the env.mk
12+
ifneq ($(wildcard $(C_SRC_DIR)),)
13+
GEN_ENV ?= $(shell erl -noshell -s init stop -eval "file:write_file(\"$(C_SRC_ENV)\", \
14+
io_lib:format( \
15+
\"ERTS_INCLUDE_DIR ?= ~s/erts-~s/include/~n\" \
16+
\"ERL_INTERFACE_INCLUDE_DIR ?= ~s~n\" \
17+
\"ERL_INTERFACE_LIB_DIR ?= ~s~n\", \
18+
[code:root_dir(), erlang:system_info(version), \
19+
code:lib_dir(erl_interface, include), \
20+
code:lib_dir(erl_interface, lib)])), \
21+
halt().")
22+
$(GEN_ENV)
23+
endif
24+
25+
include $(C_SRC_ENV)
26+
27+
# System type and C compiler/flags.
28+
29+
UNAME_SYS := $(shell uname -s)
30+
31+
ifeq ($(UNAME_SYS), Darwin)
32+
CC ?= cc
33+
CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall -Wmissing-prototypes
34+
CXXFLAGS ?= -O3 -arch x86_64 -Wall
35+
LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress
36+
else ifeq ($(UNAME_SYS),freebsd)
37+
CC ?= cc
38+
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
39+
CXXFLAGS ?= -O3 -finline-functions -Wall
40+
else ifeq ($(UNAME_SYS),linux)
41+
CC ?= gcc
42+
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall -Wmissing-prototypes
43+
CXXFLAGS ?= -O3 -finline-functions -Wall
44+
endif
45+
46+
CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
47+
CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
48+
LDFLAGS += -L $(ERL_INTERFACE_LIB_DIR) -shared -lerl_interface -lei
49+
50+
# Verbosity.
51+
52+
c_verbose_0 = @echo " C " $(?F);
53+
c_verbose = $(c_verbose_$(V))
54+
55+
cpp_verbose_0 = @echo " CPP " $(?F);
56+
cpp_verbose = $(cpp_verbose_$(V))
57+
58+
link_verbose_0 = @echo " LD " $(@F);
59+
link_verbose = $(link_verbose_$(V))
60+
61+
SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \))
62+
OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))
63+
64+
COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c
65+
COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c
66+
67+
$(C_SRC_OUTPUT): $(OBJECTS)
68+
@mkdir -p $(BASEDIR)/priv/
69+
$(link_verbose) $(CC) $(OBJECTS) $(LDFLAGS) -o $(C_SRC_OUTPUT)
70+
71+
%.o: %.c
72+
$(COMPILE_C) $(OUTPUT_OPTION) $<
73+
74+
%.o: %.cc
75+
$(COMPILE_CPP) $(OUTPUT_OPTION) $<
76+
77+
%.o: %.C
78+
$(COMPILE_CPP) $(OUTPUT_OPTION) $<
79+
80+
%.o: %.cpp
81+
$(COMPILE_CPP) $(OUTPUT_OPTION) $<
82+
83+
clean:
84+
@rm -f $(C_SRC_OUTPUT) $(OBJECTS); rm -f $(C_SRC_ENV)

rebar.config

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
1-
{port_specs, [
2-
3-
{"darwin","priv/epqueue_nif.so", ["c_src/*.cc"],[
4-
{env, [
5-
{".*", "CXXFLAGS", "$CXXFLAGS -g -Wall -Wextra -Werror -Wno-missing-field-initializers -fno-exceptions -fno-rtti -O3"},
6-
{".*", "LDFLAGS", "$LDFLAGS -lstdc++"}
7-
]}
8-
]},
9-
10-
{"linux","priv/epqueue_nif.so", ["c_src/*.cc"],[
11-
{env, [
12-
{".*", "CXXFLAGS", "$CXXFLAGS -g -Wall -Wextra -Werror -Wno-missing-field-initializers -fno-exceptions -fno-rtti -O3"},
13-
{".*", "LDFLAGS", "$LDFLAGS -lstdc++"}
14-
]}
15-
]}
16-
]}.
1+
{pre_hooks, [{"(linux|darwin)", compile, "make V=0 -C c_src -j 8"}]}.
2+
{post_hooks, [{"(linux|darwin)", clean, "make -C c_src clean"}]}.
173

184
%because enif_binary_to_term and enif_term_to_binary are not available in previous versions.
195
%this can be changed if it's required by moving the encoding/decoding in erlang and in NIF only store the received binary

rebar.lock

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[].

0 commit comments

Comments
 (0)