From 339c20aea74d5b4c6b8b178b478cc5530bc9af79 Mon Sep 17 00:00:00 2001
From: Tavian Barnes <tavianator@tavianator.com>
Date: Thu, 25 Apr 2024 12:01:40 -0400
Subject: config: Add BFS_USE_LIB* to config.h instead of CPPFLAGS

---
 config/config.mk  | 23 ++++-------------------
 config/deps.mk    |  2 +-
 config/header.mk  |  1 +
 config/pkg.mk     | 23 -----------------------
 config/pkg.sh     | 26 --------------------------
 config/pkgconf.sh | 12 ++++++++++++
 config/pkgs.mk    | 35 ++++++++++++++++++++++++++++++-----
 config/prelude.mk |  2 +-
 8 files changed, 49 insertions(+), 75 deletions(-)
 delete mode 100644 config/pkg.mk
 delete mode 100755 config/pkg.sh

(limited to 'config')

diff --git a/config/config.mk b/config/config.mk
index 5750b49..280c6ac 100644
--- a/config/config.mk
+++ b/config/config.mk
@@ -44,34 +44,19 @@ ${GEN}/vars.mk::
 # Sets the build flags.  This depends on vars.mk and uses a recursive make so
 # that the default flags can depend on variables like ${OS}.
 ${GEN}/flags.mk: ${GEN}/vars.mk
-	@+${MAKE} -sf config/flags.mk
+	@+${MAKE} -sf config/flags.mk $@
 .PHONY: ${GEN}/flags.mk
 
 # Check for dependency generation support
 ${GEN}/deps.mk: ${GEN}/flags.mk
-	@+${MAKE} -sf config/deps.mk
+	@+${MAKE} -sf config/deps.mk $@
 .PHONY: ${GEN}/deps.mk
 
-# External dependencies
-PKG_MKS := \
-    ${GEN}/libacl.mk \
-    ${GEN}/libcap.mk \
-    ${GEN}/libselinux.mk \
-    ${GEN}/liburing.mk \
-    ${GEN}/oniguruma.mk
-
 # Auto-detect dependencies and their build flags
-${GEN}/pkgs.mk: ${PKG_MKS}
-	@printf '# %s\n' "${TGT}" >$@
-	@printf 'include $${GEN}/%s\n' ${.ALLSRC:${GEN}/%=%} >>$@
-	@+${MAKE} -sf config/pkgs.mk
+${GEN}/pkgs.mk: ${GEN}/flags.mk
+	@+${MAKE} -sf config/pkgs.mk $@
 .PHONY: ${GEN}/pkgs.mk
 
-# Auto-detect dependencies
-${PKG_MKS}: ${GEN}/flags.mk
-	@+${MAKE} -sf config/pkg.mk TARGET=$@
-.PHONY: ${PKG_MKS}
-
 # Compile-time feature detection
 ${GEN}/config.h: ${CONFIG}
 	@+${MAKE} -sf config/header.mk $@
diff --git a/config/deps.mk b/config/deps.mk
index 2201f06..ac394a5 100644
--- a/config/deps.mk
+++ b/config/deps.mk
@@ -15,4 +15,4 @@ ${GEN}/deps.mk::
 	    printf 'DEPFLAGS = -MD -MP\n'; \
 	fi >>$@ 2>$@.log
 	${VCAT} $@
-	@printf -- '-include %s\n' ${OBJS:.o=.d} >>$@
+	printf -- '-include %s\n' ${OBJS:.o=.d} >>$@
diff --git a/config/header.mk b/config/header.mk
index 7fd2f78..ccc36d3 100644
--- a/config/header.mk
+++ b/config/header.mk
@@ -48,6 +48,7 @@ ${GEN}/config.h: ${HEADERS}
 	printf '// %s\n' "${TGT}" >$@
 	printf '#ifndef BFS_CONFIG_H\n' >>$@
 	printf '#define BFS_CONFIG_H\n' >>$@
+	printf '#define BFS_USE_%s true\n' $$(printf '%s\n' ${PKGS} | tr 'a-z-' 'A-Z_') >>$@
 	cat ${.ALLSRC} >>$@
 	printf '#endif // BFS_CONFIG_H\n' >>$@
 	cat ${.ALLSRC:%=%.log} >$@.log
diff --git a/config/pkg.mk b/config/pkg.mk
deleted file mode 100644
index fafe562..0000000
--- a/config/pkg.mk
+++ /dev/null
@@ -1,23 +0,0 @@
-# Copyright © Tavian Barnes <tavianator@tavianator.com>
-# SPDX-License-Identifier: 0BSD
-
-# Makefile that generates gen/lib*.mk
-
-include config/prelude.mk
-include ${GEN}/vars.mk
-include ${GEN}/flags.mk
-include config/exports.mk
-
-# Like ${TGT} but for ${TARGET}, not $@
-SHORT = ${TARGET:${BUILDDIR}/%=%}
-
-default::
-	@printf '# %s\n' "${SHORT}" >${TARGET}
-	config/pkg.sh ${TARGET:${GEN}/%.mk=%} >>${TARGET} 2>${TARGET}.log
-	@if [ "${IS_V}" ]; then \
-	    cat ${TARGET}; \
-	elif grep -q PKGS ${TARGET}; then \
-	    printf '[ GEN] %-${MSG_WIDTH}s  ✔\n' ${SHORT}; \
-	else \
-	    printf '[ GEN] %-${MSG_WIDTH}s  ✘\n' ${SHORT}; \
-	fi
diff --git a/config/pkg.sh b/config/pkg.sh
deleted file mode 100755
index 4ebea64..0000000
--- a/config/pkg.sh
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/bin/sh
-
-# Copyright © Tavian Barnes <tavianator@tavianator.com>
-# SPDX-License-Identifier: 0BSD
-
-# pkg-config wrapper that outputs a makefile fragment
-
-set -eu
-
-NAME=$(printf '%s' "$1" | tr 'a-z' 'A-Z')
-eval "XUSE=\"\${USE_$NAME:-}\""
-
-if [ "$XUSE" ]; then
-    USE="$XUSE"
-elif config/pkgconf.sh "$1"; then
-    USE=y
-else
-    USE=n
-fi
-
-if [ "$USE" = y ]; then
-    printf 'PKGS += %s\n' "$1"
-    printf 'CPPFLAGS += -DBFS_USE_%s=1\n' "$NAME"
-else
-    printf 'CPPFLAGS += -DBFS_USE_%s=0\n' "$NAME"
-fi
diff --git a/config/pkgconf.sh b/config/pkgconf.sh
index 2dbad76..2fb2f1e 100755
--- a/config/pkgconf.sh
+++ b/config/pkgconf.sh
@@ -26,6 +26,18 @@ esac
 if [ -z "$MODE" ]; then
     # Check whether the libraries exist at all
     for LIB; do
+        # Check ${USE_$LIB}
+        USE_LIB="USE_$(printf '%s' "$LIB" | tr 'a-z-' 'A-Z_')"
+        eval "USE=\"\${$USE_LIB:-}\""
+        case "$USE" in
+            y|1)
+                continue
+                ;;
+            n|0)
+                exit 1
+                ;;
+        esac
+
         CFLAGS=$("$0" --cflags "$LIB") || exit 1
         LDFLAGS=$("$0" --ldflags "$LIB") || exit 1
         LDLIBS=$("$0" --ldlibs "$LIB") || exit 1
diff --git a/config/pkgs.mk b/config/pkgs.mk
index 3a18289..2c100ab 100644
--- a/config/pkgs.mk
+++ b/config/pkgs.mk
@@ -6,12 +6,37 @@
 include config/prelude.mk
 include ${GEN}/vars.mk
 include ${GEN}/flags.mk
-include ${GEN}/pkgs.mk
 include config/exports.mk
 
-${GEN}/pkgs.mk::
+# External dependencies
+USE_PKGS := \
+    ${GEN}/libacl.use \
+    ${GEN}/libcap.use \
+    ${GEN}/libselinux.use \
+    ${GEN}/liburing.use \
+    ${GEN}/oniguruma.use
+
+${GEN}/pkgs.mk: ${USE_PKGS}
 	${MSG} "[ GEN] ${TGT}"
-	printf 'CFLAGS += %s\n' "$$(config/pkgconf.sh --cflags ${PKGS})" >>$@ 2>>$@.log
-	printf 'LDFLAGS += %s\n' "$$(config/pkgconf.sh --ldflags ${PKGS})" >>$@ 2>>$@.log
-	printf 'LDLIBS := %s $${LDLIBS}\n' "$$(config/pkgconf.sh --ldlibs ${PKGS})" >>$@ 2>>$@.log
+	printf '# %s\n' "${TGT}" >$@
+	gen() { \
+	    printf 'PKGS := %s\n' "$$*"; \
+	    printf 'CFLAGS += %s\n' "$$(config/pkgconf.sh --cflags "$$@")"; \
+	    printf 'LDFLAGS += %s\n' "$$(config/pkgconf.sh --ldflags "$$@")"; \
+	    printf 'LDLIBS := %s $${LDLIBS}\n' "$$(config/pkgconf.sh --ldlibs "$$@")"; \
+	}; \
+	gen $$(cat ${.ALLSRC}) >>$@
 	${VCAT} $@
+.PHONY: ${GEN}/pkgs.mk
+
+# Convert ${GEN}/foo.use to foo
+PKG = ${@:${GEN}/%.use=%}
+
+${USE_PKGS}::
+	if config/pkgconf.sh ${PKG} 2>$@.log; then \
+	    printf '%s\n' ${PKG} >$@; \
+	    test "${IS_V}" || printf '[ CC ] %-${MSG_WIDTH}s  ✔\n' config/${PKG}.c; \
+	else \
+	    : >$@; \
+	    test "${IS_V}" || printf '[ CC ] %-${MSG_WIDTH}s  ✘\n' config/${PKG}.c; \
+	fi
diff --git a/config/prelude.mk b/config/prelude.mk
index b9bc61b..e1e7a4d 100644
--- a/config/prelude.mk
+++ b/config/prelude.mk
@@ -109,7 +109,7 @@ MSG = @msg() { \
       }; \
       msg
 
-# Maximum width of a short message, to align the [X]
+# Maximum width of a short message, to align the ✔/✘
 MSG_WIDTH := 33
 
 # cat a file if V=1
-- 
cgit v1.2.3