Skip to content

Commit 3d762af

Browse files
author
Silviu Caragea
committed
Make rebar3 friendly
1 parent 4f69949 commit 3d762af

7 files changed

+140
-35
lines changed

.travis.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: erlang
2+
matrix:
3+
include:
4+
- os: linux
5+
dist: xenial
6+
otp_release: 21.1
7+
- os: linux
8+
dist: xenial
9+
otp_release: 20.3.8.5
10+
- os: linux
11+
dist: xenial
12+
otp_release: 19.3
13+
- os: osx
14+
language: generic
15+
before_script:
16+
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
17+
- if [[ $TRAVIS_OS_NAME == osx ]]; then brew install erlang || true; fi
18+
- curl https://s3.amazonaws.com/rebar3/rebar3 --output rebar3 && chmod +x rebar3
19+
script:
20+
- ./rebar3 compile

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Silviu Caragea
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
C_SRC_DIR = $(shell pwd)/c_src
2+
C_SRC_ENV ?= $(C_SRC_DIR)/env.mk
3+
4+
#regenerate all the time the env.mk
5+
ifneq ($(wildcard $(C_SRC_DIR)),)
6+
GEN_ENV ?= $(shell erl -noshell -s init stop -eval "file:write_file(\"$(C_SRC_ENV)\", \
7+
io_lib:format( \
8+
\"ERTS_INCLUDE_DIR ?= ~s/erts-~s/include/~n\" \
9+
\"ERL_INTERFACE_INCLUDE_DIR ?= ~s~n\" \
10+
\"ERL_INTERFACE_LIB_DIR ?= ~s~n\", \
11+
[code:root_dir(), erlang:system_info(version), \
12+
code:lib_dir(erl_interface, include), \
13+
code:lib_dir(erl_interface, lib)])), \
14+
halt().")
15+
$(GEN_ENV)
16+
endif
17+
18+
include $(C_SRC_ENV)
19+
20+
cpplint:
21+
cpplint --counting=detailed \
22+
--filter=-legal/copyright,-build/include_subdir,-build/include_order,-whitespace/blank_line,-whitespace/braces,-whitespace/indent,-whitespace/parens,-whitespace/newline \
23+
--linelength=300 \
24+
--exclude=c_src/*.o --exclude=c_src/*.mk \
25+
c_src/*.*
26+
27+
cppcheck:
28+
cppcheck -j 8 \
29+
-I $(ERTS_INCLUDE_DIR) \
30+
-I $(ERL_INTERFACE_INCLUDE_DIR) \
31+
--force \
32+
--enable=all \
33+
--xml-version=2 \
34+
--output-file=cppcheck_results.xml \
35+
c_src/

rebar.config

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
{pre_hooks, [{"(linux|darwin)", compile, "make V=0 -C c_src -j 8"}]}.
22
{post_hooks, [{"(linux|darwin)", clean, "make -C c_src clean"}]}.
33

4+
{artifacts, ["priv/epqueue_nif.so"]}.
5+
46
%because enif_binary_to_term and enif_term_to_binary are not available in previous versions.
57
%this can be changed if it's required by moving the encoding/decoding in erlang and in NIF only store the received binary
6-
{require_min_otp_vsn, "R19.*"}.
8+
{require_min_otp_vsn, "R19.*"}.
9+
10+
{project_plugins, [rebar3_hex]}.
11+
12+
{deps, []}.
13+
14+
{erl_opts, [
15+
warnings_as_errors,
16+
warn_export_all
17+
]}.

src/epqueue.app.src

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{application, epqueue, [
22
{description, "Erlang Priority Queue"},
3-
{vsn, "1.1"},
3+
{vsn, "1.2.0"},
44
{registered, []},
5-
{applications, [kernel, stdlib]},
5+
{applications, [
6+
kernel,
7+
stdlib
8+
]},
69
{env, []}
710
]}.

src/epqueue.erl

+32-16
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,60 @@
11
-module(epqueue).
2-
-author("silviu.caragea").
32

4-
-export([new/0, new/1, size/1, insert/3, remove/2, pop/1, peek/1]).
5-
6-
7-
-spec(new() -> {ok, QueueRef::reference()} | badarg | {error, Reason :: binary()}).
3+
-export([
4+
new/0,
5+
new/1,
6+
size/1,
7+
insert/3,
8+
remove/2,
9+
pop/1,
10+
peek/1
11+
]).
12+
13+
-type error() :: badarg | {error, binary()}.
14+
-type queue_option() :: {global_lock, boolean()}.
15+
-type priority() :: non_neg_integer().
16+
-type data() :: any().
17+
-type queue_ref() :: reference().
18+
-type data_ref() :: reference().
19+
20+
-spec new() ->
21+
{ok, queue_ref()} | error().
822

923
new() ->
1024
epqueue_nif:new([]).
1125

12-
-spec(new(Options::list()) -> {ok, QueueRef :: reference()} | badarg | {error, Reason :: binary()}).
26+
-spec new([queue_option()]) ->
27+
{ok, queue_ref()} | error().
1328

1429
new(Options) ->
1530
epqueue_nif:new(Options).
1631

17-
-spec(size(QueueRef::reference()) -> badarg | integer()).
32+
-spec size(queue_ref()) ->
33+
non_neg_integer() | badarg.
1834

1935
size(QueueRef) ->
2036
epqueue_nif:size(QueueRef).
2137

22-
-spec(insert(QueueRef::reference(), Data::term(), Priority::integer()) ->
23-
{ok, ItemRef :: reference()} | badarg | {error, Reason :: binary()}).
38+
-spec insert(queue_ref(), data(), priority()) ->
39+
{ok, data_ref()} | error().
2440

2541
insert(QueueRef, Data, Priority) ->
2642
epqueue_nif:insert(QueueRef, Data, Priority).
2743

28-
-spec(remove(QueueRef::reference(), Ref::reference()) ->
29-
boolean() | badarg | {error, Reason :: binary()}).
44+
-spec remove(queue_ref(), data_ref()) ->
45+
boolean() | error().
3046

3147
remove(QueueRef, Ref) ->
3248
epqueue_nif:remove(QueueRef, Ref).
3349

34-
-spec(pop(QueueRef::reference()) ->
35-
{ok, Data::term(), Priority::integer()} | badarg | {error, Reason :: binary()}).
50+
-spec pop(queue_ref()) ->
51+
{ok, data(), priority()} | error().
3652

3753
pop(QueueRef) ->
3854
epqueue_nif:pop(QueueRef).
3955

40-
-spec(peek(QueueRef::reference()) ->
41-
{ok, Data::term(), Priority::integer()} | badarg | {error, Reason :: binary()}).
56+
-spec peek(queue_ref()) ->
57+
{ok, data(), priority()} | error().
4258

4359
peek(QueueRef) ->
44-
epqueue_nif:peek(QueueRef).
60+
epqueue_nif:peek(QueueRef).

src/epqueue_nif.erl

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
-module(epqueue_nif).
2-
-author("silviu.caragea").
32

43
-define(NOT_LOADED, not_loaded(?LINE)).
54

@@ -14,25 +13,11 @@
1413
peek/1
1514
]).
1615

17-
%% nif functions
18-
1916
load_nif() ->
2017
SoName = get_priv_path(?MODULE),
2118
io:format(<<"Loading library: ~p ~n">>, [SoName]),
2219
ok = erlang:load_nif(SoName, 0).
2320

24-
get_priv_path(File) ->
25-
case code:priv_dir(epqueue) of
26-
{error, bad_name} ->
27-
Ebin = filename:dirname(code:which(?MODULE)),
28-
filename:join([filename:dirname(Ebin), "priv", File]);
29-
Dir ->
30-
filename:join(Dir, File)
31-
end.
32-
33-
not_loaded(Line) ->
34-
erlang:nif_error({not_loaded, [{module, ?MODULE}, {line, Line}]}).
35-
3621
new(_Options) ->
3722
?NOT_LOADED.
3823

@@ -49,4 +34,18 @@ pop(_QueueRef) ->
4934
?NOT_LOADED.
5035

5136
peek(_QueueRef) ->
52-
?NOT_LOADED.
37+
?NOT_LOADED.
38+
39+
% internals
40+
41+
get_priv_path(File) ->
42+
case code:priv_dir(epqueue) of
43+
{error, bad_name} ->
44+
Ebin = filename:dirname(code:which(?MODULE)),
45+
filename:join([filename:dirname(Ebin), "priv", File]);
46+
Dir ->
47+
filename:join(Dir, File)
48+
end.
49+
50+
not_loaded(Line) ->
51+
erlang:nif_error({not_loaded, [{module, ?MODULE}, {line, Line}]}).

0 commit comments

Comments
 (0)