anchor.diff:      Acc2 = filelib:wildcard(Spec) ++ Acc,
       expand_sources(Rest, Acc2).
   
  -
  -%% CC       - C compiler
  -%% CXX      - C++ compiler
  -%% CFLAGS   - C compiler
  -%% CXXFLAGS - C++ compiler
  -%% LDFLAGS  - Link flags
  -
  -%% DRIVER_CFLAGS  - default -I paths for erts and ei
  -%% DRIVER_LDFLAGS - default -L and -lerl_interface -lei 
  -
  -
  -compile_each([], Acc, Config, Env) ->
  -    lists:reverse(Acc);
  -compile_each([Source | Rest], Acc, Config, Env) ->
  +run_precompile_hook(Config, Env) ->
  +    case rebar_config:get(Config, port_pre_script, undefined) of
  +        undefined ->
  +            ok;
  +        {Script, BypassFileName} ->
  +            case filelib:is_regular(BypassFileName) of
  +                false ->
  +                    ?CONSOLE("Running ~s\n", [Script]),
  +                    rebar_utils:sh_failfast(Script, Env);
  +                true ->
  +                    ?INFO("~s exists; not running ~s\n", [BypassFileName, Script])
  +            end
  +    end.
  +
  +run_cleanup_hook(Config) ->
  +    case rebar_config:get(Config, port_cleanup_script, undefined) of
  +        undefined ->
  +            ok;
  +        Script ->
  +            ?CONSOLE("Running ~s\n", [Script]),
  +            rebar_utils:sh_failfast(Script, [])
  +    end.
  +
  +
  +compile_each([], Config, Env, NewBins, ExistingBins) ->
  +    {lists:reverse(NewBins), lists:reverse(ExistingBins)};
  +compile_each([Source | Rest], Config, Env, NewBins, ExistingBins) ->
       Ext = filename:extension(Source),
       Bin = filename:rootname(Source, Ext) ++ ".o",
  -    ?CONSOLE("Compiling ~s\n", [Source]),
  -    Compiler = compiler(Ext),
  -    case compiler(Ext) of
  -        "$CC" ->
  -            sh(?FMT("$CC -c $CFLAGS $DRIVER_CFLAGS ~s ~s", [Source, Bin]), Env);
  -        "$CXX" ->
  -            sh(?FMT("$CXX -c $CXXFLAGS $DRIVER_CFLAGS ~s ~s", [Source, Bin]), Env)
  -    end,
  -    compile_each(Rest, [Bin | Acc], Config, Env).
  -
  -
  -
  -            
  -
  +    case needs_compile(Source, Bin) of
  +        true ->
  +            ?CONSOLE("Compiling ~s\n", [Source]),
  +            case compiler(Ext) of
  +                "$CC" ->
  +                    rebar_utils:sh_failfast(?FMT("$CC -c $CFLAGS $DRIVER_CFLAGS ~s -o ~s", [Source, Bin]), Env);
  +                "$CXX" ->
  +                    rebar_utils:sh_failfast(?FMT("$CXX -c $CXXFLAGS $DRIVER_CFLAGS ~s -o ~s", [Source, Bin]), Env)
  +            end,
  +            compile_each(Rest, Config, Env, [Bin | NewBins], ExistingBins);
  +        
  +        false ->
  +            ?INFO("Skipping ~s\n", [Source]),
  +            compile_each(Rest, Config, Env, NewBins, [Bin | ExistingBins])
  +    end.
  +
  +          
   
   needs_compile(Source, Bin) ->
       %% TODO: Generate depends using gcc -MM so we can also check for include changes
       filelib:last_modified(Bin) < filelib:last_modified(Source).
   
  +needs_link(SoName, []) ->
  +    filelib:last_modified(SoName) == 0;
  +needs_link(SoName, NewBins) ->
  +    MaxLastMod = lists:max([filelib:last_modified(B) || B <- NewBins]),
  +    case filelib:last_modified(SoName) of
  +        0 ->
  +            true;
  +        Other ->
  +            ?DEBUG("Checking ~p < ~p", [MaxLastMod, Other]),
  +            MaxLastMod < Other
  +    end.
  +    
   merge_envs(OverrideEnvs, DefaultEnvs) ->
       orddict:merge(fun(Key, Override, Default) ->
                             expand_env_variable(Override, Key, Default)
  
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";
  