Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

153 строки
5.4 KiB

14 лет назад
14 лет назад
14 лет назад
14 лет назад
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 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_protobuffs_compiler).
  28. -export([compile/2,
  29. clean/2]).
  30. %% for internal use only
  31. -export([info/2]).
  32. -include("rebar.hrl").
  33. %% ===================================================================
  34. %% Public API
  35. %% ===================================================================
  36. compile(Config, _AppFile) ->
  37. case rebar_utils:find_files("src", ".*\\.proto$") of
  38. [] ->
  39. ok;
  40. FoundFiles ->
  41. %% Check for protobuffs library -- if it's not present, fail
  42. %% since we have.proto files that need building
  43. case protobuffs_is_present() of
  44. true ->
  45. %% Build a list of output files - { Proto, Beam, Hrl }
  46. Targets = [{Proto, beam_file(Proto), hrl_file(Proto)} ||
  47. Proto <- FoundFiles],
  48. %% Compile each proto file
  49. compile_each(Config, Targets);
  50. false ->
  51. ?ERROR("Protobuffs library not present in code path!\n",
  52. []),
  53. ?FAIL
  54. end
  55. end.
  56. clean(_Config, _AppFile) ->
  57. %% Get a list of generated .beam and .hrl files and then delete them
  58. Protos = rebar_utils:find_files("src", ".*\\.proto$"),
  59. BeamFiles = [fq_beam_file(F) || F <- Protos],
  60. HrlFiles = [fq_hrl_file(F) || F <- Protos],
  61. Targets = BeamFiles ++ HrlFiles,
  62. case Targets of
  63. [] ->
  64. ok;
  65. _ ->
  66. delete_each(Targets)
  67. end.
  68. %% ===================================================================
  69. %% Internal functions
  70. %% ===================================================================
  71. info(help, compile) ->
  72. info_help("Build Protobuffs (*.proto) sources");
  73. info(help, clean) ->
  74. info_help("Delete Protobuffs (*.proto) build results").
  75. info_help(Description) ->
  76. ?CONSOLE(
  77. "~s.~n"
  78. "~n"
  79. "Valid rebar.config options:~n"
  80. " erl_opts is passed as compile_flags to "
  81. "protobuffs_compile:scan_file/2~n",
  82. [Description]).
  83. protobuffs_is_present() ->
  84. code:which(protobuffs_compile) =/= non_existing.
  85. beam_file(Proto) ->
  86. filename:basename(Proto, ".proto") ++ "_pb.beam".
  87. hrl_file(Proto) ->
  88. filename:basename(Proto, ".proto") ++ "_pb.hrl".
  89. fq_beam_file(Proto) ->
  90. filename:join(["ebin", filename:basename(Proto, ".proto") ++ "_pb.beam"]).
  91. fq_hrl_file(Proto) ->
  92. filename:join(["include", filename:basename(Proto, ".proto") ++ "_pb.hrl"]).
  93. needs_compile(Proto, Beam) ->
  94. ActualBeam = filename:join(["ebin", filename:basename(Beam)]),
  95. filelib:last_modified(ActualBeam) < filelib:last_modified(Proto).
  96. compile_each(_, []) ->
  97. ok;
  98. compile_each(Config, [{Proto, Beam, Hrl} | Rest]) ->
  99. case needs_compile(Proto, Beam) of
  100. true ->
  101. ?CONSOLE("Compiling ~s\n", [Proto]),
  102. ErlOpts = rebar_utils:erl_opts(Config),
  103. case protobuffs_compile:scan_file(Proto,
  104. [{compile_flags,ErlOpts}]) of
  105. ok ->
  106. %% Compilation worked, but we need to move the
  107. %% beam and .hrl file into the ebin/ and include/
  108. %% directories respectively
  109. %% TODO: Protobuffs really needs to be better about this
  110. ok = filelib:ensure_dir(filename:join("ebin","dummy")),
  111. ok = rebar_file_utils:mv(Beam, "ebin"),
  112. ok = filelib:ensure_dir(filename:join("include", Hrl)),
  113. ok = rebar_file_utils:mv(Hrl, "include"),
  114. ok;
  115. Other ->
  116. ?ERROR("Protobuffs compile of ~s failed: ~p\n",
  117. [Proto, Other]),
  118. ?FAIL
  119. end;
  120. false ->
  121. ok
  122. end,
  123. compile_each(Config, Rest).
  124. delete_each([]) ->
  125. ok;
  126. delete_each([File | Rest]) ->
  127. case file:delete(File) of
  128. ok ->
  129. ok;
  130. {error, enoent} ->
  131. ok;
  132. {error, Reason} ->
  133. ?ERROR("Failed to delete ~s: ~p\n", [File, Reason])
  134. end,
  135. delete_each(Rest).