# libsydtime: Rust-based C LD_PRELOAD library to replace vDSO time calls with syscalls
# Makefile: Makefile for libsydtime
#
# Copyright (c) 2024 Ali Polatel <alip@chesswob.org>
#
# SPDX-License-Identifier: GPL-3.0

# Package name and version
PACKAGE= sydtime
VERSION= 0.0.1

# Build profile
PROFILE?= release

# Installation directories
PREFIX = /usr/local
LIB_DIR = $(PREFIX)/lib

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

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

# Source files
SRC=\
    src/lib.rs \
    Cargo.toml

# Library Names
ifeq ($(PROFILE), debug)
	LIB= ./target/debug/deps/lib$(PACKAGE).so
else
	LIB= ./target/release/deps/lib$(PACKAGE).so
	CARGOFLAGS+= --release
endif

# Default target
all: $(LIB)

# QA targets
fmt:
	$(CARGO) fmt
lint:
	$(CARGO) deny check
	$(CARGO) acl -n || true
	$(CARGO) clippy $(CARGOFLAGS)

# Install and Uninstall Targets
install:
	$(INSTALL) -d $(LIB_DIR)
	$(INSTALL) -m 755 $(LIB) $(LIB_DIR)
uninstall:
	rm -f $(LIB_DIR)/libsydtime.so

# Check target
check: test

test: $(LIB)
	$(CARGO) test $(CARGOFLAGS)

# Clean Target
clean:
	$(CARGO) clean

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

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