commit 722192506bf160f6ab03460bd61eb26a6c056037
parent d92dbff8f7616b1464230881cb3b8a6cf6460fd4
Author: amin <dev@aminmesbah.com>
Date: Sun, 9 Sep 2018 17:34:07 +0000
Move most build logic out to a bash script
- Make gives me convenient shortcuts for the most common operations I
want to do with my source code. I have no need for partial builds or a
dependency graph.
- Bash gives me a way to use variables and functions in an imperative
style.
Make is crufty and ancient. Bash is crufty and ancient. But they might
be a little less painful for me if used together for specific purposes:
FossilOrigin-Name: 32dd82a3e2007e385ff1f854c587e61f94e3df8356830840aca22df897d284ef
Diffstat:
M | .gitignore | | | 3 | ++- |
M | Makefile | | | 24 | ++++++++++++++++++++---- |
A | build.sh | | | 80 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 102 insertions(+), 5 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1 +1,2 @@
-tunnel-runner
+build/
+tags
diff --git a/Makefile b/Makefile
@@ -1,5 +1,21 @@
-build:
- clang -std=c99 -Wall -Wextra -DTR_LOGLEVEL_DEBUG -g tunnel_runner.c -o tunnel-runner -lm `sdl2-config --cflags --libs`
+# This makefile provides some convenient shortcuts for common operations. The
+# actual build logic happens in build.sh.
-run: build
- ./tunnel-runner
+BUILD_SCRIPT=./build.sh
+
+.PHONY: all clean debug release run
+
+all:
+ $(BUILD_SCRIPT)
+
+clean:
+ rm build/ -r
+
+debug:
+ $(BUILD_SCRIPT) --debug-only
+
+release:
+ $(BUILD_SCRIPT) --release-only
+
+run: release
+ ./build/release/tunnel-runner
diff --git a/build.sh b/build.sh
@@ -0,0 +1,80 @@
+#!/usr/bin/env bash
+
+set -e # fail if any command has a non-zero exit status
+set -u # fail if any undefined variable is referenced
+set -o pipefail # propagate failure exit status through a pipeline
+shopt -s globstar nullglob # enable recursive and null globbing
+
+exe_name="tunnel-runner"
+build_dir="./build"
+
+cc=clang
+source_files=("tunnel_runner.c")
+cflags=("-std=c99" "-Wall" "-Wextra" "-Wshadow" "-Wswitch-enum" "-Wno-missing-braces")
+debug_flags=("-g" "-Og" "-Werror")
+release_flags=("-O2" "-Os" "-DTR_LOGLEVEL_DEBUG")
+# shellcheck disable=SC2207
+ldflags=("-lm" $(sdl2-config --cflags --libs))
+
+build_release() {
+ release_dir="${build_dir}/release"
+ release_path="${release_dir}/${exe_name}"
+ mkdir -p "$release_dir"
+ (
+ set -x;
+ "$cc" "${cflags[@]}" "${release_flags[@]}" "${source_files[@]}" -o "$release_path" "${ldflags[@]}"
+ )
+}
+
+build_debug() {
+ debug_dir="${build_dir}/debug"
+ debug_path="${debug_dir}/${exe_name}"
+ mkdir -p "$debug_dir"
+ (
+ set -x;
+ "$cc" "${cflags[@]}" "${debug_flags[@]}" "${source_files[@]}" -o "$debug_path" "${ldflags[@]}"
+ )
+}
+
+usage() {
+ echo "build.sh - Build ${exe_name}"
+ echo " "
+ echo "build.sh [options]"
+ echo " "
+ echo "options:"
+ echo "-h, --help show help"
+ echo "-r, --release-only build only release executable"
+ echo "-d, --debug-only build only debug executable"
+}
+
+release_only=false
+debug_only=false
+
+while [[ $# -gt 0 ]]; do
+ case "$1" in
+ -h|--help)
+ usage
+ exit 0
+ ;;
+ -r|--release-only)
+ release_only=true
+ shift
+ ;;
+ -d|--debug-only)
+ debug_only=true
+ shift
+ ;;
+ *)
+ break
+ ;;
+ esac
+done
+
+if [ "$debug_only" = false ]; then
+ build_release
+fi
+if [ "$release_only" = false ]; then
+ build_debug
+fi
+
+exit 0