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