commit: 75927b3e5fe2ffe79001cf87790b3482b7eebc24
parent 4a2e99906fd13447192b806f4a5aa4895560f7b7
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 4 Feb 2022 14:23:07 +0100
Adopt a ./configure
Diffstat:
8 files changed, 123 insertions(+), 46 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,3 +1,4 @@
+/config.mk
/bin/*
!/bin/*.c
!/bin/*.sh
diff --git a/Makefile.common b/Makefile.common
@@ -3,24 +3,8 @@
# - Usage of ?= for defining variables when not already defined
# - Usage of += for appending to a variable
-PREFIX ?= /usr/local
-BINDIR = $(PREFIX)/bin
-MANDIR = $(PREFIX)/share/man
-DOCDIR = $(PREFIX)/share/doc/lanodan-utils
-DATADIR = $(PREFIX)/share/lanodan-utils
-APPSDIR = $(PREFIX)/share/applications
-
-CC ?= cc
-CFLAGS ?= -g -Wall -Wextra -Wconversion -Wsign-conversion -O2
-LDFLAGS ?= -Wl,--as-needed
-MSGFMT ?= msgfmt
-
all: $(EXE)
-.PHONY: Makefile.config
-Makefile.config:
- ../gen-Makefile.config > Makefile.config
-
.PHONY: test
test: $(EXE_test)
diff --git a/bin/Makefile b/bin/Makefile
@@ -1,16 +1,9 @@
-# POSIX-ish Makefile with extensions common to *BSD and GNU such as:
-# - Usage of backticks for shell evaluation
-# - Usage of ?= for defining variables when not already defined
-# - Usage of += for appending to a variable
-
-include Makefile.config
+include config.mk
+include ../config.mk
include ../Makefile.common
SCRIPTS = archive-tags dmenu_path wordsort zalgo
-BINDIR = $(PREFIX)/bin
-MANDIR = $(PREFIX)/share/man
-
lolcat: lolcat.c Makefile
$(CC) -std=c99 $(CFLAGS) -o $@ $< -lm $(LDFLAGS)
@@ -18,10 +11,10 @@ xcd: xcd.c Makefile
$(CC) -std=c99 $(CFLAGS) -o $@ $< -lm $(LDFLAGS)
humanize: humanize.c Makefile
- $(CC) -std=c99 $(CFLAGS) `pkg-config --cflags libbsd-overlay` -o $@ $< `pkg-config --libs libbsd-overlay` $(LDFLAGS)
+ $(CC) -std=c99 $(CFLAGS) $(LIBBSD_CFLAGS) -o $@ $< $(LIBBSD_LIBS) $(LDFLAGS)
strings: strings.c Makefile
- $(CC) -std=c99 $(CFLAGS) `pkg-config --cflags libbsd-overlay` -o $@ $< `pkg-config --libs libbsd-overlay` $(LDFLAGS)
+ $(CC) -std=c99 $(CFLAGS) $(LIBBSD_CFLAGS) -o $@ $< $(LIBBSD_LIBS) $(LDFLAGS)
install:
diff --git a/bin/Makefile.config b/bin/Makefile.config
@@ -1,2 +0,0 @@
-EXE = args basename cat date del dirname echo false humanize lolcat mdate pwd range sizeof sname strings sync tee true tty xcd
-MAN1 = basename.1 date.1 del.1 dirname.1 echo.1 humanize.1 lolcat.1 sname.1 sync.1
diff --git a/configure b/configure
@@ -0,0 +1,112 @@
+#!/bin/sh
+PREFIX="${PREFIX:-/usr/local}"
+BINDIR="${BINDIR:-${PREFIX}/bin}"
+SBINDIR="${SBINDIR:-${PREFIX}/sbin}"
+MANDIR="${MANDIR:-${PREFIX}/share/man}"
+
+PKGCONFIG="${PKGCONFIG:-pkg-config}"
+MSGFMT="${MSGFMT:-msgfmt}"
+CC="${CC:-cc}"
+CFLAGS="${CFLAGS:--g -O2 -pie -fPIE} -fstack-protector-strong -D_FORTIFY_SOURCE=2 -Wall -Wextra -Wconversion -Wsign-conversion -Werror=implicit-function-declaration -Werror=implicit-int -Werror=vla ${EXTRA_CFLAGS}"
+LDFLAGS="${LDFLAGS:--Wl,--as-needed}"
+MANDOC="mandoc"
+
+DEPS=""
+
+is_ok() {
+ status="$?"
+
+ if test $status -eq 0; then
+ printf " OK\n"
+ else
+ printf " FAIL\n"
+ fi
+
+ return $status
+}
+
+or_die() {
+ is_ok || exit 1
+}
+
+pkg_config_check() {
+ printf 'Checking: %s %s ...' "${PKGCONFIG}" "$*"
+ "${PKGCONFIG}" "$@"
+ is_ok
+}
+
+gen_targets() {
+ printf 'EXE = '
+ printf '%s\n ' *.c | grep -v -f target_filter | sed 's;.c$;;' | tr -d '\n'
+ echo
+
+ printf 'MAN1 = '
+ printf '%sn ' *.1 | grep -v -f target_filter | tr -d '\n'
+ echo
+}
+
+printf 'Pruning old configurations ...'
+rm -f config.mk bin/config.mk sbin/config.mk \
+ && echo '#' > bin/target_filter \
+ && echo '#' > sbin/target_filter \
+ ; or_die
+
+printf 'Checking %s command existance ...' "${PKGCONFIG}"
+command -v "${PKGCONFIG}" >/dev/null ; or_die
+
+printf 'Checking %s command existance ...' "${CC}"
+command -v "${CC}" >/dev/null ; or_die
+
+printf 'Checking %s command existance ...' "${MANDOC}"
+if command -v "${CC}" >/dev/null ; is_ok
+then
+ :
+else
+ MANDOC="true"
+fi
+
+
+for dep in ${DEPS}
+do
+ pkg_config_check --exists "$dep" || exit 1
+done
+
+# TODO: Check for humanize_number and strtonum existance
+if pkg_config_check libbsd-overlay
+then
+ LIBBSD_CFLAGS="$("${PKGCONFIG}" --cflags libbsd-overlay) -DHAVE_LIBBSD"
+ LIBBSD_LIBS="$("${PKGCONFIG}" --libs libbsd-overlay)"
+else
+ echo 'bin/strings.c' >> bin/target_filter ; or_die # strtonum
+ echo 'bin/humanize.c' >> bin/target_filter; or_die # humanize_number
+fi
+
+printf 'Writing bin/config.mk ...'
+(cd bin && gen_targets) > bin/config.mk ; or_die
+
+printf 'Writing sbin/config.mk ...'
+(cd sbin && gen_targets) > sbin/config.mk ; or_die
+
+printf 'Writing to config.mk ...'
+cat >config.mk <<EOF
+# Autogenerated by ./configure
+PREFIX = ${PREFIX}
+BINDIR = ${BINDIR}
+SBINDIR = ${SBINDIR}
+MANDIR = ${MANDIR}
+
+PKGCONFIG = ${PKGCONFIG}
+CC = ${CC}
+MSGFMT = ${MSGFMT}
+DBG = ${DBG}
+MANDOC = ${MANDOC}
+
+CFLAGS = ${CFLAGS}
+LDFLAGS = ${LDFLAGS}
+
+LIBBSD_CFLAGS = ${LIBBSD_CFLAGS}
+LIBBSD_LIBS = ${LIBBSD_LIBS}
+EOF
+is_ok
+
+echo 'Done, you can now run make'
diff --git a/gen-Makefile.config b/gen-Makefile.config
@@ -1,3 +0,0 @@
-#!/bin/sh
-echo 'EXE = '$(ls -d *.c | sed 's;\b\.c\b;;g')
-echo 'MAN1 = '$(ls -d *.1 | sed 's;\b\.c\b;;g')
diff --git a/sbin/Makefile b/sbin/Makefile
@@ -1,15 +1,9 @@
-# POSIX-ish Makefile with extensions common to *BSD and GNU such as:
-# - Usage of backticks for shell evaluation
-# - Usage of ?= for defining variables when not already defined
-# - Usage of += for appending to a variable
-
-include Makefile.config
+include config.mk
+include ../config.mk
include ../Makefile.common
-BINDIR = $(PREFIX)/sbin
-
install:
- mkdir -p ${DESTDIR}${BINDIR}/
- cp -p memsys ${DESTDIR}${BINDIR}/
- chown 0:0 ${DESTDIR}${BINDIR}/memsys
- chmod 4755 ${DESTDIR}${BINDIR}/memsys
+ mkdir -p ${DESTDIR}${SBINDIR}/
+ cp -p memsys ${DESTDIR}${SBINDIR}/
+ chown 0:0 ${DESTDIR}${SBINDIR}/memsys
+ chmod 4755 ${DESTDIR}${SBINDIR}/memsys
diff --git a/sbin/Makefile.config b/sbin/Makefile.config
@@ -1,2 +0,0 @@
-EXE = memsys
-MAN1 =