From 409a5ac7c368e61e3b43a5f4614ec032144c466d Mon Sep 17 00:00:00 2001
From: Simon Rose <simon.rose@ess.eu>
Date: Fri, 22 Jan 2021 10:20:02 +0100
Subject: [PATCH] Removed redundant code and fixed require.Makefile to remove
 unnecessary directive

---
 require-ess/tools/build_number.sh |  9 ++++++++-
 require-ess/tools/driver.makefile | 10 +++++-----
 require.Makefile                  |  3 ---
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/require-ess/tools/build_number.sh b/require-ess/tools/build_number.sh
index 9246201e..bfef0766 100755
--- a/require-ess/tools/build_number.sh
+++ b/require-ess/tools/build_number.sh
@@ -1,4 +1,11 @@
 #!/bin/env bash
+# Small script to fetch current build nuber for a given module. If you pass a numeric version
+# but no build number, it fetches the latest build number installed from the target e3 env.
+#
+# Arguments: 
+#    $1: require module path e.g. /epics/base-$BASE_VERSION/require/$REQUIRE_VERSION/siteMods
+#    $2: module name
+#    $3: module version
 shopt -s extglob
 
 [[ $# -ne 3 ]] && exit 1
@@ -9,7 +16,7 @@ v="$3"
 
 if [[ $v =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
   # shellcheck disable=SC2012
-  basename "$(ls -dvr "$p/$m/$v"?(-+([0-9])) 2>/dev/null | head -n 1)" | sed 's/^[^-]\+$/&-0/'
+  basename "$(ls -dvr "$p/$m/$v"?(-+([0-9])) 2>/dev/null | head -n 1)"
 else
   echo "$v"
 fi
\ No newline at end of file
diff --git a/require-ess/tools/driver.makefile b/require-ess/tools/driver.makefile
index 8c25f031..967314d2 100644
--- a/require-ess/tools/driver.makefile
+++ b/require-ess/tools/driver.makefile
@@ -548,16 +548,16 @@ EPICS_INCLUDES =
 # $(eval $(foreach m,$(filter-out $(PRJ),$(notdir $(wildcard ${EPICS_MODULES}/*))),$(call ADD_FOREIGN_INCLUDES,$m)))
 
 define FETCH_BUILD_NUMBER
-$(shell $(E3_REQUIRE_TOOLS)/build_number.sh $(E3_SITEMODS_PATH) $(1) $($1_VERSION))
+$(shell $(E3_REQUIRE_TOOLS)/build_number.sh $(1) $(2) $($(2)_VERSION))
 endef
 
 define ADD_SITEMODS_INCLUDES
-INSTALL_INCLUDES += $$(patsubst %,-I${E3_SITEMODS_PATH}/$(1)/%/include,$$(shell $(E3_REQUIRE_TOOLS)/build_number.sh $(E3_SITEMODS_PATH) $(1) $$($(1)_VERSION)))
+INSTALL_INCLUDES += $$(patsubst %,-I${E3_SITEMODS_PATH}/$(1)/%/include,$$(call FETCH_BUILD_NUMBER,$(E3_SITEMODS_PATH),$(1)))
 endef
 $(foreach m,$(filter-out $(PRJ),$(notdir $(wildcard ${E3_SITEMODS_PATH}/*))),$(eval $(call ADD_SITEMODS_INCLUDES,$m)))
 
 define ADD_SITEAPPS_INCLUDES
-INSTALL_INCLUDES += $$(patsubst %,-I${E3_SITEAPPS_PATH}/$(1)/%/include,$$($(1)_VERSION))
+INSTALL_INCLUDES += $$(patsubst %,-I${E3_SITEAPPS_PATH}/$(1)/%/include,$$(call FETCH_BUILD_NUMBER,$(E3_SITEAPPS_PATH),$(1)))
 endef
 $(foreach m,$(filter-out $(PRJ),$(notdir $(wildcard ${E3_SITEAPPS_PATH}/*))),$(eval $(call ADD_SITEAPPS_INCLUDES,$m)))
 
@@ -680,7 +680,7 @@ DBDFILES += $(patsubst %.gt,%.dbd,$(notdir $(filter %.gt,${SRCS})))
 
 # snc location in 3.14: From latest version of module seq or fall back to globally installed snc.
 #SNC=$(lastword $(dir ${EPICS_BASE})seq/bin/$(EPICS_HOST_ARCH)/snc $(shell ls -dv ${EPICS_MODULES}/seq/$(or $(seq_VERSION),$(VERSIONGLOB))/bin/${EPICS_HOST_ARCH}/snc 2>/dev/null))
-SNCALL=$(shell ls  -dv $(E3_SITEMODS_PATH)/sequencer/$(call FETCH_BUILD_NUMBER,sequencer)/bin/$(EPICS_HOST_ARCH) 2> /dev/null)
+SNCALL=$(shell ls  -dv $(E3_SITEMODS_PATH)/sequencer/$(call FETCH_BUILD_NUMBER,$(E3_SITEMODS_PATH),sequencer)/bin/$(EPICS_HOST_ARCH) 2> /dev/null)
 SNC=$(lastword $(SNCALL))/snc
 
 
@@ -1011,7 +1011,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 $(call FETCH_BUILD_NUMBER,$m),$(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 $(call FETCH_BUILD_NUMBER,$(E3_SITEMODS_PATH),$m),$(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
 
 # Remove MakefileInclude after we are done because it interfers with our way to build.
diff --git a/require.Makefile b/require.Makefile
index b7d55bfd..a1acaf6e 100644
--- a/require.Makefile
+++ b/require.Makefile
@@ -98,9 +98,6 @@ USR_DBFLAGS += -I$(EPICS_BASE)/db
 
 TEMS = $(wildcard $(APPDB)/*.template)
 
-# We need to specify the correct location for add_build_no.sh when building require.dep
-require.dep: E3_REQUIRE_TOOLS = $(where_am_I)/tools
-
 db: $(TEMS)
 
 $(TEMS): 
-- 
GitLab