build.sh (1609B)
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="co2minimon" 9 build_dir="./out" 10 11 cc=clang 12 source_files=("main.c") 13 cflags=("-std=c11" "-D_XOPEN_SOURCE=700" "-Wall" "-Wextra" "-Wpedantic" "-Wsign-compare" "-Wswitch-enum") 14 debug_flags=("-g" "-O0") 15 release_flags=("-O2" "-Os") 16 17 build_release() { 18 release_dir="${build_dir}/release" 19 release_path="${release_dir}/${exe_name}" 20 mkdir -p "$release_dir" 21 ( 22 set -x; 23 "$cc" "${cflags[@]}" "${release_flags[@]}" "${source_files[@]}" -o "$release_path" 24 ) 25 } 26 27 build_debug() { 28 debug_dir="${build_dir}/debug" 29 debug_path="${debug_dir}/${exe_name}" 30 mkdir -p "$debug_dir" 31 ( 32 set -x; 33 "$cc" "${cflags[@]}" "${debug_flags[@]}" "${source_files[@]}" -o "$debug_path" 34 ) 35 } 36 37 usage() { 38 echo "build.sh - Build ${exe_name}" 39 echo " " 40 echo "build.sh [options]" 41 echo " " 42 echo "options:" 43 echo "-h, --help show help" 44 echo "-d, --debug build only debug executable" 45 } 46 47 debug_build=false 48 49 while [[ $# -gt 0 ]]; do 50 case "$1" in 51 -h|--help) 52 usage 53 exit 0 54 ;; 55 -d|--debug) 56 debug_build=true 57 shift 58 ;; 59 *) 60 break 61 ;; 62 esac 63 done 64 65 if [ "$debug_build" = true ]; then 66 build_debug 67 else 68 build_release 69 fi 70 71 exit 0