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