#!/bin/bash warnings=( -Wall -Wcast-align -Wcast-qual -Wextra -Wpedantic -Wformat=2 -Winit-self -Wmissing-prototypes -Wpointer-arith -Wshadow -Wstrict-prototypes -Wsuggest-attribute=format -Wsuggest-attribute=noreturn ) CFLAGS+=(-std=c11 -fPIC) usage () { echo "Usage: $0 [-h|options...]"; } help () { cat </dev/null 2>&1; then echo "$p" return fi done echo "$what not set or found" >&2 return 1 } CC=$(check '$CC, cc, gcc or clang' "$CC" cc gcc clang) || exit colour=auto while getopts B:C:L:P:c:defhovw opt; do case $opt in B) LDLIBS+=("$OPTARG");; C) CFLAGS+=("$OPTARG");; L) LDFLAGS+=("$OPTARG");; P) CPPFLAGS+=("$OPTARG");; c) colour="$OPTARG";; d) CFLAGS+=(-Og -g); LDFLAGS+=(-Og -g);; e) CFLAGS+=(-Werror);; f) gen_flags=1;; h) usage; help; exit;; o) CFLAGS+=(-O2 -flto); LDFLAGS+=(-O2 -flto);; v) verbose=1;; w) CFLAGS+=("${warnings[@]}");; ?) usage >&2; exit 1;; esac done if [[ $colour == auto && -t 1 ]]; then colour=always fi if [[ $colour == always ]]; then CFLAGS+=(-fdiagnostics-color) fi { echo "# generated using $0 $@" echo "CC=$CC" echo "CFLAGS=(${CFLAGS[@]@Q})" echo "CPPFLAGS=(${CPPFLAGS[@]@Q})" echo "LDFLAGS=(${LDFLAGS[@]@Q})" echo "LDLIBS=(${LDLIBS[@]@Q})" } >.config.rc [[ $verbose ]] && cat .config.rc [[ $gen_flags ]] && printf '%s\n' "${CFLAGS[@]}" "${CPPFLAGS[@]}" >compile_flags.txt