# SPDX-License-Identifier: GPL-2.0
# Copyright(c) 2023 junka <wan.junjie@foxmail.com>.

ifneq ($(KERNELRELEASE),)
# kbuild part of makefile

ccflags-y += -I$(src)
subdir-ccflags-y += -I$(src)

obj-m += pktgen.o

pktgen-y := pktgen_5.4.o

else	# ifneq($(KERNELRELEASE),)
# normal makefile

DRIVER := pktgen

# If the user just wants to print the help output, don't include common.mk or
# perform any other checks. This ensures that running "make help" will always
# work even if kernel-devel is not installed, or if the common.mk fails under
# any other error condition.
ifneq ($(MAKECMDGOALS),help)
include common.mk
# tested over this kernel version, should work for lower version
$(call minimum_kver_check,4,15,0)
endif


###############
# Build rules #
###############

# Standard compilation, with regular output
default:
	@+$(call kernelbuild,modules)

# Noisy output, for extra debugging
noisy:
	@+$(call kernelbuild,modules,V=1)

# Silence any output generated
silent:
	@+$(call kernelbuild,modules,>/dev/null)

# Enable higher warning level
checkwarnings: clean
	@+$(call kernelbuild,modules,W=1)

# Run sparse static analyzer
sparse: clean
	@+$(call kernelbuild,modules,C=2 CF="-D__CHECK_ENDIAN__ -Wbitwise -Wcontext")

# Run coccicheck static analyzer
ccc: clean
	@+$(call kernelbuild,modules,coccicheck MODE=report)


# Clean the module subdirectories
clean:
	@-rm -rf *.${MANSECTION}.gz *.ko

sign:
	@echo "Signing driver..."
	@$(call info_signed_modules)
	@$(call sign_driver)


# Install kernel module files. It is not expected to modify files outside of
# the build root. Thus, it must not update initramfs, or run depmod.
# overriding config/kernel.release the kver, so the installed package could be
# installed and depmod in the right place
modules_install: default
	@echo "Installing modules..."
	test -e ${KSRC}/include/config/kernel.release && echo ${KVER} > ${KSRC}/include/config/kernel.release
	@+$(call kernelbuild,modules_install)


# After installing all the files, perform necessary work to ensure the system
# will use the new modules. This includes running depmod to update module
# dependencies and updating the initramfs image in case the module is loaded
# during early boot.
install: modules_install
	$(call cmd_depmod)

# Remove installed module files. This target is called by the RPM specfile when
# generating binary RPMs, and is not expected to modify files outside of the
# build root. Thus, it must not update the initramfs image or run depmod.
modules_uninstall:
	rm -f ${INSTALL_MOD_PATH}/lib/modules/${KVER}/${INSTALL_MOD_DIR}/${DRIVER}.ko;

# After uninstalling all the files, perform necessary work to restore the
# system back to using the default kernel modules. This includes running depmod
# to update module dependencies and updating the initramfs image.
uninstall: modules_uninstall 
	$(call cmd_depmod)


########
# Help #
########
help:
	@echo 'Build targets:'
	@echo '  default             - Build module(s) with standard verbosity'
	@echo '  noisy               - Build module(s) with V=1 verbosity -- very noisy'
	@echo '  silent              - Build module(s), squelching all output'
	@echo ''
	@echo 'Static Analysis:'
	@echo '  checkwarnings       - Clean, then build module(s) with W=1 warnings enabled'
	@echo '  sparse              - Clean, then check module(s) using sparse'
	@echo '  ccc                 - Clean, then check module(s) using coccicheck'
	@echo ''
	@echo 'Cleaning targets:'
	@echo '  clean               - Clean files generated by kernel module build'
	@echo ''
	@echo 'Other targets:'
	@echo '  modules_install     - install the module(s) only'
	@echo '  install             - Build then install the module(s)'
	@echo '  modules_uninstall   - uninstall the module(s) only'
	@echo '  uninstall           - Uninstall the module(s)'
	@echo '  help                - Display this help message'
	@echo ''
	@echo 'Variables:'
	@echo '  LINUX_VERSION       - Debug tool to force kernel LINUX_VERSION_CODE. Use at your own risk.'
	@echo '  W=N                 - Kernel variable for setting warning levels'
	@echo '  V=N                 - Kernel variable for setting output verbosity'
	@echo '  INSTALL_MOD_PATH    - Add prefix for the module and manpage installation path'
	@echo '  INSTALL_MOD_DIR     - Use module directory other than updates'
	@echo '  KSRC                - Specifies the full path to the kernel tree to build against'
	@echo ' Other variables may be available for tuning make process, see'
	@echo ' Kernel Kbuild documentation for more information'

.PHONY: default noisy clean silent sparse ccc install uninstall help

endif	# ifneq($(KERNELRELEASE),)
