diff --git a/lib/replace/libreplace.m4 b/lib/replace/libreplace.m4
index 808d5d1..cb6ee8d 100644
--- a/lib/replace/libreplace.m4
+++ b/lib/replace/libreplace.m4
@@ -5,9 +5,9 @@ echo "LIBREPLACE_LOCATION_CHECKS: START"
 dnl find the libreplace sources. This is meant to work both for 
 dnl libreplace standalone builds, and builds of packages using libreplace
 libreplacedir=""
-libreplacepaths="$srcdir $srcdir/lib/replace $srcdir/libreplace $srcdir/../libreplace $srcdir/../replace $srcdir/../lib/replace $srcdir/../../../lib/replace"
+libreplacepaths=". lib/replace libreplace ../libreplace ../replace ../lib/replace ../../lib/replace ../../../lib/replace"
 for d in $libreplacepaths; do
-	if test -f "$d/replace.c"; then
+	if test -f "$srcdir/$d/replace.c"; then
 		libreplacedir="$d"		
 		AC_SUBST(libreplacedir)
 		break;
@@ -34,9 +34,9 @@ echo "LIBREPLACE_BROKEN_CHECKS: START"
 dnl find the libreplace sources. This is meant to work both for 
 dnl libreplace standalone builds, and builds of packages using libreplace
 libreplacedir=""
-libreplacepaths="$srcdir $srcdir/lib/replace $srcdir/libreplace $srcdir/../libreplace $srcdir/../replace $srcdir/../lib/replace $srcdir/../../../lib/replace"
+libreplacepaths=". lib/replace libreplace ../libreplace ../replace ../lib/replace ../../lib/replace ../../../lib/replace"
 for d in $libreplacepaths; do
-	if test -f "$d/replace.c"; then
+	if test -f "$srcdir/$d/replace.c"; then
 		libreplacedir="$d"		
 		AC_SUBST(libreplacedir)
 		break;
diff --git a/lib/tdb/libtdb.m4 b/lib/tdb/libtdb.m4
index b5164fc..6e203ca 100644
--- a/lib/tdb/libtdb.m4
+++ b/lib/tdb/libtdb.m4
@@ -26,7 +26,7 @@ if test x$libreplace_cv_HAVE_FDATASYNC_IN_LIBRT = xyes ; then
 fi
 AC_SUBST(TDB_DEPS)
 
-TDB_CFLAGS="-I$tdbdir/include"
+TDB_CFLAGS="-I${srcdir-.}/$tdbdir/include"
 AC_SUBST(TDB_CFLAGS)
 
 AC_CHECK_FUNCS(mmap pread pwrite getpagesize utime)
diff --git a/lib/tevent/libtevent.m4 b/lib/tevent/libtevent.m4
index 5c5969b..ba1943f 100644
--- a/lib/tevent/libtevent.m4
+++ b/lib/tevent/libtevent.m4
@@ -5,10 +5,11 @@ AC_SUBST(teventdir)
 
 if test x"$teventdir" = "x"; then
 	teventdir=""
-	teventpaths="$srcdir $srcdir/../lib/tevent $srcdir/tevent $srcdir/../tevent"
+	teventpaths=". ../lib/tevent tevent ../tevent"
 	for d in $teventpaths; do
-		if test -f "$d/tevent.c"; then
+		if test -f "$srcdir/$d/tevent.c"; then
 			teventdir="$d"
+			AC_SUBST(teventdir)
 			break;
 		fi
 	done
@@ -24,7 +25,7 @@ AC_SUBST(TEVENT_OBJ)
 AC_SUBST(TEVENT_CFLAGS)
 AC_SUBST(TEVENT_LIBS)
 
-TEVENT_CFLAGS="-I$teventdir"
+TEVENT_CFLAGS="-I$srcdir/$teventdir"
 
 TEVENT_OBJ="tevent.o tevent_debug.o tevent_util.o"
 TEVENT_OBJ="$TEVENT_OBJ tevent_fd.o tevent_timed.o tevent_immediate.o tevent_signal.o"
diff --git a/source3/Makefile.in b/source3/Makefile.in
index ff05d02..7d75391 100644
--- a/source3/Makefile.in
+++ b/source3/Makefile.in
@@ -188,7 +188,7 @@ FLAGS = -I. \
 	$(CPPFLAGS) \
 	$(ISA) \
 	-I$(srcdir)/lib \
-	-I.. \
+	-I$(srcdir)/.. \
 	-D_SAMBA_BUILD_=3
 
 PATH_FLAGS = -DSMB_PASSWD_FILE=\"$(SMB_PASSWD_FILE)\" \
@@ -2067,14 +2067,14 @@ installlibtalloc:: installdirs libtalloc
 			$(DESTDIR)$(LIBDIR)/`basename $(LIBTALLOC_SHARED_TARGET)` ; \
 	fi
 	-$(INSTALLLIBCMD_A) $(LIBTALLOC_STATIC_TARGET) $(DESTDIR)$(LIBDIR)
-	@$(SHELL) $(srcdir)/script/installdirs.sh $(INSTALLPERMS_BIN) $(DESTDIR) ${prefix}/include
-	-$(INSTALLCMD) -m $(INSTALLPERMS_DATA) $(LIBTALLOC_HEADERS) $(DESTDIR)${prefix}/include
+	@$(SHELL) $(srcdir)/script/installdirs.sh $(INSTALLPERMS_BIN) $(DESTDIR) $(INCLUDEDIR)
+	-$(INSTALLCMD) -m $(INSTALLPERMS_DATA) $(LIBTALLOC_HEADERS) $(DESTDIR)$(INCLUDEDIR)
 
 uninstalllibtalloc::
 	-rm -f $(DESTDIR)$(LIBDIR)/`basename $(LIBTALLOC_SHARED_TARGET)`
 	-$(UNINSTALLLIBCMD_SH) $(DESTDIR)$(LIBDIR)/`basename $(LIBTALLOC_SHARED_TARGET_SONAME)`
 	-$(UNINSTALLLIBCMD_A) $(DESTDIR)$(LIBDIR)/`basename $(LIBTALLOC_STATIC_TARGET)`
-	-rm -f $(DESTDIR)${prefix}/include/talloc.h
+	-rm -f $(DESTDIR)$(INCLUDEDIR)/talloc.h
 
 libs:: libtalloc
 installlibs:: @INSTALL_LIBTALLOC@
@@ -2162,14 +2162,14 @@ installlibtdb:: installdirs libtdb
 			$(DESTDIR)$(LIBDIR)/`basename $(LIBTDB_SHARED_TARGET)` ; \
 	fi
 	-$(INSTALLLIBCMD_A) $(LIBTDB_STATIC_TARGET) $(DESTDIR)$(LIBDIR)
-	@$(SHELL) $(srcdir)/script/installdirs.sh $(INSTALLPERMS_BIN) $(DESTDIR) ${prefix}/include
-	-$(INSTALLCMD) -m $(INSTALLPERMS_DATA) $(LIBTDB_HEADERS) $(DESTDIR)${prefix}/include
+	@$(SHELL) $(srcdir)/script/installdirs.sh $(INSTALLPERMS_BIN) $(DESTDIR) $(INCLUDEDIR)
+	-$(INSTALLCMD) -m $(INSTALLPERMS_DATA) $(LIBTDB_HEADERS) $(DESTDIR)$(INCLUDEDIR)
 
 uninstalllibtdb::
 	-rm -f $(DESTDIR)$(LIBDIR)/`basename $(LIBTDB_SHARED_TARGET)`
 	-$(UNINSTALLLIBCMD_SH) $(DESTDIR)$(LIBDIR)/`basename $(LIBTDB_SHARED_TARGET_SONAME)`
 	-$(UNINSTALLLIBCMD_A) $(DESTDIR)$(LIBDIR)/`basename $(LIBTDB_STATIC_TARGET)`
-	-rm -f $(DESTDIR)${prefix}/include/tdb.h
+	-rm -f $(DESTDIR)$(INCLUDEDIR)/tdb.h
 
 libs:: libtdb
 installlibs:: @INSTALL_LIBTDB@
@@ -2255,14 +2255,14 @@ installlibwbclient:: installdirs libwbclient
 			$(DESTDIR)$(LIBDIR)/`basename $(LIBWBCLIENT_SHARED_TARGET)` ; \
 	fi
 	-$(INSTALLLIBCMD_A) bin/libwbclient.a $(DESTDIR)$(LIBDIR)
-	@$(SHELL) $(srcdir)/script/installdirs.sh $(INSTALLPERMS_BIN) $(DESTDIR) ${prefix}/include
-	-$(INSTALLCMD) -m $(INSTALLPERMS_DATA) $(LIBWBCLIENT_HEADERS) $(DESTDIR)${prefix}/include
+	@$(SHELL) $(srcdir)/script/installdirs.sh $(INSTALLPERMS_BIN) $(DESTDIR) $(INCLUDEDIR)
+	-$(INSTALLCMD) -m $(INSTALLPERMS_DATA) $(LIBWBCLIENT_HEADERS) $(DESTDIR)$(INCLUDEDIR)
 
 uninstalllibwbclient::
 	-rm -f $(DESTDIR)$(LIBDIR)/`basename $(LIBWBCLIENT_SHARED_TARGET)`
 	-$(UNINSTALLLIBCMD_SH) $(DESTDIR)$(LIBDIR)/`basename $(LIBWBCLIENT_SHARED_TARGET_SONAME)`
 	-$(UNINSTALLLIBCMD_A) $(DESTDIR)$(LIBDIR)/`basename $(LIBWBCLIENT_STATIC_TARGET)`
-	-rm -f $(DESTDIR)${prefix}/include/wbclient.h
+	-rm -f $(DESTDIR)$(INCLUDEDIR)/wbclient.h
 
 libs:: libwbclient
 installlibs:: @INSTALL_LIBWBCLIENT@
@@ -2446,14 +2446,14 @@ installlibnetapi:: installdirs libnetapi
 			$(DESTDIR)$(LIBDIR)/`basename $(LIBNETAPI_SHARED_TARGET)` ; \
 	fi
 	-$(INSTALLLIBCMD_A) $(LIBNETAPI_STATIC_TARGET) $(DESTDIR)$(LIBDIR)
-	@$(SHELL) $(srcdir)/script/installdirs.sh $(INSTALLPERMS_BIN) $(DESTDIR) ${prefix}/include
-	-$(INSTALLCMD) -m $(INSTALLPERMS_DATA) $(srcdir)/lib/netapi/netapi.h $(DESTDIR)${prefix}/include
+	@$(SHELL) $(srcdir)/script/installdirs.sh $(INSTALLPERMS_BIN) $(DESTDIR) $(INCLUDEDIR)
+	-$(INSTALLCMD) -m $(INSTALLPERMS_DATA) $(srcdir)/lib/netapi/netapi.h $(DESTDIR)$(INCLUDEDIR)
 
 uninstalllibnetapi::
 	-rm -f $(DESTDIR)$(LIBDIR)/`basename $(LIBNETAPI_SHARED_TARGET)`
 	-$(UNINSTALLLIBCMD_SH) $(DESTDIR)$(LIBDIR)/`basename $(LIBNETAPI_SHARED_TARGET_SONAME)`
 	-$(UNINSTALLLIBCMD_A) $(DESTDIR)$(LIBDIR)/`basename $(LIBNETAPI_STATIC_TARGET)`
-	-rm -f $(DESTDIR)${prefix}/include/netapi.h
+	-rm -f $(DESTDIR)$(INCLUDEDIR)/netapi.h
 
 libs:: libnetapi
 installlibs:: @INSTALL_LIBNETAPI@
@@ -2526,8 +2526,8 @@ installlibsmbclient:: installdirs libsmbclient
 			$(DESTDIR)$(LIBDIR)/`basename $(LIBSMBCLIENT_SHARED_TARGET)` ; \
 	fi
 	-$(INSTALLLIBCMD_A) $(LIBSMBCLIENT_STATIC_TARGET) $(DESTDIR)$(LIBDIR)
-	@$(SHELL) $(srcdir)/script/installdirs.sh $(INSTALLPERMS_BIN) $(DESTDIR) ${prefix}/include
-	-$(INSTALLCMD) -m $(INSTALLPERMS_DATA) $(LIBSMBCLIENT_HEADERS) $(DESTDIR)${prefix}/include
+	@$(SHELL) $(srcdir)/script/installdirs.sh $(INSTALLPERMS_BIN) $(DESTDIR) $(INCLUDEDIR)
+	-$(INSTALLCMD) -m $(INSTALLPERMS_DATA) $(LIBSMBCLIENT_HEADERS) $(DESTDIR)$(INCLUDEDIR)
 
 installclientlib:: installlibsmbclient
 
@@ -2535,7 +2535,7 @@ uninstalllibsmbclient::
 	-rm -f $(DESTDIR)$(LIBDIR)/`basename $(LIBSMBCLIENT_SHARED_TARGET)`
 	-$(UNINSTALLLIBCMD_SH) $(DESTDIR)$(LIBDIR)/`basename $(LIBSMBCLIENT_SHARED_TARGET_SONAME)`
 	-$(UNINSTALLLIBCMD_A) $(DESTDIR)$(LIBDIR)/`basename $(LIBSMBCLIENT_STATIC_TARGET)`
-	-rm -f $(DESTDIR)${prefix}/include/libsmbclient.h
+	-rm -f $(DESTDIR)$(INCLUDEDIR)/libsmbclient.h
 
 uninstallclientlib:: uninstalllibsmbclient
 
@@ -2613,14 +2613,14 @@ installlibsmbsharemodes:: installdirs libsmbsharemodes
 			$(DESTDIR)$(LIBDIR)/`basename $(LIBSMBSHAREMODES_SHARED_TARGET)` ; \
 	fi
 	-$(INSTALLLIBCMD_A) $(LIBSMBSHAREMODES_STATIC_TARGET) $(DESTDIR)$(LIBDIR)
-	@$(SHELL) $(srcdir)/script/installdirs.sh $(INSTALLPERMS_BIN) $(DESTDIR) ${prefix}/include
-	-$(INSTALLCMD) -m $(INSTALLPERMS_DATA) $(LIBSMBSHAREMODES_HEADERS) $(DESTDIR)${prefix}/include
+	@$(SHELL) $(srcdir)/script/installdirs.sh $(INSTALLPERMS_BIN) $(DESTDIR) $(INCLUDEDIR)
+	-$(INSTALLCMD) -m $(INSTALLPERMS_DATA) $(LIBSMBSHAREMODES_HEADERS) $(DESTDIR)$(INCLUDEDIR)
 
 uninstalllibsmbsharemodes::
 	-rm -f $(DESTDIR)$(LIBDIR)/`basename $(LIBSMBSHAREMODES_SHARED_TARGET)`
 	-$(UNINSTALLLIBCMD_SH) $(DESTDIR)$(LIBDIR)/`basename $(LIBSMBSHAREMODES_SHARED_TARGET_SONAME)`
 	-$(UNINSTALLLIBCMD_A) $(DESTDIR)$(LIBDIR)/`basename $(LIBSMBSHAREMODES_STATIC_TARGET)`
-	-rm -f $(DESTDIR)${prefix}/include/smb_share_modes.h
+	-rm -f $(DESTDIR)$(INCLUDEDIR)/smb_share_modes.h
 
 libs:: libsmbsharemodes
 installlibs:: @INSTALL_LIBSMBSHAREMODES@
diff --git a/source3/configure.in b/source3/configure.in
index 248f319..5b453ed 100644
--- a/source3/configure.in
+++ b/source3/configure.in
@@ -7037,7 +7037,7 @@ AC_ZLIB([ZLIB_OBJS=""], [
 	do
 		ZLIB_OBJS="$ZLIB_OBJS ../lib/zlib/$o"
 	done
-	CFLAGS="-I../lib/zlib $CFLAGS"
+	CFLAGS="-I\$(srcdir)/../lib/zlib $CFLAGS"
 ])
 
 AC_ARG_ENABLE(dmalloc, [AS_HELP_STRING([--enable-dmalloc], [Enable heap debugging [default=no]])])
diff --git a/source3/m4/samba_version.m4 b/source3/m4/samba_version.m4
index dbea1f3..6622ead 100644
--- a/source3/m4/samba_version.m4
+++ b/source3/m4/samba_version.m4
@@ -8,18 +8,18 @@ dnl http://www.gnu.org/licenses/
 dnl
 dnl
 
-SMB_VERSION_STRING=`cat $srcdir/include/version.h | grep 'SAMBA_VERSION_OFFICIAL_STRING' | cut -d '"' -f2`
+SMB_VERSION_STRING=`cat ./include/version.h | grep '#define SAMBA_VERSION_OFFICIAL_STRING' | cut -d '"' -f2`
 echo "SAMBA VERSION: ${SMB_VERSION_STRING}"
 
-SAMBA_VERSION_GIT_COMMIT_FULLREV=`cat $srcdir/include/version.h | grep 'SAMBA_VERSION_GIT_COMMIT_FULLREV' | cut -d ' ' -f3- | cut -d '"' -f2`
+SAMBA_VERSION_GIT_COMMIT_FULLREV=`cat ./include/version.h | grep 'SAMBA_VERSION_GIT_COMMIT_FULLREV' | cut -d ' ' -f3- | cut -d '"' -f2`
 if test -n "${SAMBA_VERSION_GIT_COMMIT_FULLREV}";then
 	echo "BUILD COMMIT REVISION: ${SAMBA_VERSION_GIT_COMMIT_FULLREV}"
 fi
-SAMBA_VERSION_COMMIT_DATE=`cat $srcdir/include/version.h | grep 'SAMBA_VERSION_COMMIT_DATE' | cut -d ' ' -f3-`
+SAMBA_VERSION_COMMIT_DATE=`cat ./include/version.h | grep 'SAMBA_VERSION_COMMIT_DATE' | cut -d ' ' -f3-`
 if test -n "${SAMBA_VERSION_COMMIT_DATE}";then
 	echo "BUILD COMMIT DATE: ${SAMBA_VERSION_COMMIT_DATE}"
 fi
-SAMBA_VERSION_COMMIT_TIME=`cat $srcdir/include/version.h | grep 'SAMBA_VERSION_COMMIT_TIME' | cut -d ' ' -f3-`
+SAMBA_VERSION_COMMIT_TIME=`cat ./include/version.h | grep 'SAMBA_VERSION_COMMIT_TIME' | cut -d ' ' -f3-`
 if test -n "${SAMBA_VERSION_COMMIT_TIME}";then
 	echo "BUILD COMMIT TIME: ${SAMBA_VERSION_COMMIT_TIME}"
 
