From: Andre Noll Date: Sat, 27 Apr 2024 22:35:06 +0000 (+0200) Subject: build: Dynamic dependency creation and versioning. X-Git-Url: http://git.tuebingen.mpg.de/?a=commitdiff_plain;h=c7bca1d8dfa35bb0b4ef4a736d2098e6bede846e;p=dss.git build: Dynamic dependency creation and versioning. This gets rid of Makefile.deps in favor of one auto-generated .d file per .c file, and adds version-gen.sh, a shell script executed by make(1) which tries to determine the version number that is going to be incorporated into the executable via the generated version.c. --- diff --git a/.gitignore b/.gitignore index 36dfcfa..b6dcfd2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ -Makefile.deps Makefile.local +version.c *.o +*.d *.swp dss.lsg.* dss diff --git a/Makefile b/Makefile index c8242b4..d1c1866 100644 --- a/Makefile +++ b/Makefile @@ -4,14 +4,17 @@ INSTALL ?= install INSTALL_PROGRAM ?= $(INSTALL) INSTALL_DATA ?= $(INSTALL) -m 644 MKDIR_P := mkdir -p +VERSION := $(shell ./version-gen.sh dss version.c) -VERSION_STRING = 1.0.1 +units := dss str file exec sig daemon df tv snap ipc dss.lsg version +objs := $(addsuffix .o, $(units)) +deps := $(addsuffix .d, $(units)) -dss_objects := dss.o str.o file.o exec.o sig.o daemon.o df.o tv.o snap.o ipc.o dss.lsg.o all: dss dss.1 man: dss.1 +$(objs): dss.lsg.h Makefile +-include $(deps) -DSS_CPPFLAGS := -DVERSION_STRING='"$(VERSION_STRING)"' DSS_CPPFLAGS += -Wunused-macros DSS_CFLAGS := -Wno-sign-compare -g -Wunused -Wundef @@ -27,29 +30,25 @@ DSS_CFLAGS += -Wunused-parameter DSS_CFLAGS += -Wbad-function-cast DSS_CFLAGS += -Wshadow -Makefile.deps: $(wildcard *.c *.h) - $(CC) -MM -MG $(DSS_CPPFLAGS) $(CPPFLAGS) $(DSS_CFLAGS) $(CFLAGS) *.c > $@ - --include Makefile.deps - -dss: $(dss_objects) - $(CC) -o $@ $(dss_objects) $(LDFLAGS) -llopsub - -%.o: %.c Makefile - $(CC) -c $(DSS_CPPFLAGS) $(CPPFLAGS) $(DSS_CFLAGS) $(CFLAGS) $< - +version.c: + ./version-gen.sh dss version.c > /dev/null +dss: $(objs) + $(CC) -o $@ $(objs) $(LDFLAGS) -llopsub +%.o: %.c + $(CC) -c -o $@ $(DSS_CPPFLAGS) $(CPPFLAGS) $(DSS_CFLAGS) $(CFLAGS) \ + -MMD -MF $(*F).d -MT $@ $< %.lsg.h: %.suite lopsubgen --gen-h=$@ < $< %.lsg.c: %.suite lopsubgen --gen-c=$@ < $< %.1: %.suite - lopsubgen --gen-man=$@ --version-string=$(VERSION_STRING) < $< + lopsubgen --gen-man=$@ --version-string=$(VERSION) < $< %.1.html: %.1 groff -m man -Thtml -P -l -P -r $< | sed -e '1,/^/d; /^<\/body>/,$$d' > $@ clean: - rm -f *.o dss dss.1 dss.1.html Makefile.deps *~ index.html dss.lsg.h dss.lsg.c + rm -f *.[od] dss dss.1 *.html dss.lsg.[ch] version.c ifneq ($(findstring strip, $(MAKECMDGOALS)),) strip_option := -s diff --git a/dss.c b/dss.c index 0992ec6..77b2dc5 100644 --- a/dss.c +++ b/dss.c @@ -1820,6 +1820,7 @@ static int setup_signal_handling(void) return install_sighandler(SIGCHLD); } +const char *dss_version(void); static void handle_version_and_help(void) { char *txt; @@ -1829,7 +1830,7 @@ static void handle_version_and_help(void) else if (OPT_GIVEN(DSS, HELP)) txt = lls_short_help(CMD_PTR(DSS)); else if (OPT_GIVEN(DSS, VERSION)) - txt = make_message("%s\n", VERSION_STRING); + txt = make_message("%s\n", dss_version()); else return; printf("%s", txt); diff --git a/version-gen.sh b/version-gen.sh new file mode 100755 index 0000000..ba4af3e --- /dev/null +++ b/version-gen.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# SPDX-License-Identifier: GPL-3.0-only + +package="$1" +version_file="$2" + +ver='unnamed_version' +# First try git, then gitweb, then default. +if [ -e '.git' -o -e '../.git' ]; then + git_ver=$(git describe --abbrev=4 --always HEAD 2>/dev/null) + [ -z "$git_ver" ] && git_ver="$ver" + # update stat information in index to match working tree + git update-index -q --refresh > /dev/null + # if there are differences (exit code 1), the working tree is dirty + git diff-index --quiet HEAD || git_ver=$git_ver-dirty + ver=$git_ver +elif [ "${PWD%%-*}" = $package- ]; then + ver=${PWD##*/$package-} +fi +ver=${ver#v} + +echo "$ver" +[ -z "${version_file}" ] && exit 0 +# update version file if necessary +content="const char *${package}_version(void) {return \"$ver\";};" +[ -r "$version_file" ] && echo "$content" | cmp -s - $version_file && exit 0 +[ "$version_file" = "${version_file%/*}" ] || mkdir -p ${version_file%/*} +printf '%s\n' "$content" > $version_file