2011-01-31 16:43:31 +00:00
|
|
|
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
|
2009-12-31 18:42:53 +00:00
|
|
|
%% ex: ts=4 sw=4 et
|
2009-11-25 22:23:42 +00:00
|
|
|
%% -------------------------------------------------------------------
|
|
|
|
%%
|
|
|
|
%% rebar: Erlang Build Tools
|
|
|
|
%%
|
2010-02-05 17:34:38 +00:00
|
|
|
%% Copyright (c) 2009, 2010 Dave Smith (dizzyd@dizzyd.com)
|
2009-11-25 22:23:42 +00:00
|
|
|
%%
|
|
|
|
%% 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]).
|
|
|
|
|
2012-11-10 20:59:19 +00:00
|
|
|
%% for internal use only
|
|
|
|
-export([test_compile/3,
|
|
|
|
info/2]).
|
2009-12-05 22:18:09 +00:00
|
|
|
|
2009-11-25 22:23:42 +00:00
|
|
|
-include("rebar.hrl").
|
mib_to_hrl compilation verbosity via 'mib_opts'
Previously, the configuration setting 'mib_opts' in rebar.config
would affect the call to snmpc:compile/2, so that (for example)
verbosity could be controlled. However, the subsequent call to
snmpc:mib_to_hrl/1 did not include any of these options, so it
did not appear to be possible to control the verbosity of the
process of converting a MIB to a .hrl file. To make matters
worse, the default was to dump a full trace -- including debug
output and various logging -- so the act of compiling a large
number of MIBs could result in a huge amount of "noisy" output
that hid any signal (meaningful warnings, errors, etc.).
This commit addresses that issue by replacing the call to
snmpc:mib_to_hrl/1 with a call to snmpc:mib_to_hrl/3 instead,
which includes an "options" argument that, at present, is only
capable of setting verbosity. The verbosity setting is taken
from the 'mib_opts' setting in rebar_config, if present, and
the approriate kind of argument is passed to snmpc:mib_to_hrl/3.
It should be noted that snmpc:mib_to_hrl/3 is not listed in
Erlang's documentation, but does appear in the list of "API"
exports at the top of snmpc.erl in R15B01 (and remains that way
in R16B01), so this appears to be more of a documentation oversight
than the use of a deep, dark function call that was not intended
to be public. snmpc:mib_to_hrl/3 accepts an #options{} record
(defined in lib/srdlib/include/erl_compile.hrl within Erlang's
source distribution), though most of the fields in that record
are ignored by snmpc:mib_to_hrl/3; only verbosity can be controlled
this way.
2013-09-08 07:37:47 +00:00
|
|
|
-include_lib("stdlib/include/erl_compile.hrl").
|
2009-11-25 22:23:42 +00:00
|
|
|
|
2013-09-01 17:34:29 +00:00
|
|
|
-define(ERLCINFO_VSN, 1).
|
|
|
|
-define(ERLCINFO_FILE, "erlcinfo").
|
|
|
|
-type erlc_info_v() :: {digraph:vertex(), term()} | 'false'.
|
|
|
|
-type erlc_info_e() :: {digraph:vertex(), digraph:vertex()}.
|
|
|
|
-type erlc_info() :: {list(erlc_info_v()), list(erlc_info_e())}.
|
|
|
|
-record(erlcinfo,
|
|
|
|
{
|
|
|
|
vsn = ?ERLCINFO_VSN :: pos_integer(),
|
|
|
|
info = {[], []} :: erlc_info()
|
|
|
|
}).
|
2013-07-01 09:55:31 +00:00
|
|
|
|
2009-11-25 22:23:42 +00:00
|
|
|
%% ===================================================================
|
|
|
|
%% Public API
|
|
|
|
%% ===================================================================
|
|
|
|
|
2010-05-03 02:03:38 +00:00
|
|
|
%% Supported configuration variables:
|
|
|
|
%%
|
|
|
|
%% * erl_opts - Erlang list of options passed to compile:file/2
|
|
|
|
%% It is also possible to specify platform specific
|
2011-01-09 15:06:51 +00:00
|
|
|
%% options by specifying a pair or a triplet where the
|
|
|
|
%% first string is a regex that is checked against the
|
|
|
|
%% string
|
|
|
|
%%
|
|
|
|
%% OtpRelease ++ "-" ++ SysArch ++ "-" ++ Words.
|
|
|
|
%%
|
|
|
|
%% where
|
|
|
|
%%
|
|
|
|
%% OtpRelease = erlang:system_info(otp_release).
|
|
|
|
%% SysArch = erlang:system_info(system_architecture).
|
Use external wordsize to get emulator build arch
Calling erlang:system_info(wordsize) yields the internal word size of
the Erlang emulator. But due to the halfword emulator, need to pass
{wordsize, external} instead to get the word size, or pointer size, as
seen by external code such as NIFs. The halfword emulator has 4 byte
internal words but 8 byte external words due to 64-bit compilation,
which means NIFs for the halfword emulator also have to be compiled
64-bit. But just passing wordsize is equivalent to passing {wordsize,
internal}, which does not indicate the pointer size for the halfword
emulator.
Older versions of Erlang do not support {wordsize, external}, though,
so continue to pass just wordsize for those versions.
2011-05-31 01:13:38 +00:00
|
|
|
%% Words = integer_to_list(8 *
|
|
|
|
%% erlang:system_info({wordsize, external})).
|
2011-01-09 15:06:51 +00:00
|
|
|
%%
|
|
|
|
%% E.g. to define HAVE_SENDFILE only on systems with
|
|
|
|
%% sendfile(), to define BACKLOG on Linux/FreeBSD as 128,
|
|
|
|
%% and to define 'old_inets' for R13 OTP release do:
|
|
|
|
%%
|
2010-05-03 02:03:38 +00:00
|
|
|
%% {erl_opts, [{platform_define,
|
|
|
|
%% "(linux|solaris|freebsd|darwin)",
|
|
|
|
%% 'HAVE_SENDFILE'},
|
2010-06-12 11:38:41 +00:00
|
|
|
%% {platform_define, "(linux|freebsd)",
|
2011-01-09 15:06:51 +00:00
|
|
|
%% 'BACKLOG', 128},
|
|
|
|
%% {platform_define, "R13",
|
|
|
|
%% 'old_inets'}]}.
|
2010-05-03 02:03:38 +00:00
|
|
|
%%
|
|
|
|
|
2012-08-06 17:49:43 +00:00
|
|
|
-spec compile(rebar_config:config(), file:filename()) -> 'ok'.
|
2009-11-30 14:00:48 +00:00
|
|
|
compile(Config, _AppFile) ->
|
2010-03-31 19:03:29 +00:00
|
|
|
rebar_base_compiler:run(Config,
|
2011-01-28 15:08:27 +00:00
|
|
|
check_files(rebar_config:get_local(
|
|
|
|
Config, xrl_first_files, [])),
|
2010-03-31 19:03:29 +00:00
|
|
|
"src", ".xrl", "src", ".erl",
|
2010-03-31 19:21:13 +00:00
|
|
|
fun compile_xrl/3),
|
2010-03-31 19:03:29 +00:00
|
|
|
rebar_base_compiler:run(Config,
|
2011-01-28 15:08:27 +00:00
|
|
|
check_files(rebar_config:get_local(
|
|
|
|
Config, yrl_first_files, [])),
|
2010-03-31 15:35:06 +00:00
|
|
|
"src", ".yrl", "src", ".erl",
|
2010-03-31 19:21:13 +00:00
|
|
|
fun compile_yrl/3),
|
2010-12-01 14:44:37 +00:00
|
|
|
rebar_base_compiler:run(Config,
|
2011-01-28 15:08:27 +00:00
|
|
|
check_files(rebar_config:get_local(
|
|
|
|
Config, mib_first_files, [])),
|
2010-01-04 05:53:04 +00:00
|
|
|
"mibs", ".mib", "priv/mibs", ".bin",
|
2011-08-03 13:37:21 +00:00
|
|
|
fun compile_mib/3),
|
|
|
|
doterl_compile(Config, "ebin").
|
2009-11-25 22:23:42 +00:00
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
-spec clean(rebar_config:config(), file:filename()) -> 'ok'.
|
2013-09-01 17:34:29 +00:00
|
|
|
clean(Config, _AppFile) ->
|
2011-08-03 13:37:21 +00:00
|
|
|
MibFiles = rebar_utils:find_files("mibs", "^.*\\.mib\$"),
|
|
|
|
MIBs = [filename:rootname(filename:basename(MIB)) || MIB <- MibFiles],
|
|
|
|
rebar_file_utils:delete_each(
|
|
|
|
[filename:join(["include",MIB++".hrl"]) || MIB <- MIBs]),
|
2010-10-26 04:19:30 +00:00
|
|
|
lists:foreach(fun(F) -> ok = rebar_file_utils:rm_rf(F) end,
|
|
|
|
["ebin/*.beam", "priv/mibs/*.bin"]),
|
2010-01-02 21:17:59 +00:00
|
|
|
|
2010-03-31 19:03:29 +00:00
|
|
|
YrlFiles = rebar_utils:find_files("src", "^.*\\.[x|y]rl\$"),
|
2010-03-31 15:35:06 +00:00
|
|
|
rebar_file_utils:delete_each(
|
2010-03-31 19:03:29 +00:00
|
|
|
[ binary_to_list(iolist_to_binary(re:replace(F, "\\.[x|y]rl$", ".erl")))
|
2011-03-10 14:07:41 +00:00
|
|
|
|| F <- YrlFiles ]),
|
2010-03-31 15:35:06 +00:00
|
|
|
|
2013-07-01 09:55:31 +00:00
|
|
|
%% Delete the build graph, if any
|
2013-09-01 17:34:29 +00:00
|
|
|
rebar_file_utils:rm_rf(erlcinfo_file(Config)),
|
2013-07-01 09:55:31 +00:00
|
|
|
|
2010-01-02 21:17:59 +00:00
|
|
|
%% 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
|
2010-01-04 16:39:52 +00:00
|
|
|
%% satisfy our criteria.
|
2010-02-16 22:16:19 +00:00
|
|
|
BeamFiles = rebar_utils:find_files("ebin", "^.*\\.beam\$"),
|
2010-01-04 16:39:52 +00:00
|
|
|
rebar_file_utils:delete_each(BeamFiles),
|
|
|
|
lists:foreach(fun(Dir) -> delete_dir(Dir, dirs(Dir)) end, dirs("ebin")),
|
|
|
|
ok.
|
2009-11-25 22:23:42 +00:00
|
|
|
|
2011-03-26 16:56:54 +00:00
|
|
|
%% ===================================================================
|
|
|
|
%% .erl Compilation API (externally used by only eunit and qc)
|
|
|
|
%% ===================================================================
|
|
|
|
|
2012-08-09 16:37:26 +00:00
|
|
|
test_compile(Config, Cmd, OutDir) ->
|
2011-03-26 16:56:54 +00:00
|
|
|
%% Obtain all the test modules for inclusion in the compile stage.
|
|
|
|
TestErls = rebar_utils:find_files("test", ".*\\.erl\$"),
|
|
|
|
|
|
|
|
%% Copy source files to eunit dir for cover in case they are not directly
|
|
|
|
%% in src but in a subdirectory of src. Cover only looks in cwd and ../src
|
|
|
|
%% for source files. Also copy files from src_dirs.
|
|
|
|
ErlOpts = rebar_utils:erl_opts(Config),
|
|
|
|
|
|
|
|
SrcDirs = rebar_utils:src_dirs(proplists:append_values(src_dirs, ErlOpts)),
|
|
|
|
SrcErls = lists:foldl(
|
|
|
|
fun(Dir, Acc) ->
|
|
|
|
Files = rebar_utils:find_files(Dir, ".*\\.erl\$"),
|
|
|
|
lists:append(Acc, Files)
|
|
|
|
end, [], SrcDirs),
|
|
|
|
|
2014-02-25 20:07:56 +00:00
|
|
|
%% If it is not the first time rebar eunit or rebar qc is executed,
|
|
|
|
%% there will be source files already present in OutDir. Since some
|
|
|
|
%% SCMs (like Perforce) set the source files as being read only (unless
|
|
|
|
%% they are checked out), we need to be sure that the files already
|
|
|
|
%% present in OutDir are writable before doing the copy. This is done
|
|
|
|
%% here by removing any file that was already present before calling
|
|
|
|
%% rebar_file_utils:cp_r.
|
2011-03-26 16:56:54 +00:00
|
|
|
|
2012-08-09 16:37:26 +00:00
|
|
|
%% Get the full path to a file that was previously copied in OutDir
|
2011-03-26 16:56:54 +00:00
|
|
|
ToCleanUp = fun(F, Acc) ->
|
|
|
|
F2 = filename:basename(F),
|
2012-08-09 16:37:26 +00:00
|
|
|
F3 = filename:join([OutDir, F2]),
|
2011-03-26 16:56:54 +00:00
|
|
|
case filelib:is_regular(F3) of
|
|
|
|
true -> [F3|Acc];
|
|
|
|
false -> Acc
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
ok = rebar_file_utils:delete_each(lists:foldl(ToCleanUp, [], TestErls)),
|
|
|
|
ok = rebar_file_utils:delete_each(lists:foldl(ToCleanUp, [], SrcErls)),
|
|
|
|
|
2012-08-09 16:37:26 +00:00
|
|
|
ok = rebar_file_utils:cp_r(SrcErls ++ TestErls, OutDir),
|
2011-03-26 16:56:54 +00:00
|
|
|
|
2012-08-09 16:37:26 +00:00
|
|
|
%% Compile erlang code to OutDir, using a tweaked config
|
2011-03-26 16:56:54 +00:00
|
|
|
%% with appropriate defines for eunit, and include all the test modules
|
|
|
|
%% as well.
|
2013-09-30 16:57:07 +00:00
|
|
|
ok = doterl_compile(test_compile_config(Config, ErlOpts, Cmd),
|
|
|
|
OutDir, TestErls),
|
2011-03-26 16:56:54 +00:00
|
|
|
|
|
|
|
{ok, SrcErls}.
|
2009-11-26 03:24:51 +00:00
|
|
|
|
2009-11-25 22:23:42 +00:00
|
|
|
%% ===================================================================
|
2011-03-26 16:56:54 +00:00
|
|
|
%% Internal functions
|
2009-11-25 22:23:42 +00:00
|
|
|
%% ===================================================================
|
2009-11-25 23:03:14 +00:00
|
|
|
|
2012-11-10 20:59:19 +00:00
|
|
|
info(help, compile) ->
|
|
|
|
info_help("Build *.erl, *.yrl, *.xrl, and *.mib sources");
|
|
|
|
info(help, clean) ->
|
|
|
|
info_help("Delete *.erl, *.yrl, *.xrl, and *.mib build results").
|
|
|
|
|
|
|
|
info_help(Description) ->
|
|
|
|
?CONSOLE(
|
|
|
|
"~s.~n"
|
|
|
|
"~n"
|
|
|
|
"Valid rebar.config options:~n"
|
|
|
|
" ~p~n"
|
|
|
|
" ~p~n"
|
|
|
|
" ~p~n"
|
|
|
|
" ~p~n"
|
|
|
|
" ~p~n"
|
|
|
|
" ~p~n"
|
|
|
|
" ~p~n"
|
|
|
|
" ~p~n",
|
|
|
|
[
|
|
|
|
Description,
|
|
|
|
{erl_opts, [no_debug_info,
|
|
|
|
{i, "myinclude"},
|
|
|
|
{src_dirs, ["src", "src2", "src3"]},
|
|
|
|
{platform_define,
|
|
|
|
"(linux|solaris|freebsd|darwin)", 'HAVE_SENDFILE'},
|
|
|
|
{platform_define, "(linux|freebsd)", 'BACKLOG', 128},
|
|
|
|
{platform_define, "R13", 'old_inets'}]},
|
|
|
|
{erl_first_files, ["mymib1", "mymib2"]},
|
|
|
|
{mib_opts, []},
|
|
|
|
{mib_first_files, []},
|
|
|
|
{xrl_opts, []},
|
|
|
|
{xrl_first_files, []},
|
|
|
|
{yrl_opts, []},
|
|
|
|
{yrl_first_files, []}
|
|
|
|
]).
|
|
|
|
|
2013-09-30 16:57:07 +00:00
|
|
|
test_compile_config(Config, ErlOpts, Cmd) ->
|
2011-03-26 16:56:54 +00:00
|
|
|
{Config1, TriqOpts} = triq_opts(Config),
|
|
|
|
{Config2, PropErOpts} = proper_opts(Config1),
|
|
|
|
{Config3, EqcOpts} = eqc_opts(Config2),
|
|
|
|
|
2012-08-09 11:58:32 +00:00
|
|
|
OptsAtom = list_to_atom(Cmd ++ "_compile_opts"),
|
|
|
|
EunitOpts = rebar_config:get_list(Config3, OptsAtom, []),
|
2011-03-26 16:56:54 +00:00
|
|
|
Opts0 = [{d, 'TEST'}] ++
|
|
|
|
ErlOpts ++ EunitOpts ++ TriqOpts ++ PropErOpts ++ EqcOpts,
|
|
|
|
Opts = [O || O <- Opts0, O =/= no_debug_info],
|
|
|
|
Config4 = rebar_config:set(Config3, erl_opts, Opts),
|
|
|
|
|
2012-08-09 11:58:32 +00:00
|
|
|
FirstFilesAtom = list_to_atom(Cmd ++ "_first_files"),
|
|
|
|
FirstErls = rebar_config:get_list(Config4, FirstFilesAtom, []),
|
2011-03-26 16:56:54 +00:00
|
|
|
rebar_config:set(Config4, erl_first_files, FirstErls).
|
|
|
|
|
|
|
|
triq_opts(Config) ->
|
|
|
|
{NewConfig, IsAvail} = is_lib_avail(Config, is_triq_avail, triq,
|
|
|
|
"triq.hrl", "Triq"),
|
|
|
|
Opts = define_if('TRIQ', IsAvail),
|
|
|
|
{NewConfig, Opts}.
|
|
|
|
|
|
|
|
proper_opts(Config) ->
|
|
|
|
{NewConfig, IsAvail} = is_lib_avail(Config, is_proper_avail, proper,
|
|
|
|
"proper.hrl", "PropEr"),
|
|
|
|
Opts = define_if('PROPER', IsAvail),
|
|
|
|
{NewConfig, Opts}.
|
|
|
|
|
|
|
|
eqc_opts(Config) ->
|
|
|
|
{NewConfig, IsAvail} = is_lib_avail(Config, is_eqc_avail, eqc,
|
|
|
|
"eqc.hrl", "QuickCheck"),
|
|
|
|
Opts = define_if('EQC', IsAvail),
|
|
|
|
{NewConfig, Opts}.
|
|
|
|
|
|
|
|
define_if(Def, true) -> [{d, Def}];
|
|
|
|
define_if(_Def, false) -> [].
|
|
|
|
|
|
|
|
is_lib_avail(Config, DictKey, Mod, Hrl, Name) ->
|
|
|
|
case rebar_config:get_xconf(Config, DictKey, undefined) of
|
|
|
|
undefined ->
|
|
|
|
IsAvail = case code:lib_dir(Mod, include) of
|
|
|
|
{error, bad_name} ->
|
|
|
|
false;
|
|
|
|
Dir ->
|
|
|
|
filelib:is_regular(filename:join(Dir, Hrl))
|
|
|
|
end,
|
|
|
|
NewConfig = rebar_config:set_xconf(Config, DictKey, IsAvail),
|
|
|
|
?DEBUG("~s availability: ~p\n", [Name, IsAvail]),
|
|
|
|
{NewConfig, IsAvail};
|
|
|
|
IsAvail ->
|
|
|
|
{Config, IsAvail}
|
|
|
|
end.
|
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
-spec doterl_compile(rebar_config:config(), file:filename()) -> 'ok'.
|
2010-02-01 15:37:52 +00:00
|
|
|
doterl_compile(Config, OutDir) ->
|
|
|
|
doterl_compile(Config, OutDir, []).
|
|
|
|
|
|
|
|
doterl_compile(Config, OutDir, MoreSources) ->
|
2013-09-01 17:34:29 +00:00
|
|
|
ErlFirstFiles = rebar_config:get_list(Config, erl_first_files, []),
|
2012-07-02 09:12:17 +00:00
|
|
|
ErlOpts = rebar_utils:erl_opts(Config),
|
2011-10-22 12:27:19 +00:00
|
|
|
?DEBUG("erl_opts ~p~n", [ErlOpts]),
|
2010-02-05 17:34:38 +00:00
|
|
|
%% Support the src_dirs option allowing multiple directories to
|
2010-10-25 22:38:51 +00:00
|
|
|
%% contain erlang source. This might be used, for example, should
|
|
|
|
%% eunit tests be separated from the core application source.
|
2012-07-02 09:12:17 +00:00
|
|
|
SrcDirs = rebar_utils:src_dirs(proplists:append_values(src_dirs, ErlOpts)),
|
2010-02-05 17:34:38 +00:00
|
|
|
RestErls = [Source || Source <- gather_src(SrcDirs, []) ++ MoreSources,
|
2013-09-01 17:34:29 +00:00
|
|
|
not lists:member(Source, ErlFirstFiles)],
|
2010-05-15 20:57:07 +00:00
|
|
|
%% Make sure that ebin/ exists and is on the path
|
|
|
|
ok = filelib:ensure_dir(filename:join("ebin", "dummy.beam")),
|
2010-02-16 22:16:19 +00:00
|
|
|
CurrPath = code:get_path(),
|
2011-02-15 00:51:35 +00:00
|
|
|
true = code:add_path(filename:absname("ebin")),
|
2012-08-18 15:18:15 +00:00
|
|
|
OutDir1 = proplists:get_value(outdir, ErlOpts, OutDir),
|
2013-09-01 17:34:29 +00:00
|
|
|
G = init_erlcinfo(Config, RestErls),
|
|
|
|
%% Split RestErls so that files which are depended on are treated
|
|
|
|
%% like erl_first_files.
|
2013-07-01 09:55:31 +00:00
|
|
|
{OtherFirstErls, OtherErls} =
|
|
|
|
lists:partition(
|
|
|
|
fun(F) ->
|
2013-09-01 17:34:29 +00:00
|
|
|
Children = get_children(G, F),
|
|
|
|
log_files(?FMT("Files dependent on ~s", [F]), Children),
|
|
|
|
|
|
|
|
case erls(Children) of
|
2013-07-01 09:55:31 +00:00
|
|
|
[] ->
|
|
|
|
%% There are no files dependent on this file.
|
|
|
|
false;
|
|
|
|
_ ->
|
|
|
|
%% There are some files dependent on the file.
|
|
|
|
%% Thus the file has higher priority
|
|
|
|
%% and should be compiled in the first place.
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end, RestErls),
|
2013-09-01 17:34:29 +00:00
|
|
|
%% Dependencies of OtherFirstErls that must be compiled first.
|
|
|
|
OtherFirstErlsDeps = lists:flatmap(
|
|
|
|
fun(Erl) -> erls(get_parents(G, Erl)) end,
|
|
|
|
OtherFirstErls),
|
|
|
|
%% NOTE: In case the way we retrieve OtherFirstErlsDeps or merge
|
|
|
|
%% it with OtherFirstErls does not result in the correct compile
|
|
|
|
%% priorities, or the method in use proves to be too slow for
|
|
|
|
%% certain projects, consider using a more elaborate method (maybe
|
|
|
|
%% digraph_utils) or alternatively getting and compiling the .erl
|
|
|
|
%% parents of an individual Source in internal_erl_compile. By not
|
|
|
|
%% handling this in internal_erl_compile, we also avoid extra
|
|
|
|
%% needs_compile/2 calls.
|
|
|
|
FirstErls = ErlFirstFiles ++ uo_merge(OtherFirstErlsDeps, OtherFirstErls),
|
|
|
|
?DEBUG("Files to compile first: ~p~n", [FirstErls]),
|
2013-07-01 09:55:31 +00:00
|
|
|
rebar_base_compiler:run(
|
2013-09-01 17:34:29 +00:00
|
|
|
Config, FirstErls, OtherErls,
|
2013-07-01 09:55:31 +00:00
|
|
|
fun(S, C) ->
|
|
|
|
internal_erl_compile(C, S, OutDir1, ErlOpts, G)
|
|
|
|
end),
|
2010-10-10 21:24:20 +00:00
|
|
|
true = code:set_path(CurrPath),
|
2010-02-16 22:16:19 +00:00
|
|
|
ok.
|
2010-02-01 15:37:52 +00:00
|
|
|
|
2013-09-01 17:34:29 +00:00
|
|
|
%%
|
|
|
|
%% Return all .erl files from a list of files
|
|
|
|
%%
|
|
|
|
erls(Files) ->
|
|
|
|
[Erl || Erl <- Files, filename:extension(Erl) =:= ".erl"].
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Return a list without duplicates while preserving order
|
|
|
|
%%
|
|
|
|
ulist(L) ->
|
|
|
|
ulist(L, []).
|
|
|
|
|
|
|
|
ulist([H|T], Acc) ->
|
|
|
|
case lists:member(H, T) of
|
|
|
|
true ->
|
|
|
|
ulist(T, Acc);
|
|
|
|
false ->
|
|
|
|
ulist(T, [H|Acc])
|
|
|
|
end;
|
|
|
|
ulist([], Acc) ->
|
|
|
|
lists:reverse(Acc).
|
|
|
|
|
|
|
|
%%
|
|
|
|
%% Merge two lists without duplicates while preserving order
|
|
|
|
%%
|
|
|
|
uo_merge(L1, L2) ->
|
|
|
|
lists:foldl(fun(E, Acc) -> u_add_element(E, Acc) end, ulist(L1), L2).
|
|
|
|
|
|
|
|
u_add_element(Elem, [Elem|_]=Set) -> Set;
|
|
|
|
u_add_element(Elem, [E1|Set]) -> [E1|u_add_element(Elem, Set)];
|
|
|
|
u_add_element(Elem, []) -> [Elem].
|
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
-spec include_path(file:filename(),
|
|
|
|
rebar_config:config()) -> [file:filename(), ...].
|
2010-01-04 05:53:04 +00:00
|
|
|
include_path(Source, Config) ->
|
|
|
|
ErlOpts = rebar_config:get(Config, erl_opts, []),
|
2013-07-01 09:55:31 +00:00
|
|
|
lists:usort(["include", filename:dirname(Source)]
|
|
|
|
++ proplists:get_all_values(i, ErlOpts)).
|
2009-12-21 17:15:21 +00:00
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
-spec needs_compile(file:filename(), file:filename(),
|
|
|
|
[string()]) -> boolean().
|
2013-09-01 17:34:29 +00:00
|
|
|
needs_compile(Source, Target, Parents) ->
|
2010-01-04 05:53:04 +00:00
|
|
|
TargetLastMod = filelib:last_modified(Target),
|
|
|
|
lists:any(fun(I) -> TargetLastMod < filelib:last_modified(I) end,
|
2013-09-01 17:34:29 +00:00
|
|
|
[Source] ++ Parents).
|
2010-01-04 05:53:04 +00:00
|
|
|
|
2013-09-01 17:34:29 +00:00
|
|
|
check_erlcinfo(_Config, #erlcinfo{vsn=?ERLCINFO_VSN}) ->
|
|
|
|
ok;
|
|
|
|
check_erlcinfo(Config, #erlcinfo{vsn=Vsn}) ->
|
|
|
|
?ABORT("~s file version is incompatible. expected: ~b got: ~b~n",
|
|
|
|
[erlcinfo_file(Config), ?ERLCINFO_VSN, Vsn]);
|
|
|
|
check_erlcinfo(Config, _) ->
|
|
|
|
?ABORT("~s file is invalid. Please delete before next run.~n",
|
|
|
|
[erlcinfo_file(Config)]).
|
|
|
|
|
|
|
|
erlcinfo_file(Config) ->
|
|
|
|
filename:join([rebar_utils:base_dir(Config), ".rebar", ?ERLCINFO_FILE]).
|
|
|
|
|
|
|
|
init_erlcinfo(Config, Erls) ->
|
|
|
|
G = restore_erlcinfo(Config),
|
|
|
|
%% Get a unique list of dirs based on the source files' locations.
|
|
|
|
%% This is used for finding files in sub dirs of the configured
|
|
|
|
%% src_dirs. For example, src/sub_dir/foo.erl.
|
|
|
|
Dirs = sets:to_list(lists:foldl(
|
|
|
|
fun(Erl, Acc) ->
|
|
|
|
Dir = filename:dirname(Erl),
|
|
|
|
sets:add_element(Dir, Acc)
|
|
|
|
end, sets:new(), Erls)),
|
|
|
|
Updates = [update_erlcinfo(G, Erl, include_path(Erl, Config) ++ Dirs)
|
|
|
|
|| Erl <- Erls],
|
|
|
|
Modified = lists:member(modified, Updates),
|
|
|
|
ok = store_erlcinfo(G, Config, Modified),
|
2013-07-01 09:55:31 +00:00
|
|
|
G.
|
|
|
|
|
2013-09-01 17:34:29 +00:00
|
|
|
update_erlcinfo(G, Source, Dirs) ->
|
2013-07-01 09:55:31 +00:00
|
|
|
case digraph:vertex(G, Source) of
|
|
|
|
{_, LastUpdated} ->
|
|
|
|
LastModified = filelib:last_modified(Source),
|
|
|
|
if LastModified == 0 ->
|
|
|
|
%% The file doesn't exist anymore,
|
|
|
|
%% erase it from the graph.
|
|
|
|
%% All the edges will be erased automatically.
|
2013-09-01 17:34:29 +00:00
|
|
|
digraph:del_vertex(G, Source),
|
|
|
|
modified;
|
2013-07-01 09:55:31 +00:00
|
|
|
LastUpdated < LastModified ->
|
2013-09-01 17:34:29 +00:00
|
|
|
modify_erlcinfo(G, Source, Dirs);
|
|
|
|
modified;
|
2013-07-01 09:55:31 +00:00
|
|
|
true ->
|
2013-09-01 17:34:29 +00:00
|
|
|
unmodified
|
2013-07-01 09:55:31 +00:00
|
|
|
end;
|
|
|
|
false ->
|
2013-09-01 17:34:29 +00:00
|
|
|
modify_erlcinfo(G, Source, Dirs),
|
|
|
|
modified
|
2013-07-01 09:55:31 +00:00
|
|
|
end.
|
|
|
|
|
2013-09-01 17:34:29 +00:00
|
|
|
modify_erlcinfo(G, Source, Dirs) ->
|
|
|
|
{ok, Fd} = file:open(Source, [read]),
|
|
|
|
Incls = parse_attrs(Fd, []),
|
|
|
|
AbsIncls = expand_file_names(Incls, Dirs),
|
|
|
|
ok = file:close(Fd),
|
|
|
|
LastUpdated = {date(), time()},
|
|
|
|
digraph:add_vertex(G, Source, LastUpdated),
|
|
|
|
lists:foreach(
|
|
|
|
fun(Incl) ->
|
|
|
|
update_erlcinfo(G, Incl, Dirs),
|
|
|
|
digraph:add_edge(G, Source, Incl)
|
|
|
|
end, AbsIncls).
|
2013-07-01 09:55:31 +00:00
|
|
|
|
2013-09-01 17:34:29 +00:00
|
|
|
restore_erlcinfo(Config) ->
|
|
|
|
File = erlcinfo_file(Config),
|
2013-07-01 09:55:31 +00:00
|
|
|
G = digraph:new(),
|
|
|
|
case file:read_file(File) of
|
|
|
|
{ok, Data} ->
|
2013-09-01 17:34:29 +00:00
|
|
|
try binary_to_term(Data) of
|
|
|
|
Erlcinfo ->
|
|
|
|
ok = check_erlcinfo(Config, Erlcinfo),
|
|
|
|
#erlcinfo{info=ErlcInfo} = Erlcinfo,
|
|
|
|
{Vs, Es} = ErlcInfo,
|
2013-07-01 09:55:31 +00:00
|
|
|
lists:foreach(
|
|
|
|
fun({V, LastUpdated}) ->
|
|
|
|
digraph:add_vertex(G, V, LastUpdated)
|
|
|
|
end, Vs),
|
|
|
|
lists:foreach(
|
|
|
|
fun({V1, V2}) ->
|
|
|
|
digraph:add_edge(G, V1, V2)
|
|
|
|
end, Es)
|
2013-09-01 17:34:29 +00:00
|
|
|
catch
|
|
|
|
error:badarg ->
|
|
|
|
?ERROR(
|
|
|
|
"Failed (binary_to_term) to restore rebar info file."
|
|
|
|
" Discard file.~n", []),
|
|
|
|
ok
|
2013-07-01 09:55:31 +00:00
|
|
|
end;
|
|
|
|
_Err ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
G.
|
|
|
|
|
2013-09-01 17:34:29 +00:00
|
|
|
store_erlcinfo(_G, _Config, _Modified = false) ->
|
2013-07-01 09:55:31 +00:00
|
|
|
ok;
|
2013-09-01 17:34:29 +00:00
|
|
|
store_erlcinfo(G, Config, _Modified) ->
|
2013-07-01 09:55:31 +00:00
|
|
|
Vs = lists:map(
|
|
|
|
fun(V) ->
|
|
|
|
digraph:vertex(G, V)
|
|
|
|
end, digraph:vertices(G)),
|
|
|
|
Es = lists:flatmap(
|
|
|
|
fun({V, _}) ->
|
|
|
|
lists:map(
|
|
|
|
fun(E) ->
|
|
|
|
{_, V1, V2, _} = digraph:edge(G, E),
|
|
|
|
{V1, V2}
|
|
|
|
end, digraph:out_edges(G, V))
|
|
|
|
end, Vs),
|
2013-09-01 17:34:29 +00:00
|
|
|
File = erlcinfo_file(Config),
|
|
|
|
ok = filelib:ensure_dir(File),
|
|
|
|
Data = term_to_binary(#erlcinfo{info={Vs, Es}}, [{compressed, 9}]),
|
|
|
|
file:write_file(File, Data).
|
|
|
|
|
|
|
|
%% NOTE: If, for example, one of the entries in Files, refers to
|
|
|
|
%% gen_server.erl, that entry will be dropped. It is dropped because
|
|
|
|
%% such an entry usually refers to the beam file, and we don't pass a
|
|
|
|
%% list of OTP src dirs for finding gen_server.erl's full path. Also,
|
|
|
|
%% if gen_server.erl was modified, it's not rebar's task to compile a
|
|
|
|
%% new version of the beam file. Therefore, it's reasonable to drop
|
|
|
|
%% such entries. Also see process_attr(behaviour, Form, Includes).
|
2013-07-01 09:55:31 +00:00
|
|
|
-spec expand_file_names([file:filename()],
|
|
|
|
[file:filename()]) -> [file:filename()].
|
2013-09-01 17:34:29 +00:00
|
|
|
expand_file_names(Files, Dirs) ->
|
|
|
|
%% We check if Files exist by itself or within the directories
|
|
|
|
%% listed in Dirs.
|
2013-07-01 09:55:31 +00:00
|
|
|
%% Return the list of files matched.
|
|
|
|
lists:flatmap(
|
|
|
|
fun(Incl) ->
|
|
|
|
case filelib:is_regular(Incl) of
|
|
|
|
true ->
|
|
|
|
[Incl];
|
|
|
|
false ->
|
|
|
|
lists:flatmap(
|
2013-09-01 17:34:29 +00:00
|
|
|
fun(Dir) ->
|
|
|
|
FullPath = filename:join(Dir, Incl),
|
2013-07-01 09:55:31 +00:00
|
|
|
case filelib:is_regular(FullPath) of
|
|
|
|
true ->
|
|
|
|
[FullPath];
|
|
|
|
false ->
|
|
|
|
[]
|
|
|
|
end
|
2013-09-01 17:34:29 +00:00
|
|
|
end, Dirs)
|
2013-07-01 09:55:31 +00:00
|
|
|
end
|
|
|
|
end, Files).
|
|
|
|
|
|
|
|
-spec get_parents(digraph(), file:filename()) -> [file:filename()].
|
|
|
|
get_parents(G, Source) ->
|
|
|
|
%% Return all files which the Source depends upon.
|
|
|
|
digraph_utils:reachable_neighbours([Source], G).
|
|
|
|
|
|
|
|
-spec get_children(digraph(), file:filename()) -> [file:filename()].
|
|
|
|
get_children(G, Source) ->
|
|
|
|
%% Return all files dependent on the Source.
|
|
|
|
digraph_utils:reaching_neighbours([Source], G).
|
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
-spec internal_erl_compile(rebar_config:config(), file:filename(),
|
2013-07-01 09:55:31 +00:00
|
|
|
file:filename(), list(),
|
|
|
|
digraph()) -> 'ok' | 'skipped'.
|
2013-09-01 17:34:29 +00:00
|
|
|
internal_erl_compile(Config, Source, OutDir, ErlOpts, G) ->
|
2010-01-04 05:53:04 +00:00
|
|
|
%% Determine the target name and includes list by inspecting the source file
|
2013-07-01 09:55:31 +00:00
|
|
|
Module = filename:basename(Source, ".erl"),
|
2013-09-01 17:34:29 +00:00
|
|
|
Parents = get_parents(G, Source),
|
|
|
|
log_files(?FMT("~s depends on", [Source]), Parents),
|
2010-01-04 05:53:04 +00:00
|
|
|
|
|
|
|
%% Construct the target filename
|
2013-09-01 17:34:29 +00:00
|
|
|
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
|
2011-03-10 14:07:41 +00:00
|
|
|
%% the target
|
2013-09-01 17:34:29 +00:00
|
|
|
case needs_compile(Source, Target, Parents) of
|
2010-01-04 05:53:04 +00:00
|
|
|
true ->
|
2010-12-30 13:32:49 +00:00
|
|
|
Opts = [{outdir, filename:dirname(Target)}] ++
|
2012-05-22 16:13:38 +00:00
|
|
|
ErlOpts ++ [{i, "include"}, return],
|
2010-01-04 05:53:04 +00:00
|
|
|
case compile:file(Source, Opts) of
|
2012-05-22 16:13:38 +00:00
|
|
|
{ok, _Mod} ->
|
2010-01-04 05:53:04 +00:00
|
|
|
ok;
|
2012-05-22 16:13:38 +00:00
|
|
|
{ok, _Mod, Ws} ->
|
2012-08-05 17:56:27 +00:00
|
|
|
rebar_base_compiler:ok_tuple(Config, Source, Ws);
|
2012-05-22 16:13:38 +00:00
|
|
|
{error, Es, Ws} ->
|
2012-08-05 17:56:27 +00:00
|
|
|
rebar_base_compiler:error_tuple(Config, Source,
|
|
|
|
Es, Ws, Opts)
|
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.
|
|
|
|
|
2012-08-06 15:44:13 +00:00
|
|
|
-spec compile_mib(file:filename(), file:filename(),
|
|
|
|
rebar_config:config()) -> 'ok'.
|
|
|
|
compile_mib(Source, Target, Config) ->
|
2010-01-08 22:06:29 +00:00
|
|
|
ok = rebar_utils:ensure_dir(Target),
|
2011-08-30 07:29:31 +00:00
|
|
|
ok = rebar_utils:ensure_dir(filename:join("include", "dummy.hrl")),
|
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, _} ->
|
2011-08-03 13:37:21 +00:00
|
|
|
Mib = filename:rootname(Target),
|
mib_to_hrl compilation verbosity via 'mib_opts'
Previously, the configuration setting 'mib_opts' in rebar.config
would affect the call to snmpc:compile/2, so that (for example)
verbosity could be controlled. However, the subsequent call to
snmpc:mib_to_hrl/1 did not include any of these options, so it
did not appear to be possible to control the verbosity of the
process of converting a MIB to a .hrl file. To make matters
worse, the default was to dump a full trace -- including debug
output and various logging -- so the act of compiling a large
number of MIBs could result in a huge amount of "noisy" output
that hid any signal (meaningful warnings, errors, etc.).
This commit addresses that issue by replacing the call to
snmpc:mib_to_hrl/1 with a call to snmpc:mib_to_hrl/3 instead,
which includes an "options" argument that, at present, is only
capable of setting verbosity. The verbosity setting is taken
from the 'mib_opts' setting in rebar_config, if present, and
the approriate kind of argument is passed to snmpc:mib_to_hrl/3.
It should be noted that snmpc:mib_to_hrl/3 is not listed in
Erlang's documentation, but does appear in the list of "API"
exports at the top of snmpc.erl in R15B01 (and remains that way
in R16B01), so this appears to be more of a documentation oversight
than the use of a deep, dark function call that was not intended
to be public. snmpc:mib_to_hrl/3 accepts an #options{} record
(defined in lib/srdlib/include/erl_compile.hrl within Erlang's
source distribution), though most of the fields in that record
are ignored by snmpc:mib_to_hrl/3; only verbosity can be controlled
this way.
2013-09-08 07:37:47 +00:00
|
|
|
MibToHrlOpts =
|
|
|
|
case proplists:get_value(verbosity, Opts, undefined) of
|
|
|
|
undefined ->
|
|
|
|
#options{specific = []};
|
|
|
|
Verbosity ->
|
|
|
|
#options{specific = [{verbosity, Verbosity}]}
|
|
|
|
end,
|
|
|
|
ok = snmpc:mib_to_hrl(Mib, Mib, MibToHrlOpts),
|
2011-08-03 13:37:21 +00:00
|
|
|
Hrl_filename = Mib ++ ".hrl",
|
2011-08-30 07:29:31 +00:00
|
|
|
rebar_file_utils:mv(Hrl_filename, "include"),
|
2009-11-25 23:03:14 +00:00
|
|
|
ok;
|
|
|
|
{error, compilation_failed} ->
|
2012-07-28 17:51:57 +00:00
|
|
|
?FAIL
|
2009-11-25 23:03:14 +00:00
|
|
|
end.
|
2010-01-04 13:17:35 +00:00
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
-spec compile_xrl(file:filename(), file:filename(),
|
|
|
|
rebar_config:config()) -> 'ok'.
|
2010-03-31 19:21:13 +00:00
|
|
|
compile_xrl(Source, Target, Config) ->
|
2011-10-06 15:04:37 +00:00
|
|
|
Opts = [{scannerfile, Target} | rebar_config:get(Config, xrl_opts, [])],
|
2012-08-05 17:56:27 +00:00
|
|
|
compile_xrl_yrl(Config, Source, Target, Opts, leex).
|
2010-03-31 19:21:13 +00:00
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
-spec compile_yrl(file:filename(), file:filename(),
|
|
|
|
rebar_config:config()) -> 'ok'.
|
2010-03-31 19:21:13 +00:00
|
|
|
compile_yrl(Source, Target, Config) ->
|
2011-10-06 15:04:37 +00:00
|
|
|
Opts = [{parserfile, Target} | rebar_config:get(Config, yrl_opts, [])],
|
2012-08-05 17:56:27 +00:00
|
|
|
compile_xrl_yrl(Config, Source, Target, Opts, yecc).
|
2010-03-31 19:21:13 +00:00
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
-spec compile_xrl_yrl(rebar_config:config(), file:filename(),
|
|
|
|
file:filename(), list(), module()) -> 'ok'.
|
|
|
|
compile_xrl_yrl(Config, Source, Target, Opts, Mod) ->
|
2010-03-31 19:21:13 +00:00
|
|
|
case needs_compile(Source, Target, []) of
|
2010-03-31 15:35:06 +00:00
|
|
|
true ->
|
2012-05-22 17:13:41 +00:00
|
|
|
case Mod:file(Source, Opts ++ [{return, true}]) of
|
2011-10-06 15:04:37 +00:00
|
|
|
{ok, _} ->
|
2010-03-31 15:35:06 +00:00
|
|
|
ok;
|
2012-05-22 17:13:41 +00:00
|
|
|
{ok, _Mod, Ws} ->
|
2012-08-05 17:56:27 +00:00
|
|
|
rebar_base_compiler:ok_tuple(Config, Source, Ws);
|
2012-05-22 17:13:41 +00:00
|
|
|
{error, Es, Ws} ->
|
2012-08-05 17:56:27 +00:00
|
|
|
rebar_base_compiler:error_tuple(Config, Source,
|
|
|
|
Es, Ws, Opts)
|
2010-03-31 15:35:06 +00:00
|
|
|
end;
|
|
|
|
false ->
|
|
|
|
skipped
|
|
|
|
end.
|
|
|
|
|
2010-02-05 17:34:38 +00:00
|
|
|
gather_src([], Srcs) ->
|
|
|
|
Srcs;
|
|
|
|
gather_src([Dir|Rest], Srcs) ->
|
|
|
|
gather_src(Rest, Srcs ++ rebar_utils:find_files(Dir, ".*\\.erl\$")).
|
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
-spec dirs(file:filename()) -> [file:filename()].
|
2010-01-04 16:39:52 +00:00
|
|
|
dirs(Dir) ->
|
|
|
|
[F || F <- filelib:wildcard(filename:join([Dir, "*"])), filelib:is_dir(F)].
|
|
|
|
|
2012-08-05 17:56:27 +00:00
|
|
|
-spec delete_dir(file:filename(), [string()]) -> 'ok' | {'error', atom()}.
|
2010-01-04 16:39:52 +00:00
|
|
|
delete_dir(Dir, []) ->
|
|
|
|
file:del_dir(Dir);
|
|
|
|
delete_dir(Dir, Subdirs) ->
|
|
|
|
lists:foreach(fun(D) -> delete_dir(D, dirs(D)) end, Subdirs),
|
|
|
|
file:del_dir(Dir).
|
2010-03-02 22:58:05 +00:00
|
|
|
|
2013-07-01 09:55:31 +00:00
|
|
|
parse_attrs(Fd, Includes) ->
|
|
|
|
case io:parse_erl_form(Fd, "") of
|
|
|
|
{ok, Form, _Line} ->
|
|
|
|
case erl_syntax:type(Form) of
|
|
|
|
attribute ->
|
|
|
|
NewIncludes = process_attr(Form, Includes),
|
|
|
|
parse_attrs(Fd, NewIncludes);
|
|
|
|
_ ->
|
|
|
|
parse_attrs(Fd, Includes)
|
|
|
|
end;
|
|
|
|
{eof, _} ->
|
|
|
|
Includes;
|
|
|
|
_Err ->
|
|
|
|
parse_attrs(Fd, Includes)
|
|
|
|
end.
|
|
|
|
|
|
|
|
process_attr(Form, Includes) ->
|
|
|
|
try
|
|
|
|
AttrName = erl_syntax:atom_value(erl_syntax:attribute_name(Form)),
|
|
|
|
process_attr(AttrName, Form, Includes)
|
|
|
|
catch _:_ ->
|
2013-09-01 17:34:29 +00:00
|
|
|
%% TODO: We should probably try to be more specific here
|
|
|
|
%% and not suppress all errors.
|
2013-07-01 09:55:31 +00:00
|
|
|
Includes
|
|
|
|
end.
|
2010-03-02 22:58:05 +00:00
|
|
|
|
2013-07-01 09:55:31 +00:00
|
|
|
process_attr(import, Form, Includes) ->
|
|
|
|
case erl_syntax_lib:analyze_import_attribute(Form) of
|
|
|
|
{Mod, _Funs} ->
|
|
|
|
[atom_to_list(Mod) ++ ".erl"|Includes];
|
|
|
|
Mod ->
|
|
|
|
[atom_to_list(Mod) ++ ".erl"|Includes]
|
|
|
|
end;
|
|
|
|
process_attr(file, Form, Includes) ->
|
|
|
|
{File, _} = erl_syntax_lib:analyze_file_attribute(Form),
|
|
|
|
[File|Includes];
|
|
|
|
process_attr(include, Form, Includes) ->
|
|
|
|
[FileNode] = erl_syntax:attribute_arguments(Form),
|
|
|
|
File = erl_syntax:string_value(FileNode),
|
|
|
|
[File|Includes];
|
|
|
|
process_attr(include_lib, Form, Includes) ->
|
|
|
|
[FileNode] = erl_syntax:attribute_arguments(Form),
|
2013-09-01 17:34:29 +00:00
|
|
|
RawFile = erl_syntax:string_value(FileNode),
|
|
|
|
File = maybe_expand_include_lib_path(RawFile),
|
2013-07-01 09:55:31 +00:00
|
|
|
[File|Includes];
|
|
|
|
process_attr(behaviour, Form, Includes) ->
|
|
|
|
[FileNode] = erl_syntax:attribute_arguments(Form),
|
|
|
|
File = erl_syntax:atom_name(FileNode) ++ ".erl",
|
|
|
|
[File|Includes];
|
|
|
|
process_attr(compile, Form, Includes) ->
|
|
|
|
[Arg] = erl_syntax:attribute_arguments(Form),
|
|
|
|
case erl_syntax:concrete(Arg) of
|
|
|
|
{parse_transform, Mod} ->
|
|
|
|
[atom_to_list(Mod) ++ ".erl"|Includes];
|
2014-02-25 08:03:26 +00:00
|
|
|
{core_transform, Mod} ->
|
|
|
|
[atom_to_list(Mod) ++ ".erl"|Includes];
|
2013-07-01 09:55:31 +00:00
|
|
|
L when is_list(L) ->
|
2013-09-01 17:34:29 +00:00
|
|
|
lists:foldl(
|
|
|
|
fun({parse_transform, M}, Acc) ->
|
|
|
|
[atom_to_list(M) ++ ".erl"|Acc];
|
|
|
|
({core_transform, M}, Acc) ->
|
|
|
|
[atom_to_list(M) ++ ".erl"|Acc];
|
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
|
|
|
end, Includes, L)
|
2010-03-02 22:58:05 +00:00
|
|
|
end.
|
2010-05-03 02:03:38 +00:00
|
|
|
|
2013-09-01 17:34:29 +00:00
|
|
|
%% Given the filename from an include_lib attribute, if the path
|
|
|
|
%% exists, return unmodified, or else get the absolute ERL_LIBS
|
|
|
|
%% path.
|
|
|
|
maybe_expand_include_lib_path(File) ->
|
|
|
|
case filelib:is_regular(File) of
|
|
|
|
true ->
|
|
|
|
File;
|
|
|
|
false ->
|
|
|
|
expand_include_lib_path(File)
|
|
|
|
end.
|
|
|
|
|
|
|
|
%% Given a path like "stdlib/include/erl_compile.hrl", return
|
|
|
|
%% "OTP_INSTALL_DIR/lib/erlang/lib/stdlib-x.y.z/include/erl_compile.hrl".
|
|
|
|
%% Usually a simple [Lib, SubDir, File1] = filename:split(File) should
|
|
|
|
%% work, but to not crash when an unusual include_lib path is used,
|
|
|
|
%% utilize more elaborate logic.
|
|
|
|
expand_include_lib_path(File) ->
|
|
|
|
File1 = filename:basename(File),
|
|
|
|
Split = filename:split(filename:dirname(File)),
|
|
|
|
Lib = hd(Split),
|
|
|
|
SubDir = filename:join(tl(Split)),
|
|
|
|
Dir = code:lib_dir(list_to_atom(Lib), list_to_atom(SubDir)),
|
|
|
|
filename:join(Dir, File1).
|
|
|
|
|
2010-12-01 14:44:37 +00:00
|
|
|
%%
|
|
|
|
%% Ensure all files in a list are present and abort if one is missing
|
|
|
|
%%
|
2012-08-05 17:56:27 +00:00
|
|
|
-spec check_files([file:filename()]) -> [file:filename()].
|
2010-12-01 14:44:37 +00:00
|
|
|
check_files(FileList) ->
|
|
|
|
[check_file(F) || F <- FileList].
|
|
|
|
|
|
|
|
check_file(File) ->
|
|
|
|
case filelib:is_regular(File) of
|
|
|
|
false -> ?ABORT("File ~p is missing, aborting\n", [File]);
|
|
|
|
true -> File
|
|
|
|
end.
|
2013-09-01 17:34:29 +00:00
|
|
|
|
|
|
|
%% Print prefix followed by list of files. If the list is empty, print
|
|
|
|
%% on the same line, otherwise use a separate line.
|
|
|
|
log_files(Prefix, Files) ->
|
|
|
|
case Files of
|
|
|
|
[] ->
|
|
|
|
?DEBUG("~s: ~p~n", [Prefix, Files]);
|
|
|
|
_ ->
|
|
|
|
?DEBUG("~s:~n~p~n", [Prefix, Files])
|
|
|
|
end.
|