# Makefile for building, installing, and uninstalling the irdma kernel module
# On RHEL 9.6, SLES15SP6, Ubuntu 24.04

MAKE=make
SHELL=/bin/bash

KVER        := $(shell uname -r)
ID          := $(shell source /etc/os-release && echo $${ID} )
IDLIKE      := $(shell source /etc/os-release && echo $${ID_LIKE})
SKVER       := $(shell echo "${KVER}" | awk -F"." '{ print $$1"."$$2;}')
DIRVAR      := $(shell for v in ${KVER} ${SKVER}; do \
		echo "/usr/src/kernels/$$v/"; \
		echo "/usr/src/linux-$$v/";	\
		echo "/lib/modules/$$v/build/"; \
		done;)
KDIR        := $(shell for d in ${DIRVAR}; do if [ -d "$$d" ]; then \
		find $$d -path "*include*" -name "config" -exec echo $$d \;; \
		break; fi; done)
GCC         := $(shell which gcc)
DRACUT	    := $(shell which dracut)
RAMFS	    := $(shell which update-initramfs)
KSRC        ?= ${KDIR}
PWD         := $(shell pwd)
MOD_NAME    := irdma
MODULE_DIR  := $(PWD)/src/$(MOD_NAME)
INSMOD_DIR  := updates/drivers/infiniband/hw/$(MOD_NAME)
DRVFILE     := /lib/modules/$(KVER)/$(INSMOD_DIR)/$(MOD_NAME).ko
NPROC       := $(shell which nproc > /dev/null 2>&1 && nproc)
SLEVER      ?= $(shell if [ -d "${KSRC}" ]; then \
		find ${KSRC}/include -name autoconf.h -exec \
		awk 'BEGIN{FS="-";v=0}{if (index($$0,"CONFIG_LOCALVERSION")) v=$$2;}END{print v}' {} \; | \
		awk -F"." '{print 65536*$$1+256*$$2+$$3 }'; \
		else echo "0"; fi)

EXTRA_INCS  ?=

# Targets
all: check-deps cleanmod buildmod installmod

build: check-deps buildmod

clean: check-deps cleanmod

install: check-deps installmod initrd postinstall

check-deps:
	@echo "OS: ${ID} (${IDLIKE})"
	@echo "Kernel: ${KVER}"
	@if [ ! -d "${KSRC}" ]; then \
		echo "ERROR: No Kernel headers found for ${ID} (${IDLIKE}) ${KVER}."; \
		echo "Please install the kernel headers for your distribution or use KSRC to set it."; \
		echo "For example: make KSRC=/usr/src/linux-headers-$(KVER)"; \
		exit 1; \
	fi
	@echo "Kernel-Source (KSRC): ${KSRC}"
	@if [ -z "${GCC}" ] ;then \
		echo "ERROR: No GCC compiler found"; \
		exit 1; \
	fi

buildmod:
	$(MAKE) "CFLAGS_MODULE=-DMODULE -DSLE_LOCALVERSION_CODE=${SLEVER} -DAVOID_KCOMPAT_GEN ${EXTRA_INCS}" -j$(NPROC) -C $(KSRC) M=$(MODULE_DIR) W=1

installmod:
	$(MAKE) -C $(KSRC)  CFLAGS_MODULE="${EXTRA_INCS}" M=$(MODULE_DIR) INSTALL_MOD_DIR=$(INSMOD_DIR)  C=$CHECK CF="$CHECK_FLAGS" modules_install
	@depmod -a

initrd:
	@if [ ! -z "${DRACUT}" ]; then \
		echo 'dracut --force --omit-drivers  "irdma i40iw"'; \
		echo "omit_drivers+=\" irdma i40iw \"" > /etc/dracut.conf.d/irdma_omit.conf; \
		dracut --force --omit-drivers  "irdma i40iw"; \
	elif [ ! -z "${RAMFS}" ]; then \
		echo "update-initramfs -u -k $(KVER)"; \
		update-initramfs -u -k $(KVER) ; \
	else \
		echo "Unable to update initramfs. You may need to do this manually."; \
	fi

postinstall:
	@if [ -z "$$(which rdma-ndd)" ]; then \
		echo "NOTE: rdma-core does not seem to be installed."; \
		echo "Please install rdma-core for your distribution"; \
		echo "Or use build_core.sh/install_core.sh to compile a supported version."; \
	fi
	@if [ -z "$$(which ibv_devinfo)" ]; then \
		echo "NOTE: ibv verbs utils does not seem to be installed."; \
		echo "You can install ibverbs-utils"; \
	fi

uninstall:
	@if [ -f ${DRVFILE} ]; then rm -f ${DRVFILE}; depmod -a; fi;

cleanmod:
	$(MAKE) -C $(KSRC) M=$(MODULE_DIR) clean

.PHONY: all build install uninstall clean
