summaryrefslogtreecommitdiff
path: root/src/rebar_base_compiler.erl
blob: 75ab4900adf29e3e936b809de8eac61d0c541005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et
%% -------------------------------------------------------------------
%%
%% rebar: Erlang Build Tools
%%
%% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
%%
%% Permission is hereby granted, free of charge, to any person obtaining a copy
%% of this software and associated documentation files (the "Software"), to deal
%% in the Software without restriction, including without limitation the rights
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the Software is
%% furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in
%% all copies or substantial portions of the Software.
%%
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
%% THE SOFTWARE.
%% -------------------------------------------------------------------
-module(rebar_base_compiler).

-include("rebar.hrl").

-export([run/4,
         run/7,
         run/8,
         run/5,
         ok_tuple/3,
         error_tuple/5]).

%% ===================================================================
%% Public API
%% ===================================================================

run(Config, FirstFiles, RestFiles, CompileFn) ->
    %% Compile the first files in sequence
    compile_each(FirstFiles, Config, CompileFn),

    %% Spin up workers for the rest of the files
    case RestFiles of
        [] ->
            ok;
        _ ->
            Self = self(),
            F = fun() -> compile_worker(Self, Config, CompileFn) end,
            Jobs = rebar:get_jobs(Config),
            ?DEBUG("Starting ~B compile worker(s)~n", [Jobs]),
            Pids = [spawn_monitor(F) || _I <- lists:seq(1,Jobs)],
            compile_queue(Config, Pids, RestFiles)
    end.

run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
    Compile3Fn) ->
    run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
        Compile3Fn, [check_last_mod]).

run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
    Compile3Fn, Opts) ->

    %% Find all possible source files
    Recursive = proplists:get_value(recursive, Opts, true),
    FoundFiles = rebar_utils:find_files_by_ext(SourceDir, SourceExt, Recursive),

    %% Remove first files from found files
    RestFiles = [Source || Source <- FoundFiles,
                           not lists:member(Source, FirstFiles)],

    FirstUnits = source_to_unit_each(FirstFiles,
                                     SourceDir, SourceExt,
                                     TargetDir, TargetExt),
    RestUnits = source_to_unit_each(RestFiles,
                                    SourceDir, SourceExt,
                                    TargetDir, TargetExt),
    run(Config, FirstUnits, RestUnits, Compile3Fn, Opts).

%% FirstUnits and RestUnits are lists of tuples: {Source,Target}
run(Config, FirstUnits, RestUnits, Compile3Fn, Opts) ->

    %% Check opts for flag indicating that compile should check lastmod
    CheckLastMod = proplists:get_bool(check_last_mod, Opts),

    run(Config, FirstUnits, RestUnits,
        fun({S, Target}, C) ->
                simple_compile_wrapper(S, Target, Compile3Fn, C, CheckLastMod)
        end).

ok_tuple(Config, Source, Ws) ->
    {ok, format_warnings(Config, Source, Ws)}.

error_tuple(Config, Source, Es, Ws, Opts) ->
    {error, format_errors(Config, Source, Es),
     format_warnings(Config, Source, Ws, Opts)}.

%% ===================================================================
%% Internal functions
%% ===================================================================

simple_compile_wrapper(Source, Target, Compile3Fn, Config, false) ->
    Compile3Fn(Source, Target, Config);
simple_compile_wrapper(Source, Target, Compile3Fn, Config, true) ->
    case filelib:last_modified(Target) < filelib:last_modified(Source) of
        true ->
            Compile3Fn(Source, Target, Config);
        false ->
            skipped
    end.

source_to_unit_each(Files, SourceDir, SourceExt, TargetDir, TargetExt) ->
    [{File, target_file(File, SourceDir, SourceExt, TargetDir, TargetExt)}
     || File <- Files].

target_file(SourceFile, SourceDir, SourceExt, TargetDir, TargetExt) ->
    BaseFile = remove_common_path(SourceFile, SourceDir),
    filename:join([TargetDir, filename:basename(BaseFile, SourceExt) ++ TargetExt]).


remove_common_path(Fname, Path) ->
    remove_common_path1(filename:split(Fname), filename:split(Path)).

remove_common_path1([Part | RestFilename], [Part | RestPath]) ->
    remove_common_path1(RestFilename, RestPath);
remove_common_path1(FilenameParts, _) ->
    filename:join(FilenameParts).

compile_each([], _Config, _CompileFn) ->
    ok;
compile_each([Unit | Rest], Config, CompileFn) ->
    case CompileFn(Unit, Config) of
        ok ->
            ?CONSOLE("Compiled ~s\n", [unit_source(Unit)]);
        {ok, Warnings} ->
            report(Warnings),
            ?CONSOLE("Compiled ~s\n", [unit_source(Unit)]);
        skipped ->
            ?INFO("Skipped ~s\n", [unit_source(Unit)]);
        Error ->
            maybe_report(Error),
            ?CONSOLE("Compiling ~s failed:\n",
                     [maybe_absname(Config, unit_source(Unit))]),
            ?DEBUG("Compilation failed: ~p\n", [Error]),
            case rebar_config:get_xconf(Config, keep_going, false) of
                false ->
                    ?FAIL;
                true ->
                    ?WARN("Continuing after build error\n", [])
            end
    end,
    compile_each(Rest, Config, CompileFn).

unit_source({Source, _Target}) ->
    Source;
unit_source(Source) ->
    Source.

compile_queue(_Config, [], []) ->
    ok;
compile_queue(Config, Pids, Targets) ->
    receive
        {next, Worker} ->
            case Targets of
                [] ->
                    Worker ! empty,
                    compile_queue(Config, Pids, Targets);
                [Source | Rest] ->
                    Worker ! {compile, Source},
                    compile_queue(Config, Pids, Rest)
            end;

        {fail, {_, {source, Unit}}=Error} ->
            maybe_report(Error),
            ?CONSOLE("Compiling ~s failed:\n",
                     [maybe_absname(Config, unit_source(Unit))]),
            ?DEBUG("Worker compilation failed: ~p\n", [Error]),
            case rebar_config:get_xconf(Config, keep_going, false) of
                false ->
                    ?FAIL;
                true ->
                    ?WARN("Continuing after build error\n", []),
                    compile_queue(Config, Pids, Targets)
            end;

        {compiled, Unit, Warnings} ->
            report(Warnings),
            ?CONSOLE("Compiled ~s\n", [unit_source(Unit)]),
            compile_queue(Config, Pids, Targets);

        {compiled, Unit} ->
            ?CONSOLE("Compiled ~s\n", [unit_source(Unit)]),
            compile_queue(Config, Pids, Targets);

        {skipped, Unit} ->
            ?INFO("Skipped ~s\n", [unit_source(Unit)]),
            compile_queue(Config, Pids, Targets);

        {'DOWN', Mref, _, Pid, normal} ->
            ?DEBUG("Worker exited cleanly\n", []),
            Pids2 = lists:delete({Pid, Mref}, Pids),
            compile_queue(Config, Pids2, Targets);

        {'DOWN', _Mref, _, _Pid, Info} ->
            ?DEBUG("Worker failed: ~p\n", [Info]),
            ?FAIL
    end.

compile_worker(QueuePid, Config, CompileFn) ->
    QueuePid ! {next, self()},
    receive
        {compile, Source} ->
            case catch(CompileFn(Source, Config)) of
                {ok, Ws} ->
                    QueuePid ! {compiled, Source, Ws},
                    compile_worker(QueuePid, Config, CompileFn);
                ok ->
                    QueuePid ! {compiled, Source},
                    compile_worker(QueuePid, Config, CompileFn);
                skipped ->
                    QueuePid ! {skipped, Source},
                    compile_worker(QueuePid, Config, CompileFn);
                Error ->
                    QueuePid ! {fail, {{error, Error}, {source, Source}}},
                    case rebar_config:get_xconf(Config, keep_going, false) of
                        false ->
                            ok;
                        true ->
                            compile_worker(QueuePid, Config, CompileFn)
                    end
            end;

        empty ->
            ok
    end.

format_errors(Config, Source, Errors) ->
    format_errors(Config, Source, "", Errors).

format_warnings(Config, Source, Warnings) ->
    format_warnings(Config, Source, Warnings, []).

format_warnings(Config, Source, Warnings, Opts) ->
    Prefix = case lists:member(warnings_as_errors, Opts) of
                 true -> "";
                 false -> "Warning: "
             end,
    format_errors(Config, Source, Prefix, Warnings).

maybe_report({{error, {error, _Es, _Ws}=ErrorsAndWarnings}, {source, _}}) ->
    maybe_report(ErrorsAndWarnings);
maybe_report([{error, E}, {source, S}]) ->
    report(["unexpected error compiling " ++ S, io_lib:fwrite("~n~p~n", [E])]);
maybe_report({error, Es, Ws}) ->
    report(Es),
    report(Ws);
maybe_report(_) ->
    ok.

report(Messages) ->
    lists:foreach(fun(Msg) -> io:format("~s", [Msg]) end, Messages).

format_errors(Config, _MainSource, Extra, Errors) ->
    [begin
         AbsSource = maybe_absname(Config, Source),
         [format_error(AbsSource, Extra, Desc) || Desc <- Descs]
     end
     || {Source, Descs} <- Errors].

format_error(AbsSource, Extra, {{Line, Column}, Mod, Desc}) ->
    ErrorDesc = Mod:format_error(Desc),
    ?FMT("~s:~w:~w: ~s~s~n", [AbsSource, Line, Column, Extra, ErrorDesc]);
format_error(AbsSource, Extra, {Line, Mod, Desc}) ->
    ErrorDesc = Mod:format_error(Desc),
    ?FMT("~s:~w: ~s~s~n", [AbsSource, Line, Extra, ErrorDesc]);
format_error(AbsSource, Extra, {Mod, Desc}) ->
    ErrorDesc = Mod:format_error(Desc),
    ?FMT("~s: ~s~s~n", [AbsSource, Extra, ErrorDesc]).

maybe_absname(Config, Filename) ->
    case rebar_utils:processing_base_dir(Config) of
        true ->
            Filename;
        false ->
            filename:absname(Filename)
    end.