diff --git a/.gitignore b/.gitignore
index b1fcb2215d14a61d83964a1755fc2a83e0040266..5a2f7df852fdae11c61b2c3e93782afee47b141a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
 *~
 *.a
+*.d
 *.diff
 *.orig
 *.rej
diff --git a/Makefile b/Makefile
index 3c65b305156c29474165a35f84eb3b72a2cd56ad..a25e85df257d32e2549b098c515bb88787e9b6c1 100644
--- a/Makefile
+++ b/Makefile
@@ -311,39 +311,46 @@ example$(EXE): $(OBJEXAMPLE) $(LIBX264)
 $(OBJS) $(OBJSO): CFLAGS += $(CFLAGSSO)
 $(OBJCLI): CFLAGS += $(CFLAGSCLI)
 
-$(OBJS) $(OBJASM) $(OBJSO) $(OBJCLI) $(OBJCHK) $(OBJCHK_8) $(OBJCHK_10) $(OBJEXAMPLE): .depend
+ALLOBJS = $(OBJS) $(OBJASM) $(OBJSO) $(OBJCLI) $(OBJCHK) $(OBJCHK_8) $(OBJCHK_10) $(OBJEXAMPLE)
+$(ALLOBJS): $(GENERATED)
 
 %.o: %.c
-	$(CC) $(CFLAGS) -c $< $(CC_O)
+	$(DEPCMD)
+	$(CC) $(CFLAGS) -c $< $(CC_O) $(DEPFLAGS)
 
 %-8.o: %.c
-	$(CC) $(CFLAGS) -c $< $(CC_O) -DHIGH_BIT_DEPTH=0 -DBIT_DEPTH=8
+	$(DEPCMD)
+	$(CC) $(CFLAGS) -c $< $(CC_O) $(DEPFLAGS) -DHIGH_BIT_DEPTH=0 -DBIT_DEPTH=8
 
 %-10.o: %.c
-	$(CC) $(CFLAGS) -c $< $(CC_O) -DHIGH_BIT_DEPTH=1 -DBIT_DEPTH=10
+	$(DEPCMD)
+	$(CC) $(CFLAGS) -c $< $(CC_O) $(DEPFLAGS) -DHIGH_BIT_DEPTH=1 -DBIT_DEPTH=10
 
 %.o: %.asm common/x86/x86inc.asm common/x86/x86util.asm
-	$(AS) $(ASFLAGS) -o $@ $<
+	$(AS) $(ASFLAGS) -o $@ $< -MD $(@:.o=.d)
 	-@ $(if $(STRIP), $(STRIP) -x $@) # delete local/anonymous symbols, so they don't show up in oprofile
 
 %-8.o: %.asm common/x86/x86inc.asm common/x86/x86util.asm
-	$(AS) $(ASFLAGS) -o $@ $< -DBIT_DEPTH=8 -Dprivate_prefix=x264_8
+	$(AS) $(ASFLAGS) -o $@ $< -MD $(@:.o=.d) -DBIT_DEPTH=8 -Dprivate_prefix=x264_8
 	-@ $(if $(STRIP), $(STRIP) -x $@)
 
 %-10.o: %.asm common/x86/x86inc.asm common/x86/x86util.asm
-	$(AS) $(ASFLAGS) -o $@ $< -DBIT_DEPTH=10 -Dprivate_prefix=x264_10
+	$(AS) $(ASFLAGS) -o $@ $< -MD $(@:.o=.d) -DBIT_DEPTH=10 -Dprivate_prefix=x264_10
 	-@ $(if $(STRIP), $(STRIP) -x $@)
 
 %.o: %.S
-	$(AS) $(ASFLAGS) -o $@ $<
+	$(DEPCMD)
+	$(AS) $(ASFLAGS) -o $@ $< $(DEPFLAGS)
 	-@ $(if $(STRIP), $(STRIP) -x $@) # delete local/anonymous symbols, so they don't show up in oprofile
 
 %-8.o: %.S
-	$(AS) $(ASFLAGS) -o $@ $< -DHIGH_BIT_DEPTH=0 -DBIT_DEPTH=8
+	$(DEPCMD)
+	$(AS) $(ASFLAGS) -o $@ $< $(DEPFLAGS) -DHIGH_BIT_DEPTH=0 -DBIT_DEPTH=8
 	-@ $(if $(STRIP), $(STRIP) -x $@)
 
 %-10.o: %.S
-	$(AS) $(ASFLAGS) -o $@ $< -DHIGH_BIT_DEPTH=1 -DBIT_DEPTH=10
+	$(DEPCMD)
+	$(AS) $(ASFLAGS) -o $@ $< $(DEPFLAGS) -DHIGH_BIT_DEPTH=1 -DBIT_DEPTH=10
 	-@ $(if $(STRIP), $(STRIP) -x $@)
 
 %.dll.o: %.rc x264.h
@@ -352,34 +359,19 @@ $(OBJS) $(OBJASM) $(OBJSO) $(OBJCLI) $(OBJCHK) $(OBJCHK_8) $(OBJCHK_10) $(OBJEXA
 %.o: %.rc x264.h x264res.manifest
 	$(RC) $(RCFLAGS)$@ $<
 
-.depend: config.mak $(GENERATED)
-	@rm -f .depend
-	@echo 'dependency file generation...'
-ifeq ($(COMPILER),CL)
-	@$(foreach SRC, $(addprefix $(SRCPATH)/, $(SRCS) $(SRCCLI) $(SRCSO) $(SRCEXAMPLE)), $(SRCPATH)/tools/msvsdepend.sh "$(CC)" "$(CFLAGS)" "$(SRC)" "$(SRC:$(SRCPATH)/%.c=%.o)" 1>> .depend;)
-ifneq ($(findstring HAVE_BITDEPTH8 1, $(CONFIG)),)
-	@$(foreach SRC, $(addprefix $(SRCPATH)/, $(SRCS_X) $(SRCS_8) $(SRCCLI_X) $(SRCCHK_X)), $(SRCPATH)/tools/msvsdepend.sh "$(CC)" "$(CFLAGS)" "$(SRC)" "$(SRC:$(SRCPATH)/%.c=%-8.o)" 1>> .depend;)
-endif
-ifneq ($(findstring HAVE_BITDEPTH10 1, $(CONFIG)),)
-	@$(foreach SRC, $(addprefix $(SRCPATH)/, $(SRCS_X) $(SRCCLI_X) $(SRCCHK_X)), $(SRCPATH)/tools/msvsdepend.sh "$(CC)" "$(CFLAGS)" "$(SRC)" "$(SRC:$(SRCPATH)/%.c=%-10.o)" 1>> .depend;)
-endif
-else
-	@$(foreach SRC, $(addprefix $(SRCPATH)/, $(SRCS) $(SRCCLI) $(SRCSO) $(SRCEXAMPLE)), $(CC) $(CFLAGS) $(SRC) $(DEPMT) $(SRC:$(SRCPATH)/%.c=%.o) $(DEPMM) 1>> .depend;)
-ifneq ($(findstring HAVE_BITDEPTH8 1, $(CONFIG)),)
-	@$(foreach SRC, $(addprefix $(SRCPATH)/, $(SRCS_X) $(SRCS_8) $(SRCCLI_X) $(SRCCHK_X)), $(CC) $(CFLAGS) $(SRC) $(DEPMT) $(SRC:$(SRCPATH)/%.c=%-8.o) $(DEPMM) 1>> .depend;)
-endif
-ifneq ($(findstring HAVE_BITDEPTH10 1, $(CONFIG)),)
-	@$(foreach SRC, $(addprefix $(SRCPATH)/, $(SRCS_X) $(SRCCLI_X) $(SRCCHK_X)), $(CC) $(CFLAGS) $(SRC) $(DEPMT) $(SRC:$(SRCPATH)/%.c=%-10.o) $(DEPMM) 1>> .depend;)
-endif
-endif
-
 config.mak:
 	./configure
 
-depend: .depend
-ifneq ($(wildcard .depend),)
-include .depend
-endif
+# This is kept as a no-op
+depend:
+	@echo "make depend" is handled implicitly now
+
+-include $(wildcard $(ALLOBJS:.o=.d))
+
+# Dummy rule to avoid failing, if the dependency files specify dependencies on
+# a removed .h file.
+%.h:
+	@:
 
 OBJPROF = $(OBJS) $(OBJSO) $(OBJCLI)
 # These should cover most of the important codepaths
@@ -412,11 +404,12 @@ endif
 endif
 
 clean:
-	rm -f $(OBJS) $(OBJASM) $(OBJCLI) $(OBJSO) $(GENERATED) .depend TAGS
+	rm -f $(OBJS) $(OBJASM) $(OBJCLI) $(OBJSO) $(GENERATED) TAGS
 	rm -f $(SONAME) *.a *.lib *.exp *.pdb x264$(EXE) x264_lookahead.clbin
 	rm -f checkasm8$(EXE) checkasm10$(EXE) $(OBJCHK) $(OBJCHK_8) $(OBJCHK_10)
 	rm -f example$(EXE) $(OBJEXAMPLE)
 	rm -f $(OBJPROF:%.o=%.gcda) $(OBJPROF:%.o=%.gcno) *.dyn pgopti.dpi pgopti.dpi.lock *.pgd *.pgc
+	rm -f $(ALLOBJS:%.o=%.d)
 
 distclean: clean
 	rm -f config.mak x264_config.h config.h config.log x264.pc x264.def
diff --git a/configure b/configure
index d4615a4272a0c4f6e30637e1117baaebd32614b1..4636e52ef6e71604f95ab985efbf26d8370d3595 100755
--- a/configure
+++ b/configure
@@ -1496,9 +1496,9 @@ else
     CLI_LIBX264='$(LIBX264)'
 fi
 
-DEPMM="${QPRE}MM"
-DEPMT="${QPRE}MT"
 if [ $compiler_style = MS ]; then
+    DEPFLAGS=""
+    DEPCMD='@$(SRCPATH)/tools/msvsdepend.sh "$(CC)" "$(CFLAGS)" "$<" "$@" > $(@:.o=.d)'
     AR="lib.exe -nologo -out:"
     LD="link.exe -out:"
     if [ $compiler = ICL ]; then
@@ -1522,7 +1522,8 @@ if [ $compiler_style = MS ]; then
         CFLAGS="-DNDEBUG $CFLAGS"
     fi
 else # gcc/icc
-    DEPMM="$DEPMM -g0"
+    DEPFLAGS="${QPRE}MMD ${QPRE}MF"' $(@:.o=.d)'
+    DEPCMD=""
     AR="$AR rc "
     LD="$CC -o "
     LIBX264=libx264.a
@@ -1575,8 +1576,8 @@ CFLAGSSO=$CFLAGSSO
 CFLAGSCLI=$CFLAGSCLI
 COMPILER=$compiler
 COMPILER_STYLE=$compiler_style
-DEPMM=$DEPMM
-DEPMT=$DEPMT
+DEPCMD=$DEPCMD
+DEPFLAGS=$DEPFLAGS
 LD=$LD
 LDFLAGS=$LDFLAGS
 LDFLAGSCLI=$LDFLAGSCLI
diff --git a/tools/msvsdepend.sh b/tools/msvsdepend.sh
index 5d267ab75e6c8b0f6c373112968610de59bb69bb..d3a2db2186dbd178003bb58a708d4356c8cbbcaa 100755
--- a/tools/msvsdepend.sh
+++ b/tools/msvsdepend.sh
@@ -8,11 +8,22 @@ set -f
 [ -n "$1" ] && [ -n "$3" ] && [ -n "$4" ] || exit 1
 
 # Add flags to only perform syntax checking and output a list of included files
+# For sources that aren't C, run preprocessing to NUL instead.
+
+case "$3" in
+*.c)
+    opts="-W0 -Zs"
+    ;;
+*)
+    opts="-P -FiNUL"
+    ;;
+esac
+
 # Discard all output other than included files
 # Convert '\' directory separators to '/'
 # Remove system includes (hack: check for "/Program Files" string in path)
 # Add the source file itself as a dependency
-deps="$($1 $2 -nologo -showIncludes -W0 -Zs "$3" 2>&1 |
+deps="$($1 $2 -nologo -showIncludes $opts "$3" 2>&1 |
         grep '^Note: including file:' |
         sed 's/^Note: including file:[[:space:]]*\(.*\)$/\1/; s/\\/\//g' |
         sed '/\/[Pp]rogram [Ff]iles/d')