# (C) Copyright International Business Machines  Corp., 2025
# Makefile for the Linux lin_tape driver

ifneq ($(KERNELRELEASE),)
#include $(PWD)/Kbuild

else
VERSIONRELEASE := $(shell sed --regexp-extended -e '{/\#define LIN_TAPE_VERSION/ s/.*"([0-9.]*)-?([A-Za-z0-9.]*)"/\1 \2/p;d}' lin_tape_tape.h)
endif

ifeq ($(SFMP), 1)
obj-m := stmp.o
obj-m += sgmp.o
endif
obj-m += lin_tape.o
obj-m += pfo.o

pfo-objs := resolve_ibm_hp.o fo_util.o upper.o lower.o intercept.o trace.o

lin_tape-objs := join.o lin_tape_scsi_config.o lin_tape_scsi_tape.o lin_tape_scsi_trace.o \
             lin_tape_ioctl_tape.o lin_tape_ioctl_changer.o lin_tape_extra_ioctl.o
RHELRELEASE := $(shell [ -f "/etc/redhat-release" ] && echo 1 || echo 0)
ifeq ($(RHELRELEASE), 1)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
KERNELVER = $(shell uname -r 2>/dev/null|sed "s/[\.-]/_/g"|sed "s/\([0-9]*_[0-9]*_[0-9]*_[0-9]*\).*/\1/")
EXTRA_CFLAGS += -DKERNELVERSION_${KERNELVER}
RHEL94 := $(shell grep -qi 9.4 /etc/*release && echo 1 || echo 0)
ifeq ($(RHEL94), 1)
EXTRA_CFLAGS += -DRHEL94
else
RHEL95 := $(shell grep -qi 9.5 /etc/*release && echo 1 || echo 0)
ifeq ($(RHEL95), 1)
EXTRA_CFLAGS += -DRHEL94
EXTRA_CFLAGS += -DRHEL95
else
RHEL96 := $(shell grep -qi 9.6 /etc/*release && echo 1 || echo 0)
ifeq ($(RHEL96), 1)
EXTRA_CFLAGS += -DRHEL94
EXTRA_CFLAGS += -DRHEL95
EXTRA_CFLAGS += -DRHEL96
endif
endif
endif
else
UBUNTURELEASE := $(shell [ -f "/etc/os-release" ] && grep -qi Ubuntu /etc/os-release && echo 1 || echo 0)
ifeq ($(UBUNTURELEASE), 1)
EXTRA_CFLAGS += -DUBUNTUVERSION
else
EXTRA_CFLAGS += -DSLESVERSION
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
KERNELVER = $(shell uname -r 2>/dev/null|sed "s/[\.-]/_/g"|sed "s/\([0-9]*_[0-9]*_[0-9]*_[0-9]*\).*/\1/")
EXTRA_CFLAGS += -DSLESKERNEL_${KERNELVER}
SLESSP2 := $(shell grep -qi SP2 /etc/*release && echo 1 || echo 0)
SLESSP5 := $(shell grep -qi SP5 /etc/*release && echo 1 || echo 0)
SLES15SP6 := $(shell grep -qi "15 SP6" /etc/*release && echo 1 || echo 0)
ifeq ($(SLESSP2), 1)
EXTRA_CFLAGS += -DSLESSP2
endif
ifeq ($(SLESSP5), 1)
EXTRA_CFLAGS += -DSLESSP5
endif
ifeq ($(SLES15SP6), 1)
EXTRA_CFLAGS += -DSLES15SP6
endif
endif
endif

ifeq ($(SFMP), 1)
EXTRA_CFLAGS += -DSFMPENABLED
endif

PROC := $(shell uname -m)
ifeq ($(UBUNTURELEASE), 0)
ifeq ($(PROC), ppc64)
EXTRA_CFLAGS += -m64
endif

ifeq ($(PROC), powerpc64)
EXTRA_CFLAGS += -m64
endif

ifeq ($(PROC), powerpc)
EXTRA_CFLAGS += -m64 -msoft-float
LDFLAGS += -melf64ppc
endif

ifeq ($(PROC), x86_64)
EXTRA_CFLAGS += -mcmodel=kernel
endif
endif

VERSION := $(word 1,$(VERSIONRELEASE))
RELEASE := $(word 2,$(VERSIONRELEASE))
ifeq ($(RELEASE),)
RELEASE := 1
endif

KERNEL	:= $(shell uname -r)
SRCDIR	:= /lib/modules/$(KERNEL)/build
PWD	:= $(shell pwd)

default modules lin_tape.ko:
	export PWD
	$(MAKE) -C $(SRCDIR) M=$(PWD) PWD=$(PWD) modules

modules_install:
	export PWD
	$(MAKE) -C $(SRCDIR) M=$(PWD) PWD=$(PWD) modules_install

clean:
	$(MAKE) -C $(SRCDIR) M=$(PWD) PWD=$(PWD) clean
	@rm -rf *.tgz *.tar* *.ko bldtmp .l* .t* .o *.mod.c *odule* IBMtapeutil IBMtapeutil*/ lin_taped *~

compileclean:
	rm -f *.o

bldtmp:
	mkdir bldtmp
#
# create the IBMtapeutil package
#
UVERSION:=$(shell  [ -e "IBMtapeutil.c" ] &&  grep 'const char \*version = "IBMtapeutil for Linux, Version' IBMtapeutil.c|cut -f 2 -d ','|cut -f 3 -d ' ')
UTILDIR=IBMtapeutil.$(UVERSION)
UTILPKG=$(UTILDIR).$(PROC).tar
UTILSRCS=IBMtapeutil.h IBMtapeutil.c IBM_tape.h
IBMtapeutil: $(UTILSRCS)
	make -f makefile.tapeutil
util $(UTILPKG): IBMtapeutil
	mkdir $(UTILDIR);
	cp -a IBMtapeutil ./$(UTILDIR)/.
	cp -a IBMtapeutil.h ./$(UTILDIR)/.
	cp -a IBMtapeutil.c ./$(UTILDIR)/.
	cp -a makefile.tapeutil ./$(UTILDIR)/makefile
	cp -a IBMtapeutil.ReadMe ./$(UTILDIR)/.
	cp -a IBM_tape.h ./$(UTILDIR)/.
	cp -a IBMtapeconfig ./$(UTILDIR)/.
	tar -cvf $(UTILPKG) $(UTILDIR)
	@rm -rf $(UTILDIR)
#
BASE := $(shell echo $(KERNEL)|sed --regexp-extended -e 's/[^a-zA-Z0-9]*(smp|default|bigsmp|hugemem)*$$//p;d')
KERNELS := $(shell echo $(notdir $(subst /build,,$(shell find $(wildcard /lib/modules/$(BASE)*) -name build)))|sed -e '/$(BASE)[^a-zA-Z0-9]{,1}(smp|default|bigsmp|hugemem)/p')
$(KERNELS:%=bldtmp/lin_tape-%.ko): clean bldtmp
	$(MAKE) KERNEL=$(patsubst bldtmp/lin_tape-%.ko,%,$@) compileclean lin_tape.ko
	mv lin_tape.ko $@
	mv pfo.ko bldtmp/pfo-$(patsubst bldtmp/lin_tape-%.ko,%,$@).ko
ifeq ($(SFMP), 1)
	mv sgmp.ko bldtmp/sgmp-$(patsubst bldtmp/lin_tape-%.ko,%,$@).ko
	mv stmp.ko bldtmp/stmp-$(patsubst bldtmp/lin_tape-%.ko,%,$@).ko
endif

driver: $(KERNELS:%=bldtmp/lin_tape-%.ko)

alldevs: $(KERNELS:%=bldtmp/lin_tape-%.ko) lin_taped

cleandev:
	@rm -f $(DEVOBJS)

tarfiles = $(wildcard *.c *.h *.ReadMe Makefile* *.conf COPYING* *.spec *.service\
	   *.fixlist messages lin_tape ibmekm/)

lin_taped: lin_taped.c
	$(CC) $(EXTRA_CFLAGS) lin_taped.c -I ibmekm/inc  -D LINUX ./ibmekm/src/proxy_mgr.c ./ibmekm/src/tcpip_mgr.c -o $@ ;
	strip $@ ;

# Update the version number in the spec file
lin_tape-$(VERSION)/lin_tape.spec: lin_tape.spec lin_tape_tape.h lin_tape-$(VERSION) Makefile
	@sed <lin_tape.spec --regexp-extended -e 's/^(Version: ).*/\1$(VERSION)/;s/^(Release: ).*/\1$(RELEASE)/' >lin_tape-$(VERSION)/lin_tape.spec

lin_tape-$(VERSION):
	@mkdir lin_tape-$(VERSION)

tar lin_tape-$(VERSION).tgz: $(tarfiles) lin_tape-$(VERSION) lin_tape-$(VERSION)/lin_tape.spec
	@cp -a $(tarfiles) lin_tape-$(VERSION)
	@tar czf lin_tape-$(VERSION).tgz lin_tape-$(VERSION)
	@rm -rf lin_tape-$(VERSION)
	@ls -l lin_tape-$(VERSION).tgz

lin_taped-$(VERSION):
	@mkdir lin_taped-$(VERSION)

lin_taped-$(VERSION)/lin_taped: lin_taped-$(VERSION) lin_taped
	@cp -a $(tarfiles) lin_taped-$(VERSION)
#	@cp -a lin_taped $@
	@rm -f lin_taped-$(VERSION)/lin_tape.spec
	@tar czf lin_taped-$(VERSION).tgz lin_taped-$(VERSION)
	@rm -rf lin_taped-$(VERSION)
	@ls -l lin_taped-$(VERSION).tgz

daemonrpm: rpmdaemon
rpmdaemon: lin_taped-$(VERSION)/lin_taped
	rpmbuild -ta lin_taped-$(VERSION).tgz

rpm: lin_tape-$(VERSION).tgz rpmdaemon
	rpmbuild -tb lin_tape-$(VERSION).tgz
#	rpmbuild -ta lin_taped-$(VERSION).tgz

srctar: $(tarfiles) lin_tape-$(VERSION) lin_tape-$(VERSION)/lin_tape.spec
#	@mkdir lin_tape-$(VERSION)
	@cp -a $(tarfiles) lin_tape-$(VERSION)
	@rm -rf lin_tape-$(VERSION)/ibmekm/{ibmekm.fixlist,cfg,src,inc/proxy*,inc/ibmekm*}
	@rm -rf lin_tape-$(VERSION)/lin_taped*
	@rm -rf lin_tape-$(VERSION)/{*~,*.in,*.ac,*eutil*}
	@tar czf lin_tape-$(VERSION).tgz lin_tape-$(VERSION)
	@rm -rf lin_tape-$(VERSION)
	@ls -l lin_tape-$(VERSION).tgz

rpmsrc: srcrpm
srcrpm: srctar
	rpmbuild -ts lin_tape-$(VERSION).tgz

srcrpm_supported: srctar
	cp mdl.supported Module.supported
	cp lin_tape.spec.supported lin_tape.spec
	cp lin_taped.spec.supported lin_taped.spec
	rpmbuild -ts lin_tape-$(VERSION).tgz

allrpm: rpmall
rpmall: rpm srcrpm

maintclean: mrproper
	rm -fr config.log configure config.status autom4te.cache
#	rm -f lin_tape.spec

install:
	insmod ./pfo.ko
	insmod ./lin_tape.ko

remove:
	modprobe -f -r stmp
	modprobe -f -r sgmp
	rmmod lin_tape
	rmmod pfo

update:
	chmod 755 lin_tape.ko
	chmod 755 pfo.ko
ifeq ($(SFMP), 1)
	chmod 755 sgmp.ko
	chmod 755 stmp.ko
endif

progs: IBMtapeutil lin_taped

.PHONY: install remove update progs all clean cleandev alldevs tar rpm
