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.

266 lines
9.4 KiB

14 years ago
14 years ago
  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, run/7, run/8,
  30. ok_tuple/3, error_tuple/5]).
  31. %% ===================================================================
  32. %% Public API
  33. %% ===================================================================
  34. run(Config, FirstFiles, RestFiles, CompileFn) ->
  35. %% Compile the first files in sequence
  36. compile_each(FirstFiles, Config, CompileFn),
  37. %% Spin up workers for the rest of the files
  38. case RestFiles of
  39. [] ->
  40. ok;
  41. _ ->
  42. Self = self(),
  43. F = fun() -> compile_worker(Self, Config, CompileFn) end,
  44. Jobs = rebar:get_jobs(Config),
  45. ?DEBUG("Starting ~B compile worker(s)~n", [Jobs]),
  46. Pids = [spawn_monitor(F) || _I <- lists:seq(1,Jobs)],
  47. compile_queue(Config, Pids, RestFiles)
  48. end.
  49. run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
  50. Compile3Fn) ->
  51. run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
  52. Compile3Fn, [check_last_mod]).
  53. run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
  54. Compile3Fn, Opts) ->
  55. %% Convert simple extension to proper regex
  56. SourceExtRe = ".*\\" ++ SourceExt ++ [$$],
  57. Recursive = proplists:get_value(recursive, Opts, true),
  58. %% Find all possible source files
  59. FoundFiles = rebar_utils:find_files(SourceDir, SourceExtRe, Recursive),
  60. %% Remove first files from found files
  61. RestFiles = [Source || Source <- FoundFiles,
  62. not lists:member(Source, FirstFiles)],
  63. %% Check opts for flag indicating that compile should check lastmod
  64. CheckLastMod = proplists:get_bool(check_last_mod, Opts),
  65. run(Config, FirstFiles, RestFiles,
  66. fun(S, C) ->
  67. Target = target_file(S, SourceDir, SourceExt,
  68. TargetDir, TargetExt),
  69. simple_compile_wrapper(S, Target, Compile3Fn, C, CheckLastMod)
  70. end).
  71. ok_tuple(Config, Source, Ws) ->
  72. {ok, format_warnings(Config, Source, Ws)}.
  73. error_tuple(Config, Source, Es, Ws, Opts) ->
  74. {error, format_errors(Config, Source, Es),
  75. format_warnings(Config, Source, Ws, Opts)}.
  76. %% ===================================================================
  77. %% Internal functions
  78. %% ===================================================================
  79. simple_compile_wrapper(Source, Target, Compile3Fn, Config, false) ->
  80. Compile3Fn(Source, Target, Config);
  81. simple_compile_wrapper(Source, Target, Compile3Fn, Config, true) ->
  82. case filelib:last_modified(Target) < filelib:last_modified(Source) of
  83. true ->
  84. Compile3Fn(Source, Target, Config);
  85. false ->
  86. skipped
  87. end.
  88. target_file(SourceFile, SourceDir, SourceExt, TargetDir, TargetExt) ->
  89. %% Remove all leading components of the source dir from the file -- we want
  90. %% to maintain the deeper structure (if any) of the source file path
  91. BaseFile = remove_common_path(SourceFile, SourceDir),
  92. filename:join([TargetDir, filename:dirname(BaseFile),
  93. 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. ?CONSOLE("Compiled ~s\n", [Source]);
  115. {ok, Warnings} ->
  116. report(Warnings),
  117. ?CONSOLE("Compiled ~s\n", [Source]);
  118. skipped ->
  119. ?INFO("Skipped ~s\n", [Source]);
  120. Error ->
  121. ?CONSOLE("Compiling ~s failed:\n",
  122. [maybe_absname(Config, Source)]),
  123. maybe_report(Error),
  124. ?DEBUG("Compilation failed: ~p\n", [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. ?CONSOLE("Compiling ~s failed:\n",
  143. [maybe_absname(Config, Source)]),
  144. maybe_report(Error),
  145. ?DEBUG("Worker compilation failed: ~p\n", [Error]),
  146. ?FAIL;
  147. {compiled, Source, Warnings} ->
  148. report(Warnings),
  149. ?CONSOLE("Compiled ~s\n", [Source]),
  150. compile_queue(Config, Pids, Targets);
  151. {compiled, Source} ->
  152. ?CONSOLE("Compiled ~s\n", [Source]),
  153. compile_queue(Config, Pids, Targets);
  154. {skipped, Source} ->
  155. ?INFO("Skipped ~s\n", [Source]),
  156. compile_queue(Config, Pids, Targets);
  157. {'DOWN', Mref, _, Pid, normal} ->
  158. ?DEBUG("Worker exited cleanly\n", []),
  159. Pids2 = lists:delete({Pid, Mref}, Pids),
  160. compile_queue(Config, Pids2, Targets);
  161. {'DOWN', _Mref, _, _Pid, Info} ->
  162. ?DEBUG("Worker failed: ~p\n", [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~n", [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", [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. case rebar_utils:processing_base_dir(Config) of
  224. true ->
  225. Filename;
  226. false ->
  227. filename:absname(Filename)
  228. end.