anchor.diff:          _ ->
               %% Extract environment values from the config (if specified) and merge with the
               %% default for this operating system. This enables max flexibility for users.
  -            OperatingSystem = rebar_utils:get_os(),
  -            DefaultEnvs  = driver_envs() ++ default_envs(OperatingSystem),
  -            OverrideEnvs = rebar_config:get_list(Config, port_env, []),
  +            DefaultEnvs  = filter_envs(default_env(), []),
  +            OverrideEnvs = filter_envs(rebar_config:get_list(Config, port_envs, []), []),
               Env = merge_envs(OverrideEnvs, DefaultEnvs),
               
  -            %% One or more files are available for building. Run the pre-compile hook, if necessary.
  -%            run_precompile_hook(Config),
  +            %% One or more files are available for building. Run the pre-compile hook, if
  +            %% necessary.
  +            run_precompile_hook(Config, Env),
   
               %% Compile each of the sources
  -            compile_each(Sources, [], Config, Env),
  -            ok
  -
  -            %% Finally, link everything together
  -%            do_link(Config, AppFile, Bins)
  -    end.
  -
  -clean(Config, _AppFile) ->
  -    ok.
  +            {NewBins, ExistingBins} = compile_each(Sources, Config, Env, [], []),
  +
  +            %% Construct the driver name and make sure priv/ exists
  +            SoName = so_name(AppFile),
  +            ok = filelib:ensure_dir(SoName),
  +
  +            %% Only relink if necessary, given the SoName and list of new binaries
  +            case needs_link(SoName, NewBins) of
  +                true ->
  +                    AllBins = string:join(NewBins ++ ExistingBins, " "),
  +                    rebar_utils:sh_failfast(?FMT("$CC ~s $LDFLAGS $DRIVER_LDFLAGS -o ~s", [AllBins, SoName]), Env);
  +                false ->
  +                    ?INFO("Skipping relink of ~s\n", [SoName]),
  +                    ok
  +            end
  +    end.
  +
  +clean(Config, AppFile) ->
  +    %% Build a list of sources so as to derive all the bins we generated
  +    Sources = expand_sources(rebar_config:get_list(Config, port_sources, ["c_src/*.c"]), []),
  +    rebar_file_utils:delete_each([source_to_bin(S) || S <- Sources]),
  +
  +    %% Delete the .so file
  +    rebar_file_utils:delete_each([so_name(AppFile)]),
  +
  +    %% Run the cleanup script, if it exists
  +    run_cleanup_hook(Config).
  +
  +
   
   
   %% ===================================================================
  
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";
  