Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 193 lines (163 sloc) 6.967 kB
b7e2088 @dizzyd Initial commit
dizzyd authored
1 %% -------------------------------------------------------------------
2 %%
3 %% rebar: Erlang Build Tools
4 %%
5 %% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
6 %%
7 %% Permission is hereby granted, free of charge, to any person obtaining a copy
8 %% of this software and associated documentation files (the "Software"), to deal
9 %% in the Software without restriction, including without limitation the rights
10 %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 %% copies of the Software, and to permit persons to whom the Software is
12 %% furnished to do so, subject to the following conditions:
13 %%
14 %% The above copyright notice and this permission notice shall be included in
15 %% all copies or substantial portions of the Software.
16 %%
17 %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 %% THE SOFTWARE.
24 %% -------------------------------------------------------------------
fd8794d @dizzyd Renaming compiler module
dizzyd authored
25 -module(rebar_erlc_compiler).
b7e2088 @dizzyd Initial commit
dizzyd authored
26
27 -export([compile/2,
28 clean/2]).
29
726f8e6 Added support for eunit tests as test/*_tests.erl
Jon Meredith authored
30 %% make available for rebar_eunit until there is a better option
31 -export([do_compile/7, compile_opts/2]).
32
b7e2088 @dizzyd Initial commit
dizzyd authored
33 -include("rebar.hrl").
34
35 %% ===================================================================
36 %% Public API
37 %% ===================================================================
38
be2a909 @dizzyd More work
dizzyd authored
39 compile(Config, _AppFile) ->
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
40 do_compile(Config, "src/*.erl", "ebin", ".erl", ".beam",
e4a9310 @dizzyd Adding support for basic compilation ordering
dizzyd authored
41 fun compile_erl/2,
42 rebar_config:get_list(Config, erl_first_files, [])),
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
43 do_compile(Config, "mibs/*.mib", "priv/mibs", ".mib", ".bin",
e4a9310 @dizzyd Adding support for basic compilation ordering
dizzyd authored
44 fun compile_mib/2,
45 rebar_config:get_list(Config, mib_first_files, [])).
b7e2088 @dizzyd Initial commit
dizzyd authored
46
be2a909 @dizzyd More work
dizzyd authored
47 clean(_Config, _AppFile) ->
620867d @dizzyd More work on getting compilation and clean working properly
dizzyd authored
48 %% TODO: This would be more portable if it used Erlang to traverse
49 %% the dir structure and delete each file; however it would also
50 %% much slower.
51 [] = os:cmd("rm -f ebin/*.beam priv/mibs/*.bin"),
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
52 ok.
b7e2088 @dizzyd Initial commit
dizzyd authored
53
54
620867d @dizzyd More work on getting compilation and clean working properly
dizzyd authored
55
b7e2088 @dizzyd Initial commit
dizzyd authored
56 %% ===================================================================
57 %% Internal functions
58 %% ===================================================================
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
59
e4a9310 @dizzyd Adding support for basic compilation ordering
dizzyd authored
60 do_compile(Config, SrcWildcard, OutDir, InExt, OutExt, CompileFn, FirstFiles) ->
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
61 case filelib:wildcard(SrcWildcard) of
62 [] ->
63 ok;
e4a9310 @dizzyd Adding support for basic compilation ordering
dizzyd authored
64 FoundFiles when is_list(FoundFiles) ->
65 %% Ensure that the FirstFiles are compiled first; drop them from the
a658e97 @dizzyd Adding support for parallel compilation; use 3 workers by default
dizzyd authored
66 %% FoundFiles and compile them in sequence
67 FirstTargets = [{Fs, target_file(Fs, OutDir, InExt, OutExt)} || Fs <- FirstFiles],
68 RestTargets = [{Fs, target_file(Fs, OutDir, InExt, OutExt)} ||
69 Fs <- drop_each(FirstFiles, FoundFiles)],
70
71 %% Make sure target directory exists
72 ok = filelib:ensure_dir(target_file(hd(FoundFiles), OutDir, InExt, OutExt)),
28fe3bf @dizzyd More gross structural work; now has log levels and broken out log lev…
dizzyd authored
73
a658e97 @dizzyd Adding support for parallel compilation; use 3 workers by default
dizzyd authored
74 %% Compile first targets in sequence
75 compile_each(FirstTargets, Config, CompileFn),
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
76
a658e97 @dizzyd Adding support for parallel compilation; use 3 workers by default
dizzyd authored
77 %% Spin up workers
78 Self = self(),
79 Pids = [spawn_monitor(fun() -> compile_worker(Self) end) || _I <- lists:seq(1,3)],
80
81 %% Process rest of targets
82 compile_queue(Pids, RestTargets, Config, CompileFn)
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
83 end.
84
e4a9310 @dizzyd Adding support for basic compilation ordering
dizzyd authored
85 drop_each([], List) ->
86 List;
87 drop_each([Member | Rest], List) ->
88 drop_each(Rest, lists:delete(Member, List)).
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
89
90 compile_each([], _Config, _CompileFn) ->
91 ok;
e4a9310 @dizzyd Adding support for basic compilation ordering
dizzyd authored
92 compile_each([{Src, Target} | Rest], Config, CompileFn) ->
93 case needs_compile(Src, Target) of
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
94 true ->
95 ?CONSOLE("Compiling ~s\n", [Src]),
96 CompileFn(Src, Config);
97 false ->
28fe3bf @dizzyd More gross structural work; now has log levels and broken out log lev…
dizzyd authored
98 ?INFO("Skipping ~s\n", [Src]),
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
99 ok
100 end,
101 compile_each(Rest, Config, CompileFn).
102
e4a9310 @dizzyd Adding support for basic compilation ordering
dizzyd authored
103 needs_compile(Src, Target) ->
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
104 filelib:last_modified(Target) < filelib:last_modified(Src).
105
106
107 target_file(F, TargetDir, InExt, OutExt) ->
108 filename:join([TargetDir, filename:basename(F, InExt) ++ OutExt]).
109
28fe3bf @dizzyd More gross structural work; now has log levels and broken out log lev…
dizzyd authored
110 compile_opts(Config, Key) ->
111 rebar_config:get_list(Config, Key, []).
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
112
113 compile_erl(Source, Config) ->
417ff3a @dizzyd Adding fail_on_warning support
dizzyd authored
114 Opts = [{i, "include"}, {outdir, "ebin"}, report, return] ++ compile_opts(Config, erl_opts),
28fe3bf @dizzyd More gross structural work; now has log levels and broken out log lev…
dizzyd authored
115 case compile:file(Source, Opts) of
417ff3a @dizzyd Adding fail_on_warning support
dizzyd authored
116 {ok, _, []} ->
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
117 ok;
417ff3a @dizzyd Adding fail_on_warning support
dizzyd authored
118 {ok, _, _Warnings} ->
119 %% We got at least one warning -- if fail_on_warning is in options, fail
120 case lists:member(fail_on_warning, Opts) of
121 true ->
122 ?FAIL;
123 false ->
124 ok
125 end;
126 _ ->
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
127 ?FAIL
128 end.
129
130 compile_mib(Source, Config) ->
28fe3bf @dizzyd More gross structural work; now has log levels and broken out log lev…
dizzyd authored
131 Opts = [{outdir, "priv/mibs"}, {i, ["priv/mibs"]}] ++ compile_opts(Config, mib_opts),
132 case snmpc:compile(Source, Opts) of
e52bb67 @dizzyd Getting basic erlang compilation working
dizzyd authored
133 {ok, _} ->
134 ok;
135 {error, compilation_failed} ->
136 ?FAIL
137 end.
a658e97 @dizzyd Adding support for parallel compilation; use 3 workers by default
dizzyd authored
138
139 compile_queue([], [], _Config, _CompileFn) ->
140 ok;
141 compile_queue(Pids, Targets, Config, CompileFn) ->
142 receive
143 {next, Worker} ->
144 case Targets of
145 [] ->
146 Worker ! empty,
147 compile_queue(Pids, Targets, Config, CompileFn);
148 [{Src, Target} | Rest] ->
149 Worker ! {compile, Src, Target, Config, CompileFn},
150 compile_queue(Pids, Rest, Config, CompileFn)
151 end;
417ff3a @dizzyd Adding fail_on_warning support
dizzyd authored
152
153 {fail, Error} ->
154 ?DEBUG("Worker compilation failed: ~p\n", [Error]),
155 ?FAIL;
a658e97 @dizzyd Adding support for parallel compilation; use 3 workers by default
dizzyd authored
156
157 {compiled, Source} ->
158 ?CONSOLE("Compiled ~s\n", [Source]),
159 compile_queue(Pids, Targets, Config, CompileFn);
160
161 {'DOWN', Mref, _, Pid, normal} ->
162 ?DEBUG("Worker exited cleanly\n", []),
163 Pids2 = lists:delete({Pid, Mref}, Pids),
164 compile_queue(Pids2, Targets, Config, CompileFn);
165
d6600ab @dizzyd Fixing number of warnings and buglets
dizzyd authored
166 {'DOWN', _Mref, _, _Pid, Info} ->
a658e97 @dizzyd Adding support for parallel compilation; use 3 workers by default
dizzyd authored
167 ?DEBUG("Worker failed: ~p\n", [Info]),
168 ?FAIL
169 end.
170
171 compile_worker(QueuePid) ->
172 QueuePid ! {next, self()},
173 receive
174 {compile, Src, Target, Config, CompileFn} ->
175 case needs_compile(Src, Target) of
176 true ->
417ff3a @dizzyd Adding fail_on_warning support
dizzyd authored
177 case catch(CompileFn(Src, Config)) of
178 ok ->
179 QueuePid ! {compiled, Src},
180 compile_worker(QueuePid);
181 Error ->
182 QueuePid ! {fail, Error},
183 ok
184 end;
a658e97 @dizzyd Adding support for parallel compilation; use 3 workers by default
dizzyd authored
185 false ->
186 ?INFO("Skipping ~s\n", [Src]),
417ff3a @dizzyd Adding fail_on_warning support
dizzyd authored
187 compile_worker(QueuePid)
188 end;
189
a658e97 @dizzyd Adding support for parallel compilation; use 3 workers by default
dizzyd authored
190 empty ->
191 ok
192 end.
Something went wrong with that request. Please try again.