Przeglądaj źródła

switch from loading modules to read compile info to using `beam_lib`

fixes #216 and #220
pull/222/head
alisdair sullivan 10 lat temu
rodzic
commit
ccfebdd155
4 zmienionych plików z 25 dodań i 46 usunięć
  1. +13
    -16
      src/rebar_erlc_compiler.erl
  2. +7
    -15
      src/rebar_otp_app.erl
  3. +2
    -12
      test/rebar_compile_SUITE.erl
  4. +3
    -3
      test/rebar_eunit_SUITE.erl

+ 13
- 16
src/rebar_erlc_compiler.erl Wyświetl plik

@ -256,24 +256,21 @@ needs_compile(Source, Target, Opts, Parents) ->
source_changed(TargetLastMod, I) -> TargetLastMod < filelib:last_modified(I).
opts_changed(Opts, Target) ->
Basename = filename:basename(Target, ".beam"),
Dirname = filename:dirname(Target),
ObjectFile = filename:join([Dirname, Basename]),
_ = purge(list_to_atom(Basename)),
case code:load_abs(ObjectFile) of
{module, Mod} ->
Compile = Mod:module_info(compile),
lists:sort(Opts) =/= lists:sort(proplists:get_value(options,
Compile));
{error, nofile} -> false
opts_changed(NewOpts, Target) ->
case compile_info(Target) of
{ok, Opts} -> lists:sort(Opts) =/= lists:sort(NewOpts);
_ -> true
end.
purge(Mod) ->
%% remove old code if necessary
_ = code:purge(Mod),
%% move current code to old
_ = code:delete(Mod).
compile_info(Target) ->
case beam_lib:chunks(Target, [compile_info]) of
{ok, {_mod, Chunks}} ->
CompileInfo = proplists:get_value(compile_info, Chunks, []),
{ok, proplists:get_value(options, CompileInfo, [])};
{error, beam_lib, Reason} ->
?WARN("Couldn't read debug info from ~p for reason: ~p", [Target, Reason]),
{error, Reason}
end.
check_erlcinfo(_Config, #erlcinfo{vsn=?ERLCINFO_VSN}) ->
ok;

+ 7
- 15
src/rebar_otp_app.erl Wyświetl plik

@ -166,23 +166,15 @@ ebin_modules(App, Dir) ->
[rebar_utils:beam_to_mod(N) || N <- Filtered].
beam_src(Beam) ->
try
Mod = list_to_atom(filename:basename(Beam, ".beam")),
_ = purge(Mod),
{module, Mod} = code:load_abs(filename:rootname(Beam, ".beam")),
Compile = Mod:module_info(compile),
proplists:get_value(source, Compile, [])
catch
error:undef -> [];
error:nofile -> []
case beam_lib:chunks(Beam, [compile_info]) of
{ok, {_mod, Chunks}} ->
CompileInfo = proplists:get_value(compile_info, Chunks, []),
proplists:get_value(source, CompileInfo, []);
{error, beam_lib, Reason} ->
?WARN("Couldn't read debug info from ~p for reason: ~p", [Beam, Reason]),
[]
end.
purge(Mod) ->
%% remove old code if necessary
_ = code:purge(Mod),
%% move current code to old
_ = code:delete(Mod).
ensure_registered(AppData) ->
case lists:keyfind(registered, 1, AppData) of
false ->

+ 2
- 12
test/rebar_compile_SUITE.erl Wyświetl plik

@ -11,8 +11,7 @@
build_checkout_deps/1,
recompile_when_opts_change/1,
dont_recompile_when_opts_dont_change/1,
dont_recompile_yrl_or_xrl/1,
purge_before_load/1]).
dont_recompile_yrl_or_xrl/1]).
-include_lib("common_test/include/ct.hrl").
-include_lib("eunit/include/eunit.hrl").
@ -34,7 +33,7 @@ all() ->
[build_basic_app, build_release_apps,
build_checkout_apps, build_checkout_deps,
recompile_when_opts_change, dont_recompile_when_opts_dont_change,
dont_recompile_yrl_or_xrl, purge_before_load].
dont_recompile_yrl_or_xrl].
build_basic_app(Config) ->
AppDir = ?config(apps, Config),
@ -185,12 +184,3 @@ dont_recompile_yrl_or_xrl(Config) ->
?assert(ModTime == NewModTime).
purge_before_load(Config) ->
AppDir = ?config(apps, Config),
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]),
Tasks = ["do", "compile,compile,compile"],
rebar_test_utils:run_and_check(Config, [], Tasks, {ok, [{app, Name}]}).

+ 3
- 3
test/rebar_eunit_SUITE.erl Wyświetl plik

@ -21,7 +21,7 @@ end_per_suite(_Config) ->
ok.
init_per_testcase(_, Config) ->
rebar_test_utils:init_rebar_state(Config).
rebar_test_utils:init_rebar_state(Config, "eunit_").
all() ->
[test_basic_app].
@ -29,9 +29,9 @@ all() ->
test_basic_app(Config) ->
AppDir = ?config(apps, Config),
Name = rebar_test_utils:create_random_name("eunit_"),
Name = rebar_test_utils:create_random_name("basic_"),
Vsn = rebar_test_utils:create_random_vsn(),
rebar_test_utils:create_app(AppDir, Name, Vsn, [kernel, stdlib]),
RebarConfig = [{erl_opts, [{d, some_define}]}],
rebar_test_utils:run_and_check(Config, RebarConfig, ["eunit"], {ok, [{app, Name}]}).

Ładowanie…
Anuluj
Zapisz