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.

136 rivejä
4.9 KiB

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