sm-26/tools/gen_buildinfo.sh

41 lines
1,015 B
Bash
Raw Normal View History

#!/bin/sh
set -u
SRC_DIR="$1"
OUT_FILE="$2"
git_ver="unknown"
git_branch="unknown"
if command -v git >/dev/null 2>&1 && [ -d "$SRC_DIR/.git" ]; then
ver=$(git -C "$SRC_DIR" describe --always --dirty --tags 2>/dev/null | tr -d '\n\r')
if [ $? -eq 0 ] && [ -n "$ver" ]; then
git_ver="$ver"
fi
branch=$(git -C "$SRC_DIR" symbolic-ref --short -q HEAD 2>/dev/null | tr -d '\n\r')
if [ -z "$branch" ]; then
branch="detached"
fi
git_branch="$branch"
fi
build_time=$(date '+%Y-%m-%d %H:%M:%S' 2>/dev/null)
mkdir -p "$(dirname "$OUT_FILE")"
TMP="$OUT_FILE.tmp.$$"
cat > "$TMP" <<EOF
#pragma once
static constexpr const char* const SM26_GIT_VERSION = "$git_ver";
static constexpr const char* const SM26_GIT_BRANCH = "$git_branch";
static constexpr const char* const SM26_BUILD_TIME = "$build_time";
EOF
if [ ! -f "$OUT_FILE" ] || ! cmp -s "$TMP" "$OUT_FILE"; then
mv "$TMP" "$OUT_FILE"
else
rm -f "$TMP"
fi
echo "buildinfo: v$git_ver ($git_branch) @ $build_time"