2016-10-18 01:22:46 -07:00
|
|
|
#!/usr/bin/env zsh
|
|
|
|
|
|
|
|
compile() {
|
|
|
|
if [ $# -lt 2 ]; then
|
|
|
|
echo "usage: compile [clang|gcc] {debug|release} [flags...] {source file}" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local sepples=0
|
|
|
|
local CC=gcc
|
|
|
|
local CXX=g++
|
2016-10-19 04:31:33 -07:00
|
|
|
local our_flags=(-I.)
|
|
|
|
|
|
|
|
maybe_include() {
|
|
|
|
[ -d "$1" ] && our_flags+=("-I$1")
|
|
|
|
}
|
|
|
|
maybe_include "-I$HOME/opt/local/include"
|
|
|
|
maybe_include "-I$HOME/src/ustl"
|
|
|
|
|
2016-10-18 01:22:46 -07:00
|
|
|
local debug_flags=(-O1 -g);
|
|
|
|
local release_flags=(-Ofast -march=native -g0 -fomit-frame-pointer -s -DNDEBUG)
|
|
|
|
local dr_flags=(-Ofast -march=native -g -fomit-frame-pointer -DNDEBUG)
|
|
|
|
|
|
|
|
if [ -e /usr/bin/pprof ]; then
|
|
|
|
local malloc=(-ltcmalloc -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free)
|
|
|
|
debug_flags+=($malloc[@])
|
|
|
|
derelease_flags+=($malloc[@])
|
|
|
|
derelease_flags+=(-lprofiler)
|
|
|
|
fi
|
|
|
|
|
|
|
|
local file=${@[-1]}
|
2017-03-30 18:09:26 -07:00
|
|
|
[ "${file##*.}" = "c" ] || [ "${file##*.}" = "h" ] || sepples=1
|
2016-10-18 01:22:46 -07:00
|
|
|
|
|
|
|
{ [ $1 = clang ] && CC="clang" && CXX="clang++" && shift } || \
|
|
|
|
{ [ $1 = gcc ] && CC="gcc" && CXX="g++" && shift }
|
|
|
|
|
2017-03-30 18:09:26 -07:00
|
|
|
if [ CC=clang ]; then
|
|
|
|
debug_flags+=(-ftrapv)
|
|
|
|
if [ -z $MSYSTEM ]; then # only available on linux
|
|
|
|
debug_flags+=(-fsanitize=undefined) # this SHOULD work with mingw,
|
|
|
|
# but it fails to link.
|
|
|
|
debug_flags+=(-fsanitize=address)
|
|
|
|
debug_flags+=(-flto -fsanitize=cfi)
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2016-10-18 01:22:46 -07:00
|
|
|
{ [ $1 = debug ] && our_flags+=($debug_flags) && shift } || \
|
|
|
|
{ [ $1 = release ] && our_flags+=($release_flags) && shift } || \
|
2016-10-19 04:31:33 -07:00
|
|
|
{ [ $1 = derelease ] && our_flags+=($dr_flags) && shift } || \
|
|
|
|
{ echo "please specify either debug or (de)release" >&2; return 1 }
|
2016-10-18 01:22:46 -07:00
|
|
|
|
|
|
|
# TODO add static option
|
|
|
|
|
|
|
|
local compiler=
|
|
|
|
if [ $sepples -eq 1 ]; then
|
|
|
|
compiler=$CXX
|
|
|
|
std="-std=gnu++1z"
|
|
|
|
else
|
|
|
|
compiler=$CC
|
|
|
|
std="-std=gnu11"
|
|
|
|
fi
|
|
|
|
|
|
|
|
local flags=(${@[1,-2]})
|
|
|
|
|
|
|
|
local out=/tmp/${${file%%.*}##*/}
|
|
|
|
|
|
|
|
$compiler $std ${our_flags[@]} ${flags[@]} $file -o $out && echo "$out"
|
|
|
|
}
|
|
|
|
|
|
|
|
compile "$@"
|