summaryrefslogtreecommitdiff
path: root/sofort/tools/version.sh
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2024-04-28 17:47:08 +0000
committermidipix <writeonce@midipix.org>2024-04-28 17:47:08 +0000
commit1b64c0ca9f6438c5bb35f34a46344030b61b789a (patch)
treec1c41b3abf8af0c8fb2484681a7ef0e9707913ef /sofort/tools/version.sh
parent646efeb80e90cdc80e35a337273943b6ba87bb6f (diff)
downloadtreebnf-1b64c0ca9f6438c5bb35f34a46344030b61b789a.tar.bz2
treebnf-1b64c0ca9f6438c5bb35f34a46344030b61b789a.tar.xz
build system: imported sofort, a project-agnostic build system.
Diffstat (limited to 'sofort/tools/version.sh')
-rwxr-xr-xsofort/tools/version.sh75
1 files changed, 75 insertions, 0 deletions
diff --git a/sofort/tools/version.sh b/sofort/tools/version.sh
new file mode 100755
index 0000000..8c49cb3
--- /dev/null
+++ b/sofort/tools/version.sh
@@ -0,0 +1,75 @@
+#!/bin/sh
+
+# version.sh: detect git repository info, output defs as a C header.
+# this file is covered by COPYING.SOFORT.
+
+set -eu
+
+usage()
+{
+cat << EOF >&2
+
+Usage:
+ -h show this HELP message
+ -s SRCDIR set source directory
+ -o OUTPUT set output header
+ -p PREFIX set macro prefix
+
+EOF
+exit 1
+}
+
+
+# one
+workdir=$(pwd -P)
+srcdir=
+output=
+prefix=
+
+
+while getopts "hs:o:p:" opt; do
+ case $opt in
+ h)
+ usage
+ ;;
+ s)
+ srcdir="$OPTARG"
+ ;;
+ o)
+ output="$OPTARG"
+ ;;
+ p)
+ prefix="$OPTARG"
+ ;;
+ \?)
+ printf 'Invalid option: -%s' "$OPTARG" >&2
+ usage
+ ;;
+ esac
+done
+
+
+# two
+if [ -z "$srcdir" ] || [ -z "$output" ] || [ -z "$prefix" ]; then
+ usage
+fi
+
+cd -- "$srcdir"
+
+gitver=$(git rev-parse --verify HEAD 2>/dev/null) || gitver="unknown"
+cvdate=$(git show -s --format=%ci $gitver 2>/dev/null) || cvdate=$(date)
+
+vmacro=$(printf '%s' "$prefix"'_GIT_VERSION' | tr '[:lower:]' '[:upper:]')
+dmacro=$(printf '%s' "$prefix"'_GIT_DATE ' | tr '[:lower:]' '[:upper:]')
+
+cd -- "$workdir"
+
+
+# three
+printf '#define %s "%s"\n#define %s "%s"\n' \
+ "$vmacro" "$gitver" \
+ "$dmacro" "$cvdate" \
+ > "$output"
+
+# all done
+exit 0