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
|
-module(rebar_compiler_mib).
-behaviour(rebar_compiler).
-export([context/1,
needed_files/4,
dependencies/3,
compile/4,
clean/2]).
-include("rebar.hrl").
-include_lib("stdlib/include/erl_compile.hrl").
context(AppInfo) ->
Dir = rebar_app_info:dir(AppInfo),
Mappings = [{".bin", filename:join([Dir, "priv", "mibs"])},
{".hrl", filename:join(Dir, "include")}],
#{src_dirs => ["mibs"],
include_dirs => [],
src_ext => ".mib",
out_mappings => Mappings}.
needed_files(_, FoundFiles, _, AppInfo) ->
RebarOpts = rebar_app_info:opts(AppInfo),
MibFirstConf = rebar_opts:get(RebarOpts, mib_first_files, []),
valid_mib_first_conf(MibFirstConf),
Dir = rebar_app_info:dir(AppInfo),
MibFirstFiles = [filename:join(Dir, File) || File <- MibFirstConf],
%% Remove first files from found files
RestFiles = [Source || Source <- FoundFiles, not lists:member(Source, MibFirstFiles)],
Opts = rebar_opts:get(rebar_app_info:opts(AppInfo), mib_opts, []),
{{MibFirstFiles, Opts}, {RestFiles, Opts}}.
valid_mib_first_conf(FileList) ->
Strs = filter_file_list(FileList),
case rebar_utils:is_list_of_strings(Strs) of
true -> true;
false -> ?ABORT("An invalid file list (~p) was provided as part of your mib_first_files directive",
[FileList])
end.
filter_file_list(FileList) ->
Atoms = lists:filter( fun(X) -> is_atom(X) end, FileList),
case Atoms of
[] ->
FileList;
_ ->
atoms_in_mib_first_files_warning(Atoms),
lists:filter( fun(X) -> not(is_atom(X)) end, FileList)
end.
atoms_in_mib_first_files_warning(Atoms) ->
W = "You have provided atoms as file entries in mib_first_files; "
"mib_first_files only expects lists of filenames as strings. "
"The following MIBs (~p) may not work as expected and it is advised "
"that you change these entires to string format "
"(e.g., \"mibs/SOME-MIB.mib\") ",
?WARN(W, [Atoms]).
dependencies(_, _, _) ->
[].
compile(Source, OutDirs, _, Opts) ->
{_, BinOut} = lists:keyfind(".bin", 1, OutDirs),
{_, HrlOut} = lists:keyfind(".hrl", 1, OutDirs),
ok = rebar_file_utils:ensure_dir(BinOut),
ok = rebar_file_utils:ensure_dir(HrlOut),
Mib = filename:join(BinOut, filename:basename(Source, ".mib")),
HrlFilename = Mib ++ ".hrl",
AllOpts = [{outdir, BinOut}, {i, [BinOut]}] ++ Opts,
case snmpc:compile(Source, AllOpts) of
{ok, _} ->
MibToHrlOpts =
case proplists:get_value(verbosity, AllOpts, undefined) of
undefined ->
#options{specific = [],
cwd = rebar_dir:get_cwd()};
Verbosity ->
#options{specific = [{verbosity, Verbosity}],
cwd = rebar_dir:get_cwd()}
end,
ok = snmpc:mib_to_hrl(Mib, Mib, MibToHrlOpts),
rebar_file_utils:mv(HrlFilename, HrlOut),
ok;
{error, compilation_failed} ->
?FAIL
end.
clean(MibFiles, AppInfo) ->
AppDir = rebar_app_info:dir(AppInfo),
MIBs = [filename:rootname(filename:basename(MIB)) || MIB <- MibFiles],
rebar_file_utils:delete_each(
[filename:join([AppDir, "include", MIB++".hrl"]) || MIB <- MIBs]),
ok = rebar_file_utils:rm_rf(filename:join([AppDir, "priv/mibs/*.bin"])).
|