From 6c8d11e8e5b3457286fcda75b6516e93f1f12f17 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 10 Apr 2024 13:13:19 -0400 Subject: build: Run pkg-config with all packages at once --- Makefile | 107 ++++++++++++++++++++++++++---------------------------- config/cc.sh | 8 +--- config/deps.mk | 13 +++++++ config/pkg.mk | 11 ++++++ config/pkg.sh | 24 ++---------- config/pkgconf.sh | 51 ++++++++++++++++++-------- config/pkgs.mk | 14 +++++++ config/vars.mk | 21 +++++++++++ 8 files changed, 150 insertions(+), 99 deletions(-) create mode 100644 config/deps.mk create mode 100644 config/pkg.mk create mode 100644 config/pkgs.mk create mode 100644 config/vars.mk diff --git a/Makefile b/Makefile index ed59126..0df7ce5 100644 --- a/Makefile +++ b/Makefile @@ -166,12 +166,6 @@ _XCFLAGS := ${CFLAGS} _XLDFLAGS := ${LDFLAGS} _XLDLIBS := ${LDLIBS} -_XUSE_LIBACL := ${USE_LIBACL} -_XUSE_LIBCAP := ${USE_LIBCAP} -_XUSE_LIBSELINUX := ${USE_LIBSELINUX} -_XUSE_LIBURING := ${USE_LIBURING} -_XUSE_ONIGURUMA := ${USE_ONIGURUMA} - # GNU make supports `export VAR`, but BSD make requires `export VAR=value`. # Sadly, GNU make gives a recursion error on `export VAR=${VAR}`. @@ -197,40 +191,26 @@ export XCFLAGS=${_XCFLAGS} export XLDFLAGS=${_XLDFLAGS} export XLDLIBS=${_XLDLIBS} -export XUSE_LIBACL=${_XUSE_LIBACL} -export XUSE_LIBCAP=${_XUSE_LIBCAP} -export XUSE_LIBSELINUX=${_XUSE_LIBSELINUX} -export XUSE_LIBURING=${_XUSE_LIBURING} -export XUSE_ONIGURUMA=${_XUSE_ONIGURUMA} - # The configuration file generated by `make config` CONFIG := ${GEN}/config.mk -include ${CONFIG} ## Configuration phase (`make config`) -# External dependencies -PKGS := \ - ${GEN}/libacl.mk \ - ${GEN}/libcap.mk \ - ${GEN}/libselinux.mk \ - ${GEN}/liburing.mk \ - ${GEN}/oniguruma.mk - # Makefile fragments generated by `make config` MKS := \ ${GEN}/vars.mk \ ${GEN}/deps.mk \ ${GEN}/objs.mk \ - ${PKGS} + ${GEN}/pkgs.mk # The configuration goal itself config: ${MKS} - @printf 'include $${GEN}/%s\n' ${MKS:${GEN}/%=%} >${CONFIG} + @printf 'include $${GEN}/%s\n' ${.ALLSRC:${GEN}/%=%} >${CONFIG} .PHONY: config # Saves the configurable variables -${GEN}/vars.mk:: +${GEN}/vars.mk: @${XMKDIR} ${@D} @printf 'PREFIX := %s\n' "$$XPREFIX" >$@ @printf 'MANDIR := %s\n' "$$XMANDIR" >>$@ @@ -240,50 +220,67 @@ ${GEN}/vars.mk:: @printf 'INSTALL := %s\n' "$$XINSTALL" >>$@ @printf 'MKDIR := %s\n' "$$XMKDIR" >>$@ @printf 'RM := %s\n' "$$XRM" >>$@ - @printf 'CPPFLAGS := %s\n' "$$BFS_CPPFLAGS" >>$@ - @test "${TSAN}" != y || printf 'CPPFLAGS += %s\n' "$$TSAN_CPPFLAGS" >>$@ - @test "${LINT}" != y || printf 'CPPFLAGS += %s\n' "$$LINT_CPPFLAGS" >>$@ - @test "${RELEASE}" != y || printf 'CPPFLAGS += %s\n' "$$RELEASE_CPPFLAGS" >>$@ - @test -z "$$XCPPFLAGS" || printf 'CPPFLAGS += %s\n' "$$XCPPFLAGS" >>$@ - @test -z "$$EXTRA_CPPFLAGS" || printf 'CPPFLAGS += %s\n' "$$EXTRA_CPPFLAGS" >>$@ - @printf 'CFLAGS := %s\n' "$$BFS_CFLAGS" >>$@ - @test "${ASAN}" != y || printf 'CFLAGS += %s\n' "$$ASAN_CFLAGS" >>$@ - @test "${LSAN}" != y || printf 'CFLAGS += %s\n' "$$LSAN_CFLAGS" >>$@ - @test "${MSAN}" != y || printf 'CFLAGS += %s\n' "$$MSAN_CFLAGS" >>$@ - @test "${TSAN}" != y || printf 'CFLAGS += %s\n' "$$TSAN_CFLAGS" >>$@ - @test "${UBSAN}" != y || printf 'CFLAGS += %s\n' "$$UBSAN_CFLAGS" >>$@ - @case "${SAN}" in *y*) printf 'CFLAGS += %s\n' "$$SAN_CFLAGS" >>$@ ;; esac - @test "${GCOV}" != y || printf 'CFLAGS += %s\n' "$$GCOV_CFLAGS" >>$@ - @test "${LINT}" != y || printf 'CFLAGS += %s\n' "$$LINT_CFLAGS" >>$@ - @test "${RELEASE}" != y || printf 'CFLAGS += %s\n' "$$RELEASE_CFLAGS" >>$@ - @test -z "$$XCFLAGS" || printf 'CFLAGS += %s\n' "$$XCFLAGS" >>$@ - @test -z "$$EXTRA_CFLAGS" || printf 'CFLAGS += %s\n' "$$EXTRA_CFLAGS" >>$@ - @printf 'LDFLAGS := %s\n' "$$XLDFLAGS" >>$@ - @test -z "$$EXTRA_LDFLAGS" || printf 'LDFLAGS += %s\n' "$$EXTRA_LDFLAGS" >>$@ - @printf 'LDLIBS := %s\n' "$$XLDLIBS" >>$@ - @test -z "$$EXTRA_LDLIBS" || printf 'LDLIBS += %s\n' "$$EXTRA_LDLIBS" >>$@ - @test -z "$$BFS_LDLIBS" || printf 'LDLIBS += %s\n' "$$BFS_LDLIBS" >>$@ + @printf '# BFS_CPPFLAGS\nCPPFLAGS := %s\n' "$$BFS_CPPFLAGS" >>$@ + @test "${TSAN}" != y || printf '# TSAN\nCPPFLAGS += %s\n' "$$TSAN_CPPFLAGS" >>$@ + @test "${LINT}" != y || printf '# LINT\nCPPFLAGS += %s\n' "$$LINT_CPPFLAGS" >>$@ + @test "${RELEASE}" != y || printf '# RELEASE\nCPPFLAGS += %s\n' "$$RELEASE_CPPFLAGS" >>$@ + @printf '# CPPFLAGS\nCPPFLAGS += %s\n' "$$XCPPFLAGS" >>$@ + @printf '# EXTRA_CPPFLAGS\nCPPFLAGS += %s\n' "$$EXTRA_CPPFLAGS" >>$@ + @printf '# BFS_CFLAGS\nCFLAGS := %s\n' "$$BFS_CFLAGS" >>$@ + @test "${ASAN}" != y || printf '# ASAN\nCFLAGS += %s\n' "$$ASAN_CFLAGS" >>$@ + @test "${LSAN}" != y || printf '# LSAN\nCFLAGS += %s\n' "$$LSAN_CFLAGS" >>$@ + @test "${MSAN}" != y || printf '# MSAN\nCFLAGS += %s\n' "$$MSAN_CFLAGS" >>$@ + @test "${TSAN}" != y || printf '# TSAN\nCFLAGS += %s\n' "$$TSAN_CFLAGS" >>$@ + @test "${UBSAN}" != y || printf '# UBSAN\nCFLAGS += %s\n' "$$UBSAN_CFLAGS" >>$@ + @case "${SAN}" in *y*) printf '# *SAN\nCFLAGS += %s\n' "$$SAN_CFLAGS" >>$@ ;; esac + @test "${GCOV}" != y || printf '# GCOV\nCFLAGS += %s\n' "$$GCOV_CFLAGS" >>$@ + @test "${LINT}" != y || printf '# LINT\nCFLAGS += %s\n' "$$LINT_CFLAGS" >>$@ + @test "${RELEASE}" != y || printf '# RELEASE\nCFLAGS += %s\n' "$$RELEASE_CFLAGS" >>$@ + @printf '# CFLAGS\nCFLAGS += %s\n' "$$XCFLAGS" >>$@ + @printf '# EXTRA_CFLAGS\nCFLAGS += %s\n' "$$EXTRA_CFLAGS" >>$@ + @printf '# LDFLAGS\nLDFLAGS := %s\n' "$$XLDFLAGS" >>$@ + @printf '# EXTRA_LDFLAGS\nLDFLAGS += %s\n' "$$EXTRA_LDFLAGS" >>$@ + @printf '# LDLIBS\nLDLIBS := %s\n' "$$XLDLIBS" >>$@ + @printf '# EXTRA_LDLIBS\nLDLIBS += %s\n' "$$EXTRA_LDLIBS" >>$@ + @printf '# BFS_LDLIBS\nLDLIBS += %s\n' "$$BFS_LDLIBS" >>$@ + @printf 'PKGS :=\n' >>$@ @case "${OS}-${SAN}" in FreeBSD-*y*) printf 'POSTLINK = elfctl -e +noaslr $$@\n' >>$@ ;; esac @cat $@ +.PHONY: ${GEN}/vars.mk # Check for dependency generation support -${GEN}/deps.mk:: - @${MKDIR} ${@D} - @if config/cc.sh -MD -MP -MF /dev/null config/empty.c; then \ - echo 'DEPFLAGS = -MD -MP -MF $${@:.o=.d}'; \ - fi 2>$@.log | tee $@ +${GEN}/deps.mk: ${GEN}/vars.mk + @+${MAKE} -rs -f config/deps.mk TARGET=$@ + @cat $@ @printf -- '-include %s\n' ${OBJS:.o=.d} >>$@ +.PHONY: ${GEN}/deps.mk # Lists file.o: file.c dependencies -${GEN}/objs.mk:: +${GEN}/objs.mk: @${MKDIR} ${@D} @for obj in ${OBJS:${OBJ}/%.o=%}; do printf '$${OBJ}/%s.o: %s.c\n' "$$obj" "$$obj"; done >$@ +.PHONY: ${GEN}/objs.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 -${PKGS}:: - @${MKDIR} ${@D} - @config/pkg.sh ${@:${GEN}/%.mk=%} >$@ 2>$@.log +${GEN}/pkgs.mk: ${PKG_MKS} + @printf 'include $${GEN}/%s\n' ${.ALLSRC:${GEN}/%=%} >$@ + @+${MAKE} -rs -f config/pkgs.mk TARGET=$@ + @grep -v '^include' $@ || : +.PHONY: ${GEN}/pkgs.mk + +# Auto-detect dependencies +${PKG_MKS}: ${GEN}/vars.mk + @+${MAKE} -rs -f config/pkg.mk TARGET=$@ @cat $@ +.PHONY: ${PKG_MKS} # bfs used to have flag-like targets (`make release`, `make asan ubsan`, etc.). # Direct users to the new configuration system. diff --git a/config/cc.sh b/config/cc.sh index 7e5c0d5..2b340c0 100755 --- a/config/cc.sh +++ b/config/cc.sh @@ -7,10 +7,4 @@ set -eux -$XCC \ - $BFS_CPPFLAGS $XCPPFLAGS ${EXTRA_CPPFLAGS:-} \ - $BFS_CFLAGS $XCFLAGS ${EXTRA_CFLAGS:-} \ - $XLDFLAGS ${EXTRA_LDFLAGS:-} \ - "$@" \ - $XLDLIBS ${EXTRA_LDLIBS:-} $BFS_LDLIBS \ - -o /dev/null +$CC $CPPFLAGS $CFLAGS $LDFLAGS "$@" $LDLIBS -o /dev/null diff --git a/config/deps.mk b/config/deps.mk new file mode 100644 index 0000000..7d991ab --- /dev/null +++ b/config/deps.mk @@ -0,0 +1,13 @@ +# Copyright © Tavian Barnes +# SPDX-License-Identifier: 0BSD + +# Makefile that generates gen/deps.mk + +.OBJDIR: . + +include config/vars.mk + +default:: + if config/cc.sh -MD -MP -MF /dev/null config/empty.c; then \ + printf 'DEPFLAGS = -MD -MP -MF $${@:.o=.d}\n'; \ + fi >${TARGET} 2>${TARGET}.log diff --git a/config/pkg.mk b/config/pkg.mk new file mode 100644 index 0000000..9b32b42 --- /dev/null +++ b/config/pkg.mk @@ -0,0 +1,11 @@ +# Copyright © Tavian Barnes +# SPDX-License-Identifier: 0BSD + +# Makefile that generates gen/lib*.mk + +.OBJDIR: . + +include config/vars.mk + +default:: + config/pkg.sh ${TARGET:${GEN}/%.mk=%} >${TARGET} 2>${TARGET}.log diff --git a/config/pkg.sh b/config/pkg.sh index 6335b4b..2ca533e 100755 --- a/config/pkg.sh +++ b/config/pkg.sh @@ -8,37 +8,19 @@ set -eu NAME="${1^^}" -declare -n XUSE="XUSE_$NAME" +declare -n XUSE="USE_$NAME" -if [ "$XUSE" ]; then +if [ "${XUSE:-}" ]; then USE="$XUSE" -elif [[ "$NOLIBS" == *y* ]]; then - USE=n elif config/pkgconf.sh "$1"; then USE=y else USE=n fi -printf '%s := %s\n' "USE_$NAME" "$USE" - if [ "$USE" = y ]; then + printf 'PKGS += %s\n' "$1" printf 'CPPFLAGS += -DBFS_USE_%s=1\n' "$NAME" - - CFLAGS=$(config/pkgconf.sh --cflags "$1") - if [ "$CFLAGS" ]; then - printf 'CFLAGS += %s\n' "$CFLAGS" - fi - - LDFLAGS=$(config/pkgconf.sh --ldflags "$1") - if [ "$LDFLAGS" ]; then - printf 'LDFLAGS += %s\n' "$LDFLAGS" - fi - - LDLIBS=$(config/pkgconf.sh --ldlibs "$1") - if [ "$LDLIBS" ]; then - printf 'LDLIBS += %s\n' "$LDLIBS" - fi else printf 'CPPFLAGS += -DBFS_USE_%s=0\n' "$NAME" fi diff --git a/config/pkgconf.sh b/config/pkgconf.sh index 286c19c..a13b30f 100755 --- a/config/pkgconf.sh +++ b/config/pkgconf.sh @@ -8,29 +8,47 @@ set -eu MODE= -if [[ "$1" == --* ]]; then +if [[ "${1:-}" == --* ]]; then MODE="$1" shift fi +if (($# < 1)); then + exit +fi + +if [[ "$NOLIBS" == *y* ]]; then + exit 1 +fi + if command -v "${PKG_CONFIG:-}" &>/dev/null; then case "$MODE" in + "") + "$PKG_CONFIG" "$@" + ;; --cflags) - "$PKG_CONFIG" --cflags "$@" + OUT=$("$PKG_CONFIG" --cflags "$@") + if [ "$OUT" ]; then + printf 'CFLAGS += %s\n' "$OUT" + fi ;; --ldflags) - "$PKG_CONFIG" --libs-only-L --libs-only-other "$@" + OUT=$("$PKG_CONFIG" --libs-only-L --libs-only-other "$@") + if [ "$OUT" ]; then + printf 'LDFLAGS += %s\n' "$OUT" + fi ;; --ldlibs) - "$PKG_CONFIG" --libs-only-l "$@" - ;; - "") - "$PKG_CONFIG" "$@" + OUT=$("$PKG_CONFIG" --libs-only-l "$@") + if [ "$OUT" ]; then + printf 'LDLIBS := %s ${LDLIBS}\n' "$OUT" + fi ;; esac else - for lib; do - case "$lib" in + LDLIBS="" + for LIB; do + case "$LIB" in libacl) LDLIB=-lacl ;; @@ -47,21 +65,22 @@ else LDLIB=-lonig ;; *) - printf 'error: Unknown package %s\n' "$lib" >&2 + printf 'error: Unknown package %s\n' "$LIB" >&2 exit 1 + ;; esac case "$MODE" in - --ldlibs) - printf ' %s' "$LDLIB" - ;; "") - config/cc.sh "config/$lib.c" "$LDLIB" || exit $? + config/cc.sh "config/$LIB.c" "$LDLIB" || exit $? + ;; + --ldlibs) + LDLIBS="$LDLIBS $LDLIB" ;; esac done - if [ "$MODE" = "--ldlibs" ]; then - printf '\n' + if [ "$MODE" = "--ldlibs" ] && [ "$LDLIBS" ]; then + printf 'LDLIBS :=%s ${LDLIBS}\n' "$LDLIBS" fi fi diff --git a/config/pkgs.mk b/config/pkgs.mk new file mode 100644 index 0000000..54024b2 --- /dev/null +++ b/config/pkgs.mk @@ -0,0 +1,14 @@ +# Copyright © Tavian Barnes +# SPDX-License-Identifier: 0BSD + +# Makefile that generates gen/pkgs.mk + +.OBJDIR: . + +include config/vars.mk +include ${GEN}/pkgs.mk + +default:: + config/pkgconf.sh --cflags ${PKGS} >>${TARGET} 2>>${TARGET}.log + config/pkgconf.sh --ldflags ${PKGS} >>${TARGET} 2>>${TARGET}.log + config/pkgconf.sh --ldlibs ${PKGS} >>${TARGET} 2>>${TARGET}.log diff --git a/config/vars.mk b/config/vars.mk new file mode 100644 index 0000000..a8fae9d --- /dev/null +++ b/config/vars.mk @@ -0,0 +1,21 @@ +# Copyright © Tavian Barnes +# SPDX-License-Identifier: 0BSD + +# Makefile fragment loads and exports variables for config steps + +GEN := ${BUILDDIR}/gen +GEN := ${GEN:./%=%} + +include ${GEN}/vars.mk + +_CC := ${CC} +_CPPFLAGS := ${CPPFLAGS} +_CFLAGS := ${CFLAGS} +_LDFLAGS := ${LDFLAGS} +_LDLIBS := ${LDLIBS} + +export CC=${_CC} +export CPPFLAGS=${_CPPFLAGS} +export CFLAGS=${_CFLAGS} +export LDFLAGS=${_LDFLAGS} +export LDLIBS=${_LDLIBS} -- cgit v1.2.3