build.sh (2002B)
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="tunnel-runner" 9 build_dir="./build" 10 11 cc=clang 12 source_files=("tunnel_runner.c") 13 cflags=("-std=c99" "-Wall" "-Wextra" "-Wshadow" "-Wsign-compare" "-Wswitch-enum" "-Wno-missing-braces") 14 debug_flags=("-g" "-Og" "-Werror") 15 release_flags=("-O2" "-Os" "-DTR_LOGLEVEL_DEBUG") 16 # shellcheck disable=SC2207 17 ldflags=("-lm" $(sdl2-config --cflags --libs)) 18 19 build_release() { 20 release_dir="${build_dir}/release" 21 release_path="${release_dir}/${exe_name}" 22 mkdir -p "$release_dir" 23 ( 24 set -x; 25 "$cc" "${cflags[@]}" "${release_flags[@]}" "${source_files[@]}" -o "$release_path" "${ldflags[@]}" 26 ) 27 } 28 29 build_debug() { 30 debug_dir="${build_dir}/debug" 31 debug_path="${debug_dir}/${exe_name}" 32 mkdir -p "$debug_dir" 33 ( 34 set -x; 35 "$cc" "${cflags[@]}" "${debug_flags[@]}" "${source_files[@]}" -o "$debug_path" "${ldflags[@]}" 36 ) 37 } 38 39 usage() { 40 echo "build.sh - Build ${exe_name}" 41 echo " " 42 echo "build.sh [options]" 43 echo " " 44 echo "options:" 45 echo "-h, --help show help" 46 echo "-r, --release-only build only release executable" 47 echo "-d, --debug-only build only debug executable" 48 } 49 50 release_only=false 51 debug_only=false 52 53 while [[ $# -gt 0 ]]; do 54 case "$1" in 55 -h|--help) 56 usage 57 exit 0 58 ;; 59 -r|--release-only) 60 release_only=true 61 shift 62 ;; 63 -d|--debug-only) 64 debug_only=true 65 shift 66 ;; 67 *) 68 break 69 ;; 70 esac 71 done 72 73 if [ "$debug_only" = false ]; then 74 build_release 75 fi 76 if [ "$release_only" = false ]; then 77 build_debug 78 fi 79 80 exit 0