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.

267 line
9.7 KiB

14 年之前
14 年之前
  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)~n", [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. %% Remove all leading components of the source dir from the file -- we want
  93. %% to maintain the deeper structure (if any) of the source file path
  94. BaseFile = remove_common_path(SourceFile, SourceDir),
  95. filename:join([TargetDir, filename:dirname(BaseFile),
  96. filename:basename(BaseFile, SourceExt) ++ TargetExt]).
  97. remove_common_path(Fname, Path) ->
  98. remove_common_path1(filename:split(Fname), filename:split(Path)).
  99. remove_common_path1([Part | RestFilename], [Part | RestPath]) ->
  100. remove_common_path1(RestFilename, RestPath);
  101. remove_common_path1(FilenameParts, _) ->
  102. filename:join(FilenameParts).
  103. compile(Source, Config, CompileFn) ->
  104. case CompileFn(Source, Config) of
  105. ok ->
  106. ok;
  107. skipped ->
  108. skipped;
  109. Error ->
  110. Error
  111. end.
  112. compile_each([], _Config, _CompileFn) ->
  113. ok;
  114. compile_each([Source | Rest], Config, CompileFn) ->
  115. case compile(Source, Config, CompileFn) of
  116. ok ->
  117. ?DEBUG("~sCompiled ~s\n", [rebar_utils:indent(1), filename:basename(Source)]);
  118. {ok, Warnings} ->
  119. report(Warnings),
  120. ?DEBUG("~sCompiled ~s\n", [rebar_utils:indent(1), filename:basename(Source)]);
  121. skipped ->
  122. ?DEBUG("~sSkipped ~s\n", [rebar_utils:indent(1), filename:basename(Source)]);
  123. Error ->
  124. ?INFO("Compiling ~s failed:\n",
  125. [maybe_absname(Config, Source)]),
  126. maybe_report(Error),
  127. ?DEBUG("Compilation failed: ~p\n", [Error]),
  128. ?FAIL
  129. end,
  130. compile_each(Rest, Config, CompileFn).
  131. compile_queue(_Config, [], []) ->
  132. ok;
  133. compile_queue(Config, Pids, Targets) ->
  134. receive
  135. {next, Worker} ->
  136. case Targets of
  137. [] ->
  138. Worker ! empty,
  139. compile_queue(Config, Pids, Targets);
  140. [Source | Rest] ->
  141. Worker ! {compile, Source},
  142. compile_queue(Config, Pids, Rest)
  143. end;
  144. {fail, {_, {source, Source}}=Error} ->
  145. ?ERROR("Compiling ~s failed:\n",
  146. [maybe_absname(Config, Source)]),
  147. maybe_report(Error),
  148. ?DEBUG("Worker compilation failed: ~p\n", [Error]),
  149. ?FAIL;
  150. {compiled, Source, Warnings} ->
  151. report(Warnings),
  152. ?DEBUG("~sCompiled ~s\n", [rebar_utils:indent(1), filename:basename(Source)]),
  153. compile_queue(Config, Pids, Targets);
  154. {compiled, Source} ->
  155. ?DEBUG("~sCompiled ~s\n", [rebar_utils:indent(1), filename:basename(Source)]),
  156. compile_queue(Config, Pids, Targets);
  157. {skipped, Source} ->
  158. ?DEBUG("~sSkipped ~s~n", [rebar_utils:indent(1), filename:basename(Source)]),
  159. compile_queue(Config, Pids, Targets);
  160. {'DOWN', Mref, _, Pid, normal} ->
  161. ?DEBUG("Worker exited cleanly\n", []),
  162. Pids2 = lists:delete({Pid, Mref}, Pids),
  163. compile_queue(Config, Pids2, Targets);
  164. {'DOWN', _Mref, _, _Pid, Info} ->
  165. ?DEBUG("Worker failed: ~p\n", [Info]),
  166. ?FAIL
  167. end.
  168. compile_worker(QueuePid, Config, CompileFn) ->
  169. QueuePid ! {next, self()},
  170. receive
  171. {compile, Source} ->
  172. case catch(compile(Source, Config, CompileFn)) of
  173. {ok, Ws} ->
  174. QueuePid ! {compiled, Source, Ws},
  175. compile_worker(QueuePid, Config, CompileFn);
  176. ok ->
  177. QueuePid ! {compiled, Source},
  178. compile_worker(QueuePid, Config, CompileFn);
  179. skipped ->
  180. QueuePid ! {skipped, Source},
  181. compile_worker(QueuePid, Config, CompileFn);
  182. Error ->
  183. QueuePid ! {fail, {{error, Error}, {source, Source}}},
  184. ok
  185. end;
  186. empty ->
  187. ok
  188. end.
  189. format_errors(Config, Source, Errors) ->
  190. format_errors(Config, Source, "", Errors).
  191. format_warnings(Config, Source, Warnings) ->
  192. format_warnings(Config, Source, Warnings, []).
  193. format_warnings(Config, Source, Warnings, Opts) ->
  194. Prefix = case lists:member(warnings_as_errors, Opts) of
  195. true -> "";
  196. false -> "Warning: "
  197. end,
  198. format_errors(Config, Source, Prefix, Warnings).
  199. maybe_report({{error, {error, _Es, _Ws}=ErrorsAndWarnings}, {source, _}}) ->
  200. maybe_report(ErrorsAndWarnings);
  201. maybe_report([{error, E}, {source, S}]) ->
  202. report(["unexpected error compiling " ++ S, io_lib:fwrite("~n~p~n", [E])]);
  203. maybe_report({error, Es, Ws}) ->
  204. report(Es),
  205. report(Ws);
  206. maybe_report(_) ->
  207. ok.
  208. report(Messages) ->
  209. lists:foreach(fun(Msg) -> io:format("~s", [Msg]) end, Messages).
  210. format_errors(Config, _MainSource, Extra, Errors) ->
  211. [begin
  212. AbsSource = maybe_absname(Config, Source),
  213. [format_error(AbsSource, Extra, Desc) || Desc <- Descs]
  214. end
  215. || {Source, Descs} <- Errors].
  216. format_error(AbsSource, Extra, {{Line, Column}, Mod, Desc}) ->
  217. ErrorDesc = Mod:format_error(Desc),
  218. ?FMT("~s:~w:~w: ~s~s~n", [AbsSource, Line, Column, Extra, ErrorDesc]);
  219. format_error(AbsSource, Extra, {Line, Mod, Desc}) ->
  220. ErrorDesc = Mod:format_error(Desc),
  221. ?FMT("~s:~w: ~s~s~n", [AbsSource, Line, Extra, ErrorDesc]);
  222. format_error(AbsSource, Extra, {Mod, Desc}) ->
  223. ErrorDesc = Mod:format_error(Desc),
  224. ?FMT("~s: ~s~s~n", [AbsSource, Extra, ErrorDesc]).
  225. maybe_absname(Config, Filename) ->
  226. case rebar_utils:processing_base_dir(Config) of
  227. true ->
  228. Filename;
  229. false ->
  230. filename:absname(Filename)
  231. end.