您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

258 行
9.4 KiB

  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, Config, CompileFn),
  40. %% Spin up workers for the rest of the files
  41. case RestFiles of
  42. [] ->
  43. ok;
  44. _ ->
  45. Self = self(),
  46. F = fun() -> compile_worker(Self, Config, CompileFn) end,
  47. Jobs = rebar_state:get(Config, jobs, 3),
  48. ?DEBUG("Starting ~B compile worker(s)", [Jobs]),
  49. Pids = [spawn_monitor(F) || _I <- lists:seq(1,Jobs)],
  50. compile_queue(Config, Pids, RestFiles)
  51. end.
  52. run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
  53. Compile3Fn) ->
  54. run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
  55. Compile3Fn, [check_last_mod]).
  56. run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
  57. Compile3Fn, Opts) ->
  58. %% Convert simple extension to proper regex
  59. SourceExtRe = "^[^._].*\\" ++ SourceExt ++ [$$],
  60. Recursive = proplists:get_value(recursive, Opts, true),
  61. %% Find all possible source files
  62. FoundFiles = rebar_utils:find_files(SourceDir, SourceExtRe, Recursive),
  63. %% Remove first files from found files
  64. RestFiles = [Source || Source <- FoundFiles,
  65. not lists:member(Source, FirstFiles)],
  66. %% Check opts for flag indicating that compile should check lastmod
  67. CheckLastMod = proplists:get_bool(check_last_mod, Opts),
  68. run(Config, FirstFiles, RestFiles,
  69. fun(S, C) ->
  70. Target = target_file(S, SourceDir, SourceExt,
  71. TargetDir, TargetExt),
  72. simple_compile_wrapper(S, Target, Compile3Fn, C, CheckLastMod)
  73. end).
  74. ok_tuple(Config, Source, Ws) ->
  75. {ok, format_warnings(Config, Source, Ws)}.
  76. error_tuple(Config, Source, Es, Ws, Opts) ->
  77. {error, format_errors(Config, Source, Es),
  78. format_warnings(Config, Source, Ws, Opts)}.
  79. %% ===================================================================
  80. %% Internal functions
  81. %% ===================================================================
  82. simple_compile_wrapper(Source, Target, Compile3Fn, Config, false) ->
  83. Compile3Fn(Source, Target, Config);
  84. simple_compile_wrapper(Source, Target, Compile3Fn, Config, true) ->
  85. case filelib:last_modified(Target) < filelib:last_modified(Source) of
  86. true ->
  87. Compile3Fn(Source, Target, Config);
  88. false ->
  89. skipped
  90. end.
  91. target_file(SourceFile, SourceDir, SourceExt, TargetDir, TargetExt) ->
  92. BaseFile = remove_common_path(SourceFile, SourceDir),
  93. filename:join([TargetDir, filename:basename(BaseFile, SourceExt) ++ TargetExt]).
  94. remove_common_path(Fname, Path) ->
  95. remove_common_path1(filename:split(Fname), filename:split(Path)).
  96. remove_common_path1([Part | RestFilename], [Part | RestPath]) ->
  97. remove_common_path1(RestFilename, RestPath);
  98. remove_common_path1(FilenameParts, _) ->
  99. filename:join(FilenameParts).
  100. compile(Source, Config, CompileFn) ->
  101. case CompileFn(Source, Config) of
  102. ok ->
  103. ok;
  104. skipped ->
  105. skipped;
  106. Error ->
  107. Error
  108. end.
  109. compile_each([], _Config, _CompileFn) ->
  110. ok;
  111. compile_each([Source | Rest], Config, CompileFn) ->
  112. case compile(Source, Config, CompileFn) of
  113. ok ->
  114. ?DEBUG("~sCompiled ~s", [rebar_utils:indent(1), filename:basename(Source)]);
  115. {ok, Warnings} ->
  116. report(Warnings),
  117. ?DEBUG("~sCompiled ~s", [rebar_utils:indent(1), filename:basename(Source)]);
  118. skipped ->
  119. ?DEBUG("~sSkipped ~s", [rebar_utils:indent(1), filename:basename(Source)]);
  120. Error ->
  121. ?INFO("Compiling ~s failed:",
  122. [maybe_absname(Config, Source)]),
  123. maybe_report(Error),
  124. ?DEBUG("Compilation failed: ~p", [Error]),
  125. ?FAIL
  126. end,
  127. compile_each(Rest, Config, CompileFn).
  128. compile_queue(_Config, [], []) ->
  129. ok;
  130. compile_queue(Config, Pids, Targets) ->
  131. receive
  132. {next, Worker} ->
  133. case Targets of
  134. [] ->
  135. Worker ! empty,
  136. compile_queue(Config, Pids, Targets);
  137. [Source | Rest] ->
  138. Worker ! {compile, Source},
  139. compile_queue(Config, Pids, Rest)
  140. end;
  141. {fail, {_, {source, Source}}=Error} ->
  142. ?ERROR("Compiling ~s failed:",
  143. [maybe_absname(Config, Source)]),
  144. maybe_report(Error),
  145. ?DEBUG("Worker compilation failed: ~p", [Error]),
  146. ?FAIL;
  147. {compiled, Source, Warnings} ->
  148. report(Warnings),
  149. ?DEBUG("~sCompiled ~s", [rebar_utils:indent(1), filename:basename(Source)]),
  150. compile_queue(Config, Pids, Targets);
  151. {compiled, Source} ->
  152. ?DEBUG("~sCompiled ~s", [rebar_utils:indent(1), filename:basename(Source)]),
  153. compile_queue(Config, Pids, Targets);
  154. {skipped, Source} ->
  155. ?DEBUG("~sSkipped ~s", [rebar_utils:indent(1), filename:basename(Source)]),
  156. compile_queue(Config, Pids, Targets);
  157. {'DOWN', Mref, _, Pid, normal} ->
  158. ?DEBUG("Worker exited cleanly", []),
  159. Pids2 = lists:delete({Pid, Mref}, Pids),
  160. compile_queue(Config, Pids2, Targets);
  161. {'DOWN', _Mref, _, _Pid, Info} ->
  162. ?DEBUG("Worker failed: ~p", [Info]),
  163. ?FAIL
  164. end.
  165. compile_worker(QueuePid, Config, CompileFn) ->
  166. QueuePid ! {next, self()},
  167. receive
  168. {compile, Source} ->
  169. case catch(compile(Source, Config, CompileFn)) of
  170. {ok, Ws} ->
  171. QueuePid ! {compiled, Source, Ws},
  172. compile_worker(QueuePid, Config, CompileFn);
  173. ok ->
  174. QueuePid ! {compiled, Source},
  175. compile_worker(QueuePid, Config, CompileFn);
  176. skipped ->
  177. QueuePid ! {skipped, Source},
  178. compile_worker(QueuePid, Config, CompileFn);
  179. Error ->
  180. QueuePid ! {fail, {{error, Error}, {source, Source}}},
  181. ok
  182. end;
  183. empty ->
  184. ok
  185. end.
  186. format_errors(Config, Source, Errors) ->
  187. format_errors(Config, Source, "", Errors).
  188. format_warnings(Config, Source, Warnings) ->
  189. format_warnings(Config, Source, Warnings, []).
  190. format_warnings(Config, Source, Warnings, Opts) ->
  191. Prefix = case lists:member(warnings_as_errors, Opts) of
  192. true -> "";
  193. false -> "Warning: "
  194. end,
  195. format_errors(Config, Source, Prefix, Warnings).
  196. maybe_report({{error, {error, _Es, _Ws}=ErrorsAndWarnings}, {source, _}}) ->
  197. maybe_report(ErrorsAndWarnings);
  198. maybe_report([{error, E}, {source, S}]) ->
  199. report(["unexpected error compiling " ++ S, io_lib:fwrite("~n~p", [E])]);
  200. maybe_report({error, Es, Ws}) ->
  201. report(Es),
  202. report(Ws);
  203. maybe_report(_) ->
  204. ok.
  205. report(Messages) ->
  206. lists:foreach(fun(Msg) -> io:format("~s~n", [Msg]) end, Messages).
  207. format_errors(Config, _MainSource, Extra, Errors) ->
  208. [begin
  209. AbsSource = maybe_absname(Config, Source),
  210. [format_error(AbsSource, Extra, Desc) || Desc <- Descs]
  211. end
  212. || {Source, Descs} <- Errors].
  213. format_error(AbsSource, Extra, {{Line, Column}, Mod, Desc}) ->
  214. ErrorDesc = Mod:format_error(Desc),
  215. ?FMT("~s:~w:~w: ~s~s~n", [AbsSource, Line, Column, Extra, ErrorDesc]);
  216. format_error(AbsSource, Extra, {Line, Mod, Desc}) ->
  217. ErrorDesc = Mod:format_error(Desc),
  218. ?FMT("~s:~w: ~s~s~n", [AbsSource, Line, Extra, ErrorDesc]);
  219. format_error(AbsSource, Extra, {Mod, Desc}) ->
  220. ErrorDesc = Mod:format_error(Desc),
  221. ?FMT("~s: ~s~s~n", [AbsSource, Extra, ErrorDesc]).
  222. maybe_absname(_Config, Filename) ->
  223. Filename.