Skip to content

Commit

Permalink
rebar3.config.script: remove duplicate flags
Browse files Browse the repository at this point in the history
Avoid appending redundant compiler flags.

Use a port to spawn cc to retrieve the exit status. Store the test code
for the pcap_create test in /tmp by default (set the TMPDIR environment
variable to change this).
  • Loading branch information
msantos committed Jul 21, 2016
1 parent 5f43414 commit d25aeb4
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions rebar.config.script
@@ -1,18 +1,50 @@
TMPDIR = os:getenv("TMPDIR", "/tmp"),

Compile = fun(Name0, Prog) ->
Name = filename:join(TMPDIR, Name0),
ok = file:write_file(Name, Prog, [write, exclusive]),
Cmd = erlang:open_port({spawn, ["${CC-cc} -o /dev/null ", Name, " -lpcap"]},
[stream, exit_status]),
Status = receive
{Cmd, {exit_status, 0}} ->
true;
{Cmd, {exit_status, _}} ->
false
end,
ok = file:delete(Name),
Status
end,

Test = fun(Name, Prog, Supported, Unsupported) ->
case Compile(Name, Prog) of
true ->
Supported;
false ->
Unsupported
end
end,

Append = fun(Str, Flag) ->
string:join(sets:to_list(sets:add_element(Flag,
sets:from_list(string:tokens(Str, " ")))), " ")
end,

PFRING = fun(CONFIG) -> case os:getenv("PFRING") of
false ->
ok;
Value ->
case filelib:is_dir(Value) of
true ->
LDFLAGS = os:getenv("EPCAP_LDFLAGS", ""),
os:putenv("EPCAP_LDFLAGS",
string:join([LDFLAGS, "-L", Value], " "));
true = os:putenv("EPCAP_LDFLAGS",
Append(LDFLAGS, "-L" ++ Value));
_ ->
ok
end
end,
CONFIG
end,

Pcap_create = fun(CONFIG) ->
Check = "
#include <pcap.h>
Expand All @@ -22,18 +54,13 @@ int main(int argc, char *argv[])
(void)pcap_create(NULL, errbuf);
return 0;
}",
ok = file:write_file("test_pcap_create.c", Check),
Retval = os:cmd("cc -o /dev/null test_pcap_create.c -lpcap > /dev/null 2>&1; printf \"%d\" $?"),
file:delete("test_pcap_create.c"),
case Retval of
"0" ->
CFLAGS = os:getenv("EPCAP_CFLAGS", ""),
os:putenv("EPCAP_CFLAGS", CFLAGS ++ " -DHAVE_PCAP_CREATE");
_ ->
ok
end,

Flag = Test("test_pcap_create.c", Check, "-DHAVE_PCAP_CREATE", ""),
Define = os:getenv("EPCAP_CFLAGS", ""),
true = os:putenv("EPCAP_CFLAGS", Append(Define, Flag)),
CONFIG
end,

lists:foldl(fun(Fun, Cfg) ->
Fun(Cfg)
end,
Expand Down

0 comments on commit d25aeb4

Please sign in to comment.