ProxMon/Makefile
2025-05-23 11:34:39 -04:00

90 lines
2.4 KiB
Makefile

help: ## show help message
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[$$()% a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
CC = gcc
OUT_DIR = ./build
TARGET = ./bin/proxmon
SRC_DIRS = ./src
# Collect all C source files in the current directory
SRCS := $(shell find $(SRC_DIRS) -name '*.c')
# Generate object files in the build directory
OBJS := $(patsubst $(SRC_DIRS)/%.c, $(OUT_DIR)/%.o, $(SRCS))
INC_DIRS := $(shell find $(SRC_DIRS) -type d) $(INC_DIRS)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CCFLAGS := $(INC_FLAGS)
.PHONY: clean install build
install: build create-services install-services
build: $(TARGET)
clean:
@rm -rf bin/ $(OUT_DIR) $(SERVICE_DIR)
./bin:
@mkdir -p ./bin
@cp ./dist/* ./bin
$(OUT_DIR):
@mkdir -p $@
$(OUT_DIR)/%.o: $(SRC_DIRS)/%.c | $(OUT_DIR)
$(CC) $(CCFLAGS) -c $< -o $@
$(TARGET): $(OBJS) | ./bin
$(CC) $(CCFLAGS) $(OBJS) -o $@
TEMPLATE_DIR ?= ./template
SERVICE_DIR ?= ./systemd
INSTALL_DIR ?= /etc/systemd/system
WORKDIR := $(shell pwd)
# Collect all template service files
TEMPLATES := $(shell find $(TEMPLATE_DIR) -name '*.service.in')
# Generate names of service files that will have paths substituted
SERVICES := $(patsubst $(TEMPLATE_DIR)/%.service.in, $(SERVICE_DIR)/%.service, $(TEMPLATES))
# Generate names of installed service files
INSTALLED := $(patsubst $(SERVICE_DIR)/%, $(INSTALL_DIR)/%, $(SERVICES))
# Create services directory
$(SERVICE_DIR):
@mkdir -p $@
# Convert template service into actual service file
$(SERVICE_DIR)/%.service: $(TEMPLATE_DIR)/%.service.in | $(SERVICE_DIR)
sed 's|@WORKDIR@|$(WORKDIR)|g' $< > $@
create-services: $(SERVICES)
# install service into systemd
$(INSTALL_DIR)/%.service: $(SERVICE_DIR)/%.service
install -m 644 $< $(INSTALL_DIR)/
install-services: create-services $(INSTALLED)
systemctl daemon-reload
@for srv in $(SERVICES); do \
name=$$(basename $$srv); \
systemctl enable $$name; \
systemctl start $$name; \
done
uninstall-services:
@for srv in $(INSTALLED); do \
name=$$(basename $$srv); \
echo "Stopping and uninstalling $$name..."; \
systemctl stop $$name || true; \
systemctl disable $$name || true; \
if [ -f $$srv ]; then rm -f $$srv; fi; \
done
systemctl daemon-reload
reinstall-services: uninstall-services install-services