obj-m := ixcm.o

KDIR  ?= /lib/modules/$(shell uname -r)/build
KH    := $(KDIR)/include
PWD   := $(shell pwd)

.PHONY: all modules autoconf clean

all: modules

# Generate autoconf.h by probing actual kernel headers (not LINUX_VERSION_CODE)
# so distros that backport API changes to older-numbered kernels are handled
# correctly.
autoconf:
	@echo "/* Auto-generated by Makefile — do not edit */" > autoconf.h.tmp
	@# class_create() dropped the 'owner' parameter in 6.4. Detect by grepping
	@# the actual macro/function signature rather than trusting version numbers.
	@grep -rq 'class_create(owner, *name)' $(KH)/linux/device/class.h $(KH)/linux/device.h 2>/dev/null && \
	    echo "#define HAVE_CLASS_CREATE_OWNER 1" >> autoconf.h.tmp || true
	@if cmp -s autoconf.h.tmp autoconf.h 2>/dev/null; then \
	    rm autoconf.h.tmp; \
	else \
	    mv autoconf.h.tmp autoconf.h; \
	    echo "autoconf.h generated for kernel $(shell uname -r)"; \
	fi

modules: autoconf
	$(MAKE) -C $(KDIR) M=$(PWD) modules

clean:
	rm -f autoconf.h autoconf.h.tmp
	$(MAKE) -C $(KDIR) M=$(PWD) clean
