Skip to content

Commit

Permalink
Make debug_info rules clear
Browse files Browse the repository at this point in the history
Current rebar3 uses debug_info rules where debug_info is added by
default, and no_debug_info prevents the default from being added, and
removes any explicit debug_info, if any. The problem is that if
'no_debug_info' is anywhere in the config for a run, it cannot be
removed even with other profiles. additionally, no_debug_info ignores
special tuples like {debug_info, {Mod, Data}} and {debug_info_key, Key},
which can be used to add debug info and encrypt it (in lieu of plain
debug_info) respectively.

This patch makes it so that the following rules are in place:

- the first option seen takes priority, allowing profile overrides by
  the ordering rules
- because the compiler apparently does not care for that order (it does
not have to), the overriden options shall be explicitly deleted
- any option related to debug info seen first cancels any no_debug_info
that follows
- any no_debug_info option seen first cancels all of the other
debug_info options
- if debug_info is seen first, it cancels out {debug_info_key, Key}
- if {debug_info_key, Key} is seen first, it cancels out debug_info
- All other options are left untouched in that context (defines can
still be expanded and so on)

This should allow proper profile rules to be followed.
  • Loading branch information
ferd committed Nov 6, 2017
1 parent 387b89a commit e3f2b3b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/rebar_opts.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,40 @@ erl_opts(Opts) ->
Defines = [{d, list_to_atom(D)} ||
D <- ?MODULE:get(Opts, defines, [])],
AllOpts = Defines ++ RawErlOpts,
case proplists:is_defined(no_debug_info, AllOpts) of
true ->
[O || O <- AllOpts, O =/= no_debug_info];
false ->
[debug_info|AllOpts]
end.
filter_debug_info(AllOpts).

filter_debug_info([]) ->
%% Default == ON
[debug_info];
filter_debug_info([debug_info|_] = L) ->
%% drop no_debug_info and {debug_info_key, _} since those would
%% conflict with a plain debug_info
[debug_info |
lists:filter(fun(K) ->
K =/= no_debug_info andalso K =/= debug_info andalso
not (is_tuple(K) andalso element(1,K) =:= debug_info_key)
end, L)];
filter_debug_info([{debug_info, _} = H | T]) ->
%% custom debug_info field; keep and filter the rest except
%% without no_debug_info. Still have to filter for regular or crypto
%% debug_info.
[H | filter_debug_info(lists:filter(fun(K) -> K =/= no_debug_info end, T))];
filter_debug_info([{debug_info_key, _}=H | T]) ->
%% Drop no_debug_info and regular debug_info
[H | lists:filter(fun(K) ->
K =/= no_debug_info andalso K =/= debug_info andalso
not (is_tuple(K) andalso element(1,K) =:= debug_info_key)
end, T)];
filter_debug_info([no_debug_info|T]) ->
%% Drop all debug info
lists:filter(fun(debug_info) -> false
; ({debug_info, _}) -> false
; ({debug_info_key, _}) -> false
; (no_debug_info) -> false
; (_Other) -> true
end, T);
filter_debug_info([H|T]) ->
[H|filter_debug_info(T)].

apply_overrides(Opts, Name, Overrides) ->
%% Inefficient. We want the order we get here though.
Expand Down
24 changes: 24 additions & 0 deletions test/rebar_profiles_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
test_profile_erl_opts_order_3/1,
test_profile_erl_opts_order_4/1,
test_profile_erl_opts_order_5/1,
test_erl_opts_debug_info/1,
first_files_exception/1]).

-include_lib("common_test/include/ct.hrl").
Expand All @@ -50,6 +51,7 @@ all() ->
test_profile_erl_opts_order_3,
test_profile_erl_opts_order_4,
test_profile_erl_opts_order_5,
test_erl_opts_debug_info,
first_files_exception].

init_per_suite(Config) ->
Expand Down Expand Up @@ -501,6 +503,28 @@ test_profile_erl_opts_order_5(Config) ->
Opt = last_erl_opt(Opts, [warn_export_all, nowarn_export_all], undefined),
warn_export_all = Opt.

test_erl_opts_debug_info(_Config) ->
ToOpts = fun(List) -> rebar_opts:erl_opts(dict:from_list([{erl_opts, List}])) end,
?assertEqual([a,b,c,debug_info],
ToOpts([a,b,c])),
?assertEqual([debug_info,a,b,c,{debug_info,{mod,123}}],
ToOpts([debug_info,a,b,c,{debug_info,{mod,123}}])),
?assertEqual([debug_info,a,b,c],
ToOpts([debug_info,a,b,no_debug_info,c])),
?assertEqual([debug_info,a,b,c],
ToOpts([debug_info,a,b,no_debug_info,c,
{debug_info_key, "12345"}])),
?assertEqual([a,b,c],
ToOpts([no_debug_info,debug_info,a,b,no_debug_info,c,
{debug_info_key, "12345"},{debug_info,{mod,123}}])),
?assertEqual([{debug_info_key,"123"},a,b,c],
ToOpts([{debug_info_key, "123"},a,b,debug_info,no_debug_info,c,
{debug_info_key, "12345"}])),
?assertEqual([{debug_info,{mod,"123"}},a,b,c,{debug_info_key, "12345"}],
ToOpts([{debug_info,{mod,"123"}},a,b,no_debug_info,c,
{debug_info_key, "12345"}, debug_info])),
ok.

first_files_exception(_Config) ->
RebarConfig = [{erl_first_files, ["c","a","b"]},
{mib_first_files, ["c","a","b"]},
Expand Down

0 comments on commit e3f2b3b

Please sign in to comment.