From 51217e01d2c855de3994775e9c3e72c7c83f11eb Mon Sep 17 00:00:00 2001
From: Tony Theodore <tonyt@logyst.com>
Date: Sat, 4 Nov 2017 20:45:55 +1100
Subject: updates: move to separate file and add generic updater function

---
 Makefile          | 48 ++-------------------------------
 tools/skeleton.py | 15 +++++++++--
 updates.mk        | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 48 deletions(-)
 create mode 100644 updates.mk

diff --git a/Makefile b/Makefile
index 5a2dd77..35bcc84 100644
--- a/Makefile
+++ b/Makefile
@@ -784,50 +784,6 @@ clean-pkg:
 clean-junk: clean-pkg
 	rm -rf $(LOG_DIR) $(call TMP_DIR,*)
 
-COMPARE_VERSIONS = $(strip \
-    $(if $($(1)_BRANCH),$(call seq,$($(1)_VERSION),$(2)),\
-    $(filter $(2),$(shell printf '$($(1)_VERSION)\n$(2)' | $(SORT) -V | head -1))))
-
-.PHONY: update
-define UPDATE
-    $(if $(2),
-        $(if $(filter $($(1)_IGNORE),$(2)),
-            $(info IGNORED  $(1)  $(2)),
-            $(if $(COMPARE_VERSIONS),
-                $(if $(filter $(2),$($(1)_VERSION)),
-                    $(info .        $(1)  $(2)),
-                    $(info OLD      $(1)  $($(1)_VERSION) --> $(2) ignoring)),
-                $(info NEW      $(1)  $($(1)_VERSION) --> $(2))
-                $(if $(findstring undefined, $(origin UPDATE_DRYRUN)),
-                    $(SED) -i 's/^\([^ ]*_VERSION *:=\).*/\1 $(2)/' '$($(1)_MAKEFILE)'
-                    $(MAKE) -f '$(MAKEFILE)' 'update-checksum-$(1)' \
-                        || { $(SED) -i 's/^\([^ ]*_VERSION *:=\).*/\1 $($(1)_VERSION)/' '$($(1)_MAKEFILE)'; \
-                             exit 1; }))),
-        $(info Unable to update version number of package $(1) \
-            $(newline)$(newline)$($(1)_UPDATE)$(newline)))
-
-endef
-update:
-	$(foreach PKG,$(PKGS),\
-	    $(and $($(PKG)_UPDATE),$(call UPDATE,$(PKG),$(shell $($(PKG)_UPDATE)))))
-
-update-package-%:
-	$(if $(call set_is_member,$*,$(PKGS)), \
-	    $(and $($*_UPDATE),$(call UPDATE,$*,$(shell $($*_UPDATE)))), \
-	    $(error Package $* not found))
-	    @echo -n
-
-check-update-package-%: UPDATE_DRYRUN = true
-check-update-package-%: update-package-% ;
-
-update-checksum-%: MXE_NO_BACKUP_DL = true
-update-checksum-%: SKIP_CHECHSUM = true
-update-checksum-%:
-	$(if $(call set_is_member,$*,$(PKGS)), \
-	    $(call DOWNLOAD_PKG_ARCHIVE,$*) && \
-	    $(SED) -i 's/^\([^ ]*_CHECKSUM *:=\).*/\1 '"`$(call PKG_CHECKSUM,$*)`"'/' '$($*_MAKEFILE)', \
-	    $(error Package $* not found))
-
 .PHONY: cleanup-style
 define CLEANUP_STYLE
     @$(SED) ' \
@@ -965,6 +921,6 @@ docs/packages.json: $(foreach 1,$(PKGS),$(PKG_MAKEFILES))
 	@echo '    "": null'             >> $@
 	@echo '}'                        >> $@
 
-# for patch-tool-mxe
-
+# for other mxe functions
 include patch.mk
+include updates.mk
diff --git a/tools/skeleton.py b/tools/skeleton.py
index 065bf16..2dd50cf 100755
--- a/tools/skeleton.py
+++ b/tools/skeleton.py
@@ -47,9 +47,18 @@ $(PKG)_URL      := %(file_url_template)s
 
 UPDATE = r'''
 define $(PKG)_UPDATE
-    echo 'TODO: write update script for %(name)s.' >&2;
-    echo $(%(name)s_VERSION)
+    $(call GET_LATEST_VERSION, %(update_url_template)s)
 endef
+# $(call GET_LATEST_VERSION, base url[, prefix, ext, filter, separator])
+#  base url : required page returning list of versions
+#               e.g https://ftp.gnu.org/gnu/libfoo
+#  prefix   : segment before version
+#               defaults to lastword of url with dash i.e. `libfoo-`
+#  ext      : segment ending version - default `\.tar`
+#  filter   : `grep -i` filter-out pattern - default alpha\|beta\|rc
+#  separator: transform char to `.` - typically `_`
+
+# test with make check-update-package-%(name)s and delete comments
 '''
 
 CMAKE_BUILD = r'''
@@ -177,6 +186,7 @@ def make_skeleton(
     filename = get_filename(file_url)
     filename_template = filename.replace(version, '$($(PKG)_VERSION)')
     file_url_template = file_url.replace(version, '$($(PKG)_VERSION)')
+    update_url_template = file_url.replace('/' + filename,'')
     subdir_template = subdir.replace(version, '$($(PKG)_VERSION)')
     libname = name
     if libname.startswith('lib'):
@@ -188,6 +198,7 @@ def make_skeleton(
             'libname': libname,
             'website': website,
             'file_url_template': file_url_template,
+            'update_url_template': update_url_template,
             'gh_conf': gh_conf,
             'checksum': checksum,
             'version': version,
diff --git a/updates.mk b/updates.mk
new file mode 100644
index 0000000..9a4455d
--- /dev/null
+++ b/updates.mk
@@ -0,0 +1,79 @@
+# This file is part of MXE. See LICENSE.md for licensing information.
+
+# Generic updater
+# $(call GET_LATEST_VERSION, base url[, prefix, ext, filter, separator])
+#  base url : required page returning list of versions
+#               e.g https://ftp.gnu.org/gnu/libfoo
+#  prefix   : segment before version
+#               defaults to lastword of url with dash i.e. `libfoo-`
+#  ext      : segment ending version - default `\.tar`
+#  filter   : `grep -i` filter-out pattern - default alpha\|beta\|rc
+#  separator: transform char to `.` - typically `_`
+#
+# test changes with:
+# make check-get-latest-version
+#
+# and update tools/skeleton.py with usage notes
+
+define GET_LATEST_VERSION
+    $(WGET) -q -O- '$(strip $(1))' | \
+    $(SED) -n 's,.*<a href=".*$(strip $(or $(2),$(lastword $(subst /,$(space),$(1)))-))\([0-9][^"]*\)$(strip $(or $(3),\.tar)).*,\1,p' | \
+    grep -vi '$(strip $(or $(4),alpha\|beta\|rc))' | \
+    tr '$(strip $(5))' '.' | \
+    $(SORT) -V | \
+    tail -1
+endef
+
+ALL_DIRS := $(MXE_PLUGIN_DIRS) $(shell find $(realpath $(TOP_DIR)/plugins) -type d)
+GET_LATEST_VERSION_PKGS := $(sort \
+    $(basename $(notdir $(shell grep -l GET_LATEST_VERSION -r $(ALL_DIRS)))))
+
+.PHONY: check-get-latest-version
+check-get-latest-version:
+	@$(MAKE) -f '$(MAKEFILE)' \
+	    $(addprefix check-update-package-,$(GET_LATEST_VERSION_PKGS)) \
+	    MXE_PLUGIN_DIRS='$(ALL_DIRS)'
+
+COMPARE_VERSIONS = $(strip \
+    $(if $($(1)_BRANCH),$(call seq,$($(1)_VERSION),$(2)),\
+    $(filter $(2),$(shell printf '$($(1)_VERSION)\n$(2)' | $(SORT) -V | head -1))))
+
+.PHONY: update
+define UPDATE
+    $(if $(2),
+        $(if $(filter $($(1)_IGNORE),$(2)),
+            $(info IGNORED  $(1)  $(2)),
+            $(if $(COMPARE_VERSIONS),
+                $(if $(filter $(2),$($(1)_VERSION)),
+                    $(info .        $(1)  $(2)),
+                    $(info OLD      $(1)  $($(1)_VERSION) --> $(2) ignoring)),
+                $(info NEW      $(1)  $($(1)_VERSION) --> $(2))
+                $(if $(findstring undefined, $(origin UPDATE_DRYRUN)),
+                    $(SED) -i 's/^\([^ ]*_VERSION *:=\).*/\1 $(2)/' '$($(1)_MAKEFILE)'
+                    $(MAKE) -f '$(MAKEFILE)' 'update-checksum-$(1)' \
+                        || { $(SED) -i 's/^\([^ ]*_VERSION *:=\).*/\1 $($(1)_VERSION)/' '$($(1)_MAKEFILE)'; \
+                             exit 1; }))),
+        $(info Unable to update version number of package $(1) \
+            $(newline)$(newline)$($(1)_UPDATE)$(newline)))
+
+endef
+update:
+	$(foreach PKG,$(PKGS),\
+	    $(and $($(PKG)_UPDATE),$(call UPDATE,$(PKG),$(shell $($(PKG)_UPDATE)))))
+
+update-package-%:
+	$(if $(call set_is_member,$*,$(PKGS)), \
+	    $(and $($*_UPDATE),$(call UPDATE,$*,$(shell $($*_UPDATE)))), \
+	    $(error Package $* not found))
+	    @echo -n
+
+check-update-package-%: UPDATE_DRYRUN = true
+check-update-package-%: update-package-% ;
+
+update-checksum-%: MXE_NO_BACKUP_DL = true
+update-checksum-%: SKIP_CHECHSUM = true
+update-checksum-%:
+	$(if $(call set_is_member,$*,$(PKGS)), \
+	    $(call DOWNLOAD_PKG_ARCHIVE,$*) && \
+	    $(SED) -i 's/^\([^ ]*_CHECKSUM *:=\).*/\1 '"`$(call PKG_CHECKSUM,$*)`"'/' '$($*_MAKEFILE)', \
+	    $(error Package $* not found))
-- 
cgit v0.12