summaryrefslogtreecommitdiff
path: root/src/rebar_erlc_compiler.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/rebar_erlc_compiler.erl')
-rw-r--r--src/rebar_erlc_compiler.erl43
1 files changed, 30 insertions, 13 deletions
diff --git a/src/rebar_erlc_compiler.erl b/src/rebar_erlc_compiler.erl
index 792d358..8fe03b7 100644
--- a/src/rebar_erlc_compiler.erl
+++ b/src/rebar_erlc_compiler.erl
@@ -148,8 +148,8 @@ doterl_compile(Config, Dir, OutDir, MoreSources, ErlOpts) ->
%% Make sure that ebin/ exists and is on the path
ok = filelib:ensure_dir(filename:join(OutDir, "dummy.beam")),
- CurrPath = code:get_path(),
- true = code:add_path(filename:absname(OutDir)),
+ true = code:add_patha(filename:absname(OutDir)),
+
OutDir1 = proplists:get_value(outdir, ErlOpts, OutDir),
G = init_erlcinfo(proplists:get_all_values(i, ErlOpts), AllErlFiles, Dir),
@@ -161,7 +161,7 @@ doterl_compile(Config, Dir, OutDir, MoreSources, ErlOpts) ->
filename:extension(File) =:= ".erl"],
NeededErlFiles = needed_files(G, ErlOpts, Dir, OutDir1, AllErlFiles),
- ErlFirstFiles = erl_first_files(Config, NeededErlFiles),
+ {ErlFirstFiles, ErlOptsFirst} = erl_first_files(Config, ErlOpts, Dir, NeededErlFiles),
{DepErls, OtherErls} = lists:partition(
fun(Source) -> digraph:in_degree(G, Source) > 0 end,
[File || File <- NeededErlFiles, not lists:member(File, ErlFirstFiles)]),
@@ -171,15 +171,36 @@ doterl_compile(Config, Dir, OutDir, MoreSources, ErlOpts) ->
rebar_base_compiler:run(
Config, FirstErls, OtherErls,
fun(S, C) ->
- internal_erl_compile(C, Dir, S, OutDir1, ErlOpts)
+ ErlOpts1 = case lists:member(S, ErlFirstFiles) of
+ true -> ErlOptsFirst;
+ false -> ErlOpts
+ end,
+ internal_erl_compile(C, Dir, S, OutDir1, ErlOpts1)
end),
- true = code:set_path(CurrPath),
ok.
-erl_first_files(Config, NeededErlFiles) ->
+%% Get files which need to be compiled first, i.e. those specified in erl_first_files
+%% and parse_transform options. Also produce specific erl_opts for these first
+%% files, so that yet to be compiled parse transformations are excluded from it.
+erl_first_files(Config, ErlOpts, Dir, NeededErlFiles) ->
ErlFirstFilesConf = rebar_state:get(Config, erl_first_files, []),
- %% NOTE: order of files in ErlFirstFiles is important!
- [File || File <- ErlFirstFilesConf, lists:member(File, NeededErlFiles)].
+ NeededSrcDirs = lists:usort(lists:map(fun filename:dirname/1, NeededErlFiles)),
+ %% NOTE: order of files here is important!
+ ErlFirstFiles =
+ [filename:join(Dir, File) || File <- ErlFirstFilesConf,
+ lists:member(filename:join(Dir, File), NeededErlFiles)],
+ {ParseTransforms, ParseTransformsErls} =
+ lists:unzip(lists:flatmap(
+ fun(PT) ->
+ PTerls = [filename:join(D, module_to_erl(PT)) || D <- NeededSrcDirs],
+ [{PT, PTerl} || PTerl <- PTerls, lists:member(PTerl, NeededErlFiles)]
+ end, proplists:get_all_values(parse_transform, ErlOpts))),
+ ErlOptsFirst = lists:filter(fun({parse_transform, PT}) ->
+ not lists:member(PT, ParseTransforms);
+ (_) ->
+ true
+ end, ErlOpts),
+ {ErlFirstFiles ++ ParseTransformsErls, ErlOptsFirst}.
%% Get subset of SourceFiles which need to be recompiled, respecting
%% dependencies induced by given graph G.
@@ -541,21 +562,17 @@ maybe_expand_include_lib_path(File, Dir) ->
%% The use of -include_lib was probably incorrect by the user but lets try to make it work.
%% We search in the outdir and outdir/../include to see if the header exists.
warn_and_find_path(File, Dir) ->
- ?WARN("Bad use of -include_lib(\"~s\")."
- " First path component should be the name of an application."
- " You probably meant -include(\"~s\").", [File, File]),
SrcHeader = filename:join(Dir, File),
case filelib:is_regular(SrcHeader) of
true ->
[SrcHeader];
false ->
- IncludeDir = filename:join(filename:join(lists:droplast(filename:split(Dir))), "include"),
+ IncludeDir = filename:join(filename:join(rebar_utils:droplast(filename:split(Dir))), "include"),
IncludeHeader = filename:join(IncludeDir, File),
case filelib:is_regular(IncludeHeader) of
true ->
[filename:join(IncludeDir, File)];
false ->
- ?WARN("Could not find header for -include_lib(\"~s\").", [File]),
[]
end
end.