From 4914a2f29ff6d5ced53d5dc7c97b9e82ff7033f8 Mon Sep 17 00:00:00 2001 From: Tuncer Ayaz Date: Sat, 20 Aug 2016 23:15:55 +0200 Subject: port_compiler: clean up compile_each() Extract clang db entry generation into a separate function. --- src/rebar_port_compiler.erl | 46 ++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/rebar_port_compiler.erl b/src/rebar_port_compiler.erl index 10f8f69..48c276c 100644 --- a/src/rebar_port_compiler.erl +++ b/src/rebar_port_compiler.erl @@ -238,6 +238,20 @@ compile_each(Config, [Source | Rest], Type, Env, {NewBins, CDB}) -> Bin = replace_extension(Source, Ext, ".o"), Template = select_compile_template(Type, compiler(Ext)), Cmd = expand_command(Template, Env, Source, Bin), + CDBEnt = cdb_entry(Source, Cmd, Rest), + case needs_compile(Source, Bin) of + true -> + ShOpts = [{env, Env}, return_on_error, {use_stdout, false}], + exec_compiler(Config, Source, Cmd, ShOpts), + compile_each(Config, Rest, Type, Env, + {[Bin | NewBins], [CDBEnt | CDB]}); + false -> + ?INFO("Skipping ~s\n", [Source]), + compile_each(Config, Rest, Type, Env, {NewBins, [CDBEnt, CDB]}) + end. + +%% Generate a clang compilation db entry for Src and Cmd +cdb_entry(Src, Cmd, SrcRest) -> %% Omit all variables from cmd, and use that as cmd in %% CDB, because otherwise clang-* will complain about it. CDBCmd = string:join( @@ -246,31 +260,21 @@ compile_each(Config, [Source | Rest], Type, Env, {NewBins, CDB}) -> (_) -> true end, string:tokens(Cmd, " ")), - " "), + " "), + Cwd = rebar_utils:get_cwd(), %% If there are more source files, make sure we end the CDB entry %% with a comma. - CDBEntSep = case Rest of - [] -> "~n"; - _ -> ",~n" - end, + Sep = case SrcRest of + [] -> "~n"; + _ -> ",~n" + end, %% CDB entry - CDBEnt = ?FMT("{ \"file\" : ~p~n" - ", \"directory\" : ~p~n" - ", \"command\" : ~p~n}" - "~s", - [Source, Cwd, CDBCmd, CDBEntSep] - ), - case needs_compile(Source, Bin) of - true -> - ShOpts = [{env, Env}, return_on_error, {use_stdout, false}], - exec_compiler(Config, Source, Cmd, ShOpts), - compile_each(Config, Rest, Type, Env, - {[Bin | NewBins], [CDBEnt | CDB]}); - false -> - ?INFO("Skipping ~s\n", [Source]), - compile_each(Config, Rest, Type, Env, {NewBins, [CDBEnt, CDB]}) - end. + ?FMT("{ \"file\" : ~p~n" + ", \"directory\" : ~p~n" + ", \"command\" : ~p~n" + "}~s", + [Src, Cwd, CDBCmd, Sep]). exec_compiler(Config, Source, Cmd, ShOpts) -> case rebar_utils:sh(Cmd, ShOpts) of -- cgit v1.2.1