diff --git a/debuild/Makefile b/debuild/Makefile
index 9367e67dfbf1ac15a7d1c654767250265de8e6d0..ff171f00cafde27987201bb4a5d07b82a73ab32c 100644
--- a/debuild/Makefile
+++ b/debuild/Makefile
@@ -98,6 +98,10 @@ deb-us:
 debsrc-us:
 	$(MAKE) deb DEBUILD_FLAGS="-S -us -uc"
 
+# Determine the build version which needs git to be computed, so we can't do
+# it in a stand-alone build from a tarball.
+PD_BUILD_VERSION := $(shell git log -1 --format=%cd --date=short | sed -e 's/-//g')-rev.$(shell git rev-parse --short HEAD)
+
 $(debsrc):
 	rm -rf $(debdist)
 # Make sure that the submodules are initialized.
@@ -106,6 +110,12 @@ $(debsrc):
 	git -C .. archive --format=tar.gz --prefix=$(debdist)/ HEAD | tar xfz -
 # Grab the submodules.
 	for x in $(submodules); do (cd $(debdist) && rm -rf $$x && git -C ../../$$x archive --format=tar.gz --prefix=$$x/ HEAD | tar xfz -); done
+# Pre-generate and put s_stuff.h into the tarball (see above; the build
+# version is generated using git which can't be done outside the git repo).
+	sed 's|^\(#define PD_BUILD_VERSION "\).*"|\1$(PD_BUILD_VERSION)"|' ../pd/src/s_stuff.h.in > $(debdist)/pd/src/s_stuff.h
+# Patch packages/Makefile so that we do *not* try to regenerate s_stuff.h
+# during the build.
+	cd $(debdist) && patch -p1 < ../packages-Makefile.patch
 # Create the source tarball.
 	tar cfz $(debsrc) $(debdist)
 	rm -rf $(debdist)
diff --git a/debuild/packages-Makefile.patch b/debuild/packages-Makefile.patch
new file mode 100644
index 0000000000000000000000000000000000000000..ad3e9ac7d61ef11481d3f78a99a904b205cc7e08
--- /dev/null
+++ b/debuild/packages-Makefile.patch
@@ -0,0 +1,32 @@
+diff --git a/packages/Makefile b/packages/Makefile
+index f5c1f8c0..0a285b52 100644
+--- a/packages/Makefile
++++ b/packages/Makefile
+@@ -373,12 +373,14 @@ doc_format:
+ # AG: The set_version target is broken since it modifies m_pd.h in-place
+ # during the build, which is a bad thing to do to files which are supposed to
+ # be kept in a source code repository. Instead, we create s_stuff.h from
+-# s_stuff.h.in, where the latter is kept in the repo and the former gets created
+-# from the latter by substituting PD_BUILD_VERSION.
++# s_stuff.h.in, where the latter is kept in the repo and the former gets
++# created from the latter by substituting PD_BUILD_VERSION. (Only try to do
++# this if we're actually in the git repo, otherwise we'll get an empty
++# revision number.)
+ 
+ git_version: $(pd_src)/src/s_stuff.h.in
+-	cd $(pd_src)/src/ && \
+-	sed 's|^\(#define PD_BUILD_VERSION "\).*"|\1$(PD_BUILD_VERSION)"|' s_stuff.h.in > s_stuff.h
++	test -d $(cvs_root_dir)/.git && cd $(pd_src)/src/ && \
++	sed 's|^\(#define PD_BUILD_VERSION "\).*"|\1$(PD_BUILD_VERSION)"|' s_stuff.h.in > s_stuff.h || true
+ 
+ set_version:
+ # change Pd's version number to reflect the extended build
+@@ -421,7 +423,7 @@ distclean: cruft_clean
+ 	$(MAKE) -C $(abstractions_src) distclean
+ 	$(MAKE) -C $(extensions_src) distclean
+ 	$(MAKE) -C $(externals_src) distclean
+-	-$(MAKE) -C $(pd_src) distclean
++	-$(MAKE) -C $(pd_src) clean
+ 
+ test_locations:
+ 	@echo "PD_VERSION: $(PD_VERSION)"