c-md/bootstrap.sh
lohhiiccc 80f7a1b9b6 feat(build): add build instructions in README and convert sources to .c.md format
- Add detailed build and bootstrap instructions to README.md.
 - Convert all source and header files from .c/.h to .c.md/.h.md.
 - Add bootstrap.sh script for automated building across version history.
 - Update Makefile and sources.mk to reflect new markdown-based source organization.
2026-01-12 14:54:49 +01:00

103 lines
2.5 KiB
Bash
Executable file

#!/usr/bin/env sh
# bootstrap.sh - Build c-md by traversing major version tags
#
# This script builds c-md from scratch by going through each major version
# tag in order. Each version is built using the binary from the previous
# version, starting from v1.0.0 which uses plain C sources.
#
# Use this script after a fresh clone or after 'make fclean'.
set -e
TMP_BIN="/tmp/c-md-bootstrap-$$"
CURRENT=""
STASH_NEEDED=0
cleanup() {
# Restore original branch if we switched
if [ -n "$CURRENT" ]; then
CURRENT_BRANCH=$(git branch --show-current)
if [ "$CURRENT_BRANCH" != "$CURRENT" ]; then
echo "Restoring original branch..."
git switch "$CURRENT" --quiet 2>/dev/null || git checkout "$CURRENT" --quiet 2>/dev/null || true
fi
fi
# Restore stashed changes
if [ "$STASH_NEEDED" -eq 1 ]; then
echo "Restoring stashed changes..."
git stash pop --quiet 2>/dev/null || echo "Warning: Could not restore stashed changes"
fi
rm -f "$TMP_BIN"
}
trap cleanup EXIT INT TERM
get_highest_major_tags() {
git tag -l 'v*' \
| grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' \
| sort -V \
| awk -F. '
{
m=substr($1, 2)+0;
tag = $0
tags[m] = tag # Overwrite; since input is sorted, last wins (highest)
} END {
for (m in tags) print tags[m]
}' \
| sort -V
}
echo "Bootstrapping c-md..."
CURRENT=$(git branch --show-current)
if [ -n "$(git status --porcelain)" ]; then
echo "Stashing local changes..."
git stash --include-untracked --quiet
STASH_NEEDED=1
fi
TAGS=$(get_highest_major_tags)
if [ -z "$TAGS" ]; then
echo "No version tags found. Building current version from C sources..."
make --quiet
echo "Bootstrap complete. Binary ready at ./c-md"
exit 0
fi
echo "Found major versions: $(echo $TAGS | tr '\n' ' ')"
for TAG in $TAGS; do
echo "Building $TAG..."
git switch --detach "$TAG" --quiet
if [ -f "$TMP_BIN" ]; then
cp "$TMP_BIN" ./c-md
fi
make --quiet
cp c-md "$TMP_BIN"
make fclean --quiet
done
echo "Restoring current branch..."
git switch "$CURRENT" --quiet
if [ "$STASH_NEEDED" -eq 1 ]; then
echo "Restoring stashed changes..."
git stash pop --quiet
STASH_NEEDED=0
fi
echo "Building current version..."
cp "$TMP_BIN" ./c-md
make --quiet
cp c-md "$TMP_BIN"
make fclean --quiet
mv "$TMP_BIN" ./c-md
echo ""
echo "Bootstrap complete. Binary ready at ./c-md"
echo "Run 'make' to rebuild from sources."