CARGO ?= cargo
STRIP ?= strip

FEATURES :=

THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
REPO_ROOT := $(realpath $(THIS_DIR)/..)
export SERVICEPOINT_HEADER_OUT := $(REPO_ROOT)/include

override CFG_MUSL := $(if $(CFG_MUSL),$(CFG_MUSL),$(if $(MUSL),$(MUSL),0))
override CFG_PROFILE := $(if $(CFG_PROFILE),$(CFG_PROFILE),$(if $(PROFILE),$(PROFILE),release))

CCFLAGS += -Wall -fwhole-program

STRIPFLAGS := -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag

ifeq ($(CFG_MUSL), 1)
	TARGET ?= x86_64-unknown-linux-musl
	CC ?= musl-gcc
	CCFLAGS += -static -lservicepoint_binding_c
	RUSTFLAGS += --crate-type=staticlib
else
	TARGET ?= x86_64-unknown-linux-gnu
	CC ?= gcc
	#CCFLAGS += -shared
	CCFLAGS += -Wl,-Bstatic -lservicepoint_binding_c -Wl,-Bdynamic
endif

#ifeq ($(CFG_PROFILE), size-optimized)
#	CCFLAGS += -nodefaultlibs -lc
#endif

RUST_TARGET_DIR := $(REPO_ROOT)/target/$(TARGET)/$(CFG_PROFILE)
OUT_DIR := $(realpath $(THIS_DIR)/out/)

ifeq ($(CFG_PROFILE), size-optimized)
	CARGO_PROFILE := size-optimized
	CCFLAGS += -Oz \
		-fwrapv -fomit-frame-pointer -fno-stack-protector\
		-fno-unroll-loops \
		-fno-unwind-tables -fno-asynchronous-unwind-tables \
		-fmerge-all-constants \
		-Wl,-z,norelro \
		-Wl,--hash-style=gnu \
		-fvisibility=hidden \
		-Bsymbolic \
		-Wl,--exclude-libs,ALL \
		-fno-ident \
		-fno-exceptions
	CARGOFLAGS += -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \
		-Zbuild-std-features="panic_immediate_abort"
	RUSTFLAGS += -Zlocation-detail=none \
		-Zfmt-debug=none \
		-C link-arg=-z,norelro \
		-C panic=abort
		#-C link-arg=--hash-style=gnu
else ifeq ($(CFG_PROFILE), release)
	CARGO_PROFILE := release
	CCFLAGS += -O2
else ifeq ($(CFG_PROFILE), debug)
	CCFLAGS += -Og
	CARGO_PROFILE := dev
else
	CFG_PROFILE := $(error "PROFILE has to be set to one of: debug, release, size-optimized")
endif

CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \
	--profile=$(CARGO_PROFILE) \
	--no-default-features \
	--features=$(FEATURES) \
	--target=$(TARGET)

ifneq ($(CFG_PROFILE), debug)
	CCFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections
	RUSTFLAGS += -C link-arg=-s -C link-arg=-Wl,--gc-sections
endif

ifeq ($(LTO), 1)
	CCFLAGS += -flto
endif

_c_src := $(wildcard ./src/*.c)
_programs := $(basename $(notdir $(_c_src)))
_bins := $(addprefix out/, $(_programs))
_unstripped_bins := $(addsuffix _unstripped, $(_bins))
_run_programs := $(addprefix run_, $(_programs))
_rs_src := $(wildcard ../src/**.rs) ../Cargo.lock ../Cargo.toml ../cbindgen.toml
_sp_artifacts := $(SERVICEPOINT_HEADER_OUT)/servicepoint.h $(RUST_TARGET_DIR)/libservicepoint_binding_c.a $(RUST_TARGET_DIR)/libservicepoint_binding_c.so

all: $(_bins)

clean: clean-c clean-rust

clean-c:
	rm -r $(OUT_DIR) || true

clean-rust:
	rm $(SERVICEPOINT_HEADER_OUT)/servicepoint.h || true
	cargo clean

.PHONY: all clean sizes $(_run_programs) clean-c clean-rust

$(_unstripped_bins): out/%_unstripped: src/%.c $(SERVICEPOINT_HEADER_OUT)/servicepoint.h $(_sp_artifacts)
	mkdir -p out || true
	${CC} $< \
		-I $(SERVICEPOINT_HEADER_OUT) \
		-L $(RUST_TARGET_DIR) \
		$(CCFLAGS) \
		-o $@

$(_bins): out/%: out/%_unstripped
	$(STRIP) $(STRIPFLAGS) $^ -o $@

$(_sp_artifacts): $(_rs_src)
	mkdir -p $(SERVICEPOINT_HEADER_OUT) || true
	# generate servicepoint header and library to link against
	${CARGO} rustc $(CARGOFLAGS) -- $(RUSTFLAGS)

$(_run_programs): run_%: out/% FORCE
	./$<

sizes: $(_bins)
	ls -lB out

analyze-size: out/$(BIN)_unstripped
	nm --print-size --size-sort --reverse-sort --radix=d --demangle $(OUT_DIR)/$(BIN)_unstripped \
		| awk '{size=$$2+0; print size "\t" $$4}' \
		| less

FORCE: ;