# pandora: Syd helper to make sandboxing practical
# Makefile: Makefile for pandora
#
# Copyright (c) 2023, 2024, 2025, 2026 Ali Polatel <alip@chesswob.org>
#
# SPDX-License-Identifier: GPL-3.0

SHELL= /bin/bash

# Package name and version
PACKAGE= pandora

# Git root
GITROOT= $(shell git rev-parse --show-toplevel || pwd)

# Build profile
PROFILE?= release

# Installation directories
PREFIX= $(HOME)/.local
BIN_DIR = $(PREFIX)/bin

# Cargo and Installer
CARGO?= cargo
INSTALL?= install

# Cargo flags
CARGOFLAGS?= -j$(shell nproc)

# Source files
SRC=\
    pandora.rs \
    Cargo.toml

# Path to the binary
ifeq ($(PROFILE), debug)
	BIN= $(GITROOT)/target/debug/$(PACKAGE)
else
	BIN= $(GITROOT)/target/release/$(PACKAGE)
	CARGOFLAGS+= --release
endif

# Default target
all: $(BIN)

# QA targets
fmt:
	astyle --indent=tab --style=linux t/*.c
	$(CARGO) fmt
lint:
	$(CARGO) deny check
	$(CARGO) acl -n || true
	$(CARGO) clippy $(CARGOFLAGS)

# Install and Uninstall Targets
install: $(BIN)
	$(INSTALL) -d $(BIN_DIR)
	$(INSTALL) -m 755 $(BIN) $(BIN_DIR)
uninstall:
	rm -f $(BIN_DIR)/$(PACKAGE)

# Tests
check:
	PANDORA_QUIET=1 $(MAKE) checkverbose
checkverbose: $(BIN)
	$(CARGO) test $(CARGOFLAGS) -p pandora_box

cov: clean
	source <($(CARGO) llvm-cov show-env --export-prefix 2>/dev/null) && \
		$(CARGO) build -p pandora_box $(CARGOFLAGS) && \
		PANDORA_QUIET=1 $(CARGO) test $(CARGOFLAGS) -p pandora_box && \
		$(CARGO) llvm-cov report --release --ignore-filename-regex='/.cargo/' && \
		$(CARGO) llvm-cov report --release --ignore-filename-regex='/.cargo/' --html
	@echo "HTML report: $(GITROOT)/target/llvm-cov/html/index.html"

clean:
	$(CARGO) clean

$(BIN): $(SRC)
	$(CARGO) build $(CARGOFLAGS)

# Phony Targets
.PHONY: all clean check checkverbose cov test install uninstall fmt lint
