# Builds the LD_PRELOAD interposer, or installs the prebuilt copy when the host
# has no compiler. See ../target/Makefile for why gcc is not bundled.

# make defines CC=cc as a built-in, so `CC ?= ...` never fires. Only override it
# when it is still that built-in default - an explicit CC= from the environment
# or the command line must win.
ifeq ($(origin CC),default)
CC := $(shell command -v gcc 2>/dev/null || command -v cc 2>/dev/null)
endif
CFLAGS   ?= -O2 -fPIC -Wall
PREBUILT := $(CURDIR)/../prebuilt

all: libpreload_audit.so

ifeq ($(strip $(CC)),)
libpreload_audit.so: $(PREBUILT)/libpreload_audit.so
	@echo "  no C compiler on this host - installing prebuilt $@"
	@cp -f $< $@ && chmod +x $@
else
libpreload_audit.so: preload_audit.c
	$(CC) $(CFLAGS) -shared -o $@ $< -ldl
endif

check:
	@echo "CC = $(if $(CC),$(CC),none - will use $(PREBUILT))"

clean:
	rm -f libpreload_audit.so

.PHONY: all clean check
