CC      ?= cc
STD     := -std=c11

WARNS   := -Wall -Wextra -Wshadow -Wformat=2 \
           -Wmissing-prototypes -Wstrict-prototypes

# Hardening flags shared between release and debug builds
HARDEN  := -D_FORTIFY_SOURCE=2 \
           -fstack-protector-strong \
           -fstack-clash-protection \
           -fPIE -fno-plt -fno-common \
           -fwrapv

# Release build: no debug symbols, NDEBUG, strip binary
CFLAGS  ?= -O2 $(STD) $(WARNS) $(HARDEN) -DNDEBUG
LDFLAGS ?= -pie -Wl,-z,relro,-z,now,-z,noexecstack -Wl,-s

# Debug build: sanitisers, no strip, no NDEBUG
DEBUG_CFLAGS  := -Og -g $(STD) $(WARNS) $(HARDEN) \
                 -fsanitize=address,undefined -fno-omit-frame-pointer
DEBUG_LDFLAGS := -pie -Wl,-z,relro,-z,now,-z,noexecstack \
                 -fsanitize=address,undefined

PREFIX  ?= /usr/local
BINDIR  ?= $(PREFIX)/bin

BIN := abysscrypt
SRC := abysscrypt.c

.PHONY: all debug clean install uninstall

all: $(BIN)

$(BIN): $(SRC)
	$(CC) $(CFLAGS) -o $@ $(SRC) $(LDFLAGS)

debug: $(SRC)
	$(CC) $(DEBUG_CFLAGS) -o $(BIN) $(SRC) $(DEBUG_LDFLAGS)

clean:
	rm -f $(BIN)

install: $(BIN)
	install -d $(DESTDIR)$(BINDIR)
	install -m 0755 $(BIN) $(DESTDIR)$(BINDIR)/abysscrypt

uninstall:
	rm -f $(DESTDIR)$(BINDIR)/abysscrypt
