diff --git a/debuild/Makefile b/debuild/Makefile
index 053f9746fddfd7b068c89bacfdf8f0b236e50907..9359cea82ff29454e7420dd9067f4c678577ec5b 100644
--- a/debuild/Makefile
+++ b/debuild/Makefile
@@ -32,9 +32,15 @@
 # 4. Run 'make debclean' to get rid of any files that were created in steps 2
 # and 3.
 
+# Determine the version number of this build. We get this from m_pd.h.
+PD_L2ORK_VERSION := $(shell grep PD_L2ORK_VERSION ../pd/src/m_pd.h | sed 's|^.define *PD_L2ORK_VERSION *"\(.*\)".*|\1|')
+# 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)
+
 # The Debian version gets derived from the date and serial number of the last
 # commit. You can print it with 'make debversion'.
-debversion = $(shell grep PD_L2ORK_VERSION ../pd/src/m_pd.h | sed 's|^.define *PD_L2ORK_VERSION *"\(.*\)".*|\1|')+git$(shell git rev-list --count HEAD)+$(shell git rev-parse --short HEAD)
+debversion = $(PD_L2ORK_VERSION)+git$(shell git rev-list --count HEAD)+$(shell git rev-parse --short HEAD)
 # Debian revision number of the package.
 debrevision = 1
 # Source tarball and folder.
@@ -97,10 +103,6 @@ deb-us:
 debsrc-us:
 	$(MAKE) deb DEBUILD_FLAGS="-S -d -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.
@@ -112,6 +114,14 @@ $(debsrc):
 # Pre-generate and put s_version.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_version.h.in > $(debdist)/pd/src/s_version.h
+# Pre-generate the markdown and html docs so that they will be included in the
+# source. This means that we don't need pandoc as a build dependency, which
+# makes live a lot easier since the pandoc versions on older Debian/Ubuntu
+# releases are quite ancient.
+	make -C ../packages/gendoc version="$(PD_L2ORK_VERSION)" build_version="$(PD_BUILD_VERSION)"
+	mv ../packages/gendoc/*-Linux* $(debdist)/packages/gendoc
+	make -C ../packages/gendoc clean
+	ls -l $(debdist)/packages/gendoc/*.html
 # Create the source tarball.
 	tar cfz $(debsrc) $(debdist)
 	rm -rf $(debdist)
diff --git a/debuild/debian/rules b/debuild/debian/rules
index d8c498f3f007b25e07521b149956bd167a71ae33..2cf8bac94b45dbc56a8e47f7d6fccfe977c40fef 100755
--- a/debuild/debian/rules
+++ b/debuild/debian/rules
@@ -44,6 +44,9 @@ override_dh_auto_build:
 # Copy the nw.js sources to where purr-data wants them.
 	@echo "DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)"
 	tar -xf debian/nwjs/$(nwjspkg)-$(arch).tar.gz && rm -rf pd/nw/nw && mv $(nwjsdir)-$(arch) pd/nw/nw
+# Make sure that the html docs in packages/gendoc are current so that we don't
+# accidentally try to rebuild them.
+	cd packages/gendoc && touch *-Linux*.html
 	cd l2ork_addons && (export V=0 inst_dir=$(prefix); ./tar_em_up.sh -Bn)
 	make check prefix=$(prefix)
 
diff --git a/packages/Makefile b/packages/Makefile
index 7e40a3f5b248fbb0c7a1d0eda39b06884914be6e..0cd93a968f31757e7a94801639c3b7677534e539 100644
--- a/packages/Makefile
+++ b/packages/Makefile
@@ -315,7 +315,10 @@ abstractions_clean:
 	-$(MAKE) -C $(abstractions_src) $(DEST_PATHS) clean
 
 doc_clean:
-	$(MAKE) -C gendoc clean
+# These are precious, don't clean them! Otherwise pre-generated docs will be
+# removed before the build starts, and the build system may not have the
+# required tools (i.e., pandoc) installed to re-generate them.
+#	$(MAKE) -C gendoc clean
 
 externals_clean:
 	-$(MAKE) -C $(externals_src) $(DEST_PATHS) clean