Skip to content

Instantly share code, notes, and snippets.

@doccaico
Last active July 24, 2025 15:49
Show Gist options
  • Select an option

  • Save doccaico/a84e33ee5c515fe3062b6b5f4273f9c4 to your computer and use it in GitHub Desktop.

Select an option

Save doccaico/a84e33ee5c515fe3062b6b5f4273f9c4 to your computer and use it in GitHub Desktop.
build.sh
#!/bin/bash
set -eu
cd "$(dirname "$0")"
exe_name=linked_list
# build -> debug mode (implicit)
# build debug -> debug mode
# build asan -> debug mode with asan
# build release -> release mode
if [ "$#" -eq 0 ]; then
debug=
echo "[debug mode]"
else
for arg in "$@"; do declare $arg=; done
if [ -v debug ]; then
echo "[debug mode]"
elif [ -v asan ]; then
debug=
echo "[debug mode with asan]"
elif [ -v release ]; then
echo "[release mode]"
fi
fi
compiler=gcc
auto_compile_flags=
if [ -v asan ]; then auto_compile_flags="$auto_compile_flags -fsanitize=address"; fi
gcc_common="-std=c99 -Wall -Wextra -Wpedantic -Wconversion -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-sign-conversion"
gcc_debug="$compiler -g -O0 -DDEBUG ${gcc_common} ${auto_compile_flags}"
gcc_release="$compiler -O2 -DNDEBUG ${gcc_common} ${auto_compile_flags}"
gcc_link=""
gcc_out="-o"
if [ -v debug ]; then compile="$gcc_debug"; fi
if [ -v release ]; then compile="$gcc_release"; fi
compile_link="$gcc_link"
out="$gcc_out"
mkdir -p build
# Build
cd build
$compile ../src/main.c $compile_link $out $exe_name
cd ..
#!/bin/sh
# build -> debug mode (implicit)
# build debug -> debug mode
# build asan -> debug mode with asan
# build release -> release mode
for arg in "$@"; do eval "$arg=1"; done
if [ "$#" -eq 1 ]; then
debug=1
echo "[debug mode]"
else
if [ -v debug ]; then
echo "[debug mode]"
elif [ -v asan ]; then
debug=1
echo "[debug mode with asan]"
elif [ -v release ]; then
echo "[release mode]"
fi
fi
compiler=gcc
auto_compile_flags=
[ "${asan:+defined}" ] && auto_compile_flags="$auto_compile_flags -fsanitize=address"
gcc_common="-I../src/ -std=c99 -Wall -Wextra -Wpedantic -Wconversion -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-sign-conversion"
gcc_debug="$compiler -g -O0 -DDEBUG ${gcc_common} ${auto_compile_flags}"
gcc_release="$compiler -O2 -DNDEBUG ${gcc_common} ${auto_compile_flags}"
gcc_link=""
gcc_out="-o"
[ "${debug:+defined}" ] && compile="$gcc_debug"
[ "${release:+defined}" ] && compile="$gcc_release"
compile_link="$gcc_link"
out="$gcc_out"
mkdir -p build
# Build
cd build
[ "${singly_linked_list:+defined}" ] && {
didbuild=1
$compile ../tests/test_singly_linked_list.c ../src/singly_linked_list.c $compile_link $out test_singly_linked_list
}
cd ..
[ "${didbuild:+defined}" ] || {
echo "[WARNING] no valid build target specified; must use build target names as arguments to this script, like \`./build.sh singly_linked_list\` or \`./build.sh doubly_linked_list\`."
exit 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment