From 425a6862b62890db43dfa143e2cf0a58a6224424 Mon Sep 17 00:00:00 2001
From: Simon Rose <simon.rose@ess.eu>
Date: Tue, 19 Jan 2021 12:25:02 +0100
Subject: [PATCH] Updated dependency fetcher to add build number if need be

---
 .gitignore                        |  2 ++
 configure/modules/DECOUPLE_FLAGS  |  4 ++--
 require-ess/tools/driver.makefile | 21 ++++++++++++++-------
 3 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/.gitignore b/.gitignore
index 990fb908..73d283f5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,3 +17,5 @@ e3.cfg
 tools/ess-env.conf
 .vscode
 require-ess/require.Makefile
+require-ess/Db/*.db
+require-ess/Db/*.d
\ No newline at end of file
diff --git a/configure/modules/DECOUPLE_FLAGS b/configure/modules/DECOUPLE_FLAGS
index a2eae503..6904d96a 100644
--- a/configure/modules/DECOUPLE_FLAGS
+++ b/configure/modules/DECOUPLE_FLAGS
@@ -3,10 +3,10 @@
 # The only way to exclude header files is to set $(module)_VERSION to a blank.
 
 # We first collect all installed modules from $(E3_SITEMODS_PATH) ...
-$(foreach dir,$(patsubst $(E3_SITEMODS_PATH)/%,%,$(wildcard $(E3_SITEMODS_PATH)/*)),$(eval DECOUPLE_MODULES += $(dir)))
+#$(foreach dir,$(patsubst $(E3_SITEMODS_PATH)/%,%,$(wildcard $(E3_SITEMODS_PATH)/*)),$(eval DECOUPLE_MODULES += $(dir)))
 
 # ... except that we don't want to overwrite the versions from required modules, so keep those.
-$(foreach mod,$(filter-out $(REQ),$(DECOUPLE_MODULES)),$(eval $(mod)_VERSION =))
+#$(foreach mod,$(filter-out $(REQ),$(DECOUPLE_MODULES)),$(eval $(mod)_VERSION =))
 
 # In addition to decouple the existent e3 module,
 # We use this opportunatiy in order to apply global configuration
diff --git a/require-ess/tools/driver.makefile b/require-ess/tools/driver.makefile
index 977895d8..32333d7a 100644
--- a/require-ess/tools/driver.makefile
+++ b/require-ess/tools/driver.makefile
@@ -547,24 +547,31 @@ EPICS_INCLUDES =
 # endef
 # $(eval $(foreach m,$(filter-out $(PRJ),$(notdir $(wildcard ${EPICS_MODULES}/*))),$(call ADD_FOREIGN_INCLUDES,$m)))
 
+# Note: Due to the addition of build numbers, we have to be somewhat more careful here.
+#       If module A depends on module B version 3.1.0, then it should record the current build number and save
+#       that. Otherwise we can end up in a situation where a new module being installed can cause dependencies
+#       to fail to load correctly. The goal of the below is to explicitly add the latest build number, if none 
+#       is specified. It works as follows:
+#       $(if $(shell [[ "$($1_VERSION)" =~ ^[0-9]+\.[0-9]+\.[0-9]+$$ ]] && echo match),                             ## We test that the module version is of the form x.y.z exactly. In that case...
+#           $(notdir $(firstword $(shell ls -dvr $(E3_SITEMODS_PATH)/$1/$($1_VERSION)?(-+([0-9])) 2> /dev/null ))), ## ...find the highest build number with the given version...
+#           $($1_VERSION))                                                                                          ## ... else preserve the module number as is.
+
 define ADD_SITEMODS_INCLUDES
-$(eval $(1)_VERSION := $(patsubst ${E3_SITEMODS_PATH}/$(1)/%/include,%,$(firstword $(shell ls -dvr ${E3_SITEMODS_PATH}/$(1)/$(VERSIONGLOB)/include 2>/dev/null))))
-INSTALL_INCLUDES += $$(patsubst %,-I${E3_SITEMODS_PATH}/$(1)/%/include,$$($(1)_VERSION))
+INSTALL_INCLUDES += $$(patsubst %,-I$(E3_SITEMODS_PATH)/$1/%/include,$$(if $$(shell [[ "$$($1_VERSION)" =~ ^[0-9]+\.[0-9]+\.[0-9]+$$$$ ]] && echo match),$$(notdir $$(firstword $$(shell ls -dvr $(E3_SITEMODS_PATH)/$1/$$($1_VERSION)?(-+([0-9])) 2> /dev/null ))),$$($1_VERSION)))
 endef
 $(eval $(foreach m,$(filter-out $(PRJ),$(notdir $(wildcard ${E3_SITEMODS_PATH}/*))),$(call ADD_SITEMODS_INCLUDES,$m)))
 
 define ADD_SITEAPPS_INCLUDES
-$(eval $(1)_VERSION := $(patsubst ${E3_SITEAPPS_PATH}/$(1)/%/include,%,$(firstword $(shell ls -dvr ${E3_SITEAPPS_PATH}/$(1)/$(VERSIONGLOB)/include 2>/dev/null))))
-INSTALL_INCLUDES += $$(patsubst %,-I${E3_SITEAPPS_PATH}/$(1)/%/include,$$($(1)_VERSION))
+INSTALL_INCLUDES += $$(patsubst %,-I$(E3_SITEAPPS_PATH)/$1/%/include,$$(if $$(shell [[ "$$($1_VERSION)" =~ ^[0-9]+\.[0-9]+\.[0-9]+$$$$ ]] && echo match),$$(notdir $$(firstword $$(shell ls -dvr $(E3_SITEAPPS_PATH)/$1/$$($1_VERSION)?(-+([0-9])) 2> /dev/null ))),$$($1_VERSION)))
 endef
 $(eval $(foreach m,$(filter-out $(PRJ),$(notdir $(wildcard ${E3_SITEAPPS_PATH}/*))),$(call ADD_SITEAPPS_INCLUDES,$m)))
 
 
 # Manually required modules.
 define ADD_MANUAL_DEPENDENCIES
-$(eval $(1)_VERSION := $(patsubst ${E3_SITEMODS_PATH}/$(1)/%,%,$(firstword $(shell ls -dvr ${E3_SITEMODS_PATH}/$(1)/$(VERSIONGLOB) 2>/dev/null))))
+$1_VERSION_BUILD = $$(if $$(shell [[ "$$($1_VERSION)" =~ ^[0-9]+\.[0-9]+\.[0-9]+$$$$ ]] && echo match),$$(notdir $$(firstword $$(shell ls -dvr $(E3_SITEMODS_PATH)/$1/$$($1_VERSION)?(-+([0-9])) 2> /dev/null ))),$$($1_VERSION))
 endef
-$(eval $(foreach m,${REQ},$(call ADD_MANUAL_DEPENDENCIES,$m)))
+$(foreach m,${REQ},$(eval $(call ADD_MANUAL_DEPENDENCIES,$m)))
 
 BASERULES=${EPICS_BASE}/configure/RULES
 
@@ -1016,7 +1023,7 @@ ${DEPFILE}: ${LIBOBJS} $(USERMAKEFILE)
 	cat *.d 2>/dev/null | sed 's/ /\n/g' | sed -n '$(DEP_PARSER)' | grep -v "include" | sort -u >> $@
 ifneq ($(strip ${REQ}),)
 # Manully added dependencies: ${REQ}
-	@$(foreach m,${REQ},echo "$m $(or ${$m_VERSION},$(and $(wildcard ${E3_SITEMODS_PATH}/$m),$(error REQUIRED module $m has no numbered version. Set $m_VERSION)),$(warning REQUIRED module $m not found for ${T_A}.))" >> $@;)
+	@$(foreach m,${REQ},echo "$m $(or ${$m_VERSION_BUILD},$(and $(wildcard ${E3_SITEMODS_PATH}/$m),$(error REQUIRED module $m has no numbered version. Set $m_VERSION)),$(warning REQUIRED module $m not found for ${T_A}.))" >> $@;)
 endif
 # Update the version numbers to ensure that the build number is included
 	sed -i 's/^\([a-z_][a-z0-9_]*\) \([0-9]\+\.[0-9]\+\.[0-9]\+\)$$/\1 \2-0/' $@
-- 
GitLab