#!/usr/bin/env sh set -e mkf=config.mk requiredcflags="-Jstatic" srcs="$(find . -name '*.d' | tr "\n" " ")" objs="$(printf '%s' "$srcs" | sed 's/\.d[ $]/.o /g')" # utility functions present () { command -v "$1" 1>/dev/null 2>/dev/null } using () { >&2 printf "using %s\n" "$1" } throw () { >&2 printf "%s: %s\n" "$(basename "$0")" "$1" exit 1 } # generators ## D compiler gen_DC () { if [ -n "$dc" ]; then using "$dc" return 0 fi if present ldc2; then dc=ldc2 elif present dmd; then dc=dmd else throw "D compiler not found; install ldc or dmd" fi using "$dc" } ## flags used in the compilation step gen_CFLAGS () { if [ -z "$debug" ]; then case "$dc" in ldc2) cflags="-Oz";; dmd) cflags="-O";; esac else cflags="-g" case "$dc" in ldc2) cflags="$cflags -O0 -d-debug";; dmd) cflags="$cflags -debug";; esac fi for flag in $cflags; do using "$flag" done } ## flags used in the linking step gen_LDFLAGS () { if [ "$dc" = ldc2 ]; then if present ld.lld; then ldflags="-linker=lld" elif present ld.gold; then ldflags="-linker=gold" fi fi if [ -z "$debug" ]; then [ -n "$ldflags" ] && ldflags="$ldflags " ldflags="$ldflags-L--gc-sections" fi for flag in $ldflags; do using "$flag" done } # command line interface while getopts c:dhr ch; do case "$ch" in c) case "$OPTARG" in ldc2) dc="ldc2" ;; dmd) dc="dmd" ;; *) throw "unknown D compiler '$OPTARG' specified (valid options: ldc2, dmd)" ;; esac ;; d) debug=1 ;; r) unset debug ;; h) cat <>"$mkf"