You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.4 KiB

  1. -module(rebar_compiler_mib).
  2. -behaviour(rebar_compiler).
  3. -export([context/1,
  4. needed_files/4,
  5. dependencies/3,
  6. compile/4,
  7. clean/2]).
  8. -include("rebar.hrl").
  9. -include_lib("stdlib/include/erl_compile.hrl").
  10. context(AppInfo) ->
  11. Dir = rebar_app_info:dir(AppInfo),
  12. Mappings = [{".bin", filename:join([Dir, "priv", "mibs"])},
  13. {".hrl", filename:join(Dir, "include")}],
  14. #{src_dirs => ["mibs"],
  15. include_dirs => [],
  16. src_ext => ".mib",
  17. out_mappings => Mappings}.
  18. needed_files(_, FoundFiles, _, AppInfo) ->
  19. RebarOpts = rebar_app_info:opts(AppInfo),
  20. MibFirstConf = rebar_opts:get(RebarOpts, mib_first_files, []),
  21. valid_mib_first_conf(MibFirstConf),
  22. Dir = rebar_app_info:dir(AppInfo),
  23. MibFirstFiles = [filename:join(Dir, File) || File <- MibFirstConf],
  24. %% Remove first files from found files
  25. RestFiles = [Source || Source <- FoundFiles, not lists:member(Source, MibFirstFiles)],
  26. Opts = rebar_opts:get(rebar_app_info:opts(AppInfo), mib_opts, []),
  27. {{MibFirstFiles, Opts}, {RestFiles, Opts}}.
  28. valid_mib_first_conf(FileList) ->
  29. Strs = filter_file_list(FileList),
  30. case rebar_utils:is_list_of_strings(Strs) of
  31. true -> true;
  32. false -> ?ABORT("An invalid file list (~p) was provided as part of your mib_first_files directive",
  33. [FileList])
  34. end.
  35. filter_file_list(FileList) ->
  36. Atoms = lists:filter( fun(X) -> is_atom(X) end, FileList),
  37. case Atoms of
  38. [] ->
  39. FileList;
  40. _ ->
  41. atoms_in_mib_first_files_warning(Atoms),
  42. lists:filter( fun(X) -> not(is_atom(X)) end, FileList)
  43. end.
  44. atoms_in_mib_first_files_warning(Atoms) ->
  45. W = "You have provided atoms as file entries in mib_first_files; "
  46. "mib_first_files only expects lists of filenames as strings. "
  47. "The following MIBs (~p) may not work as expected and it is advised "
  48. "that you change these entires to string format "
  49. "(e.g., \"mibs/SOME-MIB.mib\") ",
  50. ?WARN(W, [Atoms]).
  51. dependencies(_, _, _) ->
  52. [].
  53. compile(Source, OutDirs, _, Opts) ->
  54. {_, BinOut} = lists:keyfind(".bin", 1, OutDirs),
  55. {_, HrlOut} = lists:keyfind(".hrl", 1, OutDirs),
  56. ok = rebar_file_utils:ensure_dir(BinOut),
  57. ok = rebar_file_utils:ensure_dir(HrlOut),
  58. Mib = filename:join(BinOut, filename:basename(Source, ".mib")),
  59. HrlFilename = Mib ++ ".hrl",
  60. AllOpts = [{outdir, BinOut}, {i, [BinOut]}] ++ Opts,
  61. case snmpc:compile(Source, AllOpts) of
  62. {ok, _} ->
  63. MibToHrlOpts =
  64. case proplists:get_value(verbosity, AllOpts, undefined) of
  65. undefined ->
  66. #options{specific = [],
  67. cwd = rebar_dir:get_cwd()};
  68. Verbosity ->
  69. #options{specific = [{verbosity, Verbosity}],
  70. cwd = rebar_dir:get_cwd()}
  71. end,
  72. ok = snmpc:mib_to_hrl(Mib, Mib, MibToHrlOpts),
  73. rebar_file_utils:mv(HrlFilename, HrlOut),
  74. ok;
  75. {error, compilation_failed} ->
  76. ?FAIL
  77. end.
  78. clean(MibFiles, AppInfo) ->
  79. AppDir = rebar_app_info:dir(AppInfo),
  80. MIBs = [filename:rootname(filename:basename(MIB)) || MIB <- MibFiles],
  81. rebar_file_utils:delete_each(
  82. [filename:join([AppDir, "include", MIB++".hrl"]) || MIB <- MIBs]),
  83. ok = rebar_file_utils:rm_rf(filename:join([AppDir, "priv/mibs/*.bin"])).