From 556a5d390cd82804237cdd65cb3907c2e67435f0 Mon Sep 17 00:00:00 2001
From: "Lucas A. M. Magalhaes" <lucas.magalhaes@ess.eu>
Date: Fri, 20 Jan 2023 09:31:35 +0100
Subject: [PATCH] Move module_VERSION definitions to first make call (fix
 E3-1112)

From e3-require 4.0.0 to 5.0.0 we changed FETCH_BUILD_NUMBER to
FETCH_REVISION_NUMBER. But the following error happens if you try to
build e3-adandor, for example:

2023-01-19 13:41:49,129 [  DEBUG  ] e3.utils: stderr: HEAD is now at 6f3a1f4... Merge pull request #44 from sudilav/master
msi: Can't open file 'ADBase.template'
input: 'include "ADBase.template"
' at line 5 of  file andorApp/Db/andorCCD.template

The root cause is a mixture of two things, a change we made for 5.0.0
and an old behavior:
1. We changed when the db expansions happen in
b226a847c403eba79bc91efee18d225ed2753c7
2. The variables holding the dependency versions are defined on the
second make call after defining T_A.

This commit fixes the issue by moving the module_VERISON definitions
to the first make call. Whit this we restrict the usage of
FETCH_REVISION_NUMBER to the modules defined as a dependency in
configure/CONFIG_MODULE.
---
 require-ess/tools/driver.makefile | 55 +++++++++++++++++--------------
 1 file changed, 30 insertions(+), 25 deletions(-)

diff --git a/require-ess/tools/driver.makefile b/require-ess/tools/driver.makefile
index 7d95aca8..ceab7598 100644
--- a/require-ess/tools/driver.makefile
+++ b/require-ess/tools/driver.makefile
@@ -143,6 +143,20 @@ endef
 define FETCH_REVISION_NUMBER
 $(shell $(MAKEHOME)/revision_number $(1) $(2) $($(2)_VERSION))
 endef
+# Create the module_VERSION variables giving the module and check if the module
+# is actually installed. Add the module to the MODULES and REQ variables. That
+# will be used later.
+define fetch_module_versions
+  lm := $$(shell echo $1 | tr '[:upper:]' '[:lower:]')
+  ifneq ($$(strip $$(filter $(INSTALLED_MODULES),$$(lm))),)
+    $$(lm)_VERSION := $($1_DEP_VERSION$2)
+    $$(lm)_VERSION := $$(lastword $$(call FETCH_REVISION_NUMBER,$(E3_SITEMODS_PATH),$$(lm)) $$(call FETCH_REVISION_NUMBER,$(EPICS_MODULES),$$(lm)))
+    MODULES += $$(lm)
+    REQ += $$(lm)
+  else
+    $$(warning Invalid dependency "$1_DEP_VERSION$2"; pruning)
+  endif
+endef
 
 # Functions used for recursive dependency fetching. These are modified from https://github.com/markpiffer/gmtt.git
 space := $(strip) $(strip)#
@@ -282,6 +296,20 @@ $1-%: | $(COMMON_DIR)
 endef
 $(foreach target,install build debug,$(eval $(call target_rule,$(target))))
 
+MODULES :=
+REQ :=
+INSTALLED_MODULES := $(sort $(notdir $(wildcard $(E3_SITEMODS_PATH)/* $(EPICS_MODULES)/*)))
+export INSTALLED_MODULES
+
+# Converts all of the X_DEP_VERSIONs to x_VERSION and records them
+# Calls fetch_module_version for each module that appears in a
+# module_DEP_VERSION variable. This is defined on the wrapper CONFIG_MODULE
+$(foreach m,$(patsubst %_DEP_VERSION,%,$(filter %_DEP_VERSION,$(.VARIABLES))),$(eval $(call fetch_module_versions,$m)))
+export REQ
+export MODULES
+
+$(foreach m,$(MODULES),$(eval export $m_VERSION))
+
 .SECONDEXPANSION:
 
 # This has to fit under .SECONDEXPANSION in order to catch TMPS and SUBS, which are typically defined
@@ -306,37 +334,14 @@ ARCH_PARTS = ${T_A} $(subst -, ,${T_A}) ${OS_CLASS}
 VAR_EXTENSIONS = ${EPICSVERSION} ${ARCH_PARTS} ${ARCH_PARTS:%=${EPICSVERSION}_%}
 export VAR_EXTENSIONS
 
-MODULES :=
-PROCESSED_MODULES :=
-REQ :=
-INSTALLED_MODULES := $(sort $(notdir $(wildcard $(E3_SITEMODS_PATH)/* $(EPICS_MODULES)/*)))
-
-# Converts all of the X_DEP_VERSIONs to x_VERSION and records them
-
-# Create the module_VERSION variables giving the module and check if the module
-# is actually installed. Add the module to the MODULES and REQ variables. That
-# will be used later.
-define fetch_module_versions
-  lm := $$(shell echo $1 | tr '[:upper:]' '[:lower:]')
-  ifneq ($$(strip $$(filter $(INSTALLED_MODULES),$$(lm))),)
-    $$(lm)_VERSION := $($1_DEP_VERSION$2)
-    $$(lm)_VERSION := $$(lastword $$(call FETCH_REVISION_NUMBER,$(E3_SITEMODS_PATH),$$(lm)) $$(call FETCH_REVISION_NUMBER,$(EPICS_MODULES),$$(lm)))
-    MODULES += $$(lm)
-    REQ += $$(lm)
-  else
-    $$(warning Invalid dependency "$1_DEP_VERSION$2"; pruning)
-  endif
-endef
-# Calls fetch_module_version for each module that appears in a
-# module_DEP_VERSION variable. This is defined on the wrapper CONFIG_MODULE
-$(foreach m,$(patsubst %_DEP_VERSION,%,$(filter %_DEP_VERSION,$(.VARIABLES))),$(eval $(call fetch_module_versions,$m)))
 # Look for dependencies on extension files the same way as above
 $(foreach x,$(VAR_EXTENSIONS),\
   $(foreach m,$(patsubst %_DEP_VERSION_$(x),%,$(filter %_DEP_VERSION_$(x),$(.VARIABLES))),$(eval $(call fetch_module_versions,$m,_$(x))))\
 )
 export REQ
+export MODULES
 
-
+PROCESSED_MODULES :=
 # Fetches the data from .dep files to be parsed by the above
 define fetch_deps
 $(shell cat $(or \
-- 
GitLab