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
5
6KERNELPATH := kernel-$(subst -,_,$(KERNELRELEASE))
7KBUILD_PKG_ROOTCMD ?="fakeroot -u"
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*)
14MKSPEC     := $(srctree)/scripts/package/mkspec
15
16quiet_cmd_src_tar = TAR     $(2).tar.gz
17      cmd_src_tar = \
18if test "$(objtree)" != "$(srctree)"; then \
19	echo >&2; \
20	echo >&2 "  ERROR:"; \
21	echo >&2 "  Building source tarball is not possible outside the"; \
22	echo >&2 "  kernel source tree. Don't set KBUILD_OUTPUT"; \
23	echo >&2; \
24	false; \
25fi ; \
26tar -I $(KGZIP) -c $(RCS_TAR_IGNORE) -f $(2).tar.gz \
27	--transform 's:^:$(2)/:S' $(TAR_CONTENT) $(3)
28
29# .tmp_filelist .tmp_filelist_exclude
30# ---------------------------------------------------------------------------
31
32scripts/list-gitignored: FORCE
33	$(Q)$(MAKE) -f $(srctree)/Makefile scripts_package
34
35# 1f5d3a6b6532e25a5cdf1f311956b2b03d343a48 removed '*.rej' from .gitignore,
36# but it is definitely a generated file.
37filechk_filelist = \
38	$< --exclude='*.rej' --output=$@_exclude --prefix=./ --rootdir=$(srctree) --stat=-
39
40.tmp_filelist: scripts/list-gitignored FORCE
41	$(call filechk,filelist)
42
43# tarball
44# ---------------------------------------------------------------------------
45
46quiet_cmd_tar = TAR     $@
47      cmd_tar = tar -c -f $@ $(tar-compress-opt) $(tar-exclude-opt) \
48                --owner=0 --group=0 --sort=name \
49                --transform 's:^\.:$*:S' -C $(tar-rootdir) .
50
51tar-rootdir := $(srctree)
52
53%.tar:
54	$(call cmd,tar)
55
56%.tar.gz: private tar-compress-opt := -I $(KGZIP)
57%.tar.gz:
58	$(call cmd,tar)
59
60%.tar.bz2: private tar-compress-opt := -I $(KBZIP2)
61%.tar.bz2:
62	$(call cmd,tar)
63
64%.tar.xz: private tar-compress-opt := -I $(XZ)
65%.tar.xz:
66	$(call cmd,tar)
67
68%.tar.zst: private tar-compress-opt := -I $(ZSTD)
69%.tar.zst:
70	$(call cmd,tar)
71
72# Linux source tarball
73# ---------------------------------------------------------------------------
74
75linux.tar.gz: tar-exclude-opt = --exclude=./$@ --exclude-from=$<_exclude
76linux.tar.gz: .tmp_filelist
77
78# rpm-pkg
79# ---------------------------------------------------------------------------
80PHONY += rpm-pkg
81rpm-pkg: srpm = $(shell rpmspec --srpm --query --queryformat='%{name}-%{VERSION}-%{RELEASE}.src.rpm' kernel.spec)
82rpm-pkg: srcrpm-pkg
83	+rpmbuild $(RPMOPTS) --target $(UTS_MACHINE)-linux -rb $(srpm) \
84	--define='_smp_mflags %{nil}'
85
86# srcrpm-pkg
87# ---------------------------------------------------------------------------
88PHONY += srcrpm-pkg
89srcrpm-pkg: linux.tar.gz
90	$(CONFIG_SHELL) $(MKSPEC) >$(objtree)/kernel.spec
91	+rpmbuild $(RPMOPTS) --target $(UTS_MACHINE)-linux -bs kernel.spec \
92	--define='_smp_mflags %{nil}' --define='_sourcedir .' --define='_srcrpmdir .'
93
94# binrpm-pkg
95# ---------------------------------------------------------------------------
96PHONY += binrpm-pkg
97binrpm-pkg:
98	$(MAKE) -f $(srctree)/Makefile
99	$(CONFIG_SHELL) $(MKSPEC) prebuilt > $(objtree)/binkernel.spec
100	+rpmbuild $(RPMOPTS) --define "_builddir $(objtree)" --target \
101		$(UTS_MACHINE)-linux -bb $(objtree)/binkernel.spec
102
103quiet_cmd_debianize = GEN     $@
104      cmd_debianize = $(srctree)/scripts/package/mkdebian
105
106debian: FORCE
107	$(call cmd,debianize)
108
109PHONY += debian-orig
110debian-orig: private source = $(shell dpkg-parsechangelog -S Source)
111debian-orig: private version = $(shell dpkg-parsechangelog -S Version | sed 's/-[^-]*$$//')
112debian-orig: private orig-name = $(source)_$(version).orig.tar.gz
113debian-orig: linux.tar.gz debian
114	$(Q)if [ "$(df  --output=target .. 2>/dev/null)" = "$(df --output=target $< 2>/dev/null)" ]; then \
115		ln -f $< ../$(orig-name); \
116	else \
117		cp $< ../$(orig-name); \
118	fi
119
120PHONY += deb-pkg
121deb-pkg: debian-orig
122	+dpkg-buildpackage -r$(KBUILD_PKG_ROOTCMD) -a$$(cat debian/arch) $(DPKG_FLAGS) \
123		--build=source,binary -nc -us -uc
124
125PHONY += bindeb-pkg
126bindeb-pkg: debian
127	+dpkg-buildpackage -r$(KBUILD_PKG_ROOTCMD) -a$$(cat debian/arch) $(DPKG_FLAGS) -b -nc -uc
128
129PHONY += intdeb-pkg
130intdeb-pkg:
131	+$(CONFIG_SHELL) $(srctree)/scripts/package/builddeb
132
133# snap-pkg
134# ---------------------------------------------------------------------------
135PHONY += snap-pkg
136snap-pkg:
137	rm -rf $(objtree)/snap
138	mkdir $(objtree)/snap
139	$(MAKE) clean
140	$(call cmd,src_tar,$(KERNELPATH))
141	sed "s@KERNELRELEASE@$(KERNELRELEASE)@; \
142		s@SRCTREE@$(shell realpath $(KERNELPATH).tar.gz)@" \
143		$(srctree)/scripts/package/snapcraft.template > \
144		$(objtree)/snap/snapcraft.yaml
145	cd $(objtree)/snap && \
146	snapcraft --target-arch=$(UTS_MACHINE)
147
148# dir-pkg tar*-pkg - tarball targets
149# ---------------------------------------------------------------------------
150
151tar-pkg-tarball = linux-$(KERNELRELEASE)-$(ARCH).$(1)
152tar-pkg-phony = $(subst .,,$(1))-pkg
153
154tar-install: FORCE
155	$(Q)$(MAKE) -f $(srctree)/Makefile
156	+$(Q)$(srctree)/scripts/package/buildtar $@
157
158PHONY += dir-pkg
159dir-pkg: tar-install
160	@echo "Kernel tree successfully created in $<"
161
162define tar-pkg-rule
163PHONY += $(tar-pkg-phony)
164$(tar-pkg-phony): $(tar-pkg-tarball)
165	@:
166
167$(tar-pkg-tarball): private tar-rootdir := tar-install
168$(tar-pkg-tarball): tar-install
169endef
170
171$(foreach x, tar tar.gz tar.bz2 tar.xz tar.zst, $(eval $(call tar-pkg-rule,$(x))))
172
173# perf-tar*-src-pkg - generate a source tarball with perf source
174# ---------------------------------------------------------------------------
175
176perf-tar-src-pkg-tarball = perf-$(KERNELVERSION).$(1)
177perf-tar-src-pkg-phony   = perf-$(subst .,,$(1))-src-pkg
178
179quiet_cmd_stage_perf_src = STAGE   $@
180      cmd_stage_perf_src = \
181	rm -rf $@; \
182	mkdir -p $@; \
183	tar -c -f - --exclude-from=$<_exclude -C $(srctree) --files-from=$(srctree)/tools/perf/MANIFEST | \
184	tar -x -f - -C $@
185
186.tmp_perf: .tmp_filelist
187	$(call cmd,stage_perf_src)
188
189filechk_perf_head = \
190	if test -z "$(git -C $(srctree) rev-parse --show-cdup 2>/dev/null)" && \
191	       head=$$(git -C $(srctree) rev-parse --verify HEAD 2>/dev/null); then \
192		echo $$head; \
193	else \
194		echo "not a git tree"; \
195	fi
196
197.tmp_perf/HEAD: .tmp_perf FORCE
198	$(call filechk,perf_head)
199
200quiet_cmd_perf_version_file = GEN     $@
201      cmd_perf_version_file = cd $(srctree)/tools/perf; util/PERF-VERSION-GEN $(dir $(abspath $@))
202
203# PERF-VERSION-FILE and HEAD are independent, but this avoids updating the
204# timestamp of PERF-VERSION-FILE.
205# The best is to fix tools/perf/util/PERF-VERSION-GEN.
206.tmp_perf/PERF-VERSION-FILE: .tmp_perf/HEAD $(srctree)/tools/perf/util/PERF-VERSION-GEN
207	$(call cmd,perf_version_file)
208
209define perf-tar-src-pkg-rule
210PHONY += $(perf-tar-src-pkg-phony)
211$(perf-tar-src-pkg-phony): $(perf-tar-src-pkg-tarball)
212	@:
213
214$(perf-tar-src-pkg-tarball): private tar-rootdir := .tmp_perf
215$(perf-tar-src-pkg-tarball): .tmp_filelist .tmp_perf/HEAD .tmp_perf/PERF-VERSION-FILE
216endef
217
218$(foreach x, tar tar.gz tar.bz2 tar.xz tar.zst, $(eval $(call perf-tar-src-pkg-rule,$(x))))
219
220# Help text displayed when executing 'make help'
221# ---------------------------------------------------------------------------
222PHONY += help
223help:
224	@echo '  rpm-pkg             - Build both source and binary RPM kernel packages'
225	@echo '  srcrpm-pkg          - Build only the source kernel RPM package'
226	@echo '  binrpm-pkg          - Build only the binary kernel RPM package'
227	@echo '  deb-pkg             - Build both source and binary deb kernel packages'
228	@echo '  bindeb-pkg          - Build only the binary kernel deb package'
229	@echo '  snap-pkg            - Build only the binary kernel snap package'
230	@echo '                        (will connect to external hosts)'
231	@echo '  dir-pkg             - Build the kernel as a plain directory structure'
232	@echo '  tar-pkg             - Build the kernel as an uncompressed tarball'
233	@echo '  targz-pkg           - Build the kernel as a gzip compressed tarball'
234	@echo '  tarbz2-pkg          - Build the kernel as a bzip2 compressed tarball'
235	@echo '  tarxz-pkg           - Build the kernel as a xz compressed tarball'
236	@echo '  tarzst-pkg          - Build the kernel as a zstd compressed tarball'
237	@echo '  perf-tar-src-pkg    - Build the perf source tarball with no compression'
238	@echo '  perf-targz-src-pkg  - Build the perf source tarball with gzip compression'
239	@echo '  perf-tarbz2-src-pkg - Build the perf source tarball with bz2 compression'
240	@echo '  perf-tarxz-src-pkg  - Build the perf source tarball with xz compression'
241	@echo '  perf-tarzst-src-pkg - Build the perf source tarball with zst compression'
242
243PHONY += FORCE
244FORCE:
245
246.PHONY: $(PHONY)
247