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.

70 line
2.8 KiB

14 年之前
14 年之前
14 年之前
  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% Copyright (c) 2009, 2010 Dave Smith (dizzyd@dizzyd.com)
  8. %%
  9. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  10. %% of this software and associated documentation files (the "Software"), to deal
  11. %% in the Software without restriction, including without limitation the rights
  12. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. %% copies of the Software, and to permit persons to whom the Software is
  14. %% furnished to do so, subject to the following conditions:
  15. %%
  16. %% The above copyright notice and this permission notice shall be included in
  17. %% all copies or substantial portions of the Software.
  18. %%
  19. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. %% THE SOFTWARE.
  26. %% -------------------------------------------------------------------
  27. -module(rebar_asn1_compiler).
  28. -author('ruslan@babayev.com').
  29. -export([compile/2,
  30. clean/2]).
  31. -include("rebar.hrl").
  32. %% ===================================================================
  33. %% Public API
  34. %% ===================================================================
  35. -spec compile(Config::rebar_config:config(), AppFile::file:filename()) -> 'ok'.
  36. compile(Config, _AppFile) ->
  37. rebar_base_compiler:run(Config, filelib:wildcard("asn1/*.asn1"),
  38. "asn1", ".asn1", "src", ".erl",
  39. fun compile_asn1/3).
  40. -spec clean(Config::rebar_config:config(), AppFile::file:filename()) -> 'ok'.
  41. clean(_Config, _AppFile) ->
  42. rebar_file_utils:delete_each(asn_generated_files("asn1", "src")),
  43. ok.
  44. -spec compile_asn1(file:filename(), file:filename(),
  45. rebar_config:config()) -> ok.
  46. compile_asn1(Source, Target, Config) ->
  47. ok = rebar_utils:ensure_dir(Target),
  48. Opts = [{outdir, "src"}, noobj] ++ rebar_config:get(Config, asn1_opts, []),
  49. case asn1ct:compile(Source, Opts) of
  50. ok ->
  51. ok;
  52. {error, _Reason} ->
  53. ?FAIL
  54. end.
  55. asn_generated_files(AsnDir, SrcDir) ->
  56. lists:foldl(
  57. fun(AsnFile, Acc) ->
  58. Base = filename:rootname(filename:basename(AsnFile)),
  59. filelib:wildcard(filename:join([SrcDir, Base ++ ".*"])) ++ Acc
  60. end,
  61. [],
  62. filelib:wildcard(filename:join([AsnDir, "*.asn1"]))
  63. ).