Reicher82 / toolchain / sbmake

Forked from toolchain/sbmake 3 years ago
Clone

Blame sofort/tools/version.sh

092ccd
#!/bin/sh
092ccd
092ccd
# version.sh: detect git repository info, output defs as a C header.
092ccd
# this file is covered by COPYING.SOFORT.
092ccd
092ccd
set -eu
092ccd
092ccd
usage()
092ccd
{
092ccd
cat << EOF >&2
092ccd
092ccd
Usage:
092ccd
  -h            show this HELP message
092ccd
  -s  SRCDIR    set source directory
092ccd
  -o  OUTPUT    set output header
092ccd
  -p  PREFIX    set macro prefix
092ccd
092ccd
EOF
092ccd
exit 1
092ccd
}
092ccd
092ccd
092ccd
# one
092ccd
workdir=$(pwd -P)
092ccd
srcdir=
092ccd
output=
092ccd
prefix=
092ccd
092ccd
092ccd
while getopts "hs:o:p:" opt; do
092ccd
	case $opt in
092ccd
	h)
092ccd
  		usage
092ccd
  		;;
092ccd
	s)
092ccd
    		srcdir="$OPTARG"
092ccd
    		;;
092ccd
	o)
092ccd
    		output="$OPTARG"
092ccd
    		;;
092ccd
	p)
092ccd
    		prefix="$OPTARG"
092ccd
    		;;
092ccd
	\?)
092ccd
		printf 'Invalid option: -%s' "$OPTARG" >&2
092ccd
    		usage
092ccd
    		;;
092ccd
	esac
092ccd
done
092ccd
092ccd
092ccd
# two
092ccd
if [ -z "$srcdir" ] || [ -z "$output" ] || [ -z "$prefix" ]; then
092ccd
	usage
092ccd
fi
092ccd
092ccd
cd -- "$srcdir"
092ccd
092ccd
gitver=$(git rev-parse --verify HEAD      2>/dev/null) || gitver="unknown"
092ccd
cvdate=$(git show -s --format=%ci $gitver 2>/dev/null) || cvdate=$(date)
092ccd
092ccd
vmacro=$(printf '%s' "$prefix"'_GIT_VERSION' | tr '[:lower:]' '[:upper:]')
092ccd
dmacro=$(printf '%s' "$prefix"'_GIT_DATE   ' | tr '[:lower:]' '[:upper:]')
092ccd
092ccd
cd -- "$workdir"
092ccd
092ccd
092ccd
# three
092ccd
printf '#define %s "%s"\n#define %s "%s"\n' \
092ccd
		"$vmacro" "$gitver" \
092ccd
		"$dmacro" "$cvdate" \
092ccd
	> "$output"
092ccd
092ccd
# all done
092ccd
exit 0