From fd683c97314ab25fe9cf06fac4e6f6a4fce63d0c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Albert=20Gr=C3=A4f?= <aggraef@gmail.com>
Date: Thu, 26 Jan 2017 01:44:31 +0100
Subject: [PATCH] Fix the build versioning so that we don't have to touch
 m_pd.h.

---
 packages/Makefile                  | 24 +++++++++---------
 packages/Makefile.buildlayout      | 40 +++++++++++-------------------
 packages/darwin_app/Makefile       |  2 +-
 packages/linux_make/Makefile       |  2 +-
 packages/win32_inno/Makefile       |  4 +--
 pd/src/import.c                    |  2 +-
 pd/src/{m_pd.h.in => m_pd.h}       |  2 +-
 pd/src/makefile.in                 |  6 +++--
 pd/src/s_main.c                    |  2 +-
 pd/src/{s_stuff.h => s_stuff.h.in} |  6 +++++
 10 files changed, 43 insertions(+), 47 deletions(-)
 rename pd/src/{m_pd.h.in => m_pd.h} (99%)
 rename pd/src/{s_stuff.h => s_stuff.h.in} (97%)

diff --git a/packages/Makefile b/packages/Makefile
index 27359ebbb..c6c6659f9 100644
--- a/packages/Makefile
+++ b/packages/Makefile
@@ -258,7 +258,7 @@ welcome_install:
 	echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://puredata.org/ploneCustom.css\" />" >> $(WELCOME_FILE)
 	echo "</head>" >> $(WELCOME_FILE)
 	echo "<body><p><p>" >> $(WELCOME_FILE)
-	echo "<h2>Pd-l2ork version $(PD_L2ORK_VERSION) ($(PD_TEST_VERSION))</h2>" >> $(WELCOME_FILE)
+	echo "<h2>Pd-l2ork version $(PD_L2ORK_VERSION) ($(PD_BUILD_VERSION))</h2>" >> $(WELCOME_FILE)
 	echo "<p>Pd-l2ork was written by Ivica Bukvic and Jonathan Wilkes. It is a fork of Hans-Christoph Steiner's Pd-extended, which in turn is based on Miller S. Puckette's Pd. The included Pd abstractions and externals are the work of many people whose contributions are gratefully acknowledged.</p>" >> $(WELCOME_FILE)
 	echo "<font size=\"-1\">" >> $(WELCOME_FILE)
 	echo "<p>`grep -A9 ACKNOWLEDG $(pd_src)/README.txt`</p>" >> $(WELCOME_FILE)
@@ -279,7 +279,7 @@ readme_install:
 	echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://puredata.org/plone.css\" />" >> $(README_FILE)
 	echo "</head>" >> $(README_FILE)
 	echo "<body>" >> $(README_FILE)
-	echo "<h2>Pd-l2ork version $(PD_L2ORK_VERSION) ($(PD_TEST_VERSION))</h2>" 	>> $(README_FILE)
+	echo "<h2>Pd-l2ork version $(PD_L2ORK_VERSION) ($(PD_BUILD_VERSION))</h2>" 	>> $(README_FILE)
 	echo "<p>Pd is a real-time, graphical programming language for media processing created by Miller S. Puckette.  It provides an environment for audio analysis, synthesis, and processing, with a rich set of multimedia capabilities.  You can get Pd for Linux, Windows, MacOS X, BSD, or IRIX.</p>" >> $(README_FILE)
 	echo "<p>Pd-l2ork is a version of Pd based on Hans-Christoph Steiner's Pd-extended which features many bugfixes and enhancements, especially in the user interface.  Pd-l2ork was created by Ivica Ico Bukvic and is maintained by a team of volunteers including Jonathan Wilkes who ported it to the Mac and Windows.  It is available for Linux, Windows and MacOS X.</p>" >> $(README_FILE)
 	echo "<p>For general information about Pd, go to: <a href=\"http://puredata.org\" target=\"pd\">http://puredata.org</a>.  More information about Pd-l2ork can be found on the <a href=\"http://l2ork.music.vt.edu/main/make-your-own-l2ork/software/\" target=\"pd-l2ork\">Pd-l2ork website</a>.</p>" >> $(README_FILE)
@@ -476,26 +476,26 @@ devsymlinks_clean:
 
 # 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, let's generate m_pd.h from
-# m_pd.h.in, where the latter is kept in the repo and the former gets created
-# from the latter when needed.
+# 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.
 
-git_version: $(pd_src)/src/m_pd.h.in
+git_version: $(pd_src)/src/s_stuff.h.in
 	cd $(pd_src)/src/ && \
-	sed 's|^\(#define PD_TEST_VERSION "\).*"|\1$(GIT_VERSION)"|' m_pd.h.in > m_pd.h
+	sed 's|^\(#define PD_BUILD_VERSION "\).*"|\1$(PD_BUILD_VERSION)"|' s_stuff.h.in > s_stuff.h
 
 set_version:
 # change Pd's version number to reflect the extended build
 # this needs the complete_version_defines patch to work
 	cd $(pd_src)/src/ && \
-		sed 's|^\(#define PD_TEST_VERSION "\).*"|\1$(GIT_VERSION)"|' m_pd.h > \
+		sed 's|^\(#define PD_TEST_VERSION "\).*"|\1$(PD_BUILD_VERSION)"|' m_pd.h > \
 			m_pd.h.tmp && mv m_pd.h.tmp m_pd.h
 
 unset_version:
 # change the version number back to the original 
 # this needs the complete_version_defines patch to work
 	cd $(pd_src)/src && \
-		sed 's|^\(#define PD_TEST_VERSION ".*\)$(GIT_VERSION_PREFIX).*"|\1"|' \
+		sed 's|^\(#define PD_TEST_VERSION ".*\)$(PD_BUILD_VERSION_PREFIX).*"|\1"|' \
 			m_pd.h > m_pd.h.tmp && \
 		mv m_pd.h.tmp m_pd.h
 
@@ -560,7 +560,7 @@ patch_pd_devel:
 # change Pd's version number to reflect the extended build
 # this needs the complete_version_defines patch to work
 	cd $(pd_src)/src/ && \
-		sed 's|^\(#define PD_TEST_VERSION "\).*"|\1$(GIT_VERSION)"|' m_pd.h > \
+		sed 's|^\(#define PD_TEST_VERSION "\).*"|\1$(PD_BUILD_VERSION)"|' m_pd.h > \
 			m_pd.h.tmp && mv m_pd.h.tmp m_pd.h
 	@echo " "
 	@echo "patching completed."
@@ -570,7 +570,7 @@ unpatch_pd_devel:
 # change the version number back to the original 
 # this needs the complete_version_defines patch to work
 	cd $(pd_src)/src && \
-		sed 's|^\(#define PD_TEST_VERSION ".*\)$(GIT_VERSION)"|\1"|' \
+		sed 's|^\(#define PD_TEST_VERSION ".*\)$(PD_BUILD_VERSION)"|\1"|' \
 			m_pd.h > m_pd.h.tmp && \
 		mv m_pd.h.tmp m_pd.h
 # apply all platform-specific patches
@@ -617,7 +617,7 @@ distclean: cruft_clean
 
 test_locations:
 	@echo "PD_VERSION: $(PD_VERSION)"
-	@echo "GIT_VERSION: $(GIT_VERSION)"
+	@echo "PD_BUILD_VERSION: $(PD_BUILD_VERSION)"
 	@echo "CWD $(CWD)"
 	@echo "DESTDIR $(DESTDIR)"
 	@echo "PREFIX $(prefix)"
diff --git a/packages/Makefile.buildlayout b/packages/Makefile.buildlayout
index 2aa13174c..496ed7eb7 100644
--- a/packages/Makefile.buildlayout
+++ b/packages/Makefile.buildlayout
@@ -168,52 +168,40 @@ installdirs: $(DESTDIR) $(bindir) $(examplesdir) $(pddocdir) $(includedir) $(hel
 #==============================================================================#
 
 
-# AG: "Abandon hope all ye who enter here." Past this point lies madness. Most
-# of Pd's version information is kept in m_pd.h, but some of it needs to be
-# generated at build time to keep in sync with git sources. Since we don't
-# want to have files in the repo which get updated with every build, we use a
-# template m_pd.h.in for m_pd.h instead. The stable version parts (only
-# updated by editing m_pd.h.in manually) can be read from there.
-
-PD_MAJOR_VERSION := $(shell grep PD_MAJOR_VERSION $(pd_src)/src/m_pd.h.in | \
+PD_MAJOR_VERSION := $(shell grep PD_MAJOR_VERSION $(pd_src)/src/m_pd.h | \
 	sed 's|^.define *PD_MAJOR_VERSION *\([0-9]*\).*|\1|' )
-PD_MINOR_VERSION := $(shell grep PD_MINOR_VERSION $(pd_src)/src/m_pd.h.in | \
+PD_MINOR_VERSION := $(shell grep PD_MINOR_VERSION $(pd_src)/src/m_pd.h | \
 	sed 's|^.define *PD_MINOR_VERSION *\([0-9]*\).*|\1|' )
-PD_BUGFIX_VERSION := $(shell grep PD_BUGFIX_VERSION $(pd_src)/src/m_pd.h.in | \
+PD_BUGFIX_VERSION := $(shell grep PD_BUGFIX_VERSION $(pd_src)/src/m_pd.h | \
 	sed 's|^.define *PD_BUGFIX_VERSION *\([0-9]*\).*|\1|' )
-#PD_TEST_VERSION := $(shell grep PD_TEST_VERSION $(pd_src)/src/m_pd.h | \
-#	sed 's|^.define *PD_TEST_VERSION *"\(.*\)".*|\1|' )
+PD_TEST_VERSION := $(shell grep PD_TEST_VERSION $(pd_src)/src/m_pd.h | \
+	sed 's|^.define *PD_TEST_VERSION *"\(.*\)".*|\1|' )
 #PD_VERSION = $(PD_MAJOR_VERSION).$(PD_MINOR_VERSION).$(PD_BUGFIX_VERSION)
 #ifneq ($(PD_TEST_VERSION),)
 #	PD_VERSION := $(PD_VERSION)-$(PD_TEST_VERSION)
 #endif
 
 # AG: Pd-l2ork version number. This becomes part of the PD_VERSION.
-# It comes from the m_pd.h.in file and will be something like 1.x for
+# It comes from the m_pd.h file and will be something like 1.x for
 # "classic" Pd-l2ork and 2.x for the new nw.js version.
-PD_L2ORK_VERSION := $(shell grep PD_L2ORK_VERSION $(pd_src)/src/m_pd.h.in | \
+PD_L2ORK_VERSION := $(shell grep PD_L2ORK_VERSION $(pd_src)/src/m_pd.h | \
 	sed 's|^.define *PD_L2ORK_VERSION *"\(.*\)".*|\1|' )
 
-# AG: version number based on the actual git revision and date. We use this
-# both for the PD_TEST_VERSION and for the package name and accompanying
+# AG: build version number based on the actual git revision and date. This is
+# substituted into s_stuff.h (rather than m_pd.h which we really don't want to
+# touch) so that it's available in the source. We use this in lieu of of
+# PD_TEST_VERSION for -version and for the package name and accompanying
 # materials (ReadMe files and such). Note that this will only work when
 # building directly from a clone of the git repo, and of course you'll need to
 # have git installed.
-GIT_VERSION := $(shell git log -1 --format=%cd --date=short | sed -e 's/-//g')-rev.$(shell git rev-parse --short HEAD)
-
-# AG: Untangle the hall of mirrors that is PD_TEST_VERSION. m_pd.h won't exist
-# or will at least be outdated when the build starts, so we can't simply read
-# it from there. Since we always use the package version (git revision and
-# date, see GIT_VERSION below), we just set it directly instead.
-
-PD_TEST_VERSION := $(GIT_VERSION)
+PD_BUILD_VERSION := $(shell git log -1 --format=%cd --date=short | sed -e 's/-//g')-rev.$(shell git rev-parse --short HEAD)
 
 # AG: This is being used for the package name and accompanying materials. For
 # Pd-l2ork the vanilla Pd version numbers are rather meaningless (they are
 # mainly kept around so that 3rd party externals and abstractions know what
 # iteration of the vanilla API they have available). Instead, we use a version
-# number derived from PD_L2ORK_VERSION and PD_TEST_VERSION.
-PD_VERSION := $(PD_L2ORK_VERSION)-$(PD_TEST_VERSION)
+# number derived from PD_L2ORK_VERSION and PD_BUILD_VERSION.
+PD_VERSION := $(PD_L2ORK_VERSION)-$(PD_BUILD_VERSION)
 
 VERSION_DATE := $(shell date +%Y%m%d)
 
diff --git a/packages/darwin_app/Makefile b/packages/darwin_app/Makefile
index ee3e1323c..fb0f951dc 100644
--- a/packages/darwin_app/Makefile
+++ b/packages/darwin_app/Makefile
@@ -286,7 +286,7 @@ test_locations:
 	@echo "OPT_CFLAGS: $(OPT_CFLAGS)"
 	@echo "PD_VERSION: $(PD_VERSION)"
 	@echo "PACKAGE_NAME: $(PACKAGE_NAME)"
-	@echo "PD-EXTENDED_VERSION: $(PD-EXTENDED_VERSION)"
+	@echo "PD_BUILD_VERSION: $(PD_BUILD_VERSION)"
 	@echo "CWD $(CWD)"
 	@echo "DESTDIR $(DESTDIR)"
 	@echo "PREFIX $(prefix)"
diff --git a/packages/linux_make/Makefile b/packages/linux_make/Makefile
index 44d52cb89..5b9cf1ff6 100644
--- a/packages/linux_make/Makefile
+++ b/packages/linux_make/Makefile
@@ -264,7 +264,7 @@ test_locations:
 	@echo --------------------------------------------------
 	@echo "PD_VERSION: $(PD_VERSION)"
 	@echo "DEB_PD_VERSION: $(DEB_PD_VERSION)"
-	@echo "PD-EXTENDED_VERSION: $(PD-EXTENDED_VERSION)"
+	@echo "PD_BUILD_VERSION: $(PD_BUILD_VERSION)"
 	@echo "CWD $(CWD)"
 	@echo "DESTDIR $(DESTDIR)"
 	@echo "PACKAGE_TYPE $(PACKAGE_TYPE)"
diff --git a/packages/win32_inno/Makefile b/packages/win32_inno/Makefile
index 7080d93b2..9ee533f5b 100755
--- a/packages/win32_inno/Makefile
+++ b/packages/win32_inno/Makefile
@@ -145,7 +145,7 @@ $(PD_INNO_SETUP): $(PD_INNO_SETUP).in
 	cat "$(PD_INNO_SETUP).in" | \
 		sed 's/PACKAGE_NAME/$(PACKAGE_NAME)/g' | \
 		sed 's/PD_VERSION/$(PD_VERSION)/g' | \
-		sed 's/GIT_VERSION/$(GIT_VERSION)/g' > \
+		sed 's/PD_BUILD_VERSION/$(PD_BUILD_VERSION)/g' > \
 		$(PD_INNO_SETUP)
 #	start $(PD_INNO_SETUP)
 
@@ -182,7 +182,7 @@ test_package:
 test_locations:
 	@echo --------------------------------------------------
 	@echo "PD_VERSION: $(PD_VERSION)"
-	@echo "GIT_VERSION: $(GIT_VERSION)"
+	@echo "PD_BUILD_VERSION: $(PD_BUILD_VERSION)"
 	@echo "CWD $(CWD)"
 	@echo "DESTDIR $(DESTDIR)"
 	@echo "PREFIX $(prefix)"
diff --git a/pd/src/import.c b/pd/src/import.c
index 886a80922..039dce878 100644
--- a/pd/src/import.c
+++ b/pd/src/import.c
@@ -174,5 +174,5 @@ void import_setup(void)
     //post("\tcompiled against Pd version %d.%d.%d", PD_MAJOR_VERSION, 
     //     PD_MINOR_VERSION, PD_BUGFIX_VERSION);
     post("\tcompiled against Pd-l2ork version %s (%s)", PD_L2ORK_VERSION,
-	 PD_TEST_VERSION);
+	 PD_BUILD_VERSION);
 }
diff --git a/pd/src/m_pd.h.in b/pd/src/m_pd.h
similarity index 99%
rename from pd/src/m_pd.h.in
rename to pd/src/m_pd.h
index 58861f5a9..4cb8aa87a 100644
--- a/pd/src/m_pd.h.in
+++ b/pd/src/m_pd.h
@@ -13,7 +13,7 @@ extern "C" {
 #define PD_MAJOR_VERSION 0
 #define PD_MINOR_VERSION 43
 #define PD_BUGFIX_VERSION 10
-#define PD_TEST_VERSION "date/revision goes here"
+#define PD_TEST_VERSION ""
 #define PD_L2ORK_VERSION "1.99"
 #define PDL2ORK
 extern int pd_compatibilitylevel;   /* e.g., 43 for pd 0.43 compatibility */
diff --git a/pd/src/makefile.in b/pd/src/makefile.in
index 79ca85d10..70318c4f5 100644
--- a/pd/src/makefile.in
+++ b/pd/src/makefile.in
@@ -98,8 +98,10 @@ ifneq ($(PD_TEST_VERSION),)
 	PD_VERSION := $(PD_VERSION)-$(PD_TEST_VERSION)
 endif
 # AG: Pd-l2ork version number
-PD_L2ORK_VERSION := $(shell grep PD_L2ORK_VERSION m_pd.h.in | \
+PD_L2ORK_VERSION := $(shell grep PD_L2ORK_VERSION m_pd.h | \
 	sed 's|^.define *PD_L2ORK_VERSION *"\(.*\)".*|\1|' )
+PD_BUILD_VERSION := $(shell grep PD_BUILD_VERSION s_stuff.h | \
+	sed 's|^.define *PD_BUILD_VERSION *"\(.*\)".*|\1|' )
 
 #
 #  ------------------ targets ------------------------------------
@@ -203,7 +205,7 @@ install: all
                     $(DESTDIR)$(pddocdir)/7.stuff/$$dir ; \
 	done
 	mv $(ABOUT_FILE) $(ABOUT_FILE).tmp
-	cat $(ABOUT_FILE).tmp | sed 's|PD_VERSION|Pd-l2ork version $(PD_L2ORK_VERSION) ($(PD_TEST_VERSION))|' \
+	cat $(ABOUT_FILE).tmp | sed 's|PD_VERSION|Pd-l2ork version $(PD_L2ORK_VERSION) ($(PD_BUILD_VERSION))|' \
 		> $(ABOUT_FILE)
 	rm $(ABOUT_FILE).tmp
 	rsync -ax --exclude=.git --exclude=.svn ../extra $(DESTDIR)$(libpddir)/
diff --git a/pd/src/s_main.c b/pd/src/s_main.c
index b17e30cdb..91ae1cfe4 100644
--- a/pd/src/s_main.c
+++ b/pd/src/s_main.c
@@ -255,7 +255,7 @@ static void pd_makeversion(void)
     //    PD_BUGFIX_VERSION, PD_TEST_VERSION);    
 
     snprintf(foo, sizeof(foo), "Pd-L2Ork version %s (%s)\n", PD_L2ORK_VERSION,
-	     PD_TEST_VERSION);
+	     PD_BUILD_VERSION);
 
     pd_version = strdup(foo);
 }
diff --git a/pd/src/s_stuff.h b/pd/src/s_stuff.h.in
similarity index 97%
rename from pd/src/s_stuff.h
rename to pd/src/s_stuff.h.in
index 107b3b809..a5b72e89a 100644
--- a/pd/src/s_stuff.h
+++ b/pd/src/s_stuff.h.in
@@ -10,6 +10,12 @@ in future releases.  The public (stable) API is in m_pd.h. */
 #ifndef __s_stuff_h_
 #define __s_stuff_h_
 
+// AG: Generated build version number. Pd-l2ork uses this in lieu of vanilla's
+// PD_TEST_VERSION. We wouldn't want to touch m_pd.h for this, so it's part of
+// the internal API. Also note that this value gets substituted automatically
+// at build time, so any manual edits will be lost.
+#define PD_BUILD_VERSION "<build version>"
+
 /* in s_path.c */
 
 typedef struct _namelist    /* element in a linked list of stored strings */
-- 
GitLab