1# SPDX-License-Identifier: GPL-2.0-only
2# Makefile for the different targets used to generate full packages of a kernel
3
4include $(srctree)/scripts/Kbuild.include
5include $(srctree)/scripts/Makefile.lib
6
7KERNELPATH := kernel-$(subst -,_,$(KERNELRELEASE))
8# Include only those top-level files that are needed by make, plus the GPL copy
9TAR_CONTENT := Documentation LICENSES arch block certs crypto drivers fs \
10               include init io_uring ipc kernel lib mm net rust \
11               samples scripts security sound tools usr virt \
12               .config Makefile \
13               Kbuild Kconfig COPYING $(wildcard localversion*)
14
15quiet_cmd_src_tar = TAR     $(2).tar.gz
16      cmd_src_tar = \
17if test "$(objtree)" != "$(srctree)"; then \
18	echo >&2; \
19	echo >&2 "  ERROR:"; \
20	echo >&2 "  Building source tarball is not possible outside the"; \
21	echo >&2 "  kernel source tree. Don't set KBUILD_OUTPUT"; \
22	echo >&2; \
23	false; \
24fi ; \
25tar -I $(KGZIP) -c $(RCS_TAR_IGNORE) -f $(2).tar.gz \
26	--transform 's:^:$(2)/:S' $(TAR_CONTENT) $(3)
27
28# Git
29# ---------------------------------------------------------------------------
30
31filechk_HEAD = git -C $(srctree) rev-parse --verify HEAD 2>/dev/null
32
33.tmp_HEAD: check-git FORCE
34	$(call filechk,HEAD)
35
36PHONY += check-git
37check-git:
38	@if ! $(srctree)/scripts/check-git; then \
39		echo >&2 "error: creating source package requires git repository"; \
40		false; \
41	fi
42
43git-config-tar.gz   = -c tar.tar.gz.command="$(KGZIP)"
44git-config-tar.bz2  = -c tar.tar.bz2.command="$(KBZIP2)"
45git-config-tar.lzma = -c tar.tar.lzma.command="$(LZMA)"
46git-config-tar.xz   = -c tar.tar.xz.command="$(XZ)"
47git-config-tar.zst  = -c tar.tar.zst.command="$(ZSTD)"
48
49quiet_cmd_archive = ARCHIVE $@
50      cmd_archive = git -C $(srctree) $(git-config-tar$(suffix $@)) archive \
51                    --output=$$(realpath $@) $(archive-args)
52
53suffix-gzip  := .gz
54suffix-bzip2 := .bz2
55suffix-lzma  := .lzma
56suffix-xz    := .xz
57
58# Linux source tarball
59# ---------------------------------------------------------------------------
60
61linux-tarballs := $(addprefix linux, .tar.gz .tar.bz2 .tar.lzma .tar.xz)
62
63targets += $(linux-tarballs)
64$(linux-tarballs): archive-args = --prefix=linux/ $$(cat $<)
65$(linux-tarballs): .tmp_HEAD FORCE
66	$(call if_changed,archive)
67
68# rpm-pkg srcrpm-pkg binrpm-pkg
69# ---------------------------------------------------------------------------
70
71quiet_cmd_mkspec = GEN     $@
72      cmd_mkspec = $(srctree)/scripts/package/mkspec > $@
73
74kernel.spec: FORCE
75	$(call cmd,mkspec)
76
77PHONY += rpm-sources
78rpm-sources: linux.tar.gz
79	$(Q)mkdir -p rpmbuild/SOURCES
80	$(Q)ln -f linux.tar.gz rpmbuild/SOURCES/linux.tar.gz
81	$(Q)cp $(KCONFIG_CONFIG) rpmbuild/SOURCES/config
82	$(Q)$(srctree)/scripts/package/gen-diff-patch rpmbuild/SOURCES/diff.patch
83
84PHONY += rpm-pkg srcrpm-pkg binrpm-pkg
85
86rpm-pkg:    private build-type := a
87srcrpm-pkg: private build-type := s
88binrpm-pkg: private build-type := b
89
90rpm-pkg srcrpm-pkg: rpm-sources
91rpm-pkg srcrpm-pkg binrpm-pkg: kernel.spec
92	+$(strip rpmbuild -b$(build-type) kernel.spec \
93	--define='_topdir $(abspath rpmbuild)' \
94	$(if $(filter a b, $(build-type)), \
95		--target $(UTS_MACHINE)-linux --build-in-place --noprep --define='_smp_mflags %{nil}' \
96		$$(rpm -q rpm >/dev/null 2>&1 || echo --nodeps)) \
97	$(if $(filter b, $(build-type)), \
98		--without devel) \
99	$(RPMOPTS))
100
101# deb-pkg srcdeb-pkg bindeb-pkg
102# ---------------------------------------------------------------------------
103
104KDEB_SOURCE_COMPRESS ?= gzip
105
106supported-deb-source-compress := gzip bzip2 lzma xz
107
108PHONY += linux.tar.unsupported-deb-src-compress
109linux.tar.unsupported-deb-src-compress:
110	@echo "error: KDEB_SOURCE_COMPRESS=$(KDEB_SOURCE_COMPRESS) is not supported. The supported values are: $(supported-deb-source-compress)" >&2
111	@false
112
113debian-orig-suffix := \
114    $(strip $(if $(filter $(supported-deb-source-compress), $(KDEB_SOURCE_COMPRESS)), \
115    $(suffix-$(KDEB_SOURCE_COMPRESS)),.unsupported-deb-src-compress))
116
117quiet_cmd_debianize = GEN     $@
118      cmd_debianize = $(srctree)/scripts/package/mkdebian $(mkdebian-opts)
119
120debian: FORCE
121	$(call cmd,debianize)
122
123PHONY += debian-orig
124debian-orig: private source = $(shell dpkg-parsechangelog -S Source)
125debian-orig: private version = $(shell dpkg-parsechangelog -S Version | sed 's/-[^-]*$$//')
126debian-orig: private orig-name = $(source)_$(version).orig.tar$(debian-orig-suffix)
127debian-orig: mkdebian-opts = --need-source
128debian-orig: linux.tar$(debian-orig-suffix) debian
129	$(Q)if [ "$(df  --output=target .. 2>/dev/null)" = "$(df --output=target $< 2>/dev/null)" ]; then \
130		ln -f $< ../$(orig-name); \
131	else \
132		cp $< ../$(orig-name); \
133	fi
134
135KBUILD_PKG_ROOTCMD ?= 'fakeroot -u'
136
137PHONY += deb-pkg srcdeb-pkg bindeb-pkg
138
139deb-pkg:    private build-type := source,binary
140srcdeb-pkg: private build-type := source
141bindeb-pkg: private build-type := binary
142
143deb-pkg srcdeb-pkg: debian-orig
144bindeb-pkg: debian
145deb-pkg srcdeb-pkg bindeb-pkg:
146	+$(strip dpkg-buildpackage \
147	--build=$(build-type) --no-pre-clean --unsigned-changes \
148	$(if $(findstring source, $(build-type)), \
149		--unsigned-source --compression=$(KDEB_SOURCE_COMPRESS)) \
150	$(if $(findstring binary, $(build-type)), \
151		--rules-file='$(MAKE) -f debian/rules' --jobs=1 -r$(KBUILD_PKG_ROOTCMD) -a$$(cat debian/arch), \
152		--no-check-builddeps) \
153	$(DPKG_FLAGS))
154
155# snap-pkg
156# ---------------------------------------------------------------------------
157PHONY += snap-pkg
158snap-pkg:
159	rm -rf $(objtree)/snap
160	mkdir $(objtree)/snap
161	$(MAKE) clean
162	$(call cmd,src_tar,$(KERNELPATH))
163	sed "s@KERNELRELEASE@$(KERNELRELEASE)@; \
164		s@SRCTREE@$(shell realpath $(KERNELPATH).tar.gz)@" \
165		$(srctree)/scripts/package/snapcraft.template > \
166		$(objtree)/snap/snapcraft.yaml
167	cd $(objtree)/snap && \
168	snapcraft --target-arch=$(UTS_MACHINE)
169
170# dir-pkg tar*-pkg - tarball targets
171# ---------------------------------------------------------------------------
172
173tar-install: FORCE
174	$(Q)$(MAKE) -f $(srctree)/Makefile
175	+$(Q)$(srctree)/scripts/package/buildtar $@
176
177compress-tar.gz  = -I "$(KGZIP)"
178compress-tar.bz2 = -I "$(KBZIP2)"
179compress-tar.xz  = -I "$(XZ)"
180compress-tar.zst = -I "$(ZSTD)"
181
182quiet_cmd_tar = TAR     $@
183      cmd_tar = cd $<; tar cf ../$@ $(compress-tar$(suffix $@)) --owner=root --group=root --sort=name *
184
185dir-tarballs := $(addprefix linux-$(KERNELRELEASE)-$(ARCH), .tar .tar.gz .tar.bz2 .tar.xz .tar.zst)
186
187$(dir-tarballs): tar-install
188	$(call cmd,tar)
189
190PHONY += dir-pkg
191dir-pkg: tar-install
192	@echo "Kernel tree successfully created in $<"
193
194PHONY += tar-pkg
195tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar
196	@:
197
198tar%-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar.% FORCE
199	@:
200
201# perf-tar*-src-pkg - generate a source tarball with perf source
202# ---------------------------------------------------------------------------
203
204.tmp_perf:
205	$(Q)mkdir .tmp_perf
206
207.tmp_perf/HEAD: .tmp_HEAD | .tmp_perf
208	$(call cmd,copy)
209
210quiet_cmd_perf_version_file = GEN     $@
211      cmd_perf_version_file = cd $(srctree)/tools/perf; util/PERF-VERSION-GEN $(dir $(abspath $@))
212
213# PERF-VERSION-FILE and .tmp_HEAD are independent, but this avoids updating the
214# timestamp of PERF-VERSION-FILE.
215# The best is to fix tools/perf/util/PERF-VERSION-GEN.
216.tmp_perf/PERF-VERSION-FILE: .tmp_HEAD $(srctree)/tools/perf/util/PERF-VERSION-GEN | .tmp_perf
217	$(call cmd,perf_version_file)
218
219perf-archive-args = --add-file=$$(realpath $(word 2, $^)) \
220	--add-file=$$(realpath $(word 3, $^)) \
221	$$(cat $(word 2, $^))^{tree} $$(cat $<)
222
223
224perf-tarballs := $(addprefix perf-$(KERNELVERSION), .tar .tar.gz .tar.bz2 .tar.xz .tar.zst)
225
226targets += $(perf-tarballs)
227$(perf-tarballs): archive-args = --prefix=perf-$(KERNELVERSION)/ $(perf-archive-args)
228$(perf-tarballs): tools/perf/MANIFEST .tmp_perf/HEAD .tmp_perf/PERF-VERSION-FILE FORCE
229	$(call if_changed,archive)
230
231PHONY += perf-tar-src-pkg
232perf-tar-src-pkg: perf-$(KERNELVERSION).tar
233	@:
234
235perf-tar%-src-pkg: perf-$(KERNELVERSION).tar.% FORCE
236	@:
237
238# Help text displayed when executing 'make help'
239# ---------------------------------------------------------------------------
240PHONY += help
241help:
242	@echo '  rpm-pkg             - Build both source and binary RPM kernel packages'
243	@echo '  srcrpm-pkg          - Build only the source kernel RPM package'
244	@echo '  binrpm-pkg          - Build only the binary kernel RPM package'
245	@echo '  deb-pkg             - Build both source and binary deb kernel packages'
246	@echo '  srcdeb-pkg          - Build only the source kernel deb package'
247	@echo '  bindeb-pkg          - Build only the binary kernel deb package'
248	@echo '  snap-pkg            - Build only the binary kernel snap package'
249	@echo '                        (will connect to external hosts)'
250	@echo '  dir-pkg             - Build the kernel as a plain directory structure'
251	@echo '  tar-pkg             - Build the kernel as an uncompressed tarball'
252	@echo '  targz-pkg           - Build the kernel as a gzip compressed tarball'
253	@echo '  tarbz2-pkg          - Build the kernel as a bzip2 compressed tarball'
254	@echo '  tarxz-pkg           - Build the kernel as a xz compressed tarball'
255	@echo '  tarzst-pkg          - Build the kernel as a zstd compressed tarball'
256	@echo '  perf-tar-src-pkg    - Build the perf source tarball with no compression'
257	@echo '  perf-targz-src-pkg  - Build the perf source tarball with gzip compression'
258	@echo '  perf-tarbz2-src-pkg - Build the perf source tarball with bz2 compression'
259	@echo '  perf-tarxz-src-pkg  - Build the perf source tarball with xz compression'
260	@echo '  perf-tarzst-src-pkg - Build the perf source tarball with zst compression'
261
262PHONY += FORCE
263FORCE:
264
265# Read all saved command lines and dependencies for the $(targets) we
266# may be building above, using $(if_changed{,_dep}). As an
267# optimization, we don't need to read them if the target does not
268# exist, we will rebuild anyway in that case.
269
270existing-targets := $(wildcard $(sort $(targets)))
271
272-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
273
274.PHONY: $(PHONY)
275