From 8062b794c04ad5ed9933caaaac509872823ab8d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pe=CC=81ter=20Go=CC=88mo=CC=88ri?= Date: Wed, 10 Jul 2013 01:58:52 +0100 Subject: Take compiler options from beam in cover:compile_beam Similarly to cover compiling from source (in this case some user specified compiler options are allowed) when cover compiling from existing beam take a filtered list of compiler options from the beamfile. This way e.g. export_all can be preserved. See use case in eb02beb1c3 --- lib/tools/src/cover.erl | 44 ++++++++++++++++++++++++------------- lib/tools/test/cover_SUITE.erl | 49 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/tools/src/cover.erl b/lib/tools/src/cover.erl index bf21aa6b48..13d9aefb0c 100644 --- a/lib/tools/src/cover.erl +++ b/lib/tools/src/cover.erl @@ -255,16 +255,7 @@ compile_directory(Dir, Options) when is_list(Dir), is_list(Options) -> end. compile_modules(Files,Options) -> - Options2 = lists:filter(fun(Option) -> - case Option of - {i, Dir} when is_list(Dir) -> true; - {d, _Macro} -> true; - {d, _Macro, _Value} -> true; - export_all -> true; - _ -> false - end - end, - Options), + Options2 = filter_options(Options), compile_modules(Files,Options2,[]). compile_modules([File|Files], Options, Result) -> @@ -273,6 +264,17 @@ compile_modules([File|Files], Options, Result) -> compile_modules([],_Opts,Result) -> reverse(Result). +filter_options(Options) -> + lists:filter(fun(Option) -> + case Option of + {i, Dir} when is_list(Dir) -> true; + {d, _Macro} -> true; + {d, _Macro, _Value} -> true; + export_all -> true; + _ -> false + end + end, + Options). %% compile_beam(ModFile) -> Result | {error,Reason} %% ModFile - see compile/1 @@ -622,8 +624,9 @@ main_process_loop(State) -> Compiled0 = State#main_state.compiled, case get_beam_file(Module,BeamFile0,Compiled0) of {ok,BeamFile} -> + UserOptions = get_compile_options(Module,BeamFile), {Reply,Compiled} = - case do_compile_beam(Module,BeamFile,[]) of + case do_compile_beam(Module,BeamFile,UserOptions) of {ok, Module} -> remote_load_compiled(State#main_state.nodes, [{Module,BeamFile}]), @@ -1421,12 +1424,23 @@ get_abstract_code(Module, Beam) -> end. get_source_info(Module, Beam) -> + Compile = get_compile_info(Module, Beam), + case lists:keyfind(source, 1, Compile) of + { source, _ } = Tuple -> [Tuple]; + false -> [] + end. + +get_compile_options(Module, Beam) -> + Compile = get_compile_info(Module, Beam), + case lists:keyfind(options, 1, Compile) of + {options, Options } -> filter_options(Options); + false -> [] + end. + +get_compile_info(Module, Beam) -> case beam_lib:chunks(Beam, [compile_info]) of {ok, {Module, [{compile_info, Compile}]}} -> - case lists:keyfind(source, 1, Compile) of - { source, _ } = Tuple -> [Tuple]; - false -> [] - end; + Compile; _ -> [] end. diff --git a/lib/tools/test/cover_SUITE.erl b/lib/tools/test/cover_SUITE.erl index c033be98a3..7925a47d4f 100644 --- a/lib/tools/test/cover_SUITE.erl +++ b/lib/tools/test/cover_SUITE.erl @@ -28,7 +28,7 @@ export_import/1, otp_5031/1, eif/1, otp_5305/1, otp_5418/1, otp_6115/1, otp_7095/1, otp_8188/1, otp_8270/1, otp_8273/1, otp_8340/1, - otp_10979_hanging_node/1]). + otp_10979_hanging_node/1, compile_beam_opts/1]). -include_lib("test_server/include/test_server.hrl"). @@ -53,7 +53,7 @@ all() -> dont_reconnect_after_stop, stop_node_after_disconnect, export_import, otp_5031, eif, otp_5305, otp_5418, otp_6115, otp_7095, otp_8188, otp_8270, otp_8273, - otp_8340, otp_10979_hanging_node]; + otp_8340, otp_10979_hanging_node, compile_beam_opts]; _pid -> {skip, "It looks like the test server is running " @@ -1401,6 +1401,51 @@ otp_10979_hanging_node(_Config) -> ok. +compile_beam_opts(doc) -> + ["Take compiler options from beam in cover:compile_beam"]; +compile_beam_opts(suite) -> []; +compile_beam_opts(Config) when is_list(Config) -> + ?line ok = file:set_cwd(?config(priv_dir, Config)), + ?line IncDir = filename:join(?config(data_dir, Config), + "included_functions"), + File = "t.erl", + Test = <<"-module(t). + -export([exported/0]). + -include(\"cover_inc.hrl\"). + -ifdef(BOOL). + macro() -> + ?MACRO. + -endif. + exported() -> + ok. + nonexported() -> + ok. + ">>, + ?line ok = file:write_file(File, Test), + %% use all compiler options allowed by cover:filter_options + %% i and d don't make sense when compiling from beam though + ?line {ok, t} = + compile:file(File, [{i, IncDir}, + {d, 'BOOL'}, + {d, 'MACRO', macro_defined}, + export_all, + debug_info, + return_errors]), + Exports = + [{func1,0}, + {macro, 0}, + {exported,0}, + {nonexported,0}, + {module_info,0}, + {module_info,1}], + ?line Exports = t:module_info(exports), + ?line {ok, t} = cover:compile_beam("t"), + ?line Exports = t:module_info(exports), + ?line cover:stop(), + ?line ok = file:delete(File), + + ok. + %%--Auxiliary------------------------------------------------------------ analyse_expr(Expr, Config) -> -- cgit v1.2.3 From dc672094a5c926b1b4a656c13b688bb8c06f7a3b Mon Sep 17 00:00:00 2001 From: Fredrik Gustafsson Date: Fri, 25 Oct 2013 09:14:34 +0200 Subject: tools: Fix compile_beam_opts testcase --- lib/tools/test/cover_SUITE.erl | 34 ++++++++----------------- lib/tools/test/cover_SUITE_data/otp_11439/t.erl | 11 ++++++++ 2 files changed, 22 insertions(+), 23 deletions(-) create mode 100644 lib/tools/test/cover_SUITE_data/otp_11439/t.erl (limited to 'lib') diff --git a/lib/tools/test/cover_SUITE.erl b/lib/tools/test/cover_SUITE.erl index 7925a47d4f..29b26c7a76 100644 --- a/lib/tools/test/cover_SUITE.erl +++ b/lib/tools/test/cover_SUITE.erl @@ -1405,26 +1405,14 @@ compile_beam_opts(doc) -> ["Take compiler options from beam in cover:compile_beam"]; compile_beam_opts(suite) -> []; compile_beam_opts(Config) when is_list(Config) -> - ?line ok = file:set_cwd(?config(priv_dir, Config)), - ?line IncDir = filename:join(?config(data_dir, Config), + {ok, Cwd} = file:get_cwd(), + ok = file:set_cwd(?config(priv_dir, Config)), + IncDir = filename:join(?config(data_dir, Config), "included_functions"), - File = "t.erl", - Test = <<"-module(t). - -export([exported/0]). - -include(\"cover_inc.hrl\"). - -ifdef(BOOL). - macro() -> - ?MACRO. - -endif. - exported() -> - ok. - nonexported() -> - ok. - ">>, - ?line ok = file:write_file(File, Test), + File = filename:join([?config(data_dir, Config), "otp_11439", "t.erl"]), %% use all compiler options allowed by cover:filter_options %% i and d don't make sense when compiling from beam though - ?line {ok, t} = + {ok, t} = compile:file(File, [{i, IncDir}, {d, 'BOOL'}, {d, 'MACRO', macro_defined}, @@ -1438,12 +1426,12 @@ compile_beam_opts(Config) when is_list(Config) -> {nonexported,0}, {module_info,0}, {module_info,1}], - ?line Exports = t:module_info(exports), - ?line {ok, t} = cover:compile_beam("t"), - ?line Exports = t:module_info(exports), - ?line cover:stop(), - ?line ok = file:delete(File), - + Exports = t:module_info(exports), + {ok, t} = cover:compile_beam("t"), + Exports = t:module_info(exports), + cover:stop(), + ok = file:delete("t.beam"), + ok = file:set_cwd(Cwd), ok. %%--Auxiliary------------------------------------------------------------ diff --git a/lib/tools/test/cover_SUITE_data/otp_11439/t.erl b/lib/tools/test/cover_SUITE_data/otp_11439/t.erl new file mode 100644 index 0000000000..d1eb9f16ee --- /dev/null +++ b/lib/tools/test/cover_SUITE_data/otp_11439/t.erl @@ -0,0 +1,11 @@ +-module(t). +-export([exported/0]). +-include("cover_inc.hrl"). +-ifdef(BOOL). +macro() -> + ?MACRO. +-endif. +exported() -> + ok. +nonexported() -> + ok. -- cgit v1.2.3