# Copyright (C) 2024 dwlr # # BSD 3-Clause License (BSD-3-Clause) # See LICENSE for details # This file is intended to be sourced as part of a "self-building" .c file set -e CC=${CC:-cc} STATIC=${STATIC:-yes} DEBUG=${DEBUG:-yes} RUN=${RUN:-yes} OUT=${OUT:-a.out} CFLAGS=${CFLAGS:-"-Wall -Wextra"} [ "$STATIC" = "yes" ] && CFLAGS="$CFLAGS -static" DBGFLAGS=${DBGFLAGS:-"-g -Og"} RELFLAGS=${RELFLAGS:-"-O2"} msg() { fmt="$1" shift printf "\033[1m --- $fmt\033[0m\n" $@ } if [ -e "$OUT" ] && [ -z "$(find . -newer "$OUT" -regex '.*\.\(c\|h\)')" ] \ && [ "$(file "$OUT" | grep "not stripped" > /dev/null && echo yes || echo no)" = "$DEBUG" ] \ && [ "$(file "$OUT" | grep "statically" > /dev/null && echo yes || echo no)" = "$STATIC" ] then msg "rebuild not necessary" 1>&2 else start="$(date +%s.%N)" if [ "$DEBUG" = "yes" ] then CFLAGS="$CFLAGS $DBGFLAGS" else CFLAGS="$CFLAGS $RELFLAGS" fi "$CC" "$0" $CFLAGS -o ./"$OUT" [ "$DEBUG" != "yes" ] && strip "$OUT" end="$(date +%s.%N)" msg "cc time: %s sec" "$(echo "$end - $start" | bc)" 1>&2 msg "debug=%s; static=%s" "$DEBUG" "$STATIC" 1>&2 fi if [ "$RUN" = "yes" ]; then msg "Program output:\n" 1>&2 ./"$OUT" "$@" fi exit