xref: /linux-6.15/scripts/Makefile.package (revision 31f735c6)
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*)
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# Git
30# ---------------------------------------------------------------------------
31
32filechk_HEAD = git -C $(srctree) rev-parse --verify HEAD 2>/dev/null
33
34.tmp_HEAD: check-git FORCE
35	$(call filechk,HEAD)
36
37PHONY += check-git
38check-git:
39	@if ! $(srctree)/scripts/check-git; then \
40		echo >&2 "error: creating source package requires git repository"; \
41		false; \
42	fi
43
44git-config-tar.gz  = -c tar.tar.gz.command="$(KGZIP)"
45git-config-tar.bz2 = -c tar.tar.bz2.command="$(KBZIP2)"
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 $@) --prefix=$(basename $@)/ $(archive-args)
52
53# Linux source tarball
54# ---------------------------------------------------------------------------
55
56linux-tarballs := $(addprefix linux, .tar.gz)
57
58targets += $(linux-tarballs)
59$(linux-tarballs): archive-args = $$(cat $<)
60$(linux-tarballs): .tmp_HEAD FORCE
61	$(call if_changed,archive)
62
63# rpm-pkg
64# ---------------------------------------------------------------------------
65PHONY += rpm-pkg
66rpm-pkg: srpm = $(shell rpmspec --srpm --query --queryformat='%{name}-%{VERSION}-%{RELEASE}.src.rpm' kernel.spec)
67rpm-pkg: srcrpm-pkg
68	+rpmbuild $(RPMOPTS) --target $(UTS_MACHINE)-linux -rb $(srpm) \
69	--define='_smp_mflags %{nil}'
70
71# srcrpm-pkg
72# ---------------------------------------------------------------------------
73PHONY += srcrpm-pkg
74srcrpm-pkg: linux.tar.gz
75	$(CONFIG_SHELL) $(MKSPEC) >$(objtree)/kernel.spec
76	+rpmbuild $(RPMOPTS) --target $(UTS_MACHINE)-linux -bs kernel.spec \
77	--define='_smp_mflags %{nil}' --define='_sourcedir rpmbuild/SOURCES' --define='_srcrpmdir .'
78
79# binrpm-pkg
80# ---------------------------------------------------------------------------
81PHONY += binrpm-pkg
82binrpm-pkg:
83	$(MAKE) -f $(srctree)/Makefile
84	$(CONFIG_SHELL) $(MKSPEC) prebuilt > $(objtree)/binkernel.spec
85	+rpmbuild $(RPMOPTS) --define "_builddir $(objtree)" --target \
86		$(UTS_MACHINE)-linux -bb $(objtree)/binkernel.spec
87
88# deb-pkg srcdeb-pkg bindeb-pkg
89# ---------------------------------------------------------------------------
90
91quiet_cmd_debianize = GEN     $@
92      cmd_debianize = $(srctree)/scripts/package/mkdebian $(mkdebian-opts)
93
94debian: FORCE
95	$(call cmd,debianize)
96
97PHONY += debian-orig
98debian-orig: private source = $(shell dpkg-parsechangelog -S Source)
99debian-orig: private version = $(shell dpkg-parsechangelog -S Version | sed 's/-[^-]*$$//')
100debian-orig: private orig-name = $(source)_$(version).orig.tar.gz
101debian-orig: mkdebian-opts = --need-source
102debian-orig: linux.tar.gz debian
103	$(Q)if [ "$(df  --output=target .. 2>/dev/null)" = "$(df --output=target $< 2>/dev/null)" ]; then \
104		ln -f $< ../$(orig-name); \
105	else \
106		cp $< ../$(orig-name); \
107	fi
108
109KBUILD_PKG_ROOTCMD ?= 'fakeroot -u'
110
111PHONY += deb-pkg srcdeb-pkg bindeb-pkg
112
113deb-pkg:    private build-type := source,binary
114srcdeb-pkg: private build-type := source
115bindeb-pkg: private build-type := binary
116
117deb-pkg srcdeb-pkg: debian-orig
118bindeb-pkg: debian
119deb-pkg srcdeb-pkg bindeb-pkg:
120	+$(strip dpkg-buildpackage \
121	--build=$(build-type) --no-pre-clean --unsigned-changes \
122	$(if $(findstring source, $(build-type)), \
123		--unsigned-source) \
124	$(if $(findstring binary, $(build-type)), \
125		-r$(KBUILD_PKG_ROOTCMD) -a$$(cat debian/arch), \
126		--no-check-builddeps) \
127	$(DPKG_FLAGS))
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-install: FORCE
152	$(Q)$(MAKE) -f $(srctree)/Makefile
153	+$(Q)$(srctree)/scripts/package/buildtar $@
154
155compress-tar.gz  = -I "$(KGZIP)"
156compress-tar.bz2 = -I "$(KBZIP2)"
157compress-tar.xz  = -I "$(XZ)"
158compress-tar.zst = -I "$(ZSTD)"
159
160quiet_cmd_tar = TAR     $@
161      cmd_tar = cd $<; tar cf ../$@ $(compress-tar$(suffix $@)) --owner=root --group=root --sort=name *
162
163dir-tarballs := $(addprefix linux-$(KERNELRELEASE)-$(ARCH), .tar .tar.gz .tar.bz2 .tar.xz .tar.zst)
164
165$(dir-tarballs): tar-install
166	$(call cmd,tar)
167
168PHONY += dir-pkg
169dir-pkg: tar-install
170	@echo "Kernel tree successfully created in $<"
171
172PHONY += tar-pkg
173tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar
174	@:
175
176tar%-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar.% FORCE
177	@:
178
179# perf-tar*-src-pkg - generate a source tarball with perf source
180# ---------------------------------------------------------------------------
181
182.tmp_perf:
183	$(Q)mkdir .tmp_perf
184
185.tmp_perf/HEAD: .tmp_HEAD | .tmp_perf
186	$(call cmd,copy)
187
188quiet_cmd_perf_version_file = GEN     $@
189      cmd_perf_version_file = cd $(srctree)/tools/perf; util/PERF-VERSION-GEN $(dir $(abspath $@))
190
191# PERF-VERSION-FILE and .tmp_HEAD are independent, but this avoids updating the
192# timestamp of PERF-VERSION-FILE.
193# The best is to fix tools/perf/util/PERF-VERSION-GEN.
194.tmp_perf/PERF-VERSION-FILE: .tmp_HEAD $(srctree)/tools/perf/util/PERF-VERSION-GEN | .tmp_perf
195	$(call cmd,perf_version_file)
196
197perf-archive-args = --add-file=$$(realpath $(word 2, $^)) \
198	--add-file=$$(realpath $(word 3, $^)) \
199	$$(cat $(word 2, $^))^{tree} $$(cat $<)
200
201
202perf-tarballs := $(addprefix perf-$(KERNELVERSION), .tar .tar.gz .tar.bz2 .tar.xz .tar.zst)
203
204targets += $(perf-tarballs)
205$(perf-tarballs): archive-args = $(perf-archive-args)
206$(perf-tarballs): tools/perf/MANIFEST .tmp_perf/HEAD .tmp_perf/PERF-VERSION-FILE FORCE
207	$(call if_changed,archive)
208
209PHONY += perf-tar-src-pkg
210perf-tar-src-pkg: perf-$(KERNELVERSION).tar
211	@:
212
213perf-tar%-src-pkg: perf-$(KERNELVERSION).tar.% FORCE
214	@:
215
216# Help text displayed when executing 'make help'
217# ---------------------------------------------------------------------------
218PHONY += help
219help:
220	@echo '  rpm-pkg             - Build both source and binary RPM kernel packages'
221	@echo '  srcrpm-pkg          - Build only the source kernel RPM package'
222	@echo '  binrpm-pkg          - Build only the binary kernel RPM package'
223	@echo '  deb-pkg             - Build both source and binary deb kernel packages'
224	@echo '  srcdeb-pkg          - Build only the source kernel deb package'
225	@echo '  bindeb-pkg          - Build only the binary kernel deb package'
226	@echo '  snap-pkg            - Build only the binary kernel snap package'
227	@echo '                        (will connect to external hosts)'
228	@echo '  dir-pkg             - Build the kernel as a plain directory structure'
229	@echo '  tar-pkg             - Build the kernel as an uncompressed tarball'
230	@echo '  targz-pkg           - Build the kernel as a gzip compressed tarball'
231	@echo '  tarbz2-pkg          - Build the kernel as a bzip2 compressed tarball'
232	@echo '  tarxz-pkg           - Build the kernel as a xz compressed tarball'
233	@echo '  tarzst-pkg          - Build the kernel as a zstd compressed tarball'
234	@echo '  perf-tar-src-pkg    - Build the perf source tarball with no compression'
235	@echo '  perf-targz-src-pkg  - Build the perf source tarball with gzip compression'
236	@echo '  perf-tarbz2-src-pkg - Build the perf source tarball with bz2 compression'
237	@echo '  perf-tarxz-src-pkg  - Build the perf source tarball with xz compression'
238	@echo '  perf-tarzst-src-pkg - Build the perf source tarball with zst compression'
239
240PHONY += FORCE
241FORCE:
242
243# Read all saved command lines and dependencies for the $(targets) we
244# may be building above, using $(if_changed{,_dep}). As an
245# optimization, we don't need to read them if the target does not
246# exist, we will rebuild anyway in that case.
247
248existing-targets := $(wildcard $(sort $(targets)))
249
250-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
251
252.PHONY: $(PHONY)
253