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]).
|
|
|
|
|
2010-02-01 15:37:52 +00:00
|
|
|
-export([doterl_compile/2,
|
|
|
|
doterl_compile/3]).
|
2009-12-05 22:18:09 +00:00
|
|
|
|
2009-11-25 22:23:42 +00:00
|
|
|
-include("rebar.hrl").
|
|
|
|
|
|
|
|
%% ===================================================================
|
|
|
|
%% 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
|
|
|
%%
|
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec compile(Config::rebar_config:config(), AppFile::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
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec clean(Config::rebar_config:config(), AppFile::file:filename()) -> 'ok'.
|
2009-11-30 14:00:48 +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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec doterl_compile(Config::rebar_config:config(),
|
|
|
|
OutDir::file:filename()) -> 'ok'.
|
2010-02-01 15:37:52 +00:00
|
|
|
doterl_compile(Config, OutDir) ->
|
|
|
|
doterl_compile(Config, OutDir, []).
|
|
|
|
|
|
|
|
doterl_compile(Config, OutDir, MoreSources) ->
|
2010-01-04 05:53:04 +00:00
|
|
|
FirstErls = rebar_config:get_list(Config, erl_first_files, []),
|
2011-10-22 12:27:19 +00:00
|
|
|
ErlOpts = erl_opts(Config),
|
|
|
|
?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.
|
2010-02-05 17:34:38 +00:00
|
|
|
SrcDirs = src_dirs(proplists:append_values(src_dirs, ErlOpts)),
|
|
|
|
RestErls = [Source || Source <- gather_src(SrcDirs, []) ++ MoreSources,
|
2010-10-25 22:38:51 +00:00
|
|
|
not lists:member(Source, FirstErls)],
|
2010-02-16 22:16:19 +00:00
|
|
|
|
2011-01-28 15:08:27 +00:00
|
|
|
%% Split RestErls so that parse_transforms and behaviours are instead added
|
|
|
|
%% to erl_first_files, parse transforms first.
|
|
|
|
%% This should probably be somewhat combined with inspect_epp
|
|
|
|
[ParseTransforms, Behaviours, OtherErls] =
|
|
|
|
lists:foldl(fun(F, [A, B, C]) ->
|
|
|
|
case compile_priority(F) of
|
|
|
|
parse_transform ->
|
|
|
|
[[F | A], B, C];
|
|
|
|
behaviour ->
|
|
|
|
[A, [F | B], C];
|
2012-02-08 15:33:12 +00:00
|
|
|
callback ->
|
|
|
|
[A, [F | B], C];
|
2011-01-28 15:08:27 +00:00
|
|
|
_ ->
|
|
|
|
[A, B, [F | C]]
|
|
|
|
end
|
|
|
|
end, [[], [], []], RestErls),
|
2010-10-05 21:59:52 +00:00
|
|
|
|
|
|
|
NewFirstErls = FirstErls ++ ParseTransforms ++ Behaviours,
|
2010-03-02 22:58:05 +00:00
|
|
|
|
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")),
|
2010-10-05 21:59:52 +00:00
|
|
|
rebar_base_compiler:run(Config, NewFirstErls, OtherErls,
|
2010-10-25 22:38:51 +00:00
|
|
|
fun(S, C) ->
|
|
|
|
internal_erl_compile(S, C, OutDir, ErlOpts)
|
2010-05-03 02:03:38 +00:00
|
|
|
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
|
|
|
|
2009-11-25 23:03:14 +00:00
|
|
|
|
2010-01-04 05:53:04 +00:00
|
|
|
%% ===================================================================
|
|
|
|
%% Internal functions
|
|
|
|
%% ===================================================================
|
|
|
|
|
2011-10-22 12:27:19 +00:00
|
|
|
erl_opts(Config) ->
|
|
|
|
RawErlOpts = filter_defines(rebar_config:get(Config, erl_opts, []), []),
|
2011-10-22 23:12:19 +00:00
|
|
|
GlobalDefines = [{d, list_to_atom(D)} ||
|
|
|
|
D <- rebar_config:get_global(defines, [])],
|
2011-10-22 12:27:19 +00:00
|
|
|
Opts = GlobalDefines ++ RawErlOpts,
|
|
|
|
case proplists:is_defined(no_debug_info, Opts) of
|
|
|
|
true ->
|
|
|
|
[O || O <- Opts, O =/= no_debug_info];
|
2011-10-22 23:12:19 +00:00
|
|
|
false ->
|
2011-10-22 12:27:19 +00:00
|
|
|
[debug_info|Opts]
|
|
|
|
end.
|
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec include_path(Source::file:filename(),
|
|
|
|
Config::rebar_config:config()) -> [file:filename(), ...].
|
2010-01-04 05:53:04 +00:00
|
|
|
include_path(Source, Config) ->
|
|
|
|
ErlOpts = rebar_config:get(Config, erl_opts, []),
|
2011-01-28 15:08:27 +00:00
|
|
|
["include", filename:dirname(Source)]
|
|
|
|
++ proplists:get_all_values(i, ErlOpts).
|
2010-01-04 05:53:04 +00:00
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec inspect(Source::file:filename(),
|
|
|
|
IncludePath::[file:filename(), ...]) -> {string(), [string()]}.
|
2010-01-04 05:53:04 +00:00
|
|
|
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-03-02 23:04:08 +00:00
|
|
|
inspect_epp(Epp, Source, ModuleDefault, []);
|
2010-01-04 05:53:04 +00:00
|
|
|
{error, Reason} ->
|
|
|
|
?DEBUG("Failed to inspect ~s: ~p\n", [Source, Reason]),
|
|
|
|
{ModuleDefault, []}
|
2009-12-21 17:15:21 +00:00
|
|
|
end.
|
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec inspect_epp(Epp::pid(), Source::file:filename(), Module::file:filename(),
|
2011-01-28 15:08:27 +00:00
|
|
|
Includes::[string()]) -> {string(), [string()]}.
|
2010-03-02 23:04:08 +00:00
|
|
|
inspect_epp(Epp, Source, Module, Includes) ->
|
2009-12-21 17:15:21 +00:00
|
|
|
case epp:parse_erl_form(Epp) of
|
2010-02-12 18:57:50 +00:00
|
|
|
{ok, {attribute, _, module, ModInfo}} ->
|
|
|
|
case ModInfo of
|
|
|
|
%% Typical module name, single atom
|
|
|
|
ActualModule when is_atom(ActualModule) ->
|
|
|
|
ActualModuleStr = atom_to_list(ActualModule);
|
|
|
|
%% Packag-ized module name, list of atoms
|
|
|
|
ActualModule when is_list(ActualModule) ->
|
2011-01-28 15:08:27 +00:00
|
|
|
ActualModuleStr = string:join([atom_to_list(P) ||
|
|
|
|
P <- ActualModule], ".");
|
2010-02-12 18:57:50 +00:00
|
|
|
%% Parameterized module name, single atom
|
|
|
|
{ActualModule, _} when is_atom(ActualModule) ->
|
|
|
|
ActualModuleStr = atom_to_list(ActualModule);
|
|
|
|
%% Parameterized and packagized module name, list of atoms
|
|
|
|
{ActualModule, _} when is_list(ActualModule) ->
|
2011-01-28 15:08:27 +00:00
|
|
|
ActualModuleStr = string:join([atom_to_list(P) ||
|
|
|
|
P <- ActualModule], ".")
|
2010-01-04 05:53:04 +00:00
|
|
|
end,
|
2010-03-02 23:04:08 +00:00
|
|
|
inspect_epp(Epp, Source, ActualModuleStr, Includes);
|
2010-01-04 05:53:04 +00:00
|
|
|
{ok, {attribute, 1, file, {Module, 1}}} ->
|
2010-03-02 23:04:08 +00:00
|
|
|
inspect_epp(Epp, Source, Module, Includes);
|
|
|
|
{ok, {attribute, 1, file, {Source, 1}}} ->
|
|
|
|
inspect_epp(Epp, Source, Module, Includes);
|
2010-01-02 21:17:59 +00:00
|
|
|
{ok, {attribute, 1, file, {IncFile, 1}}} ->
|
2010-03-02 23:04:08 +00:00
|
|
|
inspect_epp(Epp, Source, 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};
|
|
|
|
_ ->
|
2010-03-02 23:04:08 +00:00
|
|
|
inspect_epp(Epp, Source, Module, Includes)
|
2009-12-21 17:15:21 +00:00
|
|
|
end.
|
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec needs_compile(Source::file:filename(), Target::file:filename(),
|
2011-01-28 15:08:27 +00:00
|
|
|
Hrls::[string()]) -> boolean().
|
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).
|
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec internal_erl_compile(Source::file:filename(),
|
|
|
|
Config::rebar_config:config(),
|
|
|
|
Outdir::file:filename(),
|
2011-01-28 15:08:27 +00:00
|
|
|
ErlOpts::list()) -> 'ok' | 'skipped'.
|
2010-05-03 02:03:38 +00:00
|
|
|
internal_erl_compile(Source, Config, Outdir, ErlOpts) ->
|
2010-01-04 05:53:04 +00:00
|
|
|
%% 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
|
2011-03-10 14:07:41 +00:00
|
|
|
%% the target
|
2010-01-04 05:53:04 +00:00
|
|
|
case needs_compile(Source, Target, Hrls) of
|
|
|
|
true ->
|
2010-12-30 13:32:49 +00:00
|
|
|
Opts = [{outdir, filename:dirname(Target)}] ++
|
2011-06-01 13:12:17 +00:00
|
|
|
ErlOpts ++ [{i, "include"}, report],
|
2010-01-04 05:53:04 +00:00
|
|
|
case compile:file(Source, Opts) of
|
2011-06-01 12:27:16 +00:00
|
|
|
{ok, _} ->
|
2010-01-04 05:53:04 +00:00
|
|
|
ok;
|
|
|
|
_ ->
|
2012-06-04 16:36:28 +00:00
|
|
|
?ABORT
|
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.
|
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec compile_mib(Source::file:filename(), Target::file:filename(),
|
|
|
|
Config::rebar_config:config()) -> 'ok'.
|
2010-01-08 22:06:29 +00:00
|
|
|
compile_mib(Source, Target, Config) ->
|
|
|
|
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),
|
|
|
|
ok = snmpc:mib_to_hrl(Mib),
|
|
|
|
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-06-04 16:36:28 +00:00
|
|
|
?ABORT
|
2009-11-25 23:03:14 +00:00
|
|
|
end.
|
2010-01-04 13:17:35 +00:00
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec compile_xrl(Source::file:filename(), Target::file:filename(),
|
|
|
|
Config::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, [])],
|
2010-10-02 17:26:03 +00:00
|
|
|
compile_xrl_yrl(Source, Target, Opts, leex).
|
2010-03-31 19:21:13 +00:00
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec compile_yrl(Source::file:filename(), Target::file:filename(),
|
|
|
|
Config::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, [])],
|
2010-10-02 17:26:03 +00:00
|
|
|
compile_xrl_yrl(Source, Target, Opts, yecc).
|
2010-03-31 19:21:13 +00:00
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec compile_xrl_yrl(Source::file:filename(), Target::file:filename(),
|
|
|
|
Opts::list(), Mod::atom()) -> 'ok'.
|
2010-10-02 17:26:03 +00:00
|
|
|
compile_xrl_yrl(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 ->
|
2010-03-31 19:21:13 +00:00
|
|
|
case Mod:file(Source, Opts) of
|
2011-10-06 15:04:37 +00:00
|
|
|
{ok, _} ->
|
2010-03-31 15:35:06 +00:00
|
|
|
ok;
|
|
|
|
_X ->
|
2012-06-04 16:36:28 +00:00
|
|
|
?ABORT
|
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\$")).
|
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec src_dirs(SrcDirs::[string()]) -> [file:filename(), ...].
|
2010-02-05 17:34:38 +00:00
|
|
|
src_dirs([]) ->
|
|
|
|
["src"];
|
|
|
|
src_dirs(SrcDirs) ->
|
2012-02-24 17:06:52 +00:00
|
|
|
SrcDirs.
|
2010-02-05 17:34:38 +00:00
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec dirs(Dir::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)].
|
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec delete_dir(Dir::file:filename(),
|
2011-01-28 15:08:27 +00:00
|
|
|
Subdirs::[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
|
|
|
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec compile_priority(File::file:filename()) -> 'normal' | 'behaviour' |
|
2012-02-08 15:33:12 +00:00
|
|
|
'callback' |
|
2011-03-10 14:07:41 +00:00
|
|
|
'parse_transform'.
|
2010-03-02 22:58:05 +00:00
|
|
|
compile_priority(File) ->
|
|
|
|
case epp_dodger:parse_file(File) of
|
|
|
|
{error, _} ->
|
2010-10-10 21:24:20 +00:00
|
|
|
normal; % couldn't parse the file, default priority
|
2010-03-02 22:58:05 +00:00
|
|
|
{ok, Trees} ->
|
|
|
|
F2 = fun({tree,arity_qualifier,_,
|
2011-01-28 15:08:27 +00:00
|
|
|
{arity_qualifier,{tree,atom,_,behaviour_info},
|
|
|
|
{tree,integer,_,1}}}, _) ->
|
|
|
|
behaviour;
|
|
|
|
({tree,arity_qualifier,_,
|
|
|
|
{arity_qualifier,{tree,atom,_,parse_transform},
|
|
|
|
{tree,integer,_,2}}}, _) ->
|
|
|
|
parse_transform;
|
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
|
|
|
end,
|
|
|
|
|
|
|
|
F = fun({tree, attribute, _,
|
|
|
|
{attribute, {tree, atom, _, export},
|
|
|
|
[{tree, list, _, {list, List, none}}]}}, Acc) ->
|
|
|
|
lists:foldl(F2, Acc, List);
|
2012-02-08 15:33:12 +00:00
|
|
|
({tree, attribute, _,
|
|
|
|
{attribute, {tree, atom, _, callback},_}}, _Acc) ->
|
|
|
|
callback;
|
2011-01-28 15:08:27 +00:00
|
|
|
(_, Acc) ->
|
|
|
|
Acc
|
|
|
|
end,
|
2010-03-02 22:58:05 +00:00
|
|
|
|
2010-10-05 21:59:52 +00:00
|
|
|
lists:foldl(F, normal, Trees)
|
2010-03-02 22:58:05 +00:00
|
|
|
end.
|
2010-05-03 02:03:38 +00:00
|
|
|
|
|
|
|
%%
|
2010-05-03 02:15:16 +00:00
|
|
|
%% Filter a list of erl_opts platform_define options such that only
|
|
|
|
%% those which match the provided architecture regex are returned.
|
2010-05-03 02:03:38 +00:00
|
|
|
%%
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec filter_defines(ErlOpts::list(), Acc::list()) -> list().
|
2010-05-03 02:03:38 +00:00
|
|
|
filter_defines([], Acc) ->
|
|
|
|
lists:reverse(Acc);
|
|
|
|
filter_defines([{platform_define, ArchRegex, Key} | Rest], Acc) ->
|
|
|
|
case rebar_utils:is_arch(ArchRegex) of
|
|
|
|
true ->
|
|
|
|
filter_defines(Rest, [{d, Key} | Acc]);
|
|
|
|
false ->
|
|
|
|
filter_defines(Rest, Acc)
|
|
|
|
end;
|
|
|
|
filter_defines([{platform_define, ArchRegex, Key, Value} | Rest], Acc) ->
|
|
|
|
case rebar_utils:is_arch(ArchRegex) of
|
|
|
|
true ->
|
|
|
|
filter_defines(Rest, [{d, Key, Value} | Acc]);
|
|
|
|
false ->
|
|
|
|
filter_defines(Rest, Acc)
|
|
|
|
end;
|
|
|
|
filter_defines([Opt | Rest], Acc) ->
|
|
|
|
filter_defines(Rest, [Opt | Acc]).
|
2010-12-01 14:44:37 +00:00
|
|
|
|
|
|
|
%%
|
|
|
|
%% Ensure all files in a list are present and abort if one is missing
|
|
|
|
%%
|
2011-03-10 14:07:41 +00:00
|
|
|
-spec check_files(FileList::[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.
|