-
Notifications
You must be signed in to change notification settings - Fork 48
/
rebar.config.script
125 lines (115 loc) · 4.94 KB
/
rebar.config.script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
%% This script tries to discover which library can be used
%% to support curve encryption (if any). Curve encryption is
%% used by the CURVE security model.
%%
%% If the CHUMAK_CURVE_LIB environment variable is set,
%% that determines what is used. Supported values are:
%% - nacerl - this is the minimal variant using tweetnacl.
%% https://github.com/willemdj/NaCerl
%% - nacl - this is similar to nacerl, but it depends on libsodium.
%% https://github.com/tonyg/erlang-nacl
%% - enacl - this also depends on libsodium, but it also requires
%% an Erlang VM that supports dirty schedulers.
%% https://github.com/jlouis/enacl
%% - none - no support for the CURVE security model.
%%
%% If the CHUMAK_CURVE_LIB environment variable is _not_ set,
%% nacerl will be used. On Windows an additional check will be
%% performed first: if gcc and make are not available, `none` will
%% be assumed.
%%
%% Since it is not possible to reliably and completely build any of
%% the options that depend on libsodium from rebar3, only in the `nacerl`
%% case the library will be fetched and built automatically as a
%% dependency. In the other cases you will have to make the library available
%% via other means.
%%
%% The selected option ("nacl", "enacl", "nacerl" or "none") will be
%% passed as a macro to the compiler. Based on this macro it is
%% determined which library is used by the code.
%% Some of the ideas used here were found in
%% https://github.com/klacke/yaws/blob/master/rebar.config.script
%% and
%% https://github.com/dgud/esdl/blob/master/rebar.config.script
UpdateCfg = fun(Config, _Key, undefined) ->
Config;
(Config, Key, NewVal) ->
case lists:keyfind(Key, 1, Config) of
{Key, Vals} ->
NVals = [NewVal | Vals],
lists:keyreplace(Key, 1, Config, {Key, NVals});
false ->
Config ++ [{Key, [NewVal]}]
end
end,
NaCerlDep = {nacerl, ".*", {git, "https://github.com/willemdj/NaCerl.git", {branch, "master"}}},
IsAvailable =
fun(Executable) ->
case os:find_executable(Executable) of
false ->
io:format("Building Chumak with curve support on windows"
" requires ~p~n", [Executable]),
false;
_ ->
true
end
end,
AllAvailable =
fun(Executables) ->
lists:all(fun(Bool) -> Bool end, [IsAvailable(E) || E <- Executables])
end,
%% The only dependency where it makes sense to fecth and build it
%% from here, is NaCerl. The other ones need libsodium and must be installed
%% as a separate step.
DetermineCurveDep =
fun() ->
case os:type() of
{win32, _} ->
case AllAvailable(["gcc", "make"]) of
true ->
NaCerlDep;
false ->
io:format("not all required tools are available to build "
"curve security mechanism.~n"),
undefined
end;
_ ->
undefined
end
end,
%% If the CHUMAK_CURVE_LIB environment variable is set, that determines
%% what is used.
CurveLibEnv = os:getenv("CHUMAK_CURVE_LIB"),
CurveDep = case CurveLibEnv of
false ->
undefined;
Val ->
case string:to_lower(Val) of
"nacerl" ->
NaCerlDep;
_ ->
DetermineCurveDep()
end
end,
CurveCompilerOption = case CurveDep of
NaCerlDep ->
{d, 'CHUMAK_CURVE_LIB_NACERL', true};
_ ->
case CurveLibEnv of
false ->
undefined;
Value ->
case string:to_lower(Value) of
"nacerl" ->
{d, 'CHUMAK_CURVE_LIB_NACERL', true};
"nacl" ->
{d, 'CHUMAK_CURVE_LIB_NACL', true};
"enacl" ->
{d, 'CHUMAK_CURVE_LIB_ENACL', true};
"none" ->
undefined
end
end
end,
Config2 = UpdateCfg(CONFIG, erl_opts, CurveCompilerOption),
UpdateCfg(Config2, deps, CurveDep).