build.sh (1156B)
1 #!/usr/bin/env bash 2 3 set -e # fail if any command has a non-zero exit status 4 set -u # fail if any undefined variable is referenced 5 set -o pipefail # propagate failure exit status through a pipeline 6 shopt -s globstar nullglob # enable recursive and null globbing 7 8 exe_name="calc" 9 build_dir="." 10 11 cc=clang 12 source_files=("calc.c") 13 cflags=("-std=c99" "-Wall" "-Wextra" "-Wshadow" "-Wsign-compare" "-Wno-missing-braces") 14 debug_flags=("-g" "-Og" "-Werror") 15 # shellcheck disable=SC2207 16 ldflags=() 17 18 build_debug() { 19 debug_dir="${build_dir}" 20 debug_path="${debug_dir}/${exe_name}" 21 mkdir -p "$debug_dir" 22 ( 23 set -x; 24 "$cc" "${cflags[@]}" "${debug_flags[@]}" "${source_files[@]}" -o "$debug_path" "${ldflags[@]}" 25 ) 26 } 27 28 usage() { 29 echo "build.sh - Build ${exe_name}" 30 echo " " 31 echo "build.sh [options]" 32 echo " " 33 echo "options:" 34 echo "-h, --help show help" 35 } 36 37 while [[ $# -gt 0 ]]; do 38 case "$1" in 39 -h|--help) 40 usage 41 exit 0 42 ;; 43 *) 44 break 45 ;; 46 esac 47 done 48 49 build_debug 50 51 exit 0