From cb1de93e1216fe33c4fed2e4c15dfb6251d6013f Mon Sep 17 00:00:00 2001
From: Ismael Luceno <ismael@iodev.co.uk>
Date: Tue, 17 Mar 2026 08:59:34 +0100
Subject: [PATCH 6/8] Makefile: Implement configuration saving

This way, changing a configuration variable is persistent; no need to
specify them every time.

The .config file is included twice so that only user-specified variables
get updated.

This is compatible with GNU make 4.4+ and NetBSD make and derivatives.

Also added a distclean target to clean the .config file.

Signed-off-by: Ismael Luceno <ismael@iodev.co.uk>
---
 .gitignore |  1 +
 Makefile   | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100644 .gitignore

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 000000000000..7fada2ab4418
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/.config
diff --git a/Makefile b/Makefile
index f67d5610be27..45a65b1095b0 100644
--- a/Makefile
+++ b/Makefile
@@ -135,3 +135,31 @@ MIMEH_test: ${MIMEH_test-objs}
 	${CC} ${LDFLAGS-y} ${CFLAGS-y} ${$@-objs} -o $@
 clean::
 	rm -f MIMEH_test
+
+# Macro to extract a quoted & unexpanded variable
+quote = '$(subst ','\'',$(value $1))'
+
+# Save and restore configuration variables
+-include .config
+_ != { \
+	echo 'AR      = '${AR:Q}$(call quote,AR); \
+	echo 'ARFLAGS = '${ARFLAGS:Q}$(call quote,ARFLAGS); \
+	echo 'CC      = '${CC:Q}$(call quote,CC); \
+	echo 'CFLAGS  = '${CFLAGS:Q}$(call quote,CFLAGS); \
+	echo 'LDFLAGS = '${LDFLAGS:Q}$(call quote,LDFLAGS); \
+	echo 'LIBS    = '${LIBS:Q}$(call quote,LIBS); \
+	echo; \
+	echo 'prefix = '${prefix:Q}$(call quote,prefix); \
+	echo '  exec_prefix = '${exec_prefix:Q}$(call quote,exec_prefix); \
+	echo '    bindir = '${bindir:Q}$(call quote,bindir); \
+	echo '    libdir = '${libdir:Q}$(call quote,libdir); \
+	echo '  datarootdir = '${datarootdir:Q}$(call quote,datarootdir); \
+	echo '    mandir = '${mandir:Q}$(call quote,mandir); \
+	} > .config
+include .config
+
+# FIXME: variables (unintuitivelly) are expanded when saving in bmake
+
+.PHONY: distclean
+distclean:: clean
+	rm -f .config
