[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 07a02d3 1/4: Purify sourcing demonstration by
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 07a02d3 1/4: Purify sourcing demonstration by removing a desirable refinement |
Date: |
Fri, 24 May 2019 09:41:07 -0400 (EDT) |
branch: master
commit 07a02d3cc805e0d736bae90124387cc64e4fc145
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Purify sourcing demonstration by removing a desirable refinement
Moved commands to set 'make' variables out of the shell script and back
into the makefile.
The parent commit is preferable for actual use. See:
https://lists.nongnu.org/archive/html/lmi/2019-05/msg00052.html
et seq.
Ask the internet how to source a shell script in a makefile, and it will
answer that it simply can't be done--that the script must be changed.
This revision proves that it can be done, without changing the script.
That is this demonstration's purpose: to show how it can be done,
regardless of whether it should be done in exactly this way.
---
gwc/parent.make | 21 +++++++++++++++------
gwc/set.sh | 10 +---------
2 files changed, 16 insertions(+), 15 deletions(-)
diff --git a/gwc/parent.make b/gwc/parent.make
index 81fd3eb..e835b9a 100644
--- a/gwc/parent.make
+++ b/gwc/parent.make
@@ -5,10 +5,9 @@
# - give the top-level makefile a target to remake itself, with
# - $(eval include $(LMI_ENV_FILE)) in its recipe, and
# - $(LMI_ENV_FILE) as a prerequisite
-# - add a phony $(LMI_ENV_FILE) target that sources the script
-# - make the script write 'make' assignments like "export foo := bar"
-# for each desired environment variable to a file named
-# $LMI_ENV_FILE, iff that filename is of nonzero length
+# - add a phony $(LMI_ENV_FILE) target whose recipe sources the
+# script and writes 'make' assignments like "export foo := bar"
+# into the file it names
# To test:
# $export LMI_IN=Russia; make -f parent.make all
# $export LMI_IN=Mongolia; make -f parent.make all
@@ -22,14 +21,24 @@ parent.make:: $(LMI_ENV_FILE)
rm $(LMI_ENV_FILE)
$(LMI_ENV_FILE):
- @echo "Sourcing 'set.sh'"; \
- . ./set.sh ; \
+ @echo "Sourcing 'set.sh'"
+ @. ./set.sh ; \
+ { \
+ echo "export LMI_OUT1 := $$LMI_OUT1"; \
+ echo "export LMI_OUT2 := $$LMI_OUT2"; \
+ } > $@ ; \
echo "'$$LMI_IN' --> '$$LMI_OUT1', '$$LMI_OUT2' : sourced in
'parent.make'"
all:
@echo "'$$LMI_IN' --> '$$LMI_OUT1', '$$LMI_OUT2' : targets in
'parent.make'"
$(MAKE) --no-print-directory -f child.make
+# For real-world use, commit 3ff6c008 seems preferable. It writes
+# 'make' assignments in the script rather than in this makefile,
+# facilitating maintenance by keeping the list of variables in a
+# single file. See this discussion:
+# https://lists.nongnu.org/archive/html/lmi/2019-05/msg00052.html
+
# Obviously one could simply write a cover script to replace direct
# invocation of 'make', but that's nasty. See:
# https://lists.gnu.org/archive/html/help-make/2006-04/msg00142.html
diff --git a/gwc/set.sh b/gwc/set.sh
index 0660dc9..924fc58 100755
--- a/gwc/set.sh
+++ b/gwc/set.sh
@@ -2,10 +2,8 @@
foo()
{
-# $LMI_ENV_FILE is defined by the makefile that sources this script.
-# shellcheck disable=SC2154
-echo "LMI_ENV_FILE in 'set.sh': $LMI_ENV_FILE"
echo "'$LMI_IN' --> '$LMI_OUT1', '$LMI_OUT2' : entering 'set.sh'"
+
export LMI_OUT1="$LMI_IN"
export LMI_OUT2="$LANG"
@@ -16,12 +14,6 @@ case "$LMI_IN" in
esac
echo "'$LMI_IN' --> '$LMI_OUT1', '$LMI_OUT2' : leaving 'set.sh'"
-if [ -n "$LMI_ENV_FILE" ]; then
- {
- echo "export LMI_OUT1 := $LMI_OUT1"
- echo "export LMI_OUT2 := $LMI_OUT2"
- } > "$LMI_ENV_FILE"
-fi
}
foo