include Kbuild

INSTALL_MOD_DIR ?= extra
INSTALL_MOD_PATH ?= $(DESTDIR)

all: modules
distclean maintainer-clean: clean
install: modules_install data_install
uninstall: modules_uninstall data_uninstall
check:

.PHONY: all distclean maintainer-clean install uninstall check distdir \
	modules modules-Linux modules-FreeBSD modules-unknown \
	clean clean-Linux clean-FreeBSD \
	modules_install modules_install-Linux modules_install-FreeBSD \
	data_install data_install-Linux data_install-FreeBSD \
	modules_uninstall modules_uninstall-Linux modules_uninstall-FreeBSD \
	data_uninstall data_uninstall-Linux data_uninstall-FreeBSD \
	cppcheck cppcheck-Linux cppcheck-FreeBSD

# For FreeBSD, use debug options from ./configure if not overridden.
export WITH_DEBUG ?= @WITH_DEBUG@
export WITH_INVARIANTS ?= @WITH_INVARIANTS@

# Filter out options that FreeBSD make doesn't understand
getflags = ( \
set -- \
  $(filter-out --%,$(firstword $(MFLAGS))) \
  $(filter -I%,$(MFLAGS)) \
  $(filter -j%,$(MFLAGS)); \
fmakeflags=""; \
while getopts :deiI:j:knqrstw flag; do \
  case $$flag in \
    \?) :;; \
    :) if [ $$OPTARG = "j" ]; then \
	 ncpus=$$(sysctl -n kern.smp.cpus 2>/dev/null || :); \
	 if [ -n "$$ncpus" ]; then fmakeflags="$$fmakeflags -j$$ncpus"; fi; \
       fi;; \
    d) fmakeflags="$$fmakeflags -dA";; \
    *) fmakeflags="$$fmakeflags -$$flag$$OPTARG";; \
  esac; \
done; \
echo $$fmakeflags \
)
FMAKEFLAGS = -C @abs_srcdir@ -f Makefile.bsd $(shell $(getflags))

ifneq (@abs_srcdir@,@abs_builddir@)
FMAKEFLAGS += MAKEOBJDIR=@abs_builddir@
endif

FMAKE = env -u MAKEFLAGS make $(FMAKEFLAGS)

modules-Linux:
	mkdir -p $(sort $(dir $(spl-objs) $(spl-)))
	mkdir -p $(sort $(dir $(zfs-objs) $(zfs-)))
	$(MAKE) -C @LINUX_OBJ@ $(if @KERNEL_CC@,CC=@KERNEL_CC@) \
		$(if @KERNEL_LD@,LD=@KERNEL_LD@) $(if @KERNEL_LLVM@,LLVM=@KERNEL_LLVM@) \
		$(if @KERNEL_CROSS_COMPILE@,CROSS_COMPILE=@KERNEL_CROSS_COMPILE@) \
		$(if @KERNEL_ARCH@,ARCH=@KERNEL_ARCH@) \
		M="$$PWD" @KERNEL_MAKE@ CONFIG_ZFS=m modules

modules-FreeBSD:
	+$(FMAKE)

modules-unknown:
	@true

modules: modules-@ac_system@

clean-Linux:
	@# Only cleanup the kernel build directories when CONFIG_KERNEL
	@# is defined.  This indicates that kernel modules should be built.
@CONFIG_KERNEL_TRUE@	$(MAKE) -C @LINUX_OBJ@ M="$$PWD" @KERNEL_MAKE@ clean

	$(RM) @LINUX_SYMBOLS@ Module.markers
	find . -name '*.ur-safe' -type f -delete

clean-FreeBSD:
	+$(FMAKE) clean

clean: clean-@ac_system@

.PHONY: modules_uninstall-Linux-legacy
modules_uninstall-Linux-legacy:
	$(RM) -r $(addprefix $(KMODDIR)/$(INSTALL_MOD_DIR)/,spl/ avl/ icp/ lua/ nvpair/ unicode/ zcommon/ zfs/ zstd/)

KMODDIR := $(INSTALL_MOD_PATH)/lib/modules/@LINUX_VERSION@
modules_install-Linux: modules_uninstall-Linux-legacy
	@# Install the kernel modules
	$(MAKE) -C @LINUX_OBJ@ M="$$PWD" modules_install \
		INSTALL_MOD_PATH=$(INSTALL_MOD_PATH) \
		INSTALL_MOD_DIR=$(INSTALL_MOD_DIR) \
		$(if @KERNEL_ARCH@,ARCH=@KERNEL_ARCH@) \
		KERNELRELEASE=@LINUX_VERSION@
	@# Remove extraneous build products when packaging
	if [ -n "$(DESTDIR)" ] && [ "$(DONT_DELETE_MODULES_FILES)" != "1" ]; then \
		find $(KMODDIR) -name 'modules.*' -delete; \
	fi
	@# Debian ships tiny fake System.map files that are
	@# syntactically valid but just say
	@# "if you want system.map go install this package"
	@# Naturally, depmod is less than amused by this.
	@# So if we find it missing or with one of these present,
	@# we check for the alternate path for the System.map
	sysmap=$(INSTALL_MOD_PATH)/boot/System.map-@LINUX_VERSION@; \
	{ [ -f "$$sysmap" ] && [ $$(wc -l < "$$sysmap") -ge 100 ]; } || \
		sysmap=$(INSTALL_MOD_PATH)/usr/lib/debug/boot/System.map-@LINUX_VERSION@; \
	if [ -f $$sysmap ]; then \
		depmod -ae -F $$sysmap @LINUX_VERSION@ -b $(INSTALL_MOD_PATH)/; \
	fi

modules_install-FreeBSD:
	@# Install the kernel modules
	+$(FMAKE) install

modules_install: modules_install-@ac_system@

data_install-Linux:
	@mkdir -p $(DESTDIR)/@prefix@/src/zfs-@VERSION@/@LINUX_VERSION@
	cp ../zfs.release ../zfs_config.h @LINUX_SYMBOLS@ $(DESTDIR)/@prefix@/src/zfs-@VERSION@/@LINUX_VERSION@

data_install-FreeBSD:
	@

data_install: data_install-@ac_system@

modules_uninstall-Linux: modules_uninstall-Linux-legacy
	@# Uninstall the kernel modules
	$(RM) $(addprefix $(KMODDIR)/$(INSTALL_MOD_DIR)/,zfs.ko spl.ko)

modules_uninstall-FreeBSD:
	@false

modules_uninstall: modules_uninstall-@ac_system@

data_uninstall-Linux:
	$(RM) $(addprefix $(DESTDIR)/@prefix@/src/zfs-@VERSION@/@LINUX_VERSION@/,zfs.release zfs_config.h @LINUX_SYMBOLS@)

data_uninstall-FreeBSD:
	@

data_uninstall: data_uninstall-@ac_system@

cppcheck-Linux:
	@CPPCHECK@ -j@CPU_COUNT@ --std=c99 --quiet --force --error-exitcode=2 \
		--inline-suppr \
		--suppress=unmatchedSuppression \
		--suppress=noValidConfiguration \
		--enable=warning,information -D_KERNEL \
		--include=@LINUX_OBJ@/include/generated/autoconf.h \
		--include=@top_builddir@/zfs_config.h \
		--config-exclude=@LINUX_OBJ@/include \
		-i zstd/lib \
		-I @LINUX_OBJ@/include \
		-I @top_srcdir@/include/os/linux/kernel \
		-I @top_srcdir@/include/os/linux/spl \
		-I @top_srcdir@/include/os/linux/zfs \
		-I @top_srcdir@/include \
		avl icp lua nvpair unicode zcommon zfs zstd os/linux

cppcheck-FreeBSD:
	@true

cppcheck: cppcheck-@ac_system@

distdir:
	cd @srcdir@ && find . -name '*.[chS]' -exec sh -c 'for f; do mkdir -p $$distdir/$${f%/*}; cp @srcdir@/$$f $$distdir/$$f; done' _ {} +
	cp @srcdir@/Makefile.bsd $$distdir/Makefile.bsd

gen-zstd-symbols:
	for obj in $(addprefix zstd/,$(ZSTD_UPSTREAM_OBJS)); do echo; echo "/* $${obj#zstd/}: */"; @OBJDUMP@ -t $$obj | awk '$$2 == "g" && !/ zfs_/ {print "#define\t" $$6 " zfs_" $$6}' | sort; done >> zstd/include/zstd_compat_wrapper.h

check-zstd-symbols:
	@OBJDUMP@ -t $(addprefix zstd/,$(ZSTD_UPSTREAM_OBJS)) | awk '/file format/ {print}  $$2 == "g" && (!/ zfs_/ && !/ __pfx_zfs_/) {++ret; print}  END {exit ret}'
