
ProCapture-objs :=	  \
	sources/dma/mw-dma-mem.o					\
	sources/dma/mw-dma-user.o					\
	sources/dma/mw-dma-kernel.o					\
	sources/dma/mw-dma-phy.o					\
	sources/ospi/ospi-linux.o					\
	sources/ospi/linux-file.o  					\
	sources/avstream/v4l2.o				        \
	sources/avstream/v4l2-sg-buf.o				\
	sources/avstream/mw-event-dev.o				\
	sources/avstream/mw-event-ioctl.o			\
	sources/avstream/alsa.o				        \
	sources/avstream/capture.o

ProCapture-objs += ProCaptureLib.o

obj-m = ProCapture.o

ccflags-y += -I$(src) -I$(src)/public -I$(src)/sources
# Distro GCC defaults to PIE, which conflicts with the kernel code model on
# pre-4.x kbuild that does not pass -fno-PIE itself.
ccflags-y += $(call cc-option, -fno-pie)
# strip debug symbols
ldflags-y += -S

ifneq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(KERNELRELEASE)/build
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
endif

PWD := $(shell pwd)

all: ProCaptureLib.o
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) clean

OS_ARCH := $(shell uname -m | sed -e 's/i.86/i386/')
ifneq ($(OS_ARCH), i386)
	ProCaptureLibFile = ProCaptureLib_64.lib
	# Mitigation-matched lib selection (64-bit only): mainline kernels
	# from 5.18 on may enable RETHUNK and take the thunk-matched variant -
	# upstream never warns about unpatched prebuilt thunks and 6.1+ even
	# re-annotates them via objtool --link. Distros that backported
	# RETHUNK onto a pre-5.18 base (RHEL 9: 5.14) point __x86_return_thunk
	# at a warn stub on mitigation-off CPUs, which the unannotated
	# prebuilt lib would trip on every return - serve those the plain lib
	# instead: their kbuild never scans prebuilt objects, so it stays
	# warning-free and fully functional. Kernels with IBT but without
	# RETHUNK are rejected outright: no shipped variant can serve them.
	# Probe the target kernel .config/Makefile directly: auto.conf is only
	# included by the kbuild sub-make, while this rule runs in the outer
	# make invocation. CONFIG_RETHUNK is the pre-6.9 spelling of
	# CONFIG_MITIGATION_RETHUNK.
	KERN_IBT := $(shell grep -s "^CONFIG_X86_KERNEL_IBT=y" $(KERNELDIR)/.config)
	KERN_RETHUNK := $(shell grep -sE "^(CONFIG_MITIGATION_RETHUNK|CONFIG_RETHUNK)=y" $(KERNELDIR)/.config)
	KERN_MAJOR := $(shell sed -n 's/^VERSION = \(.*\)/\1/p' $(KERNELDIR)/Makefile | head -1)
	KERN_MINOR := $(shell sed -n 's/^PATCHLEVEL = \(.*\)/\1/p' $(KERNELDIR)/Makefile | head -1)
	KERN_GE_5_18 := $(shell test $(KERN_MAJOR) -gt 5 -o \( $(KERN_MAJOR) -eq 5 -a $(KERN_MINOR) -ge 18 \) && echo y)
	ifneq ($(KERN_RETHUNK),)
		ifneq ($(KERN_GE_5_18),)
			ProCaptureLibFile := $(basename $(ProCaptureLibFile))-ibt-rethunk.lib
		endif
	else ifneq ($(KERN_IBT),)
		# A tab-indented line inside an active branch must parse as a
		# variable assignment, otherwise make takes it as a recipe line;
		# := evaluates the right-hand side immediately, so this aborts.
		RefuseMixedMitigation := $(error Target kernel enables IBT but not RETHUNK: no matching prebuilt lib variant, refusing to build)
	endif
else
	ProCaptureLibFile = ProCaptureLib_32.lib
endif
ProCaptureLib.o:
	cp $(ProCaptureLibFile) ProCaptureLib.o
	touch .ProCaptureLib.o.cmd


