# The Mojibake library
#
# This file is distributed under the MIT License. See LICENSE for details.

BUILD_DIR ?= build

# Build directories
CPP_BUILD_DIR ?= $(BUILD_DIR)-cpp
SHARED_BUILD_DIR ?= $(BUILD_DIR)-shared
ASAN_BUILD_DIR ?= $(BUILD_DIR)-asan
UBSAN_BUILD_DIR ?= $(BUILD_DIR)-ubsan
TIDY_BUILD_DIR ?= $(BUILD_DIR)-tidy

# Build test directories
TEST_BUILD_DIR ?= $(BUILD_DIR)-test
TEST_CPP_BUILD_DIR ?= $(BUILD_DIR)-test-cpp
TEST_RELEASE_BUILD_DIR ?= $(BUILD_DIR)-test-release
TEST_CPP_RELEASE_BUILD_DIR ?= $(BUILD_DIR)-test-cpp-release
TEST_MINIMAL_BUILD_DIR ?= $(BUILD_DIR)-test-minimal
TEST_CPP_MINIMAL_BUILD_DIR ?= $(BUILD_DIR)-test-cpp-minimal
TEST_ASAN_BUILD_DIR ?= $(BUILD_DIR)-test-asan
TEST_UBSAN_BUILD_DIR ?= $(BUILD_DIR)-test-ubsan

# Feature flags for test builds
TEST_NO_NAMES_BUILD_DIR ?= $(BUILD_DIR)-test-no-names
TEST_NO_COLLATION_BUILD_DIR ?= $(BUILD_DIR)-test-no-collation
TEST_NO_IDNA_BUILD_DIR ?= $(BUILD_DIR)-test-no-idna
TEST_NO_SECURITY_BUILD_DIR ?= $(BUILD_DIR)-test-no-security

# WASM and amalgamation build directories
WASM_BUILD_DIR ?= build-wasm
AMALGAMATION_BUILD_DIR ?= build-amalgamation

# CMake build flags
BUILD_TYPE ?= Release
TEST_BUILD_TYPE ?= Test

# Feature flags
FEATURE_CHARACTER_NAMES ?= ON
FEATURE_COLLATION ?= ON
FEATURE_IDNA ?= ON
FEATURE_SECURITY ?= ON
WARNINGS_AS_ERRORS ?= OFF

CMAKE_FEATURE_FLAGS = \
	-DMJB_FEATURE_CHARACTER_NAMES=$(FEATURE_CHARACTER_NAMES) \
	-DMJB_FEATURE_COLLATION=$(FEATURE_COLLATION) \
	-DMJB_FEATURE_IDNA=$(FEATURE_IDNA) \
	-DMJB_FEATURE_SECURITY=$(FEATURE_SECURITY)
CMAKE_NATIVE_BASE_FLAGS = -DMJB_BUILD_WASM=OFF \
	-DMJB_WARNINGS_AS_ERRORS=$(WARNINGS_AS_ERRORS) $(CMAKE_FEATURE_FLAGS)
NATIVE_CMAKE_FLAGS = -DMJB_BUILD_CPP=OFF -DBUILD_SHARED_LIBS=OFF $(CMAKE_NATIVE_BASE_FLAGS) \
	-DMJB_USE_ASAN=OFF -DMJB_USE_UBSAN=OFF
CPP_CMAKE_FLAGS = -DMJB_BUILD_CPP=ON -DBUILD_SHARED_LIBS=OFF $(CMAKE_NATIVE_BASE_FLAGS) \
	-DMJB_USE_ASAN=OFF -DMJB_USE_UBSAN=OFF
SHARED_CMAKE_FLAGS = -DMJB_BUILD_CPP=OFF -DBUILD_SHARED_LIBS=ON $(CMAKE_NATIVE_BASE_FLAGS) \
	-DMJB_USE_ASAN=OFF -DMJB_USE_UBSAN=OFF
ASAN_CMAKE_FLAGS = -DMJB_BUILD_CPP=OFF -DBUILD_SHARED_LIBS=OFF $(CMAKE_NATIVE_BASE_FLAGS) \
	-DMJB_USE_ASAN=ON -DMJB_USE_UBSAN=OFF
UBSAN_CMAKE_FLAGS = -DMJB_BUILD_CPP=OFF -DBUILD_SHARED_LIBS=OFF $(CMAKE_NATIVE_BASE_FLAGS) \
	-DMJB_USE_ASAN=OFF -DMJB_USE_UBSAN=ON
WASM_CMAKE_FLAGS = -DMJB_BUILD_CPP=OFF -DBUILD_SHARED_LIBS=OFF -DMJB_BUILD_WASM=ON \
	-DMJB_USE_ASAN=OFF -DMJB_USE_UBSAN=OFF \
	-DMJB_WARNINGS_AS_ERRORS=$(WARNINGS_AS_ERRORS) $(CMAKE_FEATURE_FLAGS)

# Source files that trigger regeneration.
GENERATE_SOURCES = \
	utils/generate/commands/* \
	utils/generate/file-generators/* \
	utils/generate/locales/*.ts \
	utils/generate/parse-ucd/*.ts \
	utils/generate/scripts/* \
	utils/generate/*.json \
	utils/generate/*.ts \

UNICODE_DATA = src/unicode-data.h

# Static analysis with clang-tidy, configured by .clang-tidy
CLANG_TIDY ?= clang-tidy
CLANG_TIDY_CMAKE_FLAGS ?=

all: configure build

.PHONY: all configure configure-cpp configure-shared configure-asan configure-ubsan \
	configure-wasm

# C targets
configure: $(UNICODE_DATA)
	@cmake -S . -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) $(NATIVE_CMAKE_FLAGS)

# C++ targets
configure-cpp: $(UNICODE_DATA)
	@cmake -S . -B $(CPP_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) $(CPP_CMAKE_FLAGS)

# Shared library targets
configure-shared: $(UNICODE_DATA)
	@cmake -S . -B $(SHARED_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) $(SHARED_CMAKE_FLAGS)

# AddressSanitizer targets
configure-asan: $(UNICODE_DATA)
	@cmake -S . -B $(ASAN_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) $(ASAN_CMAKE_FLAGS)

# UndefinedBehaviorSanitizer targets
configure-ubsan: $(UNICODE_DATA)
	@cmake -S . -B $(UBSAN_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) $(UBSAN_CMAKE_FLAGS)

# WASM targets
configure-wasm: $(UNICODE_DATA)
	@emcmake cmake -S . -B $(WASM_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) $(WASM_CMAKE_FLAGS)

.PHONY: build build-cpp build-shared build-asan build-ubsan build-wasm

# C targets
build: configure
	@cmake --build $(BUILD_DIR) --config $(BUILD_TYPE)

# C++ targets
build-cpp: configure-cpp
	@cmake --build $(CPP_BUILD_DIR) --config $(BUILD_TYPE)

# Shared library targets
build-shared: configure-shared
	@cmake --build $(SHARED_BUILD_DIR) --config $(BUILD_TYPE)

build-asan: configure-asan
	@cmake --build $(ASAN_BUILD_DIR) --config $(BUILD_TYPE)

build-ubsan: configure-ubsan
	@cmake --build $(UBSAN_BUILD_DIR) --config $(BUILD_TYPE)

# WASM targets
build-wasm: configure-wasm
	@cd $(WASM_BUILD_DIR) && emmake make

.PHONY: generate generate-locale generate-unicode-tables \
		generate-site sync-api-wasm watch-site \
		watch-api wasm coverage amalgamation update-version build-api

# Generate source files
generate: $(GENERATE_SOURCES)
	@cd ./utils/generate && ./scripts/generate.sh $(ARGS)

# Generate locale files
generate-locale:
	@cd ./utils/generate && npm run generate -- generate-locale

# Generate embedded Unicode lookup tables
generate-unicode-tables:
	@cd ./utils/generate && npm run generate -- unicode-tables

generate-site: src/site/index.html
	@cd ./utils/generate && npm run generate-site

sync-api-wasm: build-wasm
	@cp $(WASM_BUILD_DIR)/src/mojibake.js src/wasm/mojibake.js
	@cp $(WASM_BUILD_DIR)/src/mojibake.wasm src/wasm/mojibake.wasm

build-api: sync-api-wasm
	@cd ./src/wasm && npm run build

# Watch site files
watch-site: build-api src/site/index.html
	@cd ./utils/generate && npm run watch-site

# Watch API files
watch-api: sync-api-wasm
	cd ./src/wasm && npm run dev

# Generate WASM library
wasm: sync-api-wasm generate-site

# Generate TESTS.md file
coverage: test-data
	@cmake -S . -B $(TEST_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(TEST_BUILD_TYPE) $(NATIVE_CMAKE_FLAGS)
	@cmake --build $(TEST_BUILD_DIR) --config $(TEST_BUILD_TYPE)
	@cd ./utils/generate && npm run coverage -- \
		--test-executable $(TEST_BUILD_DIR)/tests/mojibake-test

# Generate amalgamation
amalgamation:
	@cd ./utils/generate && ./scripts/generate-amalgamation.sh

# Rule for generated Unicode data
$(UNICODE_DATA): $(GENERATE_SOURCES)
	@cd ./utils/generate && ./scripts/generate.sh $(ARGS)

# Update version in source files
update-version:
	@cd ./utils/generate && npm run generate -- update-version

.PHONY: lint tidy configure-tidy

# Check C and C++ formatting with Apple's clang-format
lint:
	@git ls-files -z '*.c' '*.h' '*.cpp' '*.hpp' ':!tests/attractor.h' | \
		xargs -0 xcrun clang-format --dry-run --Werror

# CMake 4 no longer sets a default macOS SDK, which non-Apple clang-tidy builds need
configure-tidy: $(UNICODE_DATA)
	@command -v $(CLANG_TIDY) >/dev/null 2>&1 || { \
		echo "clang-tidy not found. Install LLVM (brew install llvm, apt-get install clang-tidy)" \
			"or set CLANG_TIDY=/path/to/clang-tidy."; exit 1; }
	@sysroot=""; \
	if [ "$$(uname -s)" = Darwin ]; then \
		sysroot="-DCMAKE_OSX_SYSROOT=$$(xcrun --show-sdk-path)"; \
	fi; \
	cmake -S . -B $(TIDY_BUILD_DIR) -DCMAKE_BUILD_TYPE=Release $(NATIVE_CMAKE_FLAGS) \
		-DMJB_BUILD_TESTS=ON -DCMAKE_C_CLANG_TIDY=$(CLANG_TIDY) $$sysroot $(CLANG_TIDY_CMAKE_FLAGS)

# Run clang-tidy on the library, CLI, and tests while compiling them
tidy: configure-tidy
	@cmake --build $(TIDY_BUILD_DIR) --config Release --parallel

.PHONY: test-data

# Download any missing Unicode test data into unicode-data/. Files already present are kept, so this
# needs no network access once the data is there.
test-data:
	@./utils/generate/scripts/download-test-data.sh

.PHONY: test test-all test-native test-optimized test-release test-cpp-release \
	test-features test-sanitizers test-minimal test-cpp test-cpp-minimal test-asan \
	test-ubsan test-no-names test-no-collation test-no-idna test-no-security test-wasm \
	ctest ctest-cpp test-rust vendor-rust package-rust test-elixir vendor-elixir \
	package-elixir test-go test-docker

# Run tests
test: $(UNICODE_DATA) test-data
	@cmake -S . -B $(TEST_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(TEST_BUILD_TYPE) $(NATIVE_CMAKE_FLAGS)
	@cmake --build $(TEST_BUILD_DIR) --config $(TEST_BUILD_TYPE)
	$(TEST_BUILD_DIR)/tests/mojibake-test $(ARGS)

# Run tests without Unicode character name tables
test-no-names:
	$(MAKE) test TEST_BUILD_DIR=$(TEST_NO_NAMES_BUILD_DIR) FEATURE_CHARACTER_NAMES=OFF ARGS="$(ARGS)"

test-no-collation:
	$(MAKE) test TEST_BUILD_DIR=$(TEST_NO_COLLATION_BUILD_DIR) FEATURE_COLLATION=OFF ARGS="$(ARGS)"

test-no-idna:
	$(MAKE) test TEST_BUILD_DIR=$(TEST_NO_IDNA_BUILD_DIR) FEATURE_IDNA=OFF ARGS="$(ARGS)"

test-no-security:
	$(MAKE) test TEST_BUILD_DIR=$(TEST_NO_SECURITY_BUILD_DIR) FEATURE_SECURITY=OFF ARGS="$(ARGS)"

# Run tests with C++ compiler
test-cpp: $(UNICODE_DATA) test-data
	@cmake -S . -B $(TEST_CPP_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(TEST_BUILD_TYPE) $(CPP_CMAKE_FLAGS)
	@cmake --build $(TEST_CPP_BUILD_DIR) --config $(TEST_BUILD_TYPE)
	$(TEST_CPP_BUILD_DIR)/tests/mojibake-test $(ARGS)

# Run optimized C tests
test-release: $(UNICODE_DATA) test-data
	@cmake -S . -B $(TEST_RELEASE_BUILD_DIR) -DCMAKE_BUILD_TYPE=Release $(NATIVE_CMAKE_FLAGS)
	@cmake --build $(TEST_RELEASE_BUILD_DIR) --config Release
	$(TEST_RELEASE_BUILD_DIR)/tests/mojibake-test $(ARGS)

# Run optimized C++ tests
test-cpp-release: $(UNICODE_DATA) test-data
	@cmake -S . -B $(TEST_CPP_RELEASE_BUILD_DIR) -DCMAKE_BUILD_TYPE=Release $(CPP_CMAKE_FLAGS)
	@cmake --build $(TEST_CPP_RELEASE_BUILD_DIR) --config Release
	$(TEST_CPP_RELEASE_BUILD_DIR)/tests/mojibake-test $(ARGS)

# Run tests with all optional features disabled
test-minimal:
	$(MAKE) test TEST_BUILD_DIR=$(TEST_MINIMAL_BUILD_DIR) FEATURE_CHARACTER_NAMES=OFF \
		FEATURE_COLLATION=OFF FEATURE_IDNA=OFF FEATURE_SECURITY=OFF ARGS="$(ARGS)"

# Run C++ tests with all optional features disabled
test-cpp-minimal:
	$(MAKE) test-cpp TEST_CPP_BUILD_DIR=$(TEST_CPP_MINIMAL_BUILD_DIR) \
		FEATURE_CHARACTER_NAMES=OFF FEATURE_COLLATION=OFF FEATURE_IDNA=OFF \
		FEATURE_SECURITY=OFF ARGS="$(ARGS)"

# Rebuild and sync the WASM artifacts, then build the API and run its Node and browser tests
test-wasm: sync-api-wasm
	@cd ./src/wasm && npm run test

# Run tests with AddressSanitizer
test-asan: $(UNICODE_DATA) test-data
	@cmake -S . -B $(TEST_ASAN_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(TEST_BUILD_TYPE) $(ASAN_CMAKE_FLAGS)
	@cmake --build $(TEST_ASAN_BUILD_DIR) --config $(TEST_BUILD_TYPE)
	$(TEST_ASAN_BUILD_DIR)/tests/mojibake-test $(ARGS)

# Run tests with UndefinedBehaviorSanitizer
test-ubsan: $(UNICODE_DATA) test-data
	@cmake -S . -B $(TEST_UBSAN_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(TEST_BUILD_TYPE) $(UBSAN_CMAKE_FLAGS)
	@cmake --build $(TEST_UBSAN_BUILD_DIR) --config $(TEST_BUILD_TYPE)
	$(TEST_UBSAN_BUILD_DIR)/tests/mojibake-test $(ARGS)

# Run the default C and C++ configurations
test-native:
	$(MAKE) test
	$(MAKE) test-cpp

# Run optimized C and C++ configurations
test-optimized:
	$(MAKE) test-release
	$(MAKE) test-cpp-release

# Run optional-feature configurations
test-features:
	$(MAKE) test-no-names
	$(MAKE) test-no-collation
	$(MAKE) test-no-idna
	$(MAKE) test-no-security
	$(MAKE) test-minimal
	$(MAKE) test-cpp-minimal

# Run sanitizer configurations
test-sanitizers:
	$(MAKE) test-asan
	$(MAKE) test-ubsan

# Run all local test configurations
test-all:
	$(MAKE) test-native
	$(MAKE) test-optimized
	$(MAKE) test-features
	$(MAKE) test-sanitizers

# Run tests using CTest
ctest: $(UNICODE_DATA) test-data
	@cmake -S . -B $(TEST_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(TEST_BUILD_TYPE) $(NATIVE_CMAKE_FLAGS)
	@cmake --build $(TEST_BUILD_DIR) --config $(TEST_BUILD_TYPE)
	cd $(TEST_BUILD_DIR) && ctest -C $(TEST_BUILD_TYPE) $(ARGS)

# Run C++ tests using CTest
ctest-cpp: $(UNICODE_DATA) test-data
	@cmake -S . -B $(TEST_CPP_BUILD_DIR) -DCMAKE_BUILD_TYPE=$(TEST_BUILD_TYPE) $(CPP_CMAKE_FLAGS)
	@cmake --build $(TEST_CPP_BUILD_DIR) --config $(TEST_BUILD_TYPE)
	cd $(TEST_CPP_BUILD_DIR) && ctest -C $(TEST_BUILD_TYPE) $(ARGS)

# Run the Rust crates' tests, with every optional feature (and serde) on and then off, plus clippy,
# rustfmt, and warning-free docs
test-rust:
	cd src/rust && cargo fmt --check
	cd src/rust && cargo clippy --all-targets --all-features -- -D warnings
	cd src/rust && cargo clippy --all-targets -p mojibake-rs --no-default-features -- -D warnings
	cd src/rust && cargo test --all-features
	cd src/rust && cargo test -p mojibake-rs --no-default-features
	cd src/rust && cargo rustdoc -p mojibake-sys -- -D warnings
	cd src/rust && cargo rustdoc -p mojibake-rs --all-features -- -D warnings

# Copy the C sources into src/rust/mojibake-sys/csrc (git-ignored) so the crate is self-contained
vendor-rust:
	@rm -rf src/rust/mojibake-sys/csrc
	@mkdir -p src/rust/mojibake-sys/csrc
	@cp src/*.c src/*.h src/CMakeLists.txt LICENSE src/rust/mojibake-sys/csrc/

# Package both Rust crates and verify that each builds from its .crate file. Nothing is published.
# Cargo counts the git-ignored csrc/ copy as uncommitted, so --allow-dirty is always passed. The
# check below stands in for Cargo's: the packaged sources must be committed, unless ALLOW_DIRTY=1
package-rust: vendor-rust
	@if [ "$(ALLOW_DIRTY)" != "1" ] && \
		[ -n "$$(git status --porcelain --untracked-files=all -- src LICENSE)" ]; then \
		git status --short --untracked-files=all -- src LICENSE; \
		echo "Commit these changes first, or set ALLOW_DIRTY=1"; \
		exit 1; \
	fi
	cd src/rust && cargo package --workspace --locked --allow-dirty $(ARGS)
	cd src/rust && cargo publish --workspace --locked --dry-run --allow-dirty $(ARGS)

# Run the Elixir package's tests with every optional feature on and then off, plus formatting,
# warning-free Elixir and NIF compilation, and warning-free docs. Needs Elixir and Erlang/OTP.
ELIXIR_CFLAGS ?= -O2 -Werror

test-elixir: test-data
	cd src/elixir && mix deps.get
	cd src/elixir && mix format --check-formatted
	cd src/elixir && CFLAGS="$(ELIXIR_CFLAGS)" mix test --warnings-as-errors $(ARGS)
	cd src/elixir && CFLAGS="$(ELIXIR_CFLAGS)" MOJIBAKE_FEATURES=none \
		mix test --warnings-as-errors $(ARGS)
	cd src/elixir && CFLAGS="$(ELIXIR_CFLAGS)" mix docs --warnings-as-errors

# Run the Go package's tests, conformance files included, with every optional feature on and then
# off, plus gofmt, go vet, and warning-free cgo compilation. Needs Go and a C compiler.
GO ?= go
GO_CFLAGS ?= -O2 -g -Wall -Werror
GO_NO_FEATURES = mojibake_no_names,mojibake_no_collation,mojibake_no_idna,mojibake_no_security
# Go's build cache tracks only the files in src/go, not the C sources that src/go/mojibake.c
# includes from src/, so a checksum of them goes into CGO_CFLAGS, which is part of the cache key
GO_SOURCES_SUM = $(shell cat src/*.c src/*.h | cksum | cut -d ' ' -f 1)
GO_CGO_CFLAGS = $(GO_CFLAGS) -DMJB_GO_SOURCES_SUM=$(GO_SOURCES_SUM)

test-go: test-data
	@unformatted="$$(gofmt -l src/go examples/go)"; \
		if [ -n "$$unformatted" ]; then echo "gofmt needed:"; echo "$$unformatted"; exit 1; fi
	CGO_CFLAGS="$(GO_CGO_CFLAGS)" $(GO) vet ./src/go
	CGO_CFLAGS="$(GO_CGO_CFLAGS)" $(GO) vet -tags $(GO_NO_FEATURES) ./src/go
	CGO_CFLAGS="$(GO_CGO_CFLAGS)" $(GO) test $(ARGS) ./src/go
	CGO_CFLAGS="$(GO_CGO_CFLAGS)" $(GO) test -tags $(GO_NO_FEATURES) $(ARGS) ./src/go
	cd examples/go && CGO_CFLAGS="$(GO_CGO_CFLAGS)" $(GO) vet .

# Copy the C sources into src/elixir/c_src/mojibake and the license into src/elixir (both
# git-ignored) so the Hex package is self-contained
vendor-elixir:
	@rm -rf src/elixir/c_src/mojibake
	@mkdir -p src/elixir/c_src/mojibake
	@cp src/*.c src/*.h src/CMakeLists.txt src/elixir/c_src/mojibake/
	@cp LICENSE src/elixir/LICENSE

# Build the Hex package, then unpack it outside src/ and check that it compiles and loads from the
# vendored sources alone; MOJIBAKE_BUILD=1 skips the precompiled NIF. Nothing is published. The
# packaged sources must be committed, unless ALLOW_DIRTY=1
ELIXIR_PACKAGE_DIR = build-elixir-package

package-elixir: vendor-elixir
	@if [ "$(ALLOW_DIRTY)" != "1" ] && \
		[ -n "$$(git status --porcelain --untracked-files=all -- src LICENSE)" ]; then \
		git status --short --untracked-files=all -- src LICENSE; \
		echo "Commit these changes first, or set ALLOW_DIRTY=1"; \
		exit 1; \
	fi
	@rm -rf build-elixir-package
	cd src/elixir && mix deps.get && mix hex.build --unpack -o ../../$(ELIXIR_PACKAGE_DIR)
	cd $(ELIXIR_PACKAGE_DIR) && MIX_ENV=prod mix deps.get --only prod
	cd $(ELIXIR_PACKAGE_DIR) && env -u MOJIBAKE_SRC_DIR MOJIBAKE_BUILD=1 MIX_ENV=prod \
		mix run --no-start -e 'IO.puts("Mojibake " <> Mojibake.version())'

# Run tests in Docker container
test-docker:
	docker build -t mojibake .
	docker run mojibake

# Fuzz the public API with libFuzzer in a container (requires Docker)
FUZZ_TIME ?= 60

.PHONY: fuzz

fuzz:
	docker build -f fuzz/Dockerfile -t mojibake-fuzz .
	docker run --rm -e FUZZ_TIME=$(FUZZ_TIME) mojibake-fuzz

.PHONY: clean-build clean-native clean-wasm clean-amalgamation clean-unicode-data clean

# Clean targets
clean-build:
	@cmake --build $(BUILD_DIR) --config $(BUILD_TYPE) --target clean

# Clean native build
clean-native:
	@rm -rf $(BUILD_DIR) $(CPP_BUILD_DIR) $(SHARED_BUILD_DIR) $(ASAN_BUILD_DIR) \
		$(UBSAN_BUILD_DIR) $(TEST_BUILD_DIR) $(TEST_CPP_BUILD_DIR) \
		$(TEST_RELEASE_BUILD_DIR) $(TEST_CPP_RELEASE_BUILD_DIR) $(TEST_MINIMAL_BUILD_DIR) \
		$(TEST_CPP_MINIMAL_BUILD_DIR) $(TEST_ASAN_BUILD_DIR) $(TEST_UBSAN_BUILD_DIR) \
		$(TEST_NO_NAMES_BUILD_DIR) $(TEST_NO_COLLATION_BUILD_DIR) $(TEST_NO_IDNA_BUILD_DIR) \
		$(TEST_NO_SECURITY_BUILD_DIR) $(TIDY_BUILD_DIR)

# Clean WASM build
clean-wasm:
	@rm -rf $(WASM_BUILD_DIR)

# Clean amalgamation build
clean-amalgamation:
	@rm -rf $(AMALGAMATION_BUILD_DIR)

# Clean downloaded Unicode data
clean-unicode-data:
	@rm -rf unicode-data

clean: clean-native clean-wasm clean-amalgamation clean-unicode-data

.PHONY: help

help:
	@echo "Available targets:"
	@echo "  all                     - Build the project (default)"
	@echo "  amalgamation            - Generate single-file amalgamation"
	@echo "  build-asan              - Build the project with AddressSanitizer"
	@echo "  build-cpp               - Build the project with C++ compiler"
	@echo "  build-shared            - Build the project as a shared library"
	@echo "  build-ubsan             - Build the project with UndefinedBehaviorSanitizer"
	@echo "  build-wasm              - Build the project for WebAssembly"
	@echo "  build-api               - Build the JavaScript API library"
	@echo "  clean                   - Remove build artifacts and downloaded Unicode data"
	@echo "  clean-amalgamation      - Remove amalgamation build artifacts"
	@echo "  clean-build             - Remove build artifacts"
	@echo "  clean-native            - Remove all build artifacts"
	@echo "  clean-unicode-data      - Remove downloaded Unicode data"
	@echo "  clean-wasm              - Remove WASM build artifacts"
	@echo "  coverage                - Run coverage analysis"
	@echo "  ctest                   - Build and run tests using CTest"
	@echo "  ctest-cpp               - Build and run C++ tests using CTest"
	@echo "  test-rust               - Test the Rust crates in src/rust with cargo"
	@echo "  vendor-rust             - Copy the C sources into the mojibake-sys crate"
	@echo "  package-rust            - Vendor, package and dry-run publish the Rust crates"
	@echo "  test-elixir             - Test the Elixir package in src/elixir with mix"
	@echo "  vendor-elixir           - Copy the C sources into the Elixir package"
	@echo "  package-elixir          - Vendor, build and check the Elixir Hex package"
	@echo "  test-go                 - Test the Go package in src/go with go test"
	@echo "  generate                - Regenerate source files"
	@echo "  generate-locale         - Generate locale file"
	@echo "  generate-site           - Generate site"
	@echo "  generate-unicode-tables - Generate embedded Unicode lookup tables"
	@echo "  lint                    - Check C and C++ formatting with Apple's clang-format"
	@echo "  tidy                    - Run clang-tidy on the library, CLI, and tests"
	@echo "  sync-api-wasm           - Copy current WASM build artifacts into src/wasm"
	@echo "  test                    - Build and run tests"
	@echo "  test-all                - Build and run all local test configurations"
	@echo "  test-asan               - Build and run tests with AddressSanitizer"
	@echo "  test-cpp                - Build and run tests with C++ compiler"
	@echo "  test-cpp-minimal        - Build and run C++ tests without optional features"
	@echo "  test-cpp-release        - Build and run optimized C++ tests"
	@echo "  test-data               - Download any missing Unicode test data"
	@echo "  test-docker             - Build and run tests in Docker container"
	@echo "  test-features           - Build and run optional-feature configurations"
	@echo "  test-minimal            - Build and run tests without optional features"
	@echo "  test-native             - Build and run the default C and C++ configurations"
	@echo "  test-no-collation       - Build and run tests without Unicode collation support"
	@echo "  test-no-names           - Build and run tests without Unicode character names"
	@echo "  test-no-idna            - Build and run tests with IDNA feature disabled"
	@echo "  test-no-security        - Build and run tests without Unicode security support"
	@echo "  test-optimized          - Build and run optimized C and C++ configurations"
	@echo "  test-release            - Build and run optimized C tests"
	@echo "  test-sanitizers         - Build and run sanitizer configurations"
	@echo "  test-ubsan              - Build and run tests with UndefinedBehaviorSanitizer"
	@echo "  test-wasm               - Sync WASM and run the Node and browser API tests"
	@echo "  fuzz                    - Fuzz the public API with libFuzzer in Docker"
	@echo "  update-version          - Update version in source files"
	@echo "  wasm                    - Build the project for WebAssembly"
	@echo "  watch-api               - Watch API files, regenerate on changes and run tests"
	@echo "  watch-site              - Watch site files, regenerate on changes and serve at localhost:6251"
