anchor.diff:  compiler(".c++") -> "$CXX";
   compiler(".C")   -> "$CXX";
   compiler(_)      -> "$CC".
  -     
  +
  +
  +%%
  +%% Given env. variable FOO we want to expand all references to
  +%% it in InStr. References can have two forms: $FOO and ${FOO}
  +%%
   expand_env_variable(InStr, VarName, VarValue) ->
  -    %% Given env. variable FOO we want to expand all references to
  -    %% it in InStr. References can have two forms: $FOO and ${FOO}
  +
       R1 = re:replace(InStr, "\\\$" ++ VarName, VarValue),
  -    re:replace(R1, "\\\${" ++ VarName ++ "}", VarValue).
  +    re:replace(R1, "\\\${" ++ VarName ++ "}", VarValue, [{return, list}]).
  +
  +
  +%%
  +%% Filter a list of env vars such that only those which match the provided
  +%% architecture regex (or do not have a regex) are returned.
  +%%
  +filter_envs([], Acc) ->
  +    lists:reverse(Acc);
  +filter_envs([{ArchRegex, Key, Value} | Rest], Acc) ->
  +    case rebar_utils:is_arch(ArchRegex) of
  +        true ->
  +            filter_envs(Rest, [{Key, Value} | Acc]);
  +        false ->
  +            filter_envs(Rest, Acc)
  +    end;
  +filter_envs([{Key, Value} | Rest], Acc) ->
  +    filter_envs(Rest, [{Key, Value} | Acc]).
   
   
   erts_dir() ->
       lists:concat([code:root_dir(), "/erts-", erlang:system_info(version)]).
   
  -driver_envs() ->
  -    [{"DRIVER_CFLAGS", lists:concat([" -I", code:lib_dir(erl_interface, include),
  +default_env() ->
  +    [{"CC", "gcc"},
  +     {"CXX", "g++"},
  +     {"CFLAGS", "-g -Wall -fPIC"},
  +     {"CXXFLAGS", "-g -Wall -fPIC"},
  +     {"darwin", "LDFLAGS", "-bundle -flat_namespace -undefined suppress"},
  +     {"linux", "LDFLAGS", "-shared"},
  +     {"DRIVER_CFLAGS", lists:concat([" -I", code:lib_dir(erl_interface, include),
                                        " -I", filename:join(erts_dir(), include),
                                        " "])},
        {"DRIVER_LDFLAGS", lists:concat([" -L", code:lib_dir(erl_interface, lib),
                                         " -lerl_interface -lei"])}].
   
  -default_envs(darwin) ->
  -    [{"CC", "gcc"},
  -     {"CXX", "g++"},
  -     {"CFLAGS", "-g -Wall -fPIC"},
  -     {"LDFLAGS", "-bundle -flat_namespace -undefined surpress"}];
  -default_envs(linux) ->
  -    [{"CC", "gcc"},
  -     {"CXX", "g++"},
  -     {"CFLAGS", "-g -Wall -fPIC"},
  -     {"LDFLAGS", "-shared"}];
  -default_envs(Os) ->
  -    ?ERROR("Unsupported operating system ~s: can not generate default build environment.\n", [Os]),
  -    ?FAIL.
  -
  -
  -sh(Command, Env) ->
  -    ?CONSOLE("Cmd: ~p\n~p\n", [Command, Env]),
  -    Port = open_port({spawn, Command}, [{env, Env}, exit_status, {line, 16384},
  -                                        use_stdio, stderr_to_stdout]),
  -    sh_loop(Port).
  -    
  -sh_loop(Port) ->            
  -    receive
  -        {Port, {data, {_, Line}}} ->
  -            ?CONSOLE("> ~s\n", [Line]),
  -            sh_loop(Port);
  -        {Port, Other} ->
  -            ?CONSOLE(">> ~p\n", [Other])
  -    end.
  +
  +
  +source_to_bin(Source) ->
  +    Ext = filename:extension(Source),
  +    filename:rootname(Source, Ext) ++ ".o".
  +
  +so_name(AppFile) ->
  +    %% Get the app name, which we'll use to generate the linked port driver name
  +    case rebar_app_utils:load_app_file(AppFile) of
  +        {ok, AppName, _} ->
  +            ok;
  +        error ->
  +            AppName = undefined,
  +            ?FAIL
  +    end,
  +
  +    %% Construct the driver name 
  +    ?FMT("priv/~s_drv.so", [AppName]).
  +
  
positive.code: %% Public API
  %% ===================================================================
  
  %% Port driver name - determined by app name
  %% Source files (or c_src/*.c by default)
  %% Pre-compile hook (optional)
  %% Env variables
  
  compile(Config, _AppFile) ->
      %% Compose list of sources from config file -- default to c_src/*.c
      Sources = expand_sources(rebar_config:get_list(Config, port_sources, ["c_src/*.c"]), []),
      case Sources of
          [] ->
  
negative.code:                   orddict:from_list(OverrideEnvs),
                    orddict:from_list(DefaultEnvs)).
  
  
  %%
  %% Choose a compiler variable, based on a provided extension
  %%
  compiler(".cc")  -> "$CXX";
  compiler(".cp")  -> "$CXX";
  compiler(".cxx") -> "$CXX";
  