show version in header for static version resolution

This change moves the gint version declaration from a symbol in a
compile-time generated object file to a preprocessed header installed in
the library tree.

This makes it possible to determine the gint version statically from the
headers, which is much more robust in complex build systems that use
version information such as CMake's find_package().
This commit is contained in:
Lephe 2020-11-04 22:12:01 +01:00
parent ad6c108dfc
commit 7a68070bac
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
3 changed files with 34 additions and 21 deletions

19
include/gint/config.h.in Normal file
View file

@ -0,0 +1,19 @@
//---
// config - Compile-time generate configuration
//---
#ifndef GINT_CONFIG
#define GINT_CONFIG
#include <gint/defs/types.h>
/* GINT_VERSION: Latest tag and number of additional commits
"2.1.0" = Release 2.1.0
"2.1.1-5" = 5 commits after release 2.1.1 */
#define GINT_VERSION @GINT_VERSION@
/* GINT_HASH: Commit hash with 7 digits
0x03f7c0a0 = Commit 3f7c0a0 */
#define GINT_HASH @GINT_HASH@
#endif /* GINT_CONFIG */

View file

@ -6,15 +6,7 @@
#define GINT_GINT
#include <gint/defs/types.h>
/* GINT_VERSION - the library version number
gint is versioned from its repository commits on the master branch. The
GINT_VERSION integer contains the short commit hash with 7 digits.
For instance, 0x03f7c0a0 means commit 3f7c0a0. */
extern char GINT_VERSION;
#define GINT_VERSION ((uint32_t)&GINT_VERSION)
#include <gint/config.h>
/* gint_switch(): Switch out of gint to execute a function

View file

@ -26,7 +26,7 @@ machine ?= -m4-nofpu -mb
endif
# Compiler flags, assembler flags, dependency generation, archiving
inc := -I ../include
inc := -I ../include -I include
cflags := $(machine) -ffreestanding -nostdlib -Wall -Wextra -std=c11 -Os \
-fstrict-volatile-bitfields $(inc) $(CONFIG.MACROS) \
$(CONFIG.CFLAGS)
@ -64,7 +64,7 @@ src_obj := $(foreach s,$(src),$(call src2obj,$s))
# Files with special handling
spe-fx := ../src/font5x7.png
spe-cg := ../src/font8x9.png
spe_obj := version.o $(foreach s,$(spe-$(CONFIG.TARGET)),$(call src2obj,$s))
spe_obj := $(foreach s,$(spe-$(CONFIG.TARGET)),$(call src2obj,$s))
# All object files
obj := $(src_obj) $(spe_obj)
@ -87,6 +87,8 @@ objcopy = $(CONFIG.TOOLCHAIN)-objcopy
# Version symbol is obtained by using the last commit hash on 7 nibbles
version_hash = 0x0$(shell git rev-parse --short HEAD)
# Version number: closest tag, with additional commits
version_number = $(shell git describe --tag --always | cut -d- -f1-2)
#
@ -95,7 +97,7 @@ version_hash = 0x0$(shell git rev-parse --short HEAD)
all: $(target)
$(target): $(obj)
$(target): include/gint/config.h $(obj)
@ rm -f $@
$(call cmd_l,ar,$@) $(ar) rcs $(arflags) $@ $^
@ -113,6 +115,11 @@ src/%.c.o: ../src/%.c src/%.c.d
$(call cmd_b,gcc,$*.c) $(gcc) -c $< -o $@ $(dflags) $(cflags)
# Special files
include/gint/config.h: ../include/gint/config.h.in $(gitfile)
@ mkdir -p $(dir $@)
$(call cmd_m,sed,$@) cp $< $@
@ sed -i'' 's/@GINT_VERSION@/"$(version_number)"/' $@
@ sed -i'' 's/@GINT_HASH@/$(version_hash)/' $@
$(call src2obj,../src/font5x7.png): ../src/font5x7.png
@ mkdir -p $(dir $@)
$(call cmd_m,fxconv,font5x7.png) fxconv -f $< -o $@ \
@ -125,19 +132,12 @@ $(call src2obj,../src/font8x9.png): ../src/font8x9.png
charset:print grid.size:8x11 grid.padding:1 grid.border:0 \
proportional:true height:9
# Version symbol. ld generates a .stack section for unknown reasons; I remove
# it in the linker script.
version.o: $(gitfile)
@ mkdir -p $(dir $@)
@ echo "_GINT_VERSION = $(version_hash);" > $@.txt
$(call cmd_b,ld,$@) $(ld) -r -R $@.txt -o $@
#
# Cleaning
#
clean:
@ rm -rf src version.o{,txt}
@ rm -rf src include
distclean: clean
@ rm -rf Makefile $(CONFIG) $(target)
@ -156,7 +156,9 @@ install: $(target)
install -d $(PREFIX)
install $(target) $(m644) $(PREFIX)
install ../$(CONFIG.TARGET.LONG).ld $(m644) $(PREFIX)
cp -r ../include/gint $(PREFIX)/include
install -d $(PREFIX)/include/gint
cp ../include/gint/*.h $(PREFIX)/include/gint/
cp include/gint/*.h $(PREFIX)/include/gint/
uninstall:
rm -f $(PREFIX)/$(target)