2016-10-18 01:22:46 -07:00
|
|
|
#!/usr/bin/env zsh
|
|
|
|
|
2017-03-30 19:11:05 -07:00
|
|
|
# I'll just leave this here
|
|
|
|
__setup_clang_ubuntu() {
|
|
|
|
local site="http://apt.llvm.org"
|
|
|
|
local name="$(lsb_release -c | cut -f2)"
|
2017-03-30 19:46:30 -07:00
|
|
|
local version=4.0
|
|
|
|
local priority=$(( int(version * 100 + 0.5) ))
|
2017-03-30 19:11:05 -07:00
|
|
|
# TODO: use https? this is sketchy
|
|
|
|
echo wget -O - "$site/llvm-snapshot.gpg.key" \| apt-key add -
|
|
|
|
echo echo -n \""\
|
|
|
|
deb $site/$name/ llvm-toolchain-$name main\n\
|
|
|
|
# deb-src $site/$name/ llvm-toolchain-$name main\n\
|
|
|
|
# $version\n\
|
|
|
|
deb $site/$name/ llvm-toolchain-$name-$version main\n\
|
|
|
|
# deb-src $site/$name/ llvm-toolchain-$name-$version main\n\
|
|
|
|
"\" \> "/etc/apt/sources.list.d/llvm-toolchain-$name.list"
|
|
|
|
echo apt-get update
|
|
|
|
echo apt-get install clang-$version
|
|
|
|
echo apt-get install lld-$version
|
2017-03-30 19:46:30 -07:00
|
|
|
echo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-$version $priority
|
|
|
|
echo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-$version $priority
|
|
|
|
echo update-alternatives --install /usr/bin/llvm-symbolizer llvm-symbolizer /usr/bin/llvm-symbolizer-$version $priority
|
2017-03-30 19:11:05 -07:00
|
|
|
}
|
|
|
|
|
2016-10-18 01:22:46 -07:00
|
|
|
compile() {
|
2017-05-04 08:29:39 -07:00
|
|
|
local gcc="$(whence -p gcc 2>/dev/null)"
|
|
|
|
local clang="$(whence -p clang 2>/dev/null)"
|
|
|
|
|
|
|
|
local cl
|
|
|
|
local vc
|
|
|
|
if [ -n "$MSYSTEM" ]; then
|
2017-05-04 18:22:47 -07:00
|
|
|
if [ -z "$clang" ]; then
|
|
|
|
# maybe we have clang-msvc installed
|
|
|
|
local dir
|
2017-05-04 18:37:01 -07:00
|
|
|
printf "%s\n" "/c/Program Files/LLVM"*(On/N[1]) | read -r dir
|
2017-05-04 18:22:47 -07:00
|
|
|
if [ -d "$dir" ] && [ -e "$dir/bin/clang" ]; then
|
|
|
|
clang="$dir/bin/clang"
|
|
|
|
export PATH="$PATH:$dir/bin/"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2017-05-04 08:29:39 -07:00
|
|
|
local clarch
|
|
|
|
[ "$MSYSTEM" = MINGW64 ] && clarch="/amd64" || clarch=""
|
|
|
|
local arch
|
|
|
|
[ "$MSYSTEM" = MINGW64 ] && arch="x64" || arch="x86"
|
2017-05-04 18:37:01 -07:00
|
|
|
printf "%s\n" "/c/Program Files (x86)/Microsoft Visual Studio "*(On/N[1]) | read vc
|
2017-05-04 08:29:39 -07:00
|
|
|
vc="$vc/VC"
|
|
|
|
if [ -d "$vc/bin$clarch" ] && [ -e "${vc}/bin$clarch/$cl" ]; then
|
|
|
|
cl="${vc}/bin$clarch/cl"
|
|
|
|
vc="$vc"
|
|
|
|
|
|
|
|
local winkit
|
2017-05-04 18:37:01 -07:00
|
|
|
printf "%s\n" "/c/Program Files (x86)/Windows Kits/"*(on/N[1]) | read -r winkit
|
2017-05-04 08:29:39 -07:00
|
|
|
[ -n "$winkit" ] || { echo "failed glob; missing winkit" >&2; return 1 }
|
2017-05-04 18:37:01 -07:00
|
|
|
printf "%s\n" "$winkit/Lib/"*(On/N[1]) | read -r winkit
|
2017-05-04 08:29:39 -07:00
|
|
|
[ -n "$winkit" ] || { echo "failed glob; missing winkit" >&2; return 1 }
|
|
|
|
|
|
|
|
export PATH="$PATH:$vc/bin$clarch"
|
|
|
|
export INCLUDE="$vc/INCLUDE;$vc/ATLMFC/INCLUDE"
|
|
|
|
export LIB="$vc/LIB$clarch;$vc/ATLMFC/LIB$clarch;$winkit/um/$arch;$winkit/ucrt/$arch"
|
|
|
|
export LIBPATH="$vc/LIB$clarch;$vc/ATLMFC/LIB$clarch"
|
|
|
|
|
|
|
|
# convert msys2 paths to windows paths
|
|
|
|
export INCLUDE="${${INCLUDE//\/c\//C:\\}//\//\\}"
|
|
|
|
export LIB="${${LIB//\/c\//C:\\}//\//\\}"
|
|
|
|
export LIBPATH="${${LIBPATH//\/c\//C:\\}//\//\\}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2016-10-18 01:22:46 -07:00
|
|
|
if [ $# -lt 2 ]; then
|
2017-05-04 08:29:39 -07:00
|
|
|
echo -n "usage: compile [" >&2
|
|
|
|
local once=0
|
|
|
|
if [ -n "$clang" ]; then
|
|
|
|
if [ $once -eq 1 ]; then echo -n "|" >&2; fi; once=1
|
|
|
|
echo -n "clang" >&2
|
|
|
|
fi
|
|
|
|
if [ -n "$gcc" ]; then
|
|
|
|
if [ $once -eq 1 ]; then echo -n "|" >&2; fi; once=1
|
|
|
|
echo -n "gcc" >&2
|
|
|
|
fi
|
|
|
|
if [ -n "$cl" ]; then
|
|
|
|
if [ $once -eq 1 ]; then echo -n "|" >&2; fi; once=1
|
|
|
|
echo -n "msvc" >&2
|
|
|
|
fi
|
2017-05-04 18:22:47 -07:00
|
|
|
echo "] {debug|derelease|release|hardened} [flags...] {source file}" >&2
|
2016-10-18 01:22:46 -07:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local sepples=0
|
2017-05-04 18:22:47 -07:00
|
|
|
[ -n "$clang" ] && local CC=clang || local CC=gcc
|
|
|
|
[ -n "$clang" ] && local CXX=clang++ || local CXX=g++
|
2016-10-19 04:31:33 -07:00
|
|
|
local our_flags=(-I.)
|
|
|
|
|
2017-05-04 08:29:39 -07:00
|
|
|
local file=${@[-1]}
|
|
|
|
[ "${file##*.}" = "c" ] || [ "${file##*.}" = "h" ] || sepples=1
|
|
|
|
|
|
|
|
{ [ $1 = clang ] && CC="clang" && CXX="clang++" && shift } || \
|
|
|
|
{ [ $1 = gcc ] && CC="gcc" && CXX="g++" && shift } || \
|
|
|
|
{ [ $1 = msvc ] && CC="cl" && CXX="cl" && shift }
|
|
|
|
|
2016-10-19 04:31:33 -07:00
|
|
|
maybe_include() {
|
|
|
|
[ -d "$1" ] && our_flags+=("-I$1")
|
|
|
|
}
|
|
|
|
maybe_include "-I$HOME/opt/local/include"
|
|
|
|
maybe_include "-I$HOME/src/ustl"
|
|
|
|
|
2017-05-04 08:29:39 -07:00
|
|
|
if [ $CC = cl ]; then
|
|
|
|
our_flags+=(-nologo -utf-8)
|
|
|
|
local debug_flags=(-Od -Zi -ZI -sdl);
|
|
|
|
local release_flags=(-Ox)
|
|
|
|
local dr_flags=(-Ox -Zi)
|
|
|
|
local hardened_flags=(-Ox -sdl)
|
|
|
|
else
|
|
|
|
local debug_flags=(-O1 -g -D_DEBUG);
|
|
|
|
local release_flags=(-Ofast -march=native -g0 -fomit-frame-pointer -s -DNDEBUG)
|
|
|
|
local dr_flags=(-Ofast -march=native -g -fomit-frame-pointer -DNDEBUG)
|
|
|
|
local hardened_flags=(-O3 -march=native -g0 -s
|
|
|
|
-DNDEBUG -D_FORTIFY_SOURCE=2
|
|
|
|
-Wformat -Wformat-security -Werror=format-security)
|
|
|
|
|
|
|
|
if [ -z "$MSYSTEM" ]; then
|
|
|
|
hardened_flags+=(-fPIE -pie)
|
|
|
|
hardened_flags+=(-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now)
|
|
|
|
fi
|
2016-10-18 01:22:46 -07:00
|
|
|
|
2017-05-04 08:29:39 -07:00
|
|
|
local nomalloc=(-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free)
|
|
|
|
if [ -e /usr/bin/pprof ]; then
|
|
|
|
#debug_flags+=(-ltcmalloc $nomalloc)
|
|
|
|
dr_flags+=(-lprofiler $nomalloc)
|
|
|
|
elif [ -e /usr/bin/google-pprof ]; then
|
|
|
|
#debug_flags+=(-l:libtcmalloc.so.4 $nomalloc)
|
|
|
|
dr_flags+=(-l:libtcmalloc_and_profiler.so.4 $nomalloc)
|
|
|
|
fi
|
2016-10-18 01:22:46 -07:00
|
|
|
fi
|
|
|
|
|
2017-05-09 21:51:59 -07:00
|
|
|
local compiler=
|
|
|
|
if [ $sepples -eq 1 ]; then
|
|
|
|
compiler=$CXX
|
|
|
|
[ $CC = cl ] && std="-TP" || std="-std=gnu++1z"
|
|
|
|
else
|
|
|
|
compiler=$CC
|
|
|
|
[ $CC = cl ] && std="-TC" || std="-std=gnu11"
|
|
|
|
fi
|
|
|
|
|
|
|
|
local clang_msvc=0
|
|
|
|
if [ $CC = clang ]; then
|
|
|
|
if $compiler --version | grep -q windows-msvc; then
|
|
|
|
clang_msvc=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2017-04-01 15:27:46 -07:00
|
|
|
if [ $CC = clang ]; then
|
2017-03-30 18:09:26 -07:00
|
|
|
debug_flags+=(-ftrapv)
|
2017-05-09 21:51:59 -07:00
|
|
|
[ -z "$MSYSTEM" ] && local gold=gold || local gold=lld
|
|
|
|
[ -n "$MSYSTEM" ] && our_flags+=(-fansi-escape-codes) || true
|
|
|
|
if [ $clang_msvc -eq 1 ] || [ -z "$MSYSTEM" ]; then
|
2017-03-30 18:09:26 -07:00
|
|
|
debug_flags+=(-fsanitize=undefined) # this SHOULD work with mingw,
|
|
|
|
# but it fails to link.
|
|
|
|
debug_flags+=(-fsanitize=address)
|
2017-05-09 21:51:59 -07:00
|
|
|
debug_flags+=(-fvisibility=hidden -fuse-ld=$gold -flto -fsanitize=cfi)
|
2017-03-30 20:49:47 -07:00
|
|
|
|
|
|
|
hardened_flags+=(-fsanitize=safe-stack)
|
|
|
|
hardened_flags+=(-fstack-protector-strong)
|
2017-05-09 21:51:59 -07:00
|
|
|
hardened_flags+=(-fvisibility=hidden -fuse-ld=$gold -flto -fsanitize=cfi)
|
2017-04-01 15:27:46 -07:00
|
|
|
else
|
2017-03-30 18:09:26 -07:00
|
|
|
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 } || \
|
2017-03-30 20:49:47 -07:00
|
|
|
{ [ $1 = hardened ] && our_flags+=($hardened_flags) && shift } || \
|
|
|
|
{ echo "please specify either debug or (de)release or hardened" >&2; return 1 }
|
2016-10-18 01:22:46 -07:00
|
|
|
|
|
|
|
# TODO add static option
|
|
|
|
|
|
|
|
local flags=(${@[1,-2]})
|
|
|
|
|
|
|
|
local out=/tmp/${${file%%.*}##*/}
|
2017-04-02 03:37:49 -07:00
|
|
|
if [ -n "$MSYSTEM" ]; then
|
2017-04-01 19:17:40 -07:00
|
|
|
# explicitly output to .exe to avoid weirdness
|
|
|
|
out="$out.exe"
|
|
|
|
fi
|
2016-10-18 01:22:46 -07:00
|
|
|
|
2017-03-30 19:11:05 -07:00
|
|
|
# TODO: naive idea:
|
|
|
|
# allow multiple source files (using the firstmost to determine the program name)
|
|
|
|
# by generating a file that #includes each given file.
|
|
|
|
|
2017-04-02 03:37:49 -07:00
|
|
|
# move -l flags to the end because gcc won't respect them otherwise
|
2017-05-03 01:01:48 -07:00
|
|
|
# split warning flags so they don't spam the console
|
2017-04-02 03:37:49 -07:00
|
|
|
local final_flags=()
|
|
|
|
local libraries=()
|
2017-05-03 01:01:48 -07:00
|
|
|
local warnings=()
|
2017-04-02 03:37:49 -07:00
|
|
|
for flag in $our_flags $flags; do
|
|
|
|
if [[ $flag == -l* ]]; then
|
|
|
|
libraries+=($flag)
|
2017-05-04 08:29:39 -07:00
|
|
|
elif [[ $flag == -W* ]] && [[ $flag != -Wl* ]]; then
|
2017-05-03 01:01:48 -07:00
|
|
|
warnings+=($flag)
|
2017-05-04 08:29:39 -07:00
|
|
|
if [ $CC = cl ] && [ $flag = -Wall ]; then
|
|
|
|
warnings+=(-wd4711 -wd4505 -wd4514 -wd4625 -wd4626)
|
|
|
|
fi
|
2017-04-02 03:37:49 -07:00
|
|
|
else
|
2017-05-04 18:46:08 -07:00
|
|
|
if [ $CC = clang ]; then
|
2017-05-04 08:29:39 -07:00
|
|
|
if [ $flag = "-findirect-inlining" ] \
|
2017-05-04 18:46:08 -07:00
|
|
|
|| [ $flag = "-finline-small-functions" ]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [ $clang_msvc -eq 1 ]; then
|
|
|
|
if [ $flag = "-Wl,--gc-sections" ] \
|
2017-05-04 08:29:39 -07:00
|
|
|
|| [ $flag = "-s" ]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
fi
|
2017-04-02 03:37:49 -07:00
|
|
|
final_flags+=($flag)
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2017-05-04 18:22:47 -07:00
|
|
|
[ $CC = cl ] && local outflag=-Fe: || local outflag=-o
|
|
|
|
echo $compiler $std ${final_flags[@]} $file ${libraries[@]} $outflag $out >&2
|
|
|
|
$compiler $std ${final_flags[@]} $file ${libraries[@]} ${warnings[@]} $outflag $out >&2
|
2016-10-18 01:22:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
compile "$@"
|