#!/bin/sh

#set -e
#set -x

# this script just uses the right linker with the proper libraries and width,
# and merely passes $* to it.

# $1 is the name of the output file
MAPFILE=${1%$MK_DLLEXT}.map

case $MK_OPT in
fuzz)	export AFL_QUIET=1
	: ${CC:=afl-clang-fast++} ;;
*)	: ${CC:=g++} ;;
esac

LDFLAGS="
	-Wl,--as-needed
	-lm
	-lpthread
	-ldl
	-lrt
	-Xlinker -Map=$MAPFILE
	-Xlinker --cref
	-Xlinker --gc-sections
	"

case $MK_OPT in
cov)
	case $CC in
	*clang*) LDFLAGS="$LDFLAGS -fprofile-instr-generate" ;;
	*) LDFLAGS="$LDFLAGS --coverage" ;;
	esac ;;
opt)
	LDFLAGS="$LDFLAGS -Wno-stringop-overflow" ;;
esac

# On Debian-based systems you have to explicitly opt-in to using the gold
# linker. Do so, because it's much faster.
## Disabled while the build machines are still using GCC 4.7, which doesn't
## recognise this option.
##if [ -x "$(which ld.gold 2> /dev/null)" ] ; then
##	LDFLAGS="$LDFLAGS -fuse-ld=gold"
##fi

# LDFLAGS="$LDFLAGS -m$MK_BITS"
LDFLAGS="$LDFLAGS"

cd $OBJDIR

verbose $CC -o "$@" $LDFLAGS $L_FLAGS

$CC -o $* $LDFLAGS $L_FLAGS
