mihawk 8 years ago
parent
commit
ac5a624e1b
1 changed files with 19 additions and 7 deletions
  1. 19 7
      src/compile/mad_port.erl

+ 19 - 7
src/compile/mad_port.erl

@@ -4,7 +4,7 @@
 
 
 replace_env(String, []) -> String;
 replace_env(String, []) -> String;
 replace_env(String, [{K,V}|Env]) ->
 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) ->
 compile(Dir,Config) ->
     case mad_utils:get_value(port_specs, Config, []) of
     case mad_utils:get_value(port_specs, Config, []) of
@@ -17,17 +17,29 @@ compile_port(Dir,Specs0,Config) ->
     Specs = [ {O,F} || {Sys,O,F} <- Specs0, Sys == System] ++
     Specs = [ {O,F} || {Sys,O,F} <- Specs0, Sys == System] ++
             [ {O,F} || {O,F} <- Specs0],
             [ {O,F} || {O,F} <- Specs0],
     filelib:ensure_dir(Dir ++ "/priv/"),
     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, []) ] ++
           [ {Var,Val} || {Var,Val} <- mad_utils:get_value(port_env, Config, []) ] ++
-           [{"LDFLAGS",[]},{"CFLAGS",[]}],
+          [ {"LDFLAGS",[]},{"CFLAGS",[]}],
     [ begin
     [ 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),
        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),
        {_,Status,Report} = sh:run("cc",string:tokens(Args," "),binary,Dir,Env),
        case Status of
        case Status of
           0 -> false;
           0 -> false;
           _ -> mad:info("Port Compilation Error:~n" ++ io_lib:format("~ts",[Report]),[]), true end
           _ -> mad:info("Port Compilation Error:~n" ++ io_lib:format("~ts",[Report]),[]), true end
       end || {Out,Files} <- Specs ].
       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 ".
+