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.

134 line
4.6 KiB

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