Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

164 рядки
6.2 KiB

10 роки тому
14 роки тому
14 роки тому
14 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
10 роки тому
  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_base_compiler).
  28. -include("rebar.hrl").
  29. -export([run/4,
  30. run/7,
  31. run/8,
  32. ok_tuple/3,
  33. error_tuple/5]).
  34. %% ===================================================================
  35. %% Public API
  36. %% ===================================================================
  37. run(Config, FirstFiles, RestFiles, CompileFn) ->
  38. %% Compile the first files in sequence
  39. compile_each(FirstFiles++RestFiles, Config, CompileFn).
  40. run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
  41. Compile3Fn) ->
  42. run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
  43. Compile3Fn, [check_last_mod]).
  44. run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
  45. Compile3Fn, Opts) ->
  46. %% Convert simple extension to proper regex
  47. SourceExtRe = "^[^._].*\\" ++ SourceExt ++ [$$],
  48. Recursive = proplists:get_value(recursive, Opts, true),
  49. %% Find all possible source files
  50. FoundFiles = rebar_utils:find_files(SourceDir, SourceExtRe, Recursive),
  51. %% Remove first files from found files
  52. RestFiles = [Source || Source <- FoundFiles,
  53. not lists:member(Source, FirstFiles)],
  54. %% Check opts for flag indicating that compile should check lastmod
  55. CheckLastMod = proplists:get_bool(check_last_mod, Opts),
  56. run(Config, FirstFiles, RestFiles,
  57. fun(S, C) ->
  58. Target = target_file(S, SourceDir, SourceExt,
  59. TargetDir, TargetExt),
  60. simple_compile_wrapper(S, Target, Compile3Fn, C, CheckLastMod)
  61. end).
  62. ok_tuple(_Config, Source, Ws) ->
  63. {ok, format_warnings(Source, Ws)}.
  64. error_tuple(_Config, Source, Es, Ws, Opts) ->
  65. {error, format_errors(Source, Es),
  66. format_warnings(Source, Ws, Opts)}.
  67. %% ===================================================================
  68. %% Internal functions
  69. %% ===================================================================
  70. simple_compile_wrapper(Source, Target, Compile3Fn, Config, false) ->
  71. Compile3Fn(Source, Target, Config);
  72. simple_compile_wrapper(Source, Target, Compile3Fn, Config, true) ->
  73. case filelib:last_modified(Target) < filelib:last_modified(Source) of
  74. true ->
  75. Compile3Fn(Source, Target, Config);
  76. false ->
  77. skipped
  78. end.
  79. target_file(SourceFile, SourceDir, SourceExt, TargetDir, TargetExt) ->
  80. BaseFile = remove_common_path(SourceFile, SourceDir),
  81. filename:join([TargetDir, filename:basename(BaseFile, SourceExt) ++ TargetExt]).
  82. remove_common_path(Fname, Path) ->
  83. remove_common_path1(filename:split(Fname), filename:split(Path)).
  84. remove_common_path1([Part | RestFilename], [Part | RestPath]) ->
  85. remove_common_path1(RestFilename, RestPath);
  86. remove_common_path1(FilenameParts, _) ->
  87. filename:join(FilenameParts).
  88. compile_each([], _Config, _CompileFn) ->
  89. ok;
  90. compile_each([Source | Rest], Config, CompileFn) ->
  91. case CompileFn(Source, Config) of
  92. ok ->
  93. ?DEBUG("~sCompiled ~s", [rebar_utils:indent(1), filename:basename(Source)]);
  94. {ok, Warnings} ->
  95. report(Warnings),
  96. ?DEBUG("~sCompiled ~s", [rebar_utils:indent(1), filename:basename(Source)]);
  97. skipped ->
  98. ?DEBUG("~sSkipped ~s", [rebar_utils:indent(1), filename:basename(Source)]);
  99. Error ->
  100. ?ERROR("Compiling ~s failed", [Source]),
  101. maybe_report(Error),
  102. ?DEBUG("Compilation failed: ~p", [Error]),
  103. ?FAIL
  104. end,
  105. compile_each(Rest, Config, CompileFn).
  106. format_errors(Source, Errors) ->
  107. format_errors(Source, "", Errors).
  108. format_warnings(Source, Warnings) ->
  109. format_warnings(Source, Warnings, []).
  110. format_warnings(Source, Warnings, Opts) ->
  111. Prefix = case lists:member(warnings_as_errors, Opts) of
  112. true -> "";
  113. false -> "Warning: "
  114. end,
  115. format_errors(Source, Prefix, Warnings).
  116. maybe_report({{error, {error, _Es, _Ws}=ErrorsAndWarnings}, {source, _}}) ->
  117. maybe_report(ErrorsAndWarnings);
  118. maybe_report([{error, E}, {source, S}]) ->
  119. report(["unexpected error compiling " ++ S, io_lib:fwrite("~n~p", [E])]);
  120. maybe_report({error, Es, Ws}) ->
  121. report(Es),
  122. report(Ws);
  123. maybe_report(_) ->
  124. ok.
  125. report(Messages) ->
  126. lists:foreach(fun(Msg) -> io:format("~s~n", [Msg]) end, Messages).
  127. format_errors(_MainSource, Extra, Errors) ->
  128. [begin
  129. [format_error(Source, Extra, Desc) || Desc <- Descs]
  130. end
  131. || {Source, Descs} <- Errors].
  132. format_error(AbsSource, Extra, {{Line, Column}, Mod, Desc}) ->
  133. ErrorDesc = Mod:format_error(Desc),
  134. ?FMT("~s:~w:~w: ~s~s~n", [AbsSource, Line, Column, Extra, ErrorDesc]);
  135. format_error(AbsSource, Extra, {Line, Mod, Desc}) ->
  136. ErrorDesc = Mod:format_error(Desc),
  137. ?FMT("~s:~w: ~s~s~n", [AbsSource, Line, Extra, ErrorDesc]);
  138. format_error(AbsSource, Extra, {Mod, Desc}) ->
  139. ErrorDesc = Mod:format_error(Desc),
  140. ?FMT("~s: ~s~s~n", [AbsSource, Extra, ErrorDesc]).