Как я указывал в комментариях, ядро Linux добивается цели. Основной частью является сценарий setlocalversion , основной частью которого является (подробные объяснения даны внутри оригинального сценария, ниже приведены только важные части)
if test -z "$(git rev-parse --show-cdup 2>/dev/null)" &&
head=$(git rev-parse --verify --short HEAD 2>/dev/null); then
if [ -z "$(git describe --exact-match 2>/dev/null)" ]; then
printf '%s%s' -g $head
fi
if {
git --no-optional-locks status -uno --porcelain 2>/dev/null ||
git diff-index --name-only HEAD
} | grep -qvE '^(.. )?scripts/package'; then
printf '%s' -dirty
fi
fi
Теперь наш C file module. c:
#include <stdio.h>
#include "version.h"
int main(int argc, char *argv[])
{
printf("Our version is: %s\n", VERSION);
return 0;
}
и Makefile
VERSION = 0.1
version.h: FORCE
@echo "$(VERSION)$(shell $(PWD)/setlocalversion $(PWD))"
@echo "#define VERSION \"$(VERSION)$(shell $(PWD)/setlocalversion $(PWD))\"" > version.h
module.o: version.h
all: module
@$(PWD)/module
clean:
@rm module module.o version.h
FORCE:
.PHONY: FORCE
Показать время:
$ make all
0.1-gb6d5c80
cc -c -o module.o module.c
cc module.o -o module
Our version is: 0.1-gb6d5c80