From 0035cc4ff492cab91adeb49d0c577fe5982064bd Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 18 Apr 2024 15:27:27 -0400 Subject: config: Check for program_invocation_short_name This lets us pick it up on musl too, since there's no __MUSL__ macro. Link: https://wiki.musl-libc.org/faq#Q:-Why-is-there-no-%3Ccode%3E__MUSL__%3C/code%3E-macro? --- config/cc-define.sh | 19 +++++++++++++++++++ config/config.mk | 11 ++++++++++- config/flags.mk | 1 + config/getprogname-gnu.c | 9 +++++++++ config/getprogname.c | 9 +++++++++ config/header.mk | 38 ++++++++++++++++++++++++++++++++++++++ config/pkg.mk | 4 ++-- config/prelude.mk | 3 +++ 8 files changed, 91 insertions(+), 3 deletions(-) create mode 100755 config/cc-define.sh create mode 100644 config/getprogname-gnu.c create mode 100644 config/getprogname.c create mode 100644 config/header.mk (limited to 'config') diff --git a/config/cc-define.sh b/config/cc-define.sh new file mode 100755 index 0000000..edb5c87 --- /dev/null +++ b/config/cc-define.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +# Copyright © Tavian Barnes +# SPDX-License-Identifier: 0BSD + +# Output a C preprocessor definition based on whether a C source file could be +# compiled successfully + +set -eu + +SLUG="${1#config/}" +SLUG="${SLUG%.c}" +MACRO="BFS_HAS_$(printf '%s' "$SLUG" | tr 'a-z-' 'A-Z_')" + +if config/cc.sh "$1"; then + printf '#define %s true\n' "$MACRO" +else + printf '#define %s false\n' "$MACRO" +fi diff --git a/config/config.mk b/config/config.mk index 5a6f8d2..5750b49 100644 --- a/config/config.mk +++ b/config/config.mk @@ -6,6 +6,10 @@ include config/prelude.mk include config/exports.mk +# All configuration steps +config: ${CONFIG} ${GEN}/config.h +.PHONY: config + # Makefile fragments generated by `make config` MKS := \ ${GEN}/vars.mk \ @@ -17,7 +21,7 @@ MKS := \ ${CONFIG}: ${MKS} ${MSG} "[ GEN] ${TGT}" @printf '# %s\n' "${TGT}" >$@ - @printf 'include $${GEN}/%s\n' ${.ALLSRC:${GEN}/%=%} >>$@ + @printf 'include $${GEN}/%s\n' ${MKS:${GEN}/%=%} >>$@ ${VCAT} ${CONFIG} .PHONY: ${CONFIG} @@ -67,3 +71,8 @@ ${GEN}/pkgs.mk: ${PKG_MKS} ${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 $@ +.PHONY: ${GEN}/config.h diff --git a/config/flags.mk b/config/flags.mk index d02cc8b..9959d10 100644 --- a/config/flags.mk +++ b/config/flags.mk @@ -29,6 +29,7 @@ export XLDLIBS=${LDLIBS} # Immutable flags export BFS_CPPFLAGS= \ -Isrc \ + -I${GEN} \ -D__EXTENSIONS__ \ -D_ATFILE_SOURCE \ -D_BSD_SOURCE \ diff --git a/config/getprogname-gnu.c b/config/getprogname-gnu.c new file mode 100644 index 0000000..6b97c5e --- /dev/null +++ b/config/getprogname-gnu.c @@ -0,0 +1,9 @@ +// Copyright © Tavian Barnes +// SPDX-License-Identifier: 0BSD + +#include + +int main(void) { + const char *str = program_invocation_short_name; + return str[0]; +} diff --git a/config/getprogname.c b/config/getprogname.c new file mode 100644 index 0000000..83dc8e8 --- /dev/null +++ b/config/getprogname.c @@ -0,0 +1,9 @@ +// Copyright © Tavian Barnes +// SPDX-License-Identifier: 0BSD + +#include + +int main(void) { + const char *str = getprogname(); + return str[0]; +} diff --git a/config/header.mk b/config/header.mk new file mode 100644 index 0000000..3a1271e --- /dev/null +++ b/config/header.mk @@ -0,0 +1,38 @@ +# Copyright © Tavian Barnes +# SPDX-License-Identifier: 0BSD + +# Makefile that generates gen/config.h + +include config/prelude.mk +include ${GEN}/config.mk +include config/exports.mk + +# All header fragments we generate +HEADERS := \ + ${GEN}/getprogname.h \ + ${GEN}/getprogname-gnu.h + +${GEN}/config.h: ${HEADERS} + ${MSG} "[ GEN] ${TGT}" + printf '// %s\n' "${TGT}" >$@ + printf '#ifndef BFS_CONFIG_H\n' >>$@ + printf '#define BFS_CONFIG_H\n' >>$@ + cat ${.ALLSRC} >>$@ + printf '#endif // BFS_CONFIG_H\n' >>$@ + cat ${.ALLSRC:%=%.log} >$@.log + ${RM} ${.ALLSRC} ${.ALLSRC:%=%.log} + ${VCAT} $@ +.PHONY: ${GEN}/config.h + +# The C source file to attempt to compile +CSRC = ${@:${GEN}/%.h=config/%.c} + +${HEADERS}:: + config/cc-define.sh ${CSRC} >$@ 2>$@.log + if ! [ "${IS_V}" ]; then \ + if grep -q 'true$$' $@; then \ + printf '[ CC ] %-${MSG_WIDTH}s ✔\n' ${CSRC}; \ + else \ + printf '[ CC ] %-${MSG_WIDTH}s ✘\n' ${CSRC}; \ + fi; \ + fi diff --git a/config/pkg.mk b/config/pkg.mk index 6f868be..fafe562 100644 --- a/config/pkg.mk +++ b/config/pkg.mk @@ -17,7 +17,7 @@ default:: @if [ "${IS_V}" ]; then \ cat ${TARGET}; \ elif grep -q PKGS ${TARGET}; then \ - printf '[ GEN] %-18s ✔\n' ${SHORT}; \ + printf '[ GEN] %-${MSG_WIDTH}s ✔\n' ${SHORT}; \ else \ - printf '[ GEN] %-18s ✘\n' ${SHORT}; \ + printf '[ GEN] %-${MSG_WIDTH}s ✘\n' ${SHORT}; \ fi diff --git a/config/prelude.mk b/config/prelude.mk index dbc6875..0ac5fde 100644 --- a/config/prelude.mk +++ b/config/prelude.mk @@ -109,6 +109,9 @@ MSG = @msg() { \ }; \ msg +# Maximum width of a short message, to align the [X] +MSG_WIDTH := 24 + # cat a file if V=1 VCAT,y := @cat VCAT, := @: -- cgit v1.2.3