2009-12-31 18:42:53 +00:00
|
|
|
%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*-
|
|
|
|
%% ex: ts=4 sw=4 et
|
2009-11-25 22:23:42 +00:00
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% 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.
|
|
|
|
%% -------------------------------------------------------------------
|
2009-11-26 04:00:22 +00:00
|
|
|
-module(rebar_erlc_compiler).
|
2009-11-25 22:23:42 +00:00
|
|
|
|
|
|
|
-export([compile/2,
|
|
|
|
clean/2]).
|
|
|
|
|
2009-12-05 22:18:09 +00:00
|
|
|
%% make available for rebar_eunit until there is a better option
|
2010-01-02 21:17:59 +00:00
|
|
|
-export([hrls_check/3]).
|
2009-12-05 22:18:09 +00:00
|
|
|
|
2009-11-25 22:23:42 +00:00
|
|
|
-include("rebar.hrl").
|
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% Public API
|
|
|
|
%% ===================================================================
|
|
|
|
|
2009-11-30 14:00:48 +00:00
|
|
|
compile(Config, _AppFile) ->
|
2010-01-02 21:17:59 +00:00
|
|
|
rebar_base_compiler:run(Config, "src", ".erl", "ebin", ".beam",
|
|
|
|
rebar_config:get_list(Config, erl_first_files, []),
|
|
|
|
fun compile_erl/3,
|
|
|
|
[recurse_source_dir,
|
|
|
|
{needs_compile_checks, [fun hrls_check/3]}]),
|
|
|
|
|
|
|
|
rebar_base_compiler:run(Config, "mibs", ".mib", "priv/mibs", ".bin",
|
|
|
|
rebar_config:get_list(Config, mib_first_files, []),
|
|
|
|
fun compile_mib/3,
|
|
|
|
[]).
|
2009-11-25 22:23:42 +00:00
|
|
|
|
2009-11-30 14:00:48 +00:00
|
|
|
clean(_Config, _AppFile) ->
|
2009-11-26 03:24:51 +00:00
|
|
|
%% TODO: This would be more portable if it used Erlang to traverse
|
|
|
|
%% the dir structure and delete each file; however it would also
|
|
|
|
%% much slower.
|
2010-01-02 21:17:59 +00:00
|
|
|
ok = rebar_file_utils:rm_rf("ebin/*.beam priv/mibs/*.bin"),
|
|
|
|
|
|
|
|
%% Erlang compilation is recursive, so it's possible that we have a nested
|
|
|
|
%% directory structure in ebin with .beam files within. As such, we want
|
|
|
|
%% to scan whatever is left in the ebin/ directory for sub-dirs which
|
|
|
|
%% satisfy our criteria. TODO: Is there a better way to do this?
|
|
|
|
BeamFiles = filelib:fold_files("ebin", "^.*\\.beam\$", true,
|
|
|
|
fun(F, BeamFiles) -> BeamFiles ++ [F] end, []),
|
|
|
|
rebar_file_utils:delete_each(BeamFiles),
|
2009-11-25 23:03:14 +00:00
|
|
|
ok.
|
2009-11-25 22:23:42 +00:00
|
|
|
|
|
|
|
|
2009-11-26 03:24:51 +00:00
|
|
|
|
2009-11-25 22:23:42 +00:00
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
2009-11-25 23:03:14 +00:00
|
|
|
|
2010-01-02 21:17:59 +00:00
|
|
|
hrls_check(Source, Target, Config) ->
|
|
|
|
TargetLastMod = filelib:last_modified(Target),
|
|
|
|
lists:any(fun(I) -> TargetLastMod < filelib:last_modified(I) end,
|
|
|
|
list_hrls(Source, Config)).
|
2009-11-25 23:03:14 +00:00
|
|
|
|
|
|
|
|
2009-12-21 17:15:21 +00:00
|
|
|
list_hrls(Src, Config) ->
|
|
|
|
case epp:open(Src, include_path(Src, Config)) of
|
2009-12-30 12:13:39 +00:00
|
|
|
{ok, Epp} ->
|
2009-12-21 17:15:21 +00:00
|
|
|
%% check include for erlang files
|
|
|
|
extract_includes(Epp, Src);
|
|
|
|
_ ->
|
|
|
|
false
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
extract_includes(Epp, Src) ->
|
|
|
|
case epp:parse_erl_form(Epp) of
|
2010-01-02 21:17:59 +00:00
|
|
|
{ok, {attribute, 1, file, {Src, 1}}} ->
|
|
|
|
extract_includes(Epp, Src);
|
|
|
|
{ok, {attribute, 1, file, {IncFile, 1}}} ->
|
2009-12-21 17:15:21 +00:00
|
|
|
[IncFile|extract_includes(Epp, Src)];
|
2010-01-02 21:17:59 +00:00
|
|
|
{ok, _} ->
|
|
|
|
extract_includes(Epp, Src);
|
|
|
|
{eof, _} ->
|
|
|
|
epp:close(Epp),
|
|
|
|
[];
|
|
|
|
{error, _Error} ->
|
|
|
|
extract_includes(Epp, Src)
|
2009-12-21 17:15:21 +00:00
|
|
|
end.
|
|
|
|
|
2010-01-02 21:17:59 +00:00
|
|
|
include_path(Source, Config) ->
|
|
|
|
[filename:dirname(Source) | compile_opts(Config, i)].
|
|
|
|
|
|
|
|
compile_opts(Config, Key) ->
|
|
|
|
rebar_config:get_list(Config, Key, []).
|
|
|
|
|
|
|
|
compile_erl(Source, Target, Config) ->
|
|
|
|
Opts = [{i, "include"}, {outdir, filename:dirname(Target)}, report, return] ++
|
|
|
|
compile_opts(Config, erl_opts),
|
2009-11-29 23:44:30 +00:00
|
|
|
case compile:file(Source, Opts) of
|
2009-12-14 14:27:47 +00:00
|
|
|
{ok, _, []} ->
|
2009-11-25 23:03:14 +00:00
|
|
|
ok;
|
2009-12-14 14:27:47 +00:00
|
|
|
{ok, _, _Warnings} ->
|
|
|
|
%% We got at least one warning -- if fail_on_warning is in options, fail
|
|
|
|
case lists:member(fail_on_warning, Opts) of
|
|
|
|
true ->
|
|
|
|
?FAIL;
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end;
|
|
|
|
_ ->
|
2009-11-25 23:03:14 +00:00
|
|
|
?FAIL
|
|
|
|
end.
|
|
|
|
|
2010-01-02 21:17:59 +00:00
|
|
|
compile_mib(Source, _Target, Config) ->
|
2009-11-29 23:44:30 +00:00
|
|
|
Opts = [{outdir, "priv/mibs"}, {i, ["priv/mibs"]}] ++ compile_opts(Config, mib_opts),
|
|
|
|
case snmpc:compile(Source, Opts) of
|
2009-11-25 23:03:14 +00:00
|
|
|
{ok, _} ->
|
|
|
|
ok;
|
|
|
|
{error, compilation_failed} ->
|
|
|
|
?FAIL
|
|
|
|
end.
|