# Makefile

# SPDX-License-Identifier: MIT
# Copyright (c) The leveldb-rs-binding authors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

EXTENSION_SRC_DIR := extension/src
EXTENSION_TEST_DIR := extension/tests
EXTENSION_INCLUDE_DIR := extension/include
EXTENSION_SRC_FILES := $(shell find $(EXTENSION_SRC_DIR) -name "*.cpp")
EXTENSION_TEST_FILES := $(shell find $(EXTENSION_TEST_DIR) -name "*.cpp")
EXTENSION_INCLUDE_FILES := $(shell find $(EXTENSION_INCLUDE_DIR) -name "*.h")

.PHONY: lint
lint: format fix clippy clang-tidy

.PHONY: clippy
clippy:
	cargo clippy --features ci -- -D warnings

.PHONY: clippy-src
clippy-src:
	cargo clippy --manifest-path Cargo.toml --features ci -- -D warnings

.PHONY: clippy-benchmarks
clippy-benchmarks:
	cargo clippy --manifest-path benchmarks/Cargo.toml -- -D warnings

.PHONY: clippy-example
clippy-example:
	cargo clippy --manifest-path example/http_server/Cargo.toml -- -D warnings
	cargo clippy --manifest-path example/simple_app/Cargo.toml -- -D warnings

.PHONY: clang-tidy
clang-tidy:
	clang-tidy $(EXTENSION_SRC_FILES) $(EXTENSION_TEST_FILES) --extra-arg=-std=c++17 --warnings-as-errors='*' -- -I./extension/include -I./deps/leveldb/include

.PHONY: format
format: format-src format-benchmarks format-example format-extension

.PHONY: format-src
format-src:
	cargo fmt --manifest-path Cargo.toml

.PHONY: format-benchmarks
format-benchmarks:
	cargo fmt --manifest-path benchmarks/Cargo.toml

.PHONY: format-example
format-example:
	cargo fmt --manifest-path example/http_server/Cargo.toml
	cargo fmt --manifest-path example/simple_app/Cargo.toml

.PHONY: format-extension
format-extension:
	clang-format -i $(EXTENSION_SRC_FILES) $(EXTENSION_TEST_FILES) $(EXTENSION_INCLUDE_FILES)

.PHONY: fix
fix: fix-src fix-benchmarks fix-example

.PHONY: fix-src
fix-src:
	cargo fix --all-targets --allow-dirty --manifest-path Cargo.toml

.PHONY: fix-benchmarks
fix-benchmarks:
	cargo fix --all-targets --allow-dirty --manifest-path benchmarks/Cargo.toml

.PHONY: fix-example
fix-example:
	cargo fix --all-targets --allow-dirty --manifest-path example/http_server/Cargo.toml
	cargo fix --all-targets --allow-dirty --manifest-path example/simple_app/Cargo.toml

# Directory-specific lint targets
.PHONY: lint-src
lint-src: format-src fix-src clippy-src

.PHONY: lint-benchmarks
lint-benchmarks: format-benchmarks fix-benchmarks clippy-benchmarks

.PHONY: lint-example
lint-example: format-example fix-example clippy-example

.PHONY: lint-extension
lint-extension: format-extension clang-tidy

# Extension build and test targets
.PHONY: test-extension
test-extension:
	@echo "Testing extensions..."
	@if [ ! -d "extension/build" ]; then \
		echo "Extension build directory not found. Please run 'make build-extension' first."; \
		exit 1; \
	fi; \
	if [ ! -f "extension/build/logger_test" ]; then \
		echo "Test executable not found. Please run 'make build-extension' first."; \
		exit 1; \
	fi; \
	cd extension/build && ctest --output-on-failure

.PHONY: build-extension
build-extension:
	@echo "Building extensions..."
	@mkdir -p extension/build
	@cd extension/build && \
	LEVELDB_BUILD_DIR=$$(find ../../target/debug/build -name "leveldb-rs-binding-*" -type d -exec test -f "{}/out/lib/libleveldb.a" \; -print | sort | tail -1)/out && \
	LEVELDB_BUILD_DIR=$$(realpath $$LEVELDB_BUILD_DIR) && \
	if [ ! -d "$$LEVELDB_BUILD_DIR" ]; then \
		echo "LevelDB not found in target directories. Please run 'cargo build' first."; \
		exit 1; \
	fi; \
	echo "Using LevelDB from: $$LEVELDB_BUILD_DIR"; \
	cmake .. \
		-DCMAKE_CXX_STANDARD=17 \
		-DCMAKE_CXX_STANDARD_REQUIRED=ON \
		-DLEVELDB_INSTALL_DIR="$$LEVELDB_BUILD_DIR" \
		-DBUILD_TESTS=ON && \
	cmake --build .

.PHONY: clean-extension
clean-extension:
	@echo "Cleaning extension build directory..."
	@rm -rf extension/build
