1#!/usr/bin/make -f
2
3include /usr/share/dpkg/default.mk
4
5LSB_DISTRIBUTOR := $(shell lsb_release -is)
6NAME := $(shell awk '$$1 == "Name:" { print $$2; }' META)
7LINUX_MIN  := $(shell awk '/Linux-Minimum:/{print $$2}' META)
8LINUX_NEXT := $(shell awk -F'[ .]' '/Linux-Maximum:/{print $$2 "." $$3+1}' META)
9
10DKMSFILES := module include config zfs.release.in autogen.sh copy-builtin META AUTHORS \
11		COPYRIGHT LICENSE README.md CODE_OF_CONDUCT.md NEWS NOTICE RELEASES.md
12
13ifndef KVERS
14KVERS=$(shell uname -r)
15endif
16
17non_epoch_version=$(shell echo $(KVERS) | perl -pe 's/^\d+://')
18PACKAGE=openzfs-zfs
19pmodules = $(PACKAGE)-modules-$(non_epoch_version)
20
21export DEB_BUILD_MAINT_OPTIONS = hardening=+all
22
23NUM_CPUS = $(shell nproc 2>/dev/null)
24PARALLEL = $(subst parallel=,,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
25NJOBS    = -j$(or $(PARALLEL),$(NUM_CPUS),1)
26
27%:
28	dh $@ --with autoreconf,dkms,python3,sphinxdoc
29
30override_dh_autoreconf:
31	@# Embed the downstream version in the module.
32	@sed -e 's/^Version:.*/Version:       $(DEB_VERSION_UPSTREAM)/' -i.orig META
33
34	dh_autoreconf
35
36override_dh_auto_configure:
37	@# Build the userland, but don't build the kernel modules.
38	dh_auto_configure -- @CFGOPTS@ \
39	  --bindir=/usr/bin \
40	  --sbindir=/sbin \
41	  --libdir=/lib/"$(DEB_HOST_MULTIARCH)" \
42	  --with-udevdir=/lib/udev \
43	  --with-zfsexecdir=/usr/lib/zfs-linux \
44	  --enable-systemd \
45	  --enable-pyzfs \
46	  --with-python=python3 \
47	  --with-pammoduledir='/lib/$(DEB_HOST_MULTIARCH)/security' \
48	  --with-pkgconfigdir='/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig' \
49	  --with-systemdunitdir=/lib/systemd/system \
50	  --with-systemdpresetdir=/lib/systemd/system-preset \
51	  --with-systemdgeneratordir=/lib/systemd/system-generators \
52	  --with-config=user
53
54	for i in $(wildcard $(CURDIR)/debian/*.install.in) ; do \
55		basename "$$i" | grep _KVERS_ && continue ; \
56		sed 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' "$$i" > "$${i%%.in}" ; \
57	done
58
59override_dh_gencontrol:
60	dh_gencontrol -- -Vlinux:Recommends="linux-libc-dev (<< $(LINUX_NEXT)~), linux-libc-dev (>= $(LINUX_MIN)~),"
61
62override_dh_auto_build:
63	@# Get a bare copy of the source code for DKMS.
64	@# This creates the $(CURDIR)/$(NAME)-$(DEB_VERSION_UPSTREAM)/ tree, which does not
65	@# contain the userland sources. NB: Remove-userland-dist-rules.patch
66	$(MAKE) distdir
67
68	dh_auto_build
69
70override_dh_auto_install:
71	@# Install the utilities.
72	$(MAKE) install DESTDIR='$(CURDIR)/debian/tmp'
73
74	# Use upstream's bash completion
75	install -D -t '$(CURDIR)/debian/tmp/usr/share/bash-completion/completions/' \
76		'$(CURDIR)/contrib/bash_completion.d/zfs'
77
78	# Move from bin_dir to /usr/sbin
79	# Remove suffix (.py) as per policy 10.4 - Scripts
80	# https://www.debian.org/doc/debian-policy/ch-files.html#s-scripts
81	mkdir -p '$(CURDIR)/debian/tmp/usr/sbin/'
82	mv '$(CURDIR)/debian/tmp/usr/bin/arc_summary' '$(CURDIR)/debian/tmp/usr/sbin/arc_summary'
83	mv '$(CURDIR)/debian/tmp/usr/bin/arcstat' '$(CURDIR)/debian/tmp/usr/sbin/arcstat'
84	mv '$(CURDIR)/debian/tmp/usr/bin/dbufstat' '$(CURDIR)/debian/tmp/usr/sbin/dbufstat'
85	mv '$(CURDIR)/debian/tmp/usr/bin/zilstat' '$(CURDIR)/debian/tmp/usr/sbin/zilstat'
86
87	@# Zed has dependencies outside of the system root.
88	mv '$(CURDIR)/debian/tmp/sbin/zed' '$(CURDIR)/debian/tmp/usr/sbin/zed'
89
90	@# Install the DKMS source.
91	@# We only want the files needed to build the modules
92	install -D -t '$(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/scripts' \
93		'$(CURDIR)/scripts/enum-extract.pl' \
94		'$(CURDIR)/scripts/dkms.postbuild'
95	$(foreach file,$(DKMSFILES),mv '$(CURDIR)/$(NAME)-$(DEB_VERSION_UPSTREAM)/$(file)' '$(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)' || exit 1;)
96
97	@# Only ever build Linux modules
98	echo 'SUBDIRS = linux' > '$(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/include/os/Makefile.am'
99
100	@# Hellish awk line:
101	@#  * Deletes from configure.ac the parts not needed for building the kernel module
102	@#     * It deletes from inside AC_CONFIG_FILES([]) everything except:
103	@#        - Makefile$
104	@#        - include/(Makefile|sys|os/(Makefile|linux))
105	@#        - module/
106	@#        - zfs.release$
107	@#  * Takes care of spaces and tabs
108	@#  * Remove reference to ZFS_AC_PACKAGE
109	awk '/^AC_CONFIG_FILES\(\[/,/^\]\)/ {\
110		if ($$0 !~ /^(AC_CONFIG_FILES\(\[([ \t]+)?$$|\]\)([ \t]+)?$$|([ \t]+)?(include\/(Makefile|sys|os\/(Makefile|linux))|module\/|Makefile([ \t]+)?$$|zfs\.release([ \t]+)?$$))/) \
111		{next} } {print}' \
112		'$(CURDIR)/$(NAME)-$(DEB_VERSION_UPSTREAM)/configure.ac' | sed '/ZFS_AC_PACKAGE/d' > '$(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/configure.ac'
113	@# Set "SUBDIRS = module include" for CONFIG_KERNEL and remove SUBDIRS for all other configs.
114	@# Do not regenerate zfs_gitrev.h during dkms build
115	sed '1,/CONFIG_KERNEL/s/SUBDIRS.*=.*//g;s/SUBDIRS.*=.*/SUBDIRS = module include/g;/make_gitrev.sh/d' \
116		'$(CURDIR)/$(NAME)-$(DEB_VERSION_UPSTREAM)/Makefile.am' > '$(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/Makefile.am'
117	@# Sanity test
118	grep -q 'SUBDIRS = module include' '$(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/Makefile.am'
119	sed -i '/rpm.Makefile/d' $(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/Makefile.am
120	sed -i '/cmd.Makefile/d' $(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/Makefile.am
121	sed -i '/contrib.Makefile/d' $(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/Makefile.am
122	sed -i '/etc.Makefile/d' $(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/Makefile.am
123	sed -i '/lib.Makefile/d' $(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/Makefile.am
124	sed -i '/man.Makefile/d' $(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/Makefile.am
125	sed -i '/scripts.Makefile/d' $(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/Makefile.am
126	sed -i '/tests.Makefile/d' $(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/Makefile.am
127	sed -i '/udev.Makefile/d' $(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/Makefile.am
128	@# Run autogen on the stripped source tree
129	cd '$(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)'; ./autogen.sh
130	rm -fr '$(CURDIR)/debian/tmp/usr/src/$(NAME)-$(DEB_VERSION_UPSTREAM)/autom4te.cache'
131
132	for i in `ls $(CURDIR)/debian/tmp/lib/$(DEB_HOST_MULTIARCH)/*.so`; do \
133		ln -s '/lib/$(DEB_HOST_MULTIARCH)/'`readlink $${i}` '$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/'`basename $${i}`; \
134		rm $${i}; \
135	done
136
137	chmod a-x '$(CURDIR)/debian/tmp/etc/zfs/zfs-functions'
138	chmod a-x '$(CURDIR)/debian/tmp/etc/default/zfs'
139	chmod a-x '$(CURDIR)/debian/tmp/usr/share/bash-completion/completions/zfs'
140
141override_dh_python3:
142	dh_python3 -p openzfs-python3-pyzfs
143
144override_dh_dkms:
145	'$(CURDIR)/scripts/dkms.mkconf' -n $(NAME) -v $(DEB_VERSION_UPSTREAM) -f '$(CURDIR)/scripts/zfs-dkms.dkms'
146	dh_dkms
147	rm -f '$(CURDIR)/scripts/zfs-dkms.dkms'
148
149override_dh_makeshlibs:
150	dh_makeshlibs -a -V
151
152override_dh_strip:
153	dh_strip
154
155override_dh_auto_clean:
156	rm -rf zfs-$(DEB_VERSION_UPSTREAM)
157	dh_auto_clean
158	@if test -e META.orig; then mv META.orig META; fi
159
160override_dh_install:
161	find debian/tmp/lib -name '*.la' -delete
162	dh_install
163
164override_dh_missing:
165	dh_missing --fail-missing
166
167override_dh_installinit:
168	dh_installinit -r --no-restart-after-upgrade --name zfs-import
169	dh_installinit -r --no-restart-after-upgrade --name zfs-mount
170	dh_installinit -r --no-restart-after-upgrade --name zfs-load-key
171	dh_installinit -R --name zfs-share
172	dh_installinit -R --name zfs-zed
173
174override_dh_installsystemd:
175	mkdir -p debian/openzfs-zfsutils/lib/systemd/system
176	ln -sr /dev/null debian/openzfs-zfsutils/lib/systemd/system/zfs-import.service
177	dh_installsystemd --no-stop-on-upgrade -X zfs-zed.service
178	dh_installsystemd --name zfs-zed
179
180override_dh_installdocs:
181	dh_installdocs -A
182ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
183	http_proxy='127.0.0.1:9' sphinx-build -N -bhtml "$(CURDIR)/contrib/pyzfs/docs/source/" debian/openzfs-pyzfs-doc/usr/share/doc/openzfs-pyzfs-doc/html/
184endif
185
186# ------------
187
188override_dh_prep-deb-files:
189	for templ in $(wildcard $(CURDIR)/debian/*_KVERS_*.in); do \
190		sed -e 's/##KVERS##/$(KVERS)/g ; s/#KVERS#/$(KVERS)/g ; s/_KVERS_/$(KVERS)/g ; s/##KDREV##/$(KDREV)/g ; s/#KDREV#/$(KDREV)/g ; s/_KDREV_/$(KDREV)/g ; s/_ARCH_/$(DEB_HOST_ARCH)/' \
191		< $$templ > `echo $$templ | sed -e 's/_KVERS_/$(KVERS)/g ; s/_ARCH_/$(DEB_HOST_ARCH)/g ; s/\.in$$//'` ; \
192	done
193	sed -e 's/##KVERS##/$(KVERS)/g ; s/#KVERS#/$(KVERS)/g ; s/_KVERS_/$(KVERS)/g ; s/##KDREV##/$(KDREV)/g ; s/#KDREV#/$(KDREV)/g ; s/_KDREV_/$(KDREV)/g ; s/_ARCH_/$(DEB_HOST_ARCH)/g' \
194	< debian/control.modules.in > debian/control
195
196override_dh_configure_modules: override_dh_configure_modules_stamp
197override_dh_configure_modules_stamp:
198	./configure @CFGOPTS@ \
199		--with-config=kernel \
200		--with-linux=$(KSRC) \
201		--with-linux-obj=$(KOBJ)
202	touch override_dh_configure_modules_stamp
203
204override_dh_binary-modules: override_dh_prep-deb-files override_dh_configure_modules
205	dh_testdir
206	dh_testroot
207	dh_prep
208
209	$(MAKE) $(NJOBS) -C $(CURDIR)/module modules
210
211	dh_install -p${pmodules}
212	dh_installdocs -p${pmodules}
213	dh_installchangelogs -p${pmodules}
214	dh_compress -p${pmodules}
215	dh_strip -p${pmodules}
216	dh_fixperms -p${pmodules}
217	dh_installdeb -p${pmodules}
218	dh_gencontrol -p${pmodules}
219	dh_md5sums -p${pmodules}
220	dh_builddeb -p${pmodules}
221
222debian-copyright:
223	cme update dpkg-copyright -file debian/copyright.cme
224