Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

100 řádky
3.8 KiB

před 12 roky
před 12 roky
před 14 roky
  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. %% for internal use only
  32. -export([info/2]).
  33. -include("rebar.hrl").
  34. %% ===================================================================
  35. %% Public API
  36. %% ===================================================================
  37. -spec compile(rebar_config:config(), file:filename()) -> 'ok'.
  38. compile(Config, _AppFile) ->
  39. rebar_base_compiler:run(Config, filelib:wildcard("asn1/*.asn1"),
  40. "asn1", ".asn1", "src", ".erl",
  41. fun compile_asn1/3).
  42. -spec clean(rebar_config:config(), file:filename()) -> 'ok'.
  43. clean(_Config, _AppFile) ->
  44. GeneratedFiles = asn_generated_files("asn1", "src", "include"),
  45. ok = rebar_file_utils:delete_each(GeneratedFiles),
  46. ok.
  47. %% ===================================================================
  48. %% Internal functions
  49. %% ===================================================================
  50. info(help, compile) ->
  51. info_help("Build ASN.1 (*.asn1) sources");
  52. info(help, clean) ->
  53. info_help("Delete ASN.1 (*.asn1) results").
  54. info_help(Description) ->
  55. ?CONSOLE(
  56. "~s.~n"
  57. "~n"
  58. "Valid rebar.config options:~n"
  59. " {asn1_opts, []} (see asn1ct:compile/2 documentation)~n",
  60. [Description]).
  61. -spec compile_asn1(file:filename(), file:filename(),
  62. rebar_config:config()) -> ok.
  63. compile_asn1(Source, Target, Config) ->
  64. ok = filelib:ensure_dir(Target),
  65. ok = filelib:ensure_dir(filename:join("include", "dummy.hrl")),
  66. Opts = [{outdir, "src"}, noobj] ++ rebar_config:get(Config, asn1_opts, []),
  67. case asn1ct:compile(Source, Opts) of
  68. ok ->
  69. Asn1 = filename:basename(Source, ".asn1"),
  70. HrlFile = filename:join("src", Asn1 ++ ".hrl"),
  71. case filelib:is_regular(HrlFile) of
  72. true ->
  73. ok = rebar_file_utils:mv(HrlFile, "include");
  74. false ->
  75. ok
  76. end;
  77. {error, _Reason} ->
  78. ?FAIL
  79. end.
  80. asn_generated_files(AsnDir, SrcDir, IncDir) ->
  81. lists:foldl(
  82. fun(AsnFile, Acc) ->
  83. Base = filename:rootname(filename:basename(AsnFile)),
  84. [filename:join([IncDir, Base ++ ".hrl"])|
  85. filelib:wildcard(filename:join([SrcDir, Base ++ ".*"]))] ++ Acc
  86. end,
  87. [],
  88. filelib:wildcard(filename:join([AsnDir, "*.asn1"]))
  89. ).