From 9dc1ac7f585a006518af52f806eabab77288c0d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20=C5=A0tetiar?= <ynezz@true.cz>
Date: Tue, 23 Oct 2018 17:48:22 +0200
Subject: [PATCH] build: replace hardcoded paths with overridable variables
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Petr Å tetiar <ynezz@true.cz>
---
 Makefile                       | 37 +++++++++++++++++-----------------
 scripts/copy_output.sh         |  4 ++--
 scripts/generate_manifest.sh   |  4 ++--
 scripts/patch.sh               |  4 +++-
 scripts/target_config.sh       |  6 +++---
 scripts/target_config_check.sh |  6 +++---
 scripts/update-patches.sh      |  8 +++++---
 targets/brcm2708-bcm2708       |  2 +-
 targets/brcm2708-bcm2709       |  2 +-
 targets/brcm2708-bcm2710       |  2 +-
 targets/x86-64                 |  2 +-
 targets/x86-generic            |  2 +-
 12 files changed, 42 insertions(+), 37 deletions(-)

diff --git a/Makefile b/Makefile
index 47b4a5f65..5ebefa21c 100644
--- a/Makefile
+++ b/Makefile
@@ -4,15 +4,6 @@ LC_ALL:=C
 LANG:=C
 export LC_ALL LANG
 
-
-# initialize (possibly already user set) directory variables
-GLUON_SITEDIR ?= site
-GLUON_TMPDIR ?= tmp
-GLUON_OUTPUTDIR ?= output
-
-GLUON_IMAGEDIR ?= $(GLUON_OUTPUTDIR)/images
-GLUON_PACKAGEDIR ?= $(GLUON_OUTPUTDIR)/packages
-
 # check for spaces & resolve possibly relative paths
 define mkabspath
  ifneq (1,$(words [$($(1))]))
@@ -21,14 +12,8 @@ define mkabspath
  override $(1) := $(abspath $($(1)))
 endef
 
+GLUON_SITEDIR ?= site
 $(eval $(call mkabspath,GLUON_SITEDIR))
-$(eval $(call mkabspath,GLUON_TMPDIR))
-$(eval $(call mkabspath,GLUON_OUTPUTDIR))
-$(eval $(call mkabspath,GLUON_IMAGEDIR))
-$(eval $(call mkabspath,GLUON_PACKAGEDIR))
-
-export GLUON_TMPDIR GLUON_IMAGEDIR GLUON_PACKAGEDIR GLUON_DEVICES
-
 
 $(GLUON_SITEDIR)/site.mk:
 	$(error No site configuration was found. Please check out a site configuration to $(GLUON_SITEDIR))
@@ -37,11 +22,27 @@ include $(GLUON_SITEDIR)/site.mk
 
 GLUON_RELEASE ?= $(error GLUON_RELEASE not set. GLUON_RELEASE can be set in site.mk or on the command line)
 
+# initialize (possibly already user set) directory variables
+GLUON_TMPDIR ?= tmp
+GLUON_OUTPUTDIR ?= output
+GLUON_IMAGEDIR ?= $(GLUON_OUTPUTDIR)/images
+GLUON_PACKAGEDIR ?= $(GLUON_OUTPUTDIR)/packages
+GLUON_TARGETSDIR ?= targets
+GLUON_PATCHESDIR ?= patches
+
+$(eval $(call mkabspath,GLUON_TMPDIR))
+$(eval $(call mkabspath,GLUON_OUTPUTDIR))
+$(eval $(call mkabspath,GLUON_IMAGEDIR))
+$(eval $(call mkabspath,GLUON_PACKAGEDIR))
+$(eval $(call mkabspath,GLUON_TARGETSDIR))
+$(eval $(call mkabspath,GLUON_PATCHESDIR))
+
 GLUON_MULTIDOMAIN ?= 0
 GLUON_WLAN_MESH ?= 11s
 GLUON_DEBUG ?= 0
 
-export GLUON_RELEASE GLUON_REGION GLUON_MULTIDOMAIN GLUON_WLAN_MESH GLUON_DEBUG
+export GLUON_RELEASE GLUON_REGION GLUON_MULTIDOMAIN GLUON_WLAN_MESH GLUON_DEBUG GLUON_DEVICES \
+	 GLUON_TARGETSDIR GLUON_PATCHESDIR GLUON_TMPDIR GLUON_IMAGEDIR GLUON_PACKAGEDIR
 
 show-release:
 	@echo '$(GLUON_RELEASE)'
@@ -70,7 +71,7 @@ GLUON_TARGET_$$(gluon_target)_BOARD := $(1)
 GLUON_TARGET_$$(gluon_target)_SUBTARGET := $(2)
 endef
 
-include targets/targets.mk
+include $(GLUON_TARGETSDIR)/targets.mk
 
 
 OPENWRTMAKE = $(MAKE) -C openwrt
diff --git a/scripts/copy_output.sh b/scripts/copy_output.sh
index d84d5fe2c..897a3f83c 100755
--- a/scripts/copy_output.sh
+++ b/scripts/copy_output.sh
@@ -2,7 +2,7 @@
 
 set -e
 
-[ "$GLUON_IMAGEDIR" -a "$GLUON_PACKAGEDIR" -a "$OPENWRT_TARGET" -a "$GLUON_RELEASE" -a "$GLUON_SITEDIR" ] || exit 1
+[ "$GLUON_IMAGEDIR" -a "$GLUON_PACKAGEDIR" -a "$OPENWRT_TARGET" -a "$GLUON_RELEASE" -a "$GLUON_SITEDIR" -a "$GLUON_TARGETSDIR" ] || exit 1
 
 
 default_factory_ext='.bin'
@@ -191,7 +191,7 @@ no_opkg() {
 }
 
 
-. targets/"$1"; copy
+. "${GLUON_TARGETSDIR}/$1"; copy
 
 # Copy opkg repo
 if [ -z "$no_opkg" -a -z "$GLUON_DEVICES" ]; then
diff --git a/scripts/generate_manifest.sh b/scripts/generate_manifest.sh
index 85e3ef530..04494a37f 100755
--- a/scripts/generate_manifest.sh
+++ b/scripts/generate_manifest.sh
@@ -2,7 +2,7 @@
 
 set -e
 
-[ "$GLUON_IMAGEDIR" -a "$GLUON_RELEASE" -a "$GLUON_SITEDIR" ] || exit 1
+[ "$GLUON_IMAGEDIR" -a "$GLUON_RELEASE" -a "$GLUON_SITEDIR" -a "$GLUON_TARGETSDIR" ] || exit 1
 
 
 default_sysupgrade_ext='.bin'
@@ -110,4 +110,4 @@ sysupgrade() {
 	fi
 }
 
-. targets/"$1"; generate
+. "${GLUON_TARGETSDIR}/$1"; generate
diff --git a/scripts/patch.sh b/scripts/patch.sh
index 61d5f4557..082ca1fce 100755
--- a/scripts/patch.sh
+++ b/scripts/patch.sh
@@ -3,6 +3,8 @@
 set -e
 shopt -s nullglob
 
+[ "$GLUON_TMPDIR" -a "$GLUON_PATCHESDIR" ] || exit 1
+
 . scripts/modules.sh
 
 
@@ -19,7 +21,7 @@ for module in $GLUON_MODULES; do
 	git clone -s -b base --single-branch "$GLUONDIR/$module" "$PATCHDIR" 2>/dev/null
 
 	cd "$PATCHDIR"
-	for patch in "$GLUONDIR/patches/$module"/*.patch; do
+	for patch in "${GLUON_PATCHESDIR}/$module"/*.patch; do
 		git -c user.name='Gluon Patch Manager' -c user.email='gluon@void.example.com' -c commit.gpgsign=false am --whitespace=nowarn --committer-date-is-author-date "$patch"
 	done
 
diff --git a/scripts/target_config.sh b/scripts/target_config.sh
index 7ffa6403d..559eabea1 100755
--- a/scripts/target_config.sh
+++ b/scripts/target_config.sh
@@ -2,7 +2,7 @@
 
 set -e
 
-[ "$OPENWRT_TARGET" ] || exit 1
+[ "$OPENWRT_TARGET" -a "$GLUON_TARGETSDIR" ] || exit 1
 
 target="$1"
 packages=$2
@@ -74,9 +74,9 @@ packages() {
 # The sort will not only remove duplicate entries,
 # but also magically make =y entries override =m ones
 (
-	. targets/generic
+	. "${GLUON_TARGETSDIR}/generic"
 	packages $packages
 
-	. targets/"$target"
+	. "${GLUON_TARGETSDIR}/$target"
 	emit
 ) | sort -u
diff --git a/scripts/target_config_check.sh b/scripts/target_config_check.sh
index bfdc51bb0..3ae018441 100755
--- a/scripts/target_config_check.sh
+++ b/scripts/target_config_check.sh
@@ -2,7 +2,7 @@
 
 set -e
 
-[ "$OPENWRT_TARGET" ] || exit 1
+[ "$OPENWRT_TARGET" -a "$GLUON_TARGETSDIR" ] || exit 1
 
 target="$1"
 packages=$2
@@ -92,10 +92,10 @@ packages() {
 }
 
 
-. targets/generic
+. "${GLUON_TARGETSDIR}/generic"
 packages $packages
 
-. targets/"$target"
+. "${GLUON_TARGETSDIR}/$target"
 check_devices
 
 
diff --git a/scripts/update-patches.sh b/scripts/update-patches.sh
index cb38c37d9..933d20acd 100755
--- a/scripts/update-patches.sh
+++ b/scripts/update-patches.sh
@@ -3,6 +3,8 @@
 set -e
 shopt -s nullglob
 
+[ "$GLUON_PATCHESDIR" ] || exit 1
+
 . scripts/modules.sh
 
 
@@ -11,14 +13,14 @@ GLUONDIR="$(pwd)"
 for module in $GLUON_MODULES; do
 	echo "--- Updating patches for module '$module' ---"
 
-	rm -rf "$GLUONDIR"/patches/"$module"
+	rm -rf "${GLUON_PATCHESDIR}/$module"
 
 	cd "$GLUONDIR"/"$module"
 
 	n=0
 	for commit in $(git rev-list --reverse --no-merges base..patched); do
 		let n=n+1
-		mkdir -p "$GLUONDIR"/patches/"$module"
-		git -c core.abbrev=40 show --pretty=format:'From: %an <%ae>%nDate: %aD%nSubject: %B' --no-renames --binary "$commit" > "$GLUONDIR/patches/$module/$(printf '%04u' $n)-$(git show -s --pretty=format:%f "$commit").patch"
+		mkdir -p "${GLUON_PATCHESDIR}/$module"
+		git -c core.abbrev=40 show --pretty=format:'From: %an <%ae>%nDate: %aD%nSubject: %B' --no-renames --binary "$commit" > "${GLUON_PATCHESDIR}/$module/$(printf '%04u' $n)-$(git show -s --pretty=format:%f "$commit").patch"
 	done
 done
diff --git a/targets/brcm2708-bcm2708 b/targets/brcm2708-bcm2708
index 1fe6f3908..719a04cf5 100644
--- a/targets/brcm2708-bcm2708
+++ b/targets/brcm2708-bcm2708
@@ -1,4 +1,4 @@
-. targets/brcm2708.inc
+. "${GLUON_TARGETSDIR}/brcm2708.inc"
 
 device raspberry-pi rpi
 manifest_alias raspberry-pi-model-b-rev-2
diff --git a/targets/brcm2708-bcm2709 b/targets/brcm2708-bcm2709
index 78116f930..bc6321262 100644
--- a/targets/brcm2708-bcm2709
+++ b/targets/brcm2708-bcm2709
@@ -1,4 +1,4 @@
-. targets/brcm2708.inc
+. "${GLUON_TARGETSDIR}/brcm2708.inc"
 
 device raspberry-pi-2 rpi-2
 manifest_alias raspberry-pi-2-model-b-rev-1.1
diff --git a/targets/brcm2708-bcm2710 b/targets/brcm2708-bcm2710
index e523dce6e..c8f031af6 100644
--- a/targets/brcm2708-bcm2710
+++ b/targets/brcm2708-bcm2710
@@ -1,3 +1,3 @@
-. targets/brcm2708.inc
+. "${GLUON_TARGETSDIR}/brcm2708.inc"
 
 device raspberry-pi-3 rpi-3
diff --git a/targets/x86-64 b/targets/x86-64
index 1a739122c..48d95cecc 100644
--- a/targets/x86-64
+++ b/targets/x86-64
@@ -1,4 +1,4 @@
-. targets/x86.inc
+. "${GLUON_TARGETSDIR}/x86.inc"
 
 factory_image x86-64 combined-squashfs .img.gz
 factory_image x86-64 combined-squashfs .vdi
diff --git a/targets/x86-generic b/targets/x86-generic
index 2dfc852b2..2699a316c 100644
--- a/targets/x86-generic
+++ b/targets/x86-generic
@@ -1,4 +1,4 @@
-. targets/x86.inc
+. "${GLUON_TARGETSDIR}/x86.inc"
 
 factory_image x86-generic combined-squashfs .img.gz
 factory_image x86-generic combined-squashfs .vdi
-- 
GitLab