#
# FNC Makefile
#

# CONFIGURATION
.POSIX:
.include "fnc.bld.mk"

UNAME !=	uname -s
ARCH !=		uname -m
CLANG !=	($(CC) -v 2>&1 | grep -c "clang version") || true

# OSX has ncursesw, and needs iconv.
.if $(UNAME) == Darwin
# OSX
FNC_LDFLAGS +=	-lncurses -lpanel -liconv
.else
# Linux (tested on Debian), OpenBSD, FreeBSD
FNC_LDFLAGS +=	-lncursesw -lpanelw
.endif

.if $(UNAME) == OpenBSD
.if $(ARCH) == amd64 || $(ARCH) == i386
.if $(CLANG)
HARDEN +=	-fret-clean
.endif	# CLANG
.endif	# ARCH
.else
HARDEN +=	-fstack-clash-protection
.endif	# UNAME

.if $(ARCH) == amd64 || $(ARCH) == x86_64
HARDEN +=	-fcf-protection=full
.elif $(ARCH) == arm64
HARDEN +=	-mbranch-protection=standard
.endif

# make dev
.if defined(GOHARD)
CFLAGS +=	${HARDEN}
.endif

# make ubsan
.if defined(UBSAN)
.if $(UNAME) == OpenBSD
UBSAN_FLAGS += -fsanitize-minimal-runtime
.endif
FNC_CFLAGS +=	${UBSAN_FLAGS}
FNC_LDFLAGS +=	${UBSAN_FLAGS}
.endif

# make ccdb: clang compilation database build
.if defined(CCDB)
FILESEGMENT =	$(.TARGET:C/$(@D)//)
CFLAGS +=	-MJ ccdb$(FILESEGMENT:=.json)
.endif
