# NGINX module name (should match the exported symbol name).
MODULE_NAME	= ngx_http_acme_module
# Target name used by cargo build.
TARGET_NAME	= nginx_acme

# build/build-%.mk: debug, debug-static, release, release-static, sanitize, ...
BUILD		?= debug
TESTS		?= t/
NGX_CARGO	?= cargo

# extra arguments (e.g. --features) for cargo build
CARGO_BUILD_ARGS ?=
CARGO_PROFILE = debug

# the actual profile name for target/debug is "dev" (rust-lang/cargo#13495)
CARGO_PROFILE_ARG = --profile="$$(echo '$(CARGO_PROFILE)' | sed 's/^debug$$/dev/')"
# build directory containing cargo built modules
CARGO_TARGET_SUBDIR = target/$(CARGO_PROFILE)

# release archives have ./configure instead of auto/configure
NGINX_CONFIGURE	= $$(test -f configure && echo ./configure || echo auto/configure)

NGINX_CONFIGURE_ARGS	= \
	--with-http_ssl_module \
	--with-http_v2_module \
	--with-pcre \
	--with-stream \
	--with-stream_ssl_module \
	--with-compat \
	$(NGINX_EXTRA_CONFIGURE_ARGS)

MODULE_SOURCE_DIR	= $(CURDIR)
NGINX_SOURCE_DIR	?= ../nginx
NGINX_TESTS_DIR		?= $(NGINX_SOURCE_DIR)/tests
NGINX_BUILD_DIR		?= $(CURDIR)/objs-$(BUILD)

TEST_NGINX_BINARY	= $(NGINX_BUILD_DIR)/nginx
TEST_NGINX_GLOBALS	+= env RUST_BACKTRACE;

# Shared libraries generated with --add-dynamic-module=.
NGINX_BUILT_MODULE	= $(NGINX_BUILD_DIR)/$(MODULE_NAME).so
# Shared libraries generated by cargo build.
CARGO_BUILT_MODULE	= $(CARGO_TARGET_SUBDIR)/lib$(TARGET_NAME)$(SHLIB_EXT)

# "load_module" directives for test globals
LOAD_NGINX_BUILT_MODULE = load_module $(NGINX_BUILT_MODULE);
LOAD_CARGO_BUILT_MODULE = load_module $(CURDIR)/$(CARGO_BUILT_MODULE);

# Clearing this var allows to skip the build step: "make test TEST_PREREQ="
TEST_PREREQ = build


.PHONY: all

all: ## Build, lint and test the module
all: build check test


include	build/compat.mk
include	build/build-$(BUILD).mk


# Set up environment propagation

BUILD_ENV	+= NGINX_SOURCE_DIR="$(NGINX_SOURCE_DIR)"
BUILD_ENV	+= NGINX_BUILD_DIR="$(NGINX_BUILD_DIR)"

TEST_ENV	+= RUST_BACKTRACE=1
TEST_ENV	+= TEST_NGINX_BINARY="$(TEST_NGINX_BINARY)"
TEST_ENV	+= TEST_NGINX_GLOBALS="$(TEST_NGINX_GLOBALS)"


# Build targets

.NOTPARALLEL: modules

.PHONY: help build modules check unittest test full-test clean

help:
	@echo "Available targets:"
	@awk 'BEGIN {FS = ":.*?## "}; /^[ a-zA-Z_-]+:.*?## .*$$/ {printf "%-16s %s\n", $$1, $$2}' \
		Makefile $(MAKEFILE_LIST) | sort -u
	@echo "Pass BUILD=<configuration> to any target for desired build configuration."
	@echo "Pass NGINX_SOURCE_DIR to specify path to your NGINX source checkout."
	@echo "Pass NGINX_EXTRA_CONFIGURE_ARGS to specify additional arguments for auto/configure."

# A helper to trigger rebuild of targets managed by external build tools
FORCE: ;

$(NGINX_BUILD_DIR)/Makefile: $(MODULE_SOURCE_DIR)/config
$(NGINX_BUILD_DIR)/Makefile: $(MODULE_SOURCE_DIR)/config.make
$(NGINX_BUILD_DIR)/Makefile: $(MODULE_SOURCE_DIR)/auto/rust
$(NGINX_BUILD_DIR)/Makefile: $(NGINX_SOURCE_DIR)/src/core/nginx.h
	cd $(NGINX_SOURCE_DIR) \
		&& $(BUILD_ENV) $(NGINX_CONFIGURE) $(NGINX_CONFIGURE_ARGS) \
			--builddir=$(NGINX_BUILD_DIR)
	-rm -f $(NGINX_SOURCE_DIR)/Makefile

$(TEST_NGINX_BINARY): $(NGINX_BUILD_DIR)/Makefile FORCE
	cd $(NGINX_SOURCE_DIR) \
		&& $(BUILD_ENV) $(MAKE) -f $(NGINX_BUILD_DIR)/Makefile binary

$(NGINX_BUILT_MODULE): $(NGINX_BUILD_DIR)/Makefile FORCE
	cd $(NGINX_SOURCE_DIR) \
		&& $(BUILD_ENV) $(MAKE) -f $(NGINX_BUILD_DIR)/Makefile -j 1 modules

$(CARGO_BUILT_MODULE): $(NGINX_BUILD_DIR)/Makefile FORCE
	$(BUILD_ENV) $(NGX_CARGO) build $(CARGO_PROFILE_ARG) $(CARGO_BUILD_ARGS)

modules:

build: $(TEST_NGINX_BINARY) modules ## Build the module

check: $(NGINX_BUILD_DIR)/Makefile ## Check style and lint
	$(BUILD_ENV) $(NGX_CARGO) fmt --all --check
	$(BUILD_ENV) $(NGX_CARGO) clippy --workspace --all-targets --verbose -- -D warnings

unittest: $(NGINX_BUILD_DIR)/Makefile  ## Run unit-tests
	$(BUILD_ENV) $(TEST_ENV) $(NGX_CARGO) test $(CARGO_PROFILE_ARG) --workspace

PROVE = env $(TEST_ENV) prove -I $(NGINX_TESTS_DIR)/lib

test: $(TEST_PREREQ) ## Run the integration test suite
	if [ 0$(TEST_JOBS) -gt 1 ]; then \
		$(PROVE) --state=save -j $(TEST_JOBS) $(TESTS) || \
		$(PROVE) --state=failed -v; \
	else \
		$(PROVE) -v $(TESTS); \
	fi

full-test: $(TEST_PREREQ) ## Run the module and NGINX integration test suites
	# --statefile first appeared in Test-Harness-3.40_01, Jul 23, 2017
	$(PROVE) --statefile=".prove-full" --state=save -j $(TEST_JOBS) \
		$(TESTS) $(NGINX_TESTS_DIR) || \
	$(PROVE) --statefile=".prove-full" --state=failed -v

clean: ## Clean build artifacts for the specified configuration
	rm -rf $(NGINX_BUILD_DIR) $(CARGO_TARGET_SUBDIR)
	$(NGX_CARGO) clean $(CARGO_PROFILE_ARG)

# GNU make convenience targets. Will be ignored by other implementations.

build-%: ## Build with the specified configuration. E.g. make build-sanitize.
	$(MAKE) build BUILD="$*"

test-%: ## Test with the specified configuration.
	$(MAKE) test BUILD="$*"
