From ebdb623b5356f43fce841825ee473ea6ea56de20 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 28 Apr 2024 00:35:06 +0200 Subject: [PATCH] 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. --- .gitignore | 3 ++- Makefile | 31 +++++++++++++++---------------- dss.c | 3 ++- version-gen.sh | 29 +++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 18 deletions(-) create mode 100755 version-gen.sh 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 1ea8ada..7a64346 100644 --- a/dss.c +++ b/dss.c @@ -1829,6 +1829,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; @@ -1838,7 +1839,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 -- 2.39.5