.PHONY: help configure cb build test bench \
	wconfigure wcb wtest wbench \
	fmt fmt-check clean distclean

CMAKE ?= cmake
CTEST ?= ctest
CLANG_FORMAT ?= clang-format

BUILD_DIR ?= build
WIN_CL_BUILD_DIR ?= build-win-cl
WIN_GCC_BUILD_DIR ?= build-win-gcc
WIN_CLANG_BUILD_DIR ?= build-win-clang
BUILD_TYPE ?= Release

CMAKE_FLAGS ?= -DBITSERIAL_BUILD_TESTS=ON -DBITSERIAL_BUILD_BENCHMARKS=ON
USE_PRESET ?=
KNOWN_PRESETS := vs-release gcc-release clang-release

ifneq ($(strip $(USE_PRESET)),)
ifeq ($(USE_PRESET),vs-release)
PRESET_BUILD_DIR := $(WIN_CL_BUILD_DIR)
else ifeq ($(USE_PRESET),gcc-release)
PRESET_BUILD_DIR := $(WIN_GCC_BUILD_DIR)
else ifeq ($(USE_PRESET),clang-release)
PRESET_BUILD_DIR := $(WIN_CLANG_BUILD_DIR)
else
$(error Unknown USE_PRESET "$(USE_PRESET)"; expected one of: $(KNOWN_PRESETS))
endif
endif

ifeq ($(strip $(USE_PRESET)),)
CONFIGURE_CMD := $(CMAKE) -S . -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) $(CMAKE_FLAGS)
BUILD_CMD := $(CMAKE) --build $(BUILD_DIR) --config $(BUILD_TYPE)
TEST_CMD := $(CTEST) --test-dir $(BUILD_DIR) -C $(BUILD_TYPE) --output-on-failure
BENCH_CMD := $(CMAKE) --build $(BUILD_DIR) --config $(BUILD_TYPE) --target run_bitserial_benchmarks
else
CONFIGURE_CMD := $(CMAKE) --preset $(USE_PRESET)
BUILD_CMD := $(CMAKE) --build --preset $(USE_PRESET)
TEST_CMD := $(CTEST) --preset $(USE_PRESET)
BENCH_CMD := $(CMAKE) --build --preset $(USE_PRESET) --target run_bitserial_benchmarks
endif

FORMAT_FILES := \
	bitserial.hpp \
	include/bitserial/bit_io.hpp \
	include/bitserial/bitserial.hpp \
	include/bitserial/bulk.hpp \
	include/bitserial/detail/config.hpp \
	tests/bitserial_tests.cpp \
	benchmarks/baq_bulk_benchmarks.cpp

help:
	@printf '%s\n' \
		'Targets:' \
		'  make cb          Configure and build native Release tree' \
		'  make test        Build and run tests' \
		'  make bench       Build and run Catch2 benchmarks' \
		'  make wconfigure USE_PRESET=vs-release|gcc-release|clang-release' \
		'  make wcb        USE_PRESET=vs-release|gcc-release|clang-release' \
		'  make wtest      USE_PRESET=vs-release|gcc-release|clang-release' \
		'  make wbench     USE_PRESET=vs-release|gcc-release|clang-release' \
		'  make fmt         Format C++ sources with clang-format' \
		'  make fmt-check   Check formatting without writing files' \
		'  make clean       Clean configured build trees' \
		'  make distclean   Remove build directories'

configure:
	$(CONFIGURE_CMD)

cb: configure
	$(BUILD_CMD)

build: cb

test: cb
	$(TEST_CMD)

bench: configure
	$(BENCH_CMD)

wconfigure:
	$(if $(strip $(USE_PRESET)),,$(error USE_PRESET is required; expected one of: $(KNOWN_PRESETS)))
	$(CMAKE) --preset $(USE_PRESET)

wcb: wconfigure
	$(CMAKE) --build --preset $(USE_PRESET)

wtest: wcb
	$(CTEST) --preset $(USE_PRESET)

wbench: wconfigure
	$(CMAKE) --build --preset $(USE_PRESET) --target run_bitserial_benchmarks

fmt:
	$(CLANG_FORMAT) -i $(FORMAT_FILES)

fmt-check:
	$(CLANG_FORMAT) --dry-run --Werror $(FORMAT_FILES)

clean:
	-$(CMAKE) --build $(BUILD_DIR) --config $(BUILD_TYPE) --target clean
	-$(CMAKE) --build $(WIN_CL_BUILD_DIR) --config $(BUILD_TYPE) --target clean
	-$(CMAKE) --build $(WIN_GCC_BUILD_DIR) --config $(BUILD_TYPE) --target clean
	-$(CMAKE) --build $(WIN_CLANG_BUILD_DIR) --config $(BUILD_TYPE) --target clean

distclean:
	rm -rf $(BUILD_DIR) $(WIN_CL_BUILD_DIR) $(WIN_GCC_BUILD_DIR) $(WIN_CLANG_BUILD_DIR)
