1include Kbuild
2
3INSTALL_MOD_DIR ?= extra
4
5SUBDIR_TARGETS = icp lua zstd
6
7all: modules
8distclean maintainer-clean: clean
9install: modules_install
10uninstall: modules_uninstall
11check:
12
13.PHONY: all distclean maintainer-clean install uninstall check distdir \
14	modules modules-Linux modules-FreeBSD modules-unknown \
15	clean clean-Linux clean-FreeBSD \
16	modules_install modules_install-Linux modules_install-FreeBSD \
17	modules_uninstall modules_uninstall-Linux modules_uninstall-FreeBSD \
18	cppcheck cppcheck-Linux cppcheck-FreeBSD
19
20# For FreeBSD, use debug options from ./configure if not overridden.
21export WITH_DEBUG ?= @WITH_DEBUG@
22export WITH_INVARIANTS ?= @WITH_INVARIANTS@
23
24# Filter out options that FreeBSD make doesn't understand
25getflags = ( \
26set -- \
27  $(filter-out --%,$(firstword $(MFLAGS))) \
28  $(filter -I%,$(MFLAGS)) \
29  $(filter -j%,$(MFLAGS)); \
30fmakeflags=""; \
31while getopts :deiI:j:knqrstw flag; do \
32  case $$flag in \
33    \?) :;; \
34    :) if [ $$OPTARG = "j" ]; then \
35	 ncpus=$$(sysctl -n kern.smp.cpus 2>/dev/null || :); \
36	 if [ -n "$$ncpus" ]; then fmakeflags="$$fmakeflags -j$$ncpus"; fi; \
37       fi;; \
38    d) fmakeflags="$$fmakeflags -dA";; \
39    *) fmakeflags="$$fmakeflags -$$flag$$OPTARG";; \
40  esac; \
41done; \
42echo $$fmakeflags \
43)
44FMAKEFLAGS = -C @abs_srcdir@ -f Makefile.bsd $(shell $(getflags))
45
46ifneq (@abs_srcdir@,@abs_builddir@)
47FMAKEFLAGS += MAKEOBJDIR=@abs_builddir@
48endif
49
50FMAKE = env -u MAKEFLAGS make $(FMAKEFLAGS)
51
52modules-Linux:
53	list='$(SUBDIR_TARGETS)'; for targetdir in $$list; do \
54		$(MAKE) -C $$targetdir; \
55	done
56	$(MAKE) -C @LINUX_OBJ@ M=`pwd` @KERNEL_MAKE@ CONFIG_ZFS=m modules
57
58modules-FreeBSD:
59	+$(FMAKE)
60
61modules-unknown:
62	@true
63
64modules: modules-@ac_system@
65
66clean-Linux:
67	@# Only cleanup the kernel build directories when CONFIG_KERNEL
68	@# is defined.  This indicates that kernel modules should be built.
69@CONFIG_KERNEL_TRUE@	$(MAKE) -C @LINUX_OBJ@ M=`pwd` @KERNEL_MAKE@ clean
70
71	if [ -f @LINUX_SYMBOLS@ ]; then $(RM) @LINUX_SYMBOLS@; fi
72	if [ -f Module.markers ]; then $(RM) Module.markers; fi
73
74	find . -name '*.ur-safe' -type f -print | xargs $(RM)
75
76clean-FreeBSD:
77	+$(FMAKE) clean
78
79clean: clean-@ac_system@
80
81modules_install-Linux:
82	@# Install the kernel modules
83	$(MAKE) -C @LINUX_OBJ@ M=`pwd` modules_install \
84		INSTALL_MOD_PATH=$(DESTDIR)$(INSTALL_MOD_PATH) \
85		INSTALL_MOD_DIR=$(INSTALL_MOD_DIR) \
86		KERNELRELEASE=@LINUX_VERSION@
87	@# Remove extraneous build products when packaging
88	kmoddir=$(DESTDIR)$(INSTALL_MOD_PATH)/lib/modules/@LINUX_VERSION@; \
89	if [ -n "$(DESTDIR)" ]; then \
90		find $$kmoddir -name 'modules.*' | xargs $(RM); \
91	fi
92	sysmap=$(DESTDIR)$(INSTALL_MOD_PATH)/boot/System.map-@LINUX_VERSION@; \
93	if [ -f $$sysmap ]; then \
94		depmod -ae -F $$sysmap @LINUX_VERSION@; \
95	fi
96
97modules_install-FreeBSD:
98	@# Install the kernel modules
99	+$(FMAKE) install
100
101modules_install: modules_install-@ac_system@
102
103modules_uninstall-Linux:
104	@# Uninstall the kernel modules
105	kmoddir=$(DESTDIR)$(INSTALL_MOD_PATH)/lib/modules/@LINUX_VERSION@; \
106	for objdir in $(ZFS_MODULES); do \
107		$(RM) -R $$kmoddir/$(INSTALL_MOD_DIR)/$$objdir; \
108	done
109
110modules_uninstall-FreeBSD:
111	@false
112
113modules_uninstall: modules_uninstall-@ac_system@
114
115cppcheck-Linux:
116	@CPPCHECK@ -j@CPU_COUNT@ --std=c99 --quiet --force --error-exitcode=2 \
117		--inline-suppr --suppress=noValidConfiguration \
118		--enable=warning,information -D_KERNEL \
119		--include=@LINUX_OBJ@/include/generated/autoconf.h \
120		--include=@top_srcdir@/zfs_config.h \
121		--config-exclude=@LINUX_OBJ@/include \
122		-I @LINUX_OBJ@/include \
123		-I @top_srcdir@/include/os/linux/kernel \
124		-I @top_srcdir@/include/os/linux/spl \
125		-I @top_srcdir@/include/os/linux/zfs \
126		-I @top_srcdir@/include \
127		avl icp lua nvpair spl unicode zcommon zfs zstd os/linux
128
129cppcheck-FreeBSD:
130	@true
131
132cppcheck: cppcheck-@ac_system@
133
134distdir:
135	(cd @srcdir@ && find $(ZFS_MODULES) os -name '*.[chS]') | \
136	while read path; do \
137		mkdir -p $$distdir/$${path%/*}; \
138		cp @srcdir@/$$path $$distdir/$$path; \
139	done; \
140	cp @srcdir@/Makefile.bsd $$distdir/Makefile.bsd
141