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]).
|
|
|
|
|
2010-01-04 05:53:04 +00:00
|
|
|
-export([doterl_compile/2]).
|
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-04 05:53:04 +00:00
|
|
|
doterl_compile(Config, "ebin"),
|
|
|
|
rebar_base_compiler:run(Config, rebar_config:get_list(Config, mib_first_files, []),
|
|
|
|
"mibs", ".mib", "priv/mibs", ".bin",
|
|
|
|
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?
|
2010-01-04 13:17:35 +00:00
|
|
|
Dirs = ordsets:from_list([base_dir(F) ||
|
|
|
|
F <- rebar_utils:find_files("ebin", "^.*\\.beam\$")]),
|
|
|
|
case Dirs of
|
|
|
|
[] ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
ok = rebar_file_utils:rm_rf(string:join(Dirs, " "))
|
|
|
|
end.
|
2009-11-25 22:23:42 +00:00
|
|
|
|
|
|
|
|
2009-11-26 03:24:51 +00:00
|
|
|
|
2009-11-25 22:23:42 +00:00
|
|
|
%% ===================================================================
|
2010-01-04 05:53:04 +00:00
|
|
|
%% .erl Compilation API (externally used by only eunit)
|
2009-11-25 22:23:42 +00:00
|
|
|
%% ===================================================================
|
2009-11-25 23:03:14 +00:00
|
|
|
|
2010-01-04 05:53:04 +00:00
|
|
|
doterl_compile(Config, Outdir) ->
|
|
|
|
FirstErls = rebar_config:get_list(Config, erl_first_files, []),
|
2010-01-04 12:40:39 +00:00
|
|
|
RestErls = [Source || Source <- rebar_utils:find_files("src", ".*\\.erl\$"),
|
2010-01-04 05:53:04 +00:00
|
|
|
lists:member(Source, FirstErls) == false],
|
|
|
|
rebar_base_compiler:run(Config, FirstErls, RestErls,
|
|
|
|
fun(S, C) -> internal_erl_compile(S, C, Outdir) end).
|
2009-11-25 23:03:14 +00:00
|
|
|
|
|
|
|
|
2010-01-04 05:53:04 +00:00
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
|
|
|
|
|
|
|
include_path(Source, Config) ->
|
|
|
|
ErlOpts = rebar_config:get(Config, erl_opts, []),
|
|
|
|
[filename:dirname(Source)] ++ proplists:get_all_values(i, ErlOpts).
|
|
|
|
|
|
|
|
inspect(Source, IncludePath) ->
|
|
|
|
ModuleDefault = filename:basename(Source, ".erl"),
|
|
|
|
case epp:open(Source, IncludePath) of
|
2009-12-30 12:13:39 +00:00
|
|
|
{ok, Epp} ->
|
2010-01-04 05:53:04 +00:00
|
|
|
inspect_epp(Epp, ModuleDefault, []);
|
|
|
|
{error, Reason} ->
|
|
|
|
?DEBUG("Failed to inspect ~s: ~p\n", [Source, Reason]),
|
|
|
|
{ModuleDefault, []}
|
2009-12-21 17:15:21 +00:00
|
|
|
end.
|
|
|
|
|
2010-01-04 05:53:04 +00:00
|
|
|
inspect_epp(Epp, Module, Includes) ->
|
2009-12-21 17:15:21 +00:00
|
|
|
case epp:parse_erl_form(Epp) of
|
2010-01-04 05:53:04 +00:00
|
|
|
{ok, {attribute, _, module, ActualModule}} when is_list(ActualModule) ->
|
|
|
|
%% If the module name includes package info, we get a list of atoms...
|
|
|
|
case is_list(ActualModule) of
|
|
|
|
true ->
|
|
|
|
ActualModuleStr = string:join([atom_to_list(P) || P <- ActualModule], ".");
|
|
|
|
false ->
|
|
|
|
ActualModuleStr = atom_to_list(ActualModule)
|
|
|
|
end,
|
|
|
|
inspect_epp(Epp, ActualModuleStr, Includes);
|
|
|
|
{ok, {attribute, 1, file, {Module, 1}}} ->
|
|
|
|
inspect_epp(Epp, Module, Includes);
|
2010-01-02 21:17:59 +00:00
|
|
|
{ok, {attribute, 1, file, {IncFile, 1}}} ->
|
2010-01-04 05:53:04 +00:00
|
|
|
inspect_epp(Epp, Module, [IncFile | Includes]);
|
2010-01-02 21:17:59 +00:00
|
|
|
{eof, _} ->
|
|
|
|
epp:close(Epp),
|
2010-01-04 05:53:04 +00:00
|
|
|
{Module, Includes};
|
|
|
|
_ ->
|
|
|
|
inspect_epp(Epp, Module, Includes)
|
2009-12-21 17:15:21 +00:00
|
|
|
end.
|
|
|
|
|
2010-01-04 05:53:04 +00:00
|
|
|
needs_compile(Source, Target, Hrls) ->
|
|
|
|
TargetLastMod = filelib:last_modified(Target),
|
|
|
|
lists:any(fun(I) -> TargetLastMod < filelib:last_modified(I) end,
|
|
|
|
[Source] ++ Hrls).
|
|
|
|
|
|
|
|
|
|
|
|
internal_erl_compile(Source, Config, Outdir) ->
|
|
|
|
%% Determine the target name and includes list by inspecting the source file
|
|
|
|
{Module, Hrls} = inspect(Source, include_path(Source, Config)),
|
|
|
|
|
|
|
|
%% Construct the target filename
|
|
|
|
Target = filename:join([Outdir | string:tokens(Module, ".")]) ++ ".beam",
|
2010-01-04 12:47:45 +00:00
|
|
|
ok = filelib:ensure_dir(Target),
|
2010-01-04 05:53:04 +00:00
|
|
|
|
|
|
|
%% If the file needs compilation, based on last mod date of includes or
|
|
|
|
%% the target,
|
|
|
|
case needs_compile(Source, Target, Hrls) of
|
|
|
|
true ->
|
|
|
|
Opts = [{i, "include"}, {outdir, filename:dirname(Target)}, report, return] ++
|
|
|
|
rebar_config:get(Config, erl_opts, []),
|
|
|
|
case compile:file(Source, Opts) of
|
|
|
|
{ok, _, []} ->
|
|
|
|
ok;
|
|
|
|
{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;
|
|
|
|
_ ->
|
|
|
|
?FAIL
|
2009-12-14 14:27:47 +00:00
|
|
|
end;
|
2010-01-04 05:53:04 +00:00
|
|
|
false ->
|
|
|
|
skipped
|
2009-11-25 23:03:14 +00:00
|
|
|
end.
|
|
|
|
|
2010-01-02 21:17:59 +00:00
|
|
|
compile_mib(Source, _Target, Config) ->
|
2010-01-04 05:53:04 +00:00
|
|
|
Opts = [{outdir, "priv/mibs"}, {i, ["priv/mibs"]}] ++
|
|
|
|
rebar_config:get(Config, mib_opts, []),
|
2009-11-29 23:44:30 +00:00
|
|
|
case snmpc:compile(Source, Opts) of
|
2009-11-25 23:03:14 +00:00
|
|
|
{ok, _} ->
|
|
|
|
ok;
|
|
|
|
{error, compilation_failed} ->
|
|
|
|
?FAIL
|
|
|
|
end.
|
2010-01-04 13:17:35 +00:00
|
|
|
|
|
|
|
base_dir(Filename) ->
|
|
|
|
["ebin" | Rest] = filename:split(Filename),
|
|
|
|
filename:join("ebin", hd(Rest)).
|