Skip to content

Commit

Permalink
exe vs shared, fix circular compile
Browse files Browse the repository at this point in the history
  • Loading branch information
mihawk committed Oct 10, 2016
1 parent ac5a624 commit 9c4136d
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/compile/mad_port.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,40 @@ compile_port(Dir,Specs0,Config) ->
[ {Var,Val} || {Var,Val} <- mad_utils:get_value(port_env, Config, []) ] ++
[ {"LDFLAGS",[]},{"CFLAGS",[]}],
[ begin
Template = string:join(files(Dir,Files)," ")
++ " CFLAGS LDFLAGS " ++ ei() ++ " -o " ++ Dir ++ "/" ++ Out,
Args = string:strip(replace_env(Template,Env),both,32),
% mad:info("Args: ~p~n",[Args]),
% mad:info("Env: ~p~n",[Env]),
{_,Status,Report} = sh:run("cc",string:tokens(Args," "),binary,Dir,Env),
case Status of
0 -> false;
_ -> mad:info("Port Compilation Error:~n" ++ io_lib:format("~ts",[Report]),[]), true end
end || {Out,Files} <- Specs ].
Files = files(Dir,Patern),
Ouput = Dir ++ "/" ++ Out,
case is_compiled(Ouput,Files) of
false -> Template = string:join(Files," ")
++ " CFLAGS LDFLAGS " ++ ei(Flavour,Out) ++ " -o " ++ Ouput,
Args = string:strip(replace_env(Template,Env),both,32),
%mad:info("Args: ~p~n",[Args]),
%mad:info("Env: ~p~n",[Env]),
{_,Status,Report} = sh:run("cc",string:tokens(Args," "),binary,Dir,Env),
case Status of
0 -> false;
_ -> mad:info("Port Compilation Error:~n" ++ io_lib:format("~ts",[Report]),[]), true
end;
_ -> %mad:info("No Need recompile ~p~n",[{Ouput,Files}]),
false
end
end || {Out,Patern} <- Specs ].

is_compiled(O,Files) -> lists:foldl(fun(X,false) -> false;
(X,true) -> mad_utils:last_modified(O) >= mad_utils:last_modified(X)
end, true, Files).
system(Sys,System) -> Sys == System orelse match(Sys,System).
match(Re,System) -> case re:run(System, Re, [{capture,none}]) of match -> true; nomatch -> false end.
erts_dir() -> lists:concat([code:root_dir(), "/erts-", erlang:system_info(version)]).
ei_dir() -> code:lib_dir(erl_interface).
ei_dir(inc) -> case code:lib_dir(erl_interface) of {error,bad_name} -> ""; D -> "-I"++D++"/include " end;
ei_dir(lib) -> case code:lib_dir(erl_interface) of {error,bad_name} -> ""; D -> "-L"++D++"/lib " end.
files(Dir,Files) -> [string:join(filelib:wildcard(Dir ++ "/" ++ F)," ")||F<-Files].
ei() -> "-bundle -flat_namespace -undefined suppress " %% for linking phase
"-I"++ei_dir()++"/include "
ei(Flavour,Out) -> type(Flavour,Out)
++ ei_dir(inc) ++
"-I"++erts_dir()++"/include "
"-L"++ei_dir()++"/lib "
++ ei_dir(lib) ++
"-L"++erts_dir()++"/lib ".

type(Flavour,Out) -> case {Flavour, filename:extension(Out)} of %% exe or shared
{_, []} -> "";
{darwin, ".so"} -> "-bundle -flat_namespace -undefined suppress "
end.

0 comments on commit 9c4136d

Please sign in to comment.