#
# FNC GNU Makefile
#

# CONFIGURATION
include fnc.bld.mk

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

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

ifeq ($(UNAME),OpenBSD)
ifeq ($(ARCH),$(filter $(ARCH),amd64 i386))
ifeq ($(CLANG),1)
HARDEN +=	-fret-clean
endif	# CLANG
endif	# ARCH
else
HARDEN +=	-fstack-clash-protection
endif	# UNAME

ifeq ($(ARCH),$(filter $(ARCH),amd64 x86_64))
HARDEN +=	-fcf-protection=full
else ifeq ($(ARCH),arm64)
HARDEN +=	-mbranch-protection=standard
endif

# make dev
ifdef GOHARD
CFLAGS +=	${HARDEN}
endif

# make ubsan
ifdef UBSAN
ifeq ($(UNAME),OpenBSD)
UBSAN_FLAGS += -fsanitize-minimal-runtime
endif
FNC_CFLAGS +=	${UBSAN_FLAGS}
FNC_LDFLAGS +=	${UBSAN_FLAGS}
endif

# make ccdb: clang compilation database build
ifdef CCDB
CFLAGS +=	-MJ ccdb/$(@F:.o=.o.json)
endif
