diff options
Diffstat (limited to 'build')
-rwxr-xr-x | build/cc.sh | 11 | ||||
-rw-r--r-- | build/config.mk | 8 | ||||
-rw-r--r-- | build/deps.mk | 4 | ||||
-rwxr-xr-x | build/embed.sh | 12 | ||||
-rw-r--r-- | build/flags.mk | 107 | ||||
-rw-r--r-- | build/has/builtin-riscv-pause.c | 7 | ||||
-rw-r--r-- | build/header.mk | 30 | ||||
-rwxr-xr-x | build/msg-if.sh | 18 | ||||
-rwxr-xr-x | build/msg.sh | 2 | ||||
-rwxr-xr-x | build/pkgconf.sh | 18 | ||||
-rw-r--r-- | build/pkgs.mk | 10 | ||||
-rw-r--r-- | build/prelude.mk | 3 | ||||
-rw-r--r-- | build/with/libacl.c (renamed from build/use/libacl.c) | 0 | ||||
-rw-r--r-- | build/with/libcap.c (renamed from build/use/libcap.c) | 0 | ||||
-rw-r--r-- | build/with/libselinux.c (renamed from build/use/libselinux.c) | 0 | ||||
-rw-r--r-- | build/with/liburing.c (renamed from build/use/liburing.c) | 0 | ||||
-rw-r--r-- | build/with/oniguruma.c (renamed from build/use/oniguruma.c) | 0 |
17 files changed, 124 insertions, 106 deletions
diff --git a/build/cc.sh b/build/cc.sh index 45d51ca..23a4c01 100755 --- a/build/cc.sh +++ b/build/cc.sh @@ -5,12 +5,5 @@ # Run the compiler and check if it succeeded -set -eu - -TMP=$(mktemp) -trap 'rm -f "$TMP"' EXIT - -( - set -x - $XCC $XCPPFLAGS $XCFLAGS $XLDFLAGS "$@" $XLDLIBS -o "$TMP" -) +set -eux +$XCC $XCPPFLAGS $XCFLAGS $XLDFLAGS "$@" $XLDLIBS diff --git a/build/config.mk b/build/config.mk index 24873ec..80206c7 100644 --- a/build/config.mk +++ b/build/config.mk @@ -7,7 +7,7 @@ include build/prelude.mk include build/exports.mk # All configuration steps -config: gen/config.mk gen/config.h +config: gen/config.mk .PHONY: config # Makefile fragments generated by `./configure` @@ -18,10 +18,10 @@ MKS := \ gen/pkgs.mk # The main configuration file, which includes the others -gen/config.mk: ${MKS} +gen/config.mk: ${MKS} gen/config.h ${MSG} "[ GEN] $@" @printf '# %s\n' "$@" >$@ - @printf 'include %s\n' ${.ALLSRC} >>$@ + @printf 'include %s\n' ${MKS} >>$@ ${VCAT} gen/config.mk .PHONY: gen/config.mk @@ -57,6 +57,6 @@ gen/pkgs.mk: gen/flags.mk .PHONY: gen/pkgs.mk # Compile-time feature detection -gen/config.h: gen/config.mk +gen/config.h: gen/pkgs.mk @+XMAKEFLAGS="$$MAKEFLAGS" ${MAKE} -sf build/header.mk $@ .PHONY: gen/config.h diff --git a/build/deps.mk b/build/deps.mk index 3db62b6..a6ea673 100644 --- a/build/deps.mk +++ b/build/deps.mk @@ -11,8 +11,8 @@ include build/exports.mk gen/deps.mk:: ${MSG} "[ GEN] $@" @printf '# %s\n' "$@" >$@ - @if build/cc.sh -MD -MP -MF /dev/null build/empty.c; then \ - printf 'CPPFLAGS += -MD -MP\n'; \ + @if build/cc.sh -MD -MP build/empty.c -o gen/.deps.out; then \ + printf '_CPPFLAGS += -MD -MP\n'; \ fi >>$@ 2>$@.log ${VCAT} $@ @printf -- '-include %s\n' ${OBJS:.o=.d} >>$@ diff --git a/build/embed.sh b/build/embed.sh new file mode 100755 index 0000000..8d7d0f1 --- /dev/null +++ b/build/embed.sh @@ -0,0 +1,12 @@ +#!/bin/sh + +# Copyright © Tavian Barnes <tavianator@tavianator.com> +# SPDX-License-Identifier: 0BSD + +# Convert data into a C array like #embed + +set -eu + +{ cat; printf '\0'; } \ + | od -An -tx1 \ + | sed 's/\([^ ][^ ]*\)/0x\1,/g' diff --git a/build/flags.mk b/build/flags.mk index fa793b8..c50b077 100644 --- a/build/flags.mk +++ b/build/flags.mk @@ -6,28 +6,8 @@ include build/prelude.mk include gen/vars.mk -# Configurable flags -CPPFLAGS ?= -CFLAGS ?= \ - -g \ - -Wall \ - -Wformat=2 \ - -Werror=implicit \ - -Wimplicit-fallthrough \ - -Wmissing-declarations \ - -Wshadow \ - -Wsign-compare \ - -Wstrict-prototypes -LDFLAGS ?= -LDLIBS ?= - -export XCPPFLAGS=${CPPFLAGS} -export XCFLAGS=${CFLAGS} -export XLDFLAGS=${LDFLAGS} -export XLDLIBS=${LDLIBS} - -# Immutable flags -export BFS_CPPFLAGS= \ +# Internal flags +_CPPFLAGS := \ -Isrc \ -Igen \ -D__EXTENSIONS__ \ @@ -39,14 +19,17 @@ export BFS_CPPFLAGS= \ -D_POSIX_PTHREAD_SEMANTICS \ -D_FILE_OFFSET_BITS=64 \ -D_TIME_BITS=64 -export BFS_CFLAGS= -std=c17 -pthread + +_CFLAGS := -std=c17 -pthread +_LDFLAGS := +_LDLIBS := # Platform-specific system libraries LDLIBS,DragonFly := -lposix1e LDLIBS,Linux := -lrt LDLIBS,NetBSD := -lutil LDLIBS,SunOS := -lsec -lsocket -lnsl -export BFS_LDLIBS=${LDLIBS,${OS}} +_LDLIBS += ${LDLIBS,${OS}} # Build profiles _ASAN := ${TRUTHY,${ASAN}} @@ -60,7 +43,7 @@ _RELEASE := ${TRUTHY,${RELEASE}} # https://github.com/google/sanitizers/issues/342 TSAN_CPPFLAGS,y := -DBFS_USE_TARGET_CLONES=0 -export TSAN_CPPFLAGS=${TSAN_CPPFLAGS,${_TSAN}} +_CPPFLAGS += ${TSAN_CPPFLAGS,${_TSAN}} ASAN_CFLAGS,y := -fsanitize=address LSAN_CFLAGS,y := -fsanitize=leak @@ -68,69 +51,65 @@ MSAN_CFLAGS,y := -fsanitize=memory -fsanitize-memory-track-origins TSAN_CFLAGS,y := -fsanitize=thread UBSAN_CFLAGS,y := -fsanitize=undefined -export ASAN_CFLAGS=${ASAN_CFLAGS,${_ASAN}} -export LSAN_CFLAGS=${LSAN_CFLAGS,${_LSAN}} -export MSAN_CFLAGS=${MSAN_CFLAGS,${_MSAN}} -export TSAN_CFLAGS=${TSAN_CFLAGS,${_TSAN}} -export UBSAN_CFLAGS=${UBSAN_CFLAGS,${_UBSAN}} +_CFLAGS += ${ASAN_CFLAGS,${_ASAN}} +_CFLAGS += ${LSAN_CFLAGS,${_LSAN}} +_CFLAGS += ${MSAN_CFLAGS,${_MSAN}} +_CFLAGS += ${TSAN_CFLAGS,${_TSAN}} +_CFLAGS += ${UBSAN_CFLAGS,${_UBSAN}} SAN_CFLAGS,y := -fno-sanitize-recover=all INSANE := ${NOT,${_ASAN}${_LSAN}${_MSAN}${_TSAN}${_UBSAN}} SAN := ${NOT,${INSANE}} -export SAN_CFLAGS=${SAN_CFLAGS,${SAN}} +_CFLAGS += ${SAN_CFLAGS,${SAN}} # MSAN and TSAN both need all code to be instrumented YESLIBS := ${NOT,${_MSAN}${_TSAN}} NOLIBS ?= ${NOT,${YESLIBS}} -export XNOLIBS=${NOLIBS} # gcov only intercepts fork()/exec() with -std=gnu* GCOV_CFLAGS,y := -std=gnu17 --coverage -export GCOV_CFLAGS=${GCOV_CFLAGS,${_GCOV}} +_CFLAGS += ${GCOV_CFLAGS,${_GCOV}} LINT_CPPFLAGS,y := -D_FORTIFY_SOURCE=3 -DBFS_LINT LINT_CFLAGS,y := -Werror -O2 -export LINT_CPPFLAGS=${LINT_CPPFLAGS,${_LINT}} -export LINT_CFLAGS=${LINT_CFLAGS,${_LINT}} +_CPPFLAGS += ${LINT_CPPFLAGS,${_LINT}} +_CFLAGS += ${LINT_CFLAGS,${_LINT}} RELEASE_CPPFLAGS,y := -DNDEBUG RELEASE_CFLAGS,y := -O3 -flto=auto -export RELEASE_CPPFLAGS=${RELEASE_CPPFLAGS,${_RELEASE}} -export RELEASE_CFLAGS=${RELEASE_CFLAGS,${_RELEASE}} +_CPPFLAGS += ${RELEASE_CPPFLAGS,${_RELEASE}} +_CFLAGS += ${RELEASE_CFLAGS,${_RELEASE}} + +# Configurable flags +CFLAGS ?= \ + -g \ + -Wall \ + -Wformat=2 \ + -Werror=implicit \ + -Wimplicit-fallthrough \ + -Wmissing-declarations \ + -Wshadow \ + -Wsign-compare \ + -Wstrict-prototypes -# Set a variable -SETVAR = @printf '%s := %s\n' >>$@ +# Add the configurable flags last so they can override ours +_CPPFLAGS += ${CPPFLAGS} ${EXTRA_CPPFLAGS} +_CFLAGS += ${CFLAGS} ${EXTRA_CFLAGS} +_LDFLAGS += ${LDFLAGS} ${EXTRA_LDFLAGS} +# (except LDLIBS, as earlier libs override later ones) +_LDLIBS := ${LDLIBS} ${EXTRA_LDLIBS} ${_LDLIBS} -# Append to a variable, if non-empty -APPEND = @append() { test -z "$$2" || printf '%s += %s\n' "$$1" "$$2" >>$@; }; append +include build/exports.mk gen/flags.mk:: ${MSG} "[ GEN] $@" @printf '# %s\n' "$@" >$@ - ${SETVAR} _CPPFLAGS "$$BFS_CPPFLAGS" - ${APPEND} _CPPFLAGS "$$TSAN_CPPFLAGS" - ${APPEND} _CPPFLAGS "$$LINT_CPPFLAGS" - ${APPEND} _CPPFLAGS "$$RELEASE_CPPFLAGS" - ${APPEND} _CPPFLAGS "$$XCPPFLAGS" - ${APPEND} _CPPFLAGS "$$EXTRA_CPPFLAGS" - ${SETVAR} _CFLAGS "$$BFS_CFLAGS" - ${APPEND} _CFLAGS "$$ASAN_CFLAGS" - ${APPEND} _CFLAGS "$$LSAN_CFLAGS" - ${APPEND} _CFLAGS "$$MSAN_CFLAGS" - ${APPEND} _CFLAGS "$$TSAN_CFLAGS" - ${APPEND} _CFLAGS "$$UBSAN_CFLAGS" - ${APPEND} _CFLAGS "$$SAN_CFLAGS" - ${APPEND} _CFLAGS "$$GCOV_CFLAGS" - ${APPEND} _CFLAGS "$$LINT_CFLAGS" - ${APPEND} _CFLAGS "$$RELEASE_CFLAGS" - ${APPEND} _CFLAGS "$$XCFLAGS" - ${APPEND} _CFLAGS "$$EXTRA_CFLAGS" - ${SETVAR} _LDFLAGS "$$XLDFLAGS" - ${SETVAR} _LDLIBS "$$XLDLIBS" - ${APPEND} _LDLIBS "$$EXTRA_LDLIBS" - ${APPEND} _LDLIBS "$$BFS_LDLIBS" - ${SETVAR} NOLIBS "$$XNOLIBS" + @printf '_CPPFLAGS := %s\n' "$$XCPPFLAGS" >>$@ + @printf '_CFLAGS := %s\n' "$$XCFLAGS" >>$@ + @printf '_LDFLAGS := %s\n' "$$XLDFLAGS" >>$@ + @printf '_LDLIBS := %s\n' "$$XLDLIBS" >>$@ + @printf 'NOLIBS := %s\n' "$$XNOLIBS" >>$@ @test "${OS}-${SAN}" != FreeBSD-y || printf 'POSTLINK = elfctl -e +noaslr $$@\n' >>$@ ${VCAT} $@ diff --git a/build/has/builtin-riscv-pause.c b/build/has/builtin-riscv-pause.c new file mode 100644 index 0000000..24b0675 --- /dev/null +++ b/build/has/builtin-riscv-pause.c @@ -0,0 +1,7 @@ +// Copyright © Tavian Barnes <tavianator@tavianator.com> +// SPDX-License-Identifier: 0BSD + +int main(void) { + __builtin_riscv_pause(); + return 0; +} diff --git a/build/header.mk b/build/header.mk index fb8246d..afd04d0 100644 --- a/build/header.mk +++ b/build/header.mk @@ -4,7 +4,10 @@ # Makefile that generates gen/config.h include build/prelude.mk -include gen/config.mk +include gen/vars.mk +include gen/flags.mk +include gen/deps.mk +include gen/pkgs.mk include build/exports.mk # All header fragments we generate @@ -16,6 +19,7 @@ HEADERS := \ gen/has/acl-is-trivial-np.h \ gen/has/acl-trivial.h \ gen/has/aligned-alloc.h \ + gen/has/builtin-riscv-pause.h \ gen/has/confstr.h \ gen/has/extattr-get-file.h \ gen/has/extattr-get-link.h \ @@ -51,7 +55,7 @@ HEADERS := \ gen/has/uselocale.h # Previously generated by pkgs.mk -PKG_HEADERS := ${ALL_PKGS:%=gen/use/%.h} +PKG_HEADERS := ${ALL_PKGS:%=gen/with/%.h} gen/config.h: ${PKG_HEADERS} ${HEADERS} ${MSG} "[ GEN] $@" @@ -60,14 +64,30 @@ gen/config.h: ${PKG_HEADERS} ${HEADERS} @printf '#define BFS_CONFIG_H\n' >>$@ @cat ${.ALLSRC} >>$@ @printf '#endif // BFS_CONFIG_H\n' >>$@ - @cat ${.ALLSRC:%=%.log} >gen/config.log + @cat gen/cc.log ${.ALLSRC:%=%.log} >gen/config.log ${VCAT} $@ + @printf '%s' "$$CONFFLAGS" | build/embed.sh >gen/confflags.i + @printf '%s' "$$XCC" | build/embed.sh >gen/cc.i + @printf '%s' "$$XCPPFLAGS" | build/embed.sh >gen/cppflags.i + @printf '%s' "$$XCFLAGS" | build/embed.sh >gen/cflags.i + @printf '%s' "$$XLDFLAGS" | build/embed.sh >gen/ldflags.i + @printf '%s' "$$XLDLIBS" | build/embed.sh >gen/ldlibs.i .PHONY: gen/config.h # The short name of the config test SLUG = ${@:gen/%.h=%} +# The hidden output file name +OUT = ${SLUG:has/%=gen/has/.%.out} -${HEADERS}:: +${HEADERS}: cc @${MKDIR} ${@D} - @build/define-if.sh ${SLUG} build/cc.sh build/${SLUG}.c >$@ 2>$@.log; \ + @build/define-if.sh ${SLUG} build/cc.sh build/${SLUG}.c -o ${OUT} >$@ 2>$@.log; \ build/msg-if.sh "[ CC ] ${SLUG}.c" test $$? -eq 0 +.PHONY: ${HEADERS} + +# Check that the C compiler works at all +cc:: + @build/cc.sh build/empty.c -o gen/.cc.out 2>gen/cc.log; \ + ret=$$?; \ + build/msg-if.sh "[ CC ] build/empty.c" test $$ret -eq 0; \ + exit $$ret diff --git a/build/msg-if.sh b/build/msg-if.sh index 8112aea..afb478c 100755 --- a/build/msg-if.sh +++ b/build/msg-if.sh @@ -6,16 +6,26 @@ # Print a success/failure indicator from a makefile: # # $ ./configure -# [ CC ] use/liburing.c ✘ -# [ CC ] use/oniguruma.c ✔ +# [ CC ] with/liburing.c ✘ +# [ CC ] with/oniguruma.c ✔ set -eu MSG="$1" shift +if [ -z "${NO_COLOR:-}" ] && [ -t 1 ]; then + Y='\033[1;32m✔\033[0m' + N='\033[1;31m✘\033[0m' +else + Y='✔' + N='✘' +fi + if "$@"; then - build/msg.sh "$(printf '%-37s ✔' "$MSG")" + YN="$Y" else - build/msg.sh "$(printf '%-37s ✘' "$MSG")" + YN="$N" fi + +build/msg.sh "$(printf "%-37s $YN" "$MSG")" diff --git a/build/msg.sh b/build/msg.sh index a7da31b..2249125 100755 --- a/build/msg.sh +++ b/build/msg.sh @@ -59,4 +59,4 @@ if is_loud; then printf '%s\n' "$*" fi -"$@" +exec "$@" diff --git a/build/pkgconf.sh b/build/pkgconf.sh index 96e4bf1..a8a3341 100755 --- a/build/pkgconf.sh +++ b/build/pkgconf.sh @@ -26,22 +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 - ;; + # Check ${WITH_$LIB} + WITH_LIB="WITH_$(printf '%s' "$LIB" | tr 'a-z-' 'A-Z_')" + eval "WITH=\"\${$WITH_LIB:-}\"" + case "$WITH" 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 - build/cc.sh $CFLAGS $LDFLAGS build/use/$LIB.c $LDLIBS || exit 1 + build/cc.sh $CFLAGS $LDFLAGS "build/with/$LIB.c" $LDLIBS -o "gen/with/.$LIB.out" || exit 1 done fi diff --git a/build/pkgs.mk b/build/pkgs.mk index 5a26548..5de9ac2 100644 --- a/build/pkgs.mk +++ b/build/pkgs.mk @@ -8,7 +8,7 @@ include gen/vars.mk include gen/flags.mk include build/exports.mk -HEADERS := ${ALL_PKGS:%=gen/use/%.h} +HEADERS := ${ALL_PKGS:%=gen/with/%.h} gen/pkgs.mk: ${HEADERS} ${MSG} "[ GEN] $@" @@ -24,10 +24,10 @@ gen/pkgs.mk: ${HEADERS} .PHONY: gen/pkgs.mk -# Convert gen/use/foo.h to foo -PKG = ${@:gen/use/%.h=%} +# Convert gen/with/foo.h to foo +PKG = ${@:gen/with/%.h=%} ${HEADERS}:: @${MKDIR} ${@D} - @build/define-if.sh use/${PKG} build/pkgconf.sh ${PKG} >$@ 2>$@.log; \ - build/msg-if.sh "[ CC ] use/${PKG}.c" test $$? -eq 0; + @build/define-if.sh with/${PKG} build/pkgconf.sh ${PKG} >$@ 2>$@.log; \ + build/msg-if.sh "[ CC ] with/${PKG}.c" test $$? -eq 0; diff --git a/build/prelude.mk b/build/prelude.mk index d0968ea..76fbce8 100644 --- a/build/prelude.mk +++ b/build/prelude.mk @@ -97,10 +97,10 @@ LIBBFS := \ obj/src/thread.o \ obj/src/trie.o \ obj/src/typo.o \ + obj/src/version.o \ obj/src/xregex.o \ obj/src/xspawn.o \ obj/src/xtime.o \ - obj/gen/version.o # Unit test objects UNIT_OBJS := \ @@ -108,6 +108,7 @@ UNIT_OBJS := \ obj/tests/bfstd.o \ obj/tests/bit.o \ obj/tests/ioq.o \ + obj/tests/list.o \ obj/tests/main.o \ obj/tests/sighook.o \ obj/tests/trie.o \ diff --git a/build/use/libacl.c b/build/with/libacl.c index de1fe50..de1fe50 100644 --- a/build/use/libacl.c +++ b/build/with/libacl.c diff --git a/build/use/libcap.c b/build/with/libcap.c index 58e832c..58e832c 100644 --- a/build/use/libcap.c +++ b/build/with/libcap.c diff --git a/build/use/libselinux.c b/build/with/libselinux.c index bca409d..bca409d 100644 --- a/build/use/libselinux.c +++ b/build/with/libselinux.c diff --git a/build/use/liburing.c b/build/with/liburing.c index bea499a..bea499a 100644 --- a/build/use/liburing.c +++ b/build/with/liburing.c diff --git a/build/use/oniguruma.c b/build/with/oniguruma.c index cb17596..cb17596 100644 --- a/build/use/oniguruma.c +++ b/build/with/oniguruma.c |