# Copyright (c) Omnissa, LLC. All rights reserved.
# This product is protected by copyright and intellectual property laws in the
# United States and other countries as well as by international treaties.
# -- Omnissa Restricted
#

# Makefile for building omnkms.ko against the running kernel

# Module name (without .ko)
obj-m := omnkms.o

omnkms-objs := omnkms_drv.o omnkms_plane.o \
	omnkms_output.o \
	omnkms_formats.o \
	omnkms_crtc.o \
	omnkms_composer.o \
	omnkms_writeback.o \
	omnkms_ctrl.o

# Path to the currently running kernel's build system
KDIR := /lib/modules/$(shell uname -r)/build

# Current directory (where omnkms.c is located)
PWD := $(shell pwd)

# Feature probe: struct drm_driver.fbdev_probe (the drm_client_setup() /
# DRM_FBDEV_SHMEM_DRIVER_OPS path) replaces the older explicit
# drm_fbdev_shmem_setup() call. Grepped directly rather than keyed to a
# distro/version string (the previous "el10_0" check matched exactly one
# RHEL minor and silently ran the wrong branch, missing .fbdev_probe, on
# every other distro/kernel that also predates this field — confirmed to
# be the root cause of a NULL-deref crash + unkillable D-state hang on
# Ubuntu 26.04's kernel).
HAVE_DRM_DRIVER_FBDEV_PROBE := $(shell grep -q "fbdev_probe" $(KDIR)/include/drm/drm_drv.h 2>/dev/null && echo 1)
ifeq ($(HAVE_DRM_DRIVER_FBDEV_PROBE),1)
  ccflags-y += -DHAVE_DRM_DRIVER_FBDEV_PROBE
endif

# Feature probe: struct drm_driver.date was removed upstream independently
# of fbdev_probe (.major/.minor were kept), so it needs its own probe
# rather than being bundled with the fbdev_probe check.
HAVE_DRM_DRIVER_DATE_FIELD := $(shell grep -q "\*date;" $(KDIR)/include/drm/drm_drv.h 2>/dev/null && echo 1)
ifeq ($(HAVE_DRM_DRIVER_DATE_FIELD),1)
  ccflags-y += -DHAVE_DRM_DRIVER_DATE_FIELD
endif

# Feature probe: hrtimer_setup() was introduced upstream in v6.13 and
# hrtimer_init() was removed in v6.15. RHEL backports both changes without
# bumping LINUX_VERSION_CODE, so we must grep the kernel headers directly
# rather than rely on a version check.
HAVE_HRTIMER_SETUP := $(shell grep -q "hrtimer_setup" $(KDIR)/include/linux/hrtimer.h 2>/dev/null && echo 1)
ifeq ($(HAVE_HRTIMER_SETUP),1)
  ccflags-y += -DHAVE_HRTIMER_SETUP
endif

# Feature probe: drm_atomic_get_existing_plane_state() was removed upstream in
# v7.0. Distro kernels may backport its removal (or keep it) without bumping
# LINUX_VERSION_CODE, so we grep the kernel headers directly rather than rely
# on a version check.
HAVE_DRM_ATOMIC_GET_EXISTING_PLANE_STATE := $(shell grep -q "drm_atomic_get_existing_plane_state" $(KDIR)/include/drm/drm_atomic.h 2>/dev/null && echo 1)
ifeq ($(HAVE_DRM_ATOMIC_GET_EXISTING_PLANE_STATE),1)
  ccflags-y += -DHAVE_DRM_ATOMIC_GET_EXISTING_PLANE_STATE
endif

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

test:
	gcc -o omnkms_test omnkms_test.c -I$(PWD) -I/usr/include/drm -ldrm

capture_test:
	gcc -Wall -Wextra -o omnkms_capture_test omnkms_capture_test.c -I$(PWD)

cursor_test:
	gcc -Wall -Wextra -o omnkms_cursor_test omnkms_cursor_test.c -I$(PWD)

clean:
	$(MAKE) -C $(KDIR) M=$(PWD) clean
	rm -f omnkms_test omnkms_capture_test omnkms_cursor_test

