# InterWorx Hosting Control Panel
#
# Copyright (c) 2006-2026 InterWorx L.L.C., All Rights Reserved.
#
# Makefile for the interworx-c-helpers.

CC      ?= gcc
CFLAGS  := -std=c99 -O2 -Wall -Wextra -Werror -Wformat -Wformat-security \
           -D_FORTIFY_SOURCE=2 -fPIE
LDFLAGS := -pie -Wl,-z,relro,-z,now

# Prefer -fstack-protector-strong (distro security default; matches
# Fedora/RHEL hardened-rpm-config convention). Fall back to
# -fstack-protector-all on EL7's gcc 4.8.5, which predates -strong
# (added in gcc 4.9). -all costs marginally more cycles per function
# call than -strong but the difference is invisible at this binary's
# scale, and -all provides stronger coverage.
STACK_PROTECTOR := $(shell $(CC) -fstack-protector-strong -x c -E - </dev/null > /dev/null 2>&1 \
    && echo -fstack-protector-strong || echo -fstack-protector-all)
CFLAGS  += $(STACK_PROTECTOR)

BINARY_1 := process-runner
BINARY_2 := runasuser-runner
BINARY_3 := decoder
BINARIES := $(BINARY_1) $(BINARY_2) $(BINARY_3)

all: $(BINARIES)
	@echo
	@echo "Built with:"
	@echo "  CC      = $(CC)"
	@echo "  CFLAGS  = $(CFLAGS)"
	@echo "  LDFLAGS = $(LDFLAGS)"
	@echo
	@echo "To install at the production paths:"
	@for b in $(BINARIES); do \
	  echo "  sudo install -o root -g iworx -m 4750 $$b \\"; \
	  echo "    /usr/libexec/interworx/$$b"; \
	done

$(BINARY_1): process-runner.c
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<

$(BINARY_2): runasuser-runner.c
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<

$(BINARY_3): decoder.c
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<

clean:
	rm -f $(BINARIES)

.PHONY: all clean
