162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci# Makefile for the different targets used to generate full packages of a kernel
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciinclude $(srctree)/scripts/Kbuild.include
562306a36Sopenharmony_ciinclude $(srctree)/scripts/Makefile.lib
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciKERNELPATH := kernel-$(subst -,_,$(KERNELRELEASE))
862306a36Sopenharmony_ci# Include only those top-level files that are needed by make, plus the GPL copy
962306a36Sopenharmony_ciTAR_CONTENT := Documentation LICENSES arch block certs crypto drivers fs \
1062306a36Sopenharmony_ci               include init io_uring ipc kernel lib mm net rust \
1162306a36Sopenharmony_ci               samples scripts security sound tools usr virt \
1262306a36Sopenharmony_ci               .config Makefile \
1362306a36Sopenharmony_ci               Kbuild Kconfig COPYING $(wildcard localversion*)
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciquiet_cmd_src_tar = TAR     $(2).tar.gz
1662306a36Sopenharmony_ci      cmd_src_tar = \
1762306a36Sopenharmony_ciif test "$(objtree)" != "$(srctree)"; then \
1862306a36Sopenharmony_ci	echo >&2; \
1962306a36Sopenharmony_ci	echo >&2 "  ERROR:"; \
2062306a36Sopenharmony_ci	echo >&2 "  Building source tarball is not possible outside the"; \
2162306a36Sopenharmony_ci	echo >&2 "  kernel source tree. Don't set KBUILD_OUTPUT"; \
2262306a36Sopenharmony_ci	echo >&2; \
2362306a36Sopenharmony_ci	false; \
2462306a36Sopenharmony_cifi ; \
2562306a36Sopenharmony_citar -I $(KGZIP) -c $(RCS_TAR_IGNORE) -f $(2).tar.gz \
2662306a36Sopenharmony_ci	--transform 's:^:$(2)/:S' $(TAR_CONTENT) $(3)
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci# Git
2962306a36Sopenharmony_ci# ---------------------------------------------------------------------------
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cifilechk_HEAD = git -C $(srctree) rev-parse --verify HEAD 2>/dev/null
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci.tmp_HEAD: check-git FORCE
3462306a36Sopenharmony_ci	$(call filechk,HEAD)
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciPHONY += check-git
3762306a36Sopenharmony_cicheck-git:
3862306a36Sopenharmony_ci	@if ! $(srctree)/scripts/check-git; then \
3962306a36Sopenharmony_ci		echo >&2 "error: creating source package requires git repository"; \
4062306a36Sopenharmony_ci		false; \
4162306a36Sopenharmony_ci	fi
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cigit-config-tar.gz   = -c tar.tar.gz.command="$(KGZIP)"
4462306a36Sopenharmony_cigit-config-tar.bz2  = -c tar.tar.bz2.command="$(KBZIP2)"
4562306a36Sopenharmony_cigit-config-tar.lzma = -c tar.tar.lzma.command="$(LZMA)"
4662306a36Sopenharmony_cigit-config-tar.xz   = -c tar.tar.xz.command="$(XZ)"
4762306a36Sopenharmony_cigit-config-tar.zst  = -c tar.tar.zst.command="$(ZSTD)"
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciquiet_cmd_archive = ARCHIVE $@
5062306a36Sopenharmony_ci      cmd_archive = git -C $(srctree) $(git-config-tar$(suffix $@)) archive \
5162306a36Sopenharmony_ci                    --output=$$(realpath $@) $(archive-args)
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cisuffix-gzip  := .gz
5462306a36Sopenharmony_cisuffix-bzip2 := .bz2
5562306a36Sopenharmony_cisuffix-lzma  := .lzma
5662306a36Sopenharmony_cisuffix-xz    := .xz
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci# Linux source tarball
5962306a36Sopenharmony_ci# ---------------------------------------------------------------------------
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_cilinux-tarballs := $(addprefix linux, .tar.gz .tar.bz2 .tar.lzma .tar.xz)
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_citargets += $(linux-tarballs)
6462306a36Sopenharmony_ci$(linux-tarballs): archive-args = --prefix=linux/ $$(cat $<)
6562306a36Sopenharmony_ci$(linux-tarballs): .tmp_HEAD FORCE
6662306a36Sopenharmony_ci	$(call if_changed,archive)
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci# rpm-pkg srcrpm-pkg binrpm-pkg
6962306a36Sopenharmony_ci# ---------------------------------------------------------------------------
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciquiet_cmd_mkspec = GEN     $@
7262306a36Sopenharmony_ci      cmd_mkspec = $(srctree)/scripts/package/mkspec > $@
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_cikernel.spec: FORCE
7562306a36Sopenharmony_ci	$(call cmd,mkspec)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciPHONY += rpm-sources
7862306a36Sopenharmony_cirpm-sources: linux.tar.gz
7962306a36Sopenharmony_ci	$(Q)mkdir -p rpmbuild/SOURCES
8062306a36Sopenharmony_ci	$(Q)ln -f linux.tar.gz rpmbuild/SOURCES/linux.tar.gz
8162306a36Sopenharmony_ci	$(Q)cp $(KCONFIG_CONFIG) rpmbuild/SOURCES/config
8262306a36Sopenharmony_ci	$(Q)$(srctree)/scripts/package/gen-diff-patch rpmbuild/SOURCES/diff.patch
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciPHONY += rpm-pkg srcrpm-pkg binrpm-pkg
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_cirpm-pkg:    private build-type := a
8762306a36Sopenharmony_cisrcrpm-pkg: private build-type := s
8862306a36Sopenharmony_cibinrpm-pkg: private build-type := b
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_cirpm-pkg srcrpm-pkg: rpm-sources
9162306a36Sopenharmony_cirpm-pkg srcrpm-pkg binrpm-pkg: kernel.spec
9262306a36Sopenharmony_ci	+$(strip rpmbuild -b$(build-type) kernel.spec \
9362306a36Sopenharmony_ci	--define='_topdir $(abspath rpmbuild)' \
9462306a36Sopenharmony_ci	$(if $(filter a b, $(build-type)), \
9562306a36Sopenharmony_ci		--target $(UTS_MACHINE)-linux --build-in-place --noprep --define='_smp_mflags %{nil}' \
9662306a36Sopenharmony_ci		$$(rpm -q rpm >/dev/null 2>&1 || echo --nodeps)) \
9762306a36Sopenharmony_ci	$(if $(filter b, $(build-type)), \
9862306a36Sopenharmony_ci		--without devel) \
9962306a36Sopenharmony_ci	$(RPMOPTS))
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci# deb-pkg srcdeb-pkg bindeb-pkg
10262306a36Sopenharmony_ci# ---------------------------------------------------------------------------
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ciKDEB_SOURCE_COMPRESS ?= gzip
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_cisupported-deb-source-compress := gzip bzip2 lzma xz
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ciPHONY += linux.tar.unsupported-deb-src-compress
10962306a36Sopenharmony_cilinux.tar.unsupported-deb-src-compress:
11062306a36Sopenharmony_ci	@echo "error: KDEB_SOURCE_COMPRESS=$(KDEB_SOURCE_COMPRESS) is not supported. The supported values are: $(supported-deb-source-compress)" >&2
11162306a36Sopenharmony_ci	@false
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cidebian-orig-suffix := \
11462306a36Sopenharmony_ci    $(strip $(if $(filter $(supported-deb-source-compress), $(KDEB_SOURCE_COMPRESS)), \
11562306a36Sopenharmony_ci    $(suffix-$(KDEB_SOURCE_COMPRESS)),.unsupported-deb-src-compress))
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ciquiet_cmd_debianize = GEN     $@
11862306a36Sopenharmony_ci      cmd_debianize = $(srctree)/scripts/package/mkdebian $(mkdebian-opts)
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cidebian: FORCE
12162306a36Sopenharmony_ci	$(call cmd,debianize)
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ciPHONY += debian-orig
12462306a36Sopenharmony_cidebian-orig: private source = $(shell dpkg-parsechangelog -S Source)
12562306a36Sopenharmony_cidebian-orig: private version = $(shell dpkg-parsechangelog -S Version | sed 's/-[^-]*$$//')
12662306a36Sopenharmony_cidebian-orig: private orig-name = $(source)_$(version).orig.tar$(debian-orig-suffix)
12762306a36Sopenharmony_cidebian-orig: mkdebian-opts = --need-source
12862306a36Sopenharmony_cidebian-orig: linux.tar$(debian-orig-suffix) debian
12962306a36Sopenharmony_ci	$(Q)if [ "$(df  --output=target .. 2>/dev/null)" = "$(df --output=target $< 2>/dev/null)" ]; then \
13062306a36Sopenharmony_ci		ln -f $< ../$(orig-name); \
13162306a36Sopenharmony_ci	else \
13262306a36Sopenharmony_ci		cp $< ../$(orig-name); \
13362306a36Sopenharmony_ci	fi
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ciKBUILD_PKG_ROOTCMD ?= 'fakeroot -u'
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ciPHONY += deb-pkg srcdeb-pkg bindeb-pkg
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_cideb-pkg:    private build-type := source,binary
14062306a36Sopenharmony_cisrcdeb-pkg: private build-type := source
14162306a36Sopenharmony_cibindeb-pkg: private build-type := binary
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_cideb-pkg srcdeb-pkg: debian-orig
14462306a36Sopenharmony_cibindeb-pkg: debian
14562306a36Sopenharmony_cideb-pkg srcdeb-pkg bindeb-pkg:
14662306a36Sopenharmony_ci	+$(strip dpkg-buildpackage \
14762306a36Sopenharmony_ci	--build=$(build-type) --no-pre-clean --unsigned-changes \
14862306a36Sopenharmony_ci	$(if $(findstring source, $(build-type)), \
14962306a36Sopenharmony_ci		--unsigned-source --compression=$(KDEB_SOURCE_COMPRESS)) \
15062306a36Sopenharmony_ci	$(if $(findstring binary, $(build-type)), \
15162306a36Sopenharmony_ci		--rules-file='$(MAKE) -f debian/rules' --jobs=1 -r$(KBUILD_PKG_ROOTCMD) -a$$(cat debian/arch), \
15262306a36Sopenharmony_ci		--no-check-builddeps) \
15362306a36Sopenharmony_ci	$(DPKG_FLAGS))
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci# snap-pkg
15662306a36Sopenharmony_ci# ---------------------------------------------------------------------------
15762306a36Sopenharmony_ciPHONY += snap-pkg
15862306a36Sopenharmony_cisnap-pkg:
15962306a36Sopenharmony_ci	rm -rf $(objtree)/snap
16062306a36Sopenharmony_ci	mkdir $(objtree)/snap
16162306a36Sopenharmony_ci	$(MAKE) clean
16262306a36Sopenharmony_ci	$(call cmd,src_tar,$(KERNELPATH))
16362306a36Sopenharmony_ci	sed "s@KERNELRELEASE@$(KERNELRELEASE)@; \
16462306a36Sopenharmony_ci		s@SRCTREE@$(shell realpath $(KERNELPATH).tar.gz)@" \
16562306a36Sopenharmony_ci		$(srctree)/scripts/package/snapcraft.template > \
16662306a36Sopenharmony_ci		$(objtree)/snap/snapcraft.yaml
16762306a36Sopenharmony_ci	cd $(objtree)/snap && \
16862306a36Sopenharmony_ci	snapcraft --target-arch=$(UTS_MACHINE)
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci# dir-pkg tar*-pkg - tarball targets
17162306a36Sopenharmony_ci# ---------------------------------------------------------------------------
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_citar-install: FORCE
17462306a36Sopenharmony_ci	$(Q)$(MAKE) -f $(srctree)/Makefile
17562306a36Sopenharmony_ci	+$(Q)$(srctree)/scripts/package/buildtar $@
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_cicompress-tar.gz  = -I "$(KGZIP)"
17862306a36Sopenharmony_cicompress-tar.bz2 = -I "$(KBZIP2)"
17962306a36Sopenharmony_cicompress-tar.xz  = -I "$(XZ)"
18062306a36Sopenharmony_cicompress-tar.zst = -I "$(ZSTD)"
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ciquiet_cmd_tar = TAR     $@
18362306a36Sopenharmony_ci      cmd_tar = cd $<; tar cf ../$@ $(compress-tar$(suffix $@)) --owner=root --group=root --sort=name *
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_cidir-tarballs := $(addprefix linux-$(KERNELRELEASE)-$(ARCH), .tar .tar.gz .tar.bz2 .tar.xz .tar.zst)
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci$(dir-tarballs): tar-install
18862306a36Sopenharmony_ci	$(call cmd,tar)
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ciPHONY += dir-pkg
19162306a36Sopenharmony_cidir-pkg: tar-install
19262306a36Sopenharmony_ci	@echo "Kernel tree successfully created in $<"
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ciPHONY += tar-pkg
19562306a36Sopenharmony_citar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar
19662306a36Sopenharmony_ci	@:
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_citar%-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar.% FORCE
19962306a36Sopenharmony_ci	@:
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci# perf-tar*-src-pkg - generate a source tarball with perf source
20262306a36Sopenharmony_ci# ---------------------------------------------------------------------------
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci.tmp_perf:
20562306a36Sopenharmony_ci	$(Q)mkdir .tmp_perf
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci.tmp_perf/HEAD: .tmp_HEAD | .tmp_perf
20862306a36Sopenharmony_ci	$(call cmd,copy)
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ciquiet_cmd_perf_version_file = GEN     $@
21162306a36Sopenharmony_ci      cmd_perf_version_file = cd $(srctree)/tools/perf; util/PERF-VERSION-GEN $(dir $(abspath $@))
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci# PERF-VERSION-FILE and .tmp_HEAD are independent, but this avoids updating the
21462306a36Sopenharmony_ci# timestamp of PERF-VERSION-FILE.
21562306a36Sopenharmony_ci# The best is to fix tools/perf/util/PERF-VERSION-GEN.
21662306a36Sopenharmony_ci.tmp_perf/PERF-VERSION-FILE: .tmp_HEAD $(srctree)/tools/perf/util/PERF-VERSION-GEN | .tmp_perf
21762306a36Sopenharmony_ci	$(call cmd,perf_version_file)
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ciperf-archive-args = --add-file=$$(realpath $(word 2, $^)) \
22062306a36Sopenharmony_ci	--add-file=$$(realpath $(word 3, $^)) \
22162306a36Sopenharmony_ci	$$(cat $(word 2, $^))^{tree} $$(cat $<)
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ciperf-tarballs := $(addprefix perf-$(KERNELVERSION), .tar .tar.gz .tar.bz2 .tar.xz .tar.zst)
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_citargets += $(perf-tarballs)
22762306a36Sopenharmony_ci$(perf-tarballs): archive-args = --prefix=perf-$(KERNELVERSION)/ $(perf-archive-args)
22862306a36Sopenharmony_ci$(perf-tarballs): tools/perf/MANIFEST .tmp_perf/HEAD .tmp_perf/PERF-VERSION-FILE FORCE
22962306a36Sopenharmony_ci	$(call if_changed,archive)
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ciPHONY += perf-tar-src-pkg
23262306a36Sopenharmony_ciperf-tar-src-pkg: perf-$(KERNELVERSION).tar
23362306a36Sopenharmony_ci	@:
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ciperf-tar%-src-pkg: perf-$(KERNELVERSION).tar.% FORCE
23662306a36Sopenharmony_ci	@:
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci# Help text displayed when executing 'make help'
23962306a36Sopenharmony_ci# ---------------------------------------------------------------------------
24062306a36Sopenharmony_ciPHONY += help
24162306a36Sopenharmony_cihelp:
24262306a36Sopenharmony_ci	@echo '  rpm-pkg             - Build both source and binary RPM kernel packages'
24362306a36Sopenharmony_ci	@echo '  srcrpm-pkg          - Build only the source kernel RPM package'
24462306a36Sopenharmony_ci	@echo '  binrpm-pkg          - Build only the binary kernel RPM package'
24562306a36Sopenharmony_ci	@echo '  deb-pkg             - Build both source and binary deb kernel packages'
24662306a36Sopenharmony_ci	@echo '  srcdeb-pkg          - Build only the source kernel deb package'
24762306a36Sopenharmony_ci	@echo '  bindeb-pkg          - Build only the binary kernel deb package'
24862306a36Sopenharmony_ci	@echo '  snap-pkg            - Build only the binary kernel snap package'
24962306a36Sopenharmony_ci	@echo '                        (will connect to external hosts)'
25062306a36Sopenharmony_ci	@echo '  dir-pkg             - Build the kernel as a plain directory structure'
25162306a36Sopenharmony_ci	@echo '  tar-pkg             - Build the kernel as an uncompressed tarball'
25262306a36Sopenharmony_ci	@echo '  targz-pkg           - Build the kernel as a gzip compressed tarball'
25362306a36Sopenharmony_ci	@echo '  tarbz2-pkg          - Build the kernel as a bzip2 compressed tarball'
25462306a36Sopenharmony_ci	@echo '  tarxz-pkg           - Build the kernel as a xz compressed tarball'
25562306a36Sopenharmony_ci	@echo '  tarzst-pkg          - Build the kernel as a zstd compressed tarball'
25662306a36Sopenharmony_ci	@echo '  perf-tar-src-pkg    - Build the perf source tarball with no compression'
25762306a36Sopenharmony_ci	@echo '  perf-targz-src-pkg  - Build the perf source tarball with gzip compression'
25862306a36Sopenharmony_ci	@echo '  perf-tarbz2-src-pkg - Build the perf source tarball with bz2 compression'
25962306a36Sopenharmony_ci	@echo '  perf-tarxz-src-pkg  - Build the perf source tarball with xz compression'
26062306a36Sopenharmony_ci	@echo '  perf-tarzst-src-pkg - Build the perf source tarball with zst compression'
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ciPHONY += FORCE
26362306a36Sopenharmony_ciFORCE:
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci# Read all saved command lines and dependencies for the $(targets) we
26662306a36Sopenharmony_ci# may be building above, using $(if_changed{,_dep}). As an
26762306a36Sopenharmony_ci# optimization, we don't need to read them if the target does not
26862306a36Sopenharmony_ci# exist, we will rebuild anyway in that case.
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ciexisting-targets := $(wildcard $(sort $(targets)))
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci.PHONY: $(PHONY)
275