# $ZEL: Makefile,v 2.00 2026/02/18 21:47:44 wuestner/ther Exp $
#
# We're just setting the stage here and
# let the Kernel Makefile do the real work.
#

KERNVER := $(shell uname -r)
KERNNUM := $(shell uname -r | grep -Po '\d+.\d+.\d+-\d+')

ifneq (,$(wildcard /etc/redhat-release))
KERNPTH := /usr/src/kernels/$(KERNVER)
else
KERNPTH := /usr/src/linux-headers-$(KERNVER)
endif

MODULENAME := sis1100
MODSTATE = $(shell /sbin/lsmod | grep $(MODULENAME))


ccflags-y += -I$(KERNPTH)/include/linux

#01NOV2017 For DEBIAN9_Kernel4.9.0
ccflags-y += -I/usr/src/linux-headers-$(shell uname -r | grep -Po '(.*)-')common/include/linux/

.PHONY: driver

driver:
	make -C /lib/modules/$(KERNVER)/build M=$(shell pwd) modules

install:
ifneq ($(MODSTATE), )
	/sbin/rmmod $(MODULENAME)
endif
	make -C /lib/modules/$(KERNVER)/build M=$(shell pwd) modules_install
	/sbin/depmod -a
	/sbin/modprobe $(MODULENAME)
	cp -f ../../udev-rules/92-struck.rules /etc/udev/rules.d/92-struck.rules

uninstall:
ifneq ($(MODSTATE), )
	/sbin/rmmod $(MODULENAME)
endif
	rm /lib/modules/$(KERNVER)/extra/$(MODULENAME).ko
	rm /etc/udev/rules.d/92-struck.rules

clean:
	make -C /lib/modules/$(KERNVER)/build M=$(shell pwd) clean
	rm -f *.o *.d *.ko *.cmd .*.cmd *.mod.c *.mod Module.symvers modules.order
	rm -f Module.markers
	rm -r -f .tmp_versions

sign: driver
ifeq ($(wildcard $(MODULENAME).priv), )
	$(error no signing key file (*.priv) found! Run './signature.sh' to create the requierd file) 
endif
ifeq ($(wildcard $(MODULENAME).der), )
	$(error no signing certificat (*.der) found! Run './signature.sh' to create the requierd file) 
endif
	$(KERNPTH)/scripts/sign-file sha256 $(MODULENAME).priv $(MODULENAME).der $(MODULENAME).ko

sign-install:
ifeq ($(wildcard $(MODULENAME).priv), )
	$(error no signing key file (*.priv) found! Run './signature.sh' to create the requierd file) 
endif
ifeq ($(wildcard $(MODULENAME).der), )
	$(error no signing certificat (*.der) found! Run './signature.sh' to create the requierd file) 
endif
ifneq ($(MODSTATE), )
	/sbin/rmmod $(MODULENAME)
endif
	make -C /lib/modules/$(KERNVER)/build M=$(shell pwd) modules_install
	$(KERNPTH)/scripts/sign-file sha256 $(MODULENAME).priv $(MODULENAME).der /lib/modules/$(KERNVER)/extra/$(MODULENAME).ko
	/sbin/depmod -a
	/sbin/modprobe $(MODULENAME)
	cp -f ../../udev-rules/92-struck.rules /etc/udev/rules.d/92-struck.rules


ifndef src
    src:=$(shell pwd)
endif

SOURCES:=$(notdir $(wildcard $(src)/*.c))
sis1100-objs := $(patsubst %.c, %.o, $(filter-out %netbsd.c, $(SOURCES)))

ifneq ($(KERNELRELEASE), )

EXTRA_CFLAGS += -DMODULENAME=$(MODULENAME)

obj-m += sis1100.o

else

modules: sis1100.o

sis1100.o: $(sis1100-objs)
	$(LD) -r -o $@ $^

endif
