summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFred Hebert <mononcqc@ferd.ca>2015-04-30 21:05:48 -0400
committerFred Hebert <mononcqc@ferd.ca>2015-04-30 21:05:48 -0400
commit6192feea27c5ba1e93be1c43ffb55e50d13e4e97 (patch)
tree7a1c0229d6e0ea04e140ce4f83eaf93420f0bead
parent7645a1118f0e5cdc27e010905f5072021559ddfd (diff)
parentbecf29a5ac8e9b219120daef3ed46ed059d4b0d4 (diff)
Merge pull request #375 from tsloughter/kejv-support-parse-transforms
enable parse transformations in rebar config
-rw-r--r--src/rebar_erlc_compiler.erl33
-rw-r--r--test/rebar_compile_SUITE.erl27
2 files changed, 52 insertions, 8 deletions
diff --git a/src/rebar_erlc_compiler.erl b/src/rebar_erlc_compiler.erl
index 862d21e..8fe03b7 100644
--- a/src/rebar_erlc_compiler.erl
+++ b/src/rebar_erlc_compiler.erl
@@ -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, Dir, 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),
ok.
-erl_first_files(Config, Dir, 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!
- [filename:join(Dir, File) || File <- ErlFirstFilesConf,
- lists:member(filename:join(Dir, 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.
diff --git a/test/rebar_compile_SUITE.erl b/test/rebar_compile_SUITE.erl
index ee262a1..bdab075 100644
--- a/test/rebar_compile_SUITE.erl
+++ b/test/rebar_compile_SUITE.erl
@@ -19,7 +19,8 @@
delete_beam_if_source_deleted/1,
checkout_priority/1,
compile_plugins/1,
- highest_version_of_pkg_dep/1]).
+ highest_version_of_pkg_dep/1,
+ parse_transform_test/1]).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
@@ -46,7 +47,8 @@ all() ->
build_all_srcdirs, recompile_when_hrl_changes,
recompile_when_opts_change, dont_recompile_when_opts_dont_change,
dont_recompile_yrl_or_xrl, delete_beam_if_source_deleted,
- deps_in_path, checkout_priority, compile_plugins, highest_version_of_pkg_dep].
+ deps_in_path, checkout_priority, compile_plugins, highest_version_of_pkg_dep,
+ parse_transform_test].
build_basic_app(Config) ->
AppDir = ?config(apps, Config),
@@ -449,3 +451,24 @@ highest_version_of_pkg_dep(Config) ->
Config, RConf, ["compile"],
{ok, [{app, Name}, {dep, PkgName, <<"0.1.3">>}]}
).
+
+parse_transform_test(Config) ->
+ AppDir = ?config(apps, Config),
+
+ RebarConfig = [{erl_opts, [{parse_transform, pascal}]}],
+
+ Name = rebar_test_utils:create_random_name("app1_"),
+ Vsn = rebar_test_utils:create_random_vsn(),
+ rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
+
+ ExtraSrc = <<"-module(pascal). "
+ "-export([parse_transform/2]). "
+ "parse_transform(Forms, _Options) -> "
+ "Forms.">>,
+
+ ok = file:write_file(filename:join([AppDir, "src", "pascal.erl"]), ExtraSrc),
+
+ rebar_test_utils:run_and_check(Config, RebarConfig, ["compile"], {ok, [{app, Name}]}),
+
+ EbinDir = filename:join([AppDir, "_build", "default", "lib", Name, "ebin"]),
+ true = filelib:is_file(filename:join([EbinDir, "pascal.beam"])).