Skip to content

Commit

Permalink
fix port
Browse files Browse the repository at this point in the history
  • Loading branch information
mihawk committed Oct 10, 2016
1 parent 37ac4d0 commit ac5a624
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/compile/mad_port.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

replace_env(String, []) -> String;
replace_env(String, [{K,V}|Env]) ->
replace_env(re:replace(String, K, V, [global, {return, list}]),Env).
replace_env(re:replace(String, io_lib:format("\\$?{?(~s)}?",[K]), V, [global, {return, list}]),Env).

compile(Dir,Config) ->
case mad_utils:get_value(port_specs, Config, []) of
Expand All @@ -17,17 +17,29 @@ compile_port(Dir,Specs0,Config) ->
Specs = [ {O,F} || {Sys,O,F} <- Specs0, Sys == System] ++
[ {O,F} || {O,F} <- Specs0],
filelib:ensure_dir(Dir ++ "/priv/"),
Env = [ {Var,Val} || {Sys,Var,Val} <- mad_utils:get_value(port_env, Config, []), Sys == System ] ++
Env = [ {Var,Val} || {Sys,Var,Val} <- mad_utils:get_value(port_env, Config, []), system(Sys,System) ] ++
[ {Var,Val} || {Var,Val} <- mad_utils:get_value(port_env, Config, []) ] ++
[{"LDFLAGS",[]},{"CFLAGS",[]}],
[ {"LDFLAGS",[]},{"CFLAGS",[]}],
[ begin
Template = string:join(filelib:wildcard(Dir ++ "/" ++ Files)," ")
++ " CFLAGS LDFLAGS -o " ++ Dir ++ "/" ++ Out,
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]),
% 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 ].

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).
files(Dir,Files) -> [string:join(filelib:wildcard(Dir ++ "/" ++ F)," ")||F<-Files].
ei() -> "-bundle -flat_namespace -undefined suppress " %% for linking phase
"-I"++ei_dir()++"/include "
"-I"++erts_dir()++"/include "
"-L"++ei_dir()++"/lib "
"-L"++erts_dir()++"/lib ".

0 comments on commit ac5a624

Please sign in to comment.