Skip to content
Snippets Groups Projects
Makefile.package 8.55 KiB
Newer Older
# SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds's avatar
Linus Torvalds committed
# Makefile for the different targets used to generate full packages of a kernel

include $(srctree)/scripts/Kbuild.include
include $(srctree)/scripts/Makefile.lib
Linus Torvalds's avatar
Linus Torvalds committed

KERNELPATH := kernel-$(subst -,_,$(KERNELRELEASE))
KBUILD_PKG_ROOTCMD ?="fakeroot -u"
# Include only those top-level files that are needed by make, plus the GPL copy
TAR_CONTENT := Documentation LICENSES arch block certs crypto drivers fs \
               include init io_uring ipc kernel lib mm net rust \
               samples scripts security sound tools usr virt \
               Kbuild Kconfig COPYING $(wildcard localversion*)
Linus Torvalds's avatar
Linus Torvalds committed
MKSPEC     := $(srctree)/scripts/package/mkspec

quiet_cmd_src_tar = TAR     $(2).tar.gz
      cmd_src_tar = \
if test "$(objtree)" != "$(srctree)"; then \
	echo >&2; \
	echo >&2 "  ERROR:"; \
	echo >&2 "  Building source tarball is not possible outside the"; \
	echo >&2 "  kernel source tree. Don't set KBUILD_OUTPUT"; \
tar -I $(KGZIP) -c $(RCS_TAR_IGNORE) -f $(2).tar.gz \
	--transform 's:^:$(2)/:S' $(TAR_CONTENT) $(3)
# ---------------------------------------------------------------------------

%.tar.gz: %.tar
	$(call cmd,gzip)
%.tar.bz2: %.tar
	$(call cmd,bzip2)
%.tar.xz: %.tar
	$(call cmd,xzmisc)
%.tar.zst: %.tar
	$(call cmd,zstd)
# Git
# ---------------------------------------------------------------------------
filechk_HEAD = git -C $(srctree) rev-parse --verify HEAD 2>/dev/null
.tmp_HEAD: check-git FORCE
	$(call filechk,HEAD)
PHONY += check-git
check-git:
	@if ! $(srctree)/scripts/check-git; then \
		echo >&2 "error: creating source package requires git repository"; \
		false; \
	fi
git-config-tar.gz  = -c tar.tar.gz.command="$(KGZIP)"
git-config-tar.bz2 = -c tar.tar.bz2.command="$(KBZIP2)"
git-config-tar.xz  = -c tar.tar.xz.command="$(XZ)"
git-config-tar.zst = -c tar.tar.zst.command="$(ZSTD)"

quiet_cmd_archive = ARCHIVE $@
      cmd_archive = git -C $(srctree) $(git-config-tar$(suffix $@)) archive \
                    --output=$$(realpath $@) --prefix=$(basename $@)/ $(archive-args)

# Linux source tarball
# ---------------------------------------------------------------------------

linux-tarballs := $(addprefix linux, .tar.gz)

targets += $(linux-tarballs)
$(linux-tarballs): archive-args = $$(cat $<)
$(linux-tarballs): .tmp_HEAD FORCE
	$(call if_changed,archive)
Linus Torvalds's avatar
Linus Torvalds committed
# rpm-pkg
# ---------------------------------------------------------------------------
rpm-pkg: srpm = $(shell rpmspec --srpm --query --queryformat='%{name}-%{VERSION}-%{RELEASE}.src.rpm' kernel.spec)
rpm-pkg: srcrpm-pkg
	+rpmbuild $(RPMOPTS) --target $(UTS_MACHINE)-linux -rb $(srpm) \
	--define='_smp_mflags %{nil}'
Linus Torvalds's avatar
Linus Torvalds committed

# srcrpm-pkg
# ---------------------------------------------------------------------------
PHONY += srcrpm-pkg
	$(CONFIG_SHELL) $(MKSPEC) >$(objtree)/kernel.spec
	+rpmbuild $(RPMOPTS) --target $(UTS_MACHINE)-linux -bs kernel.spec \
	--define='_smp_mflags %{nil}' --define='_sourcedir rpmbuild/SOURCES' --define='_srcrpmdir .'
Linus Torvalds's avatar
Linus Torvalds committed
# binrpm-pkg
# ---------------------------------------------------------------------------
PHONY += binrpm-pkg
binrpm-pkg:
	$(CONFIG_SHELL) $(MKSPEC) prebuilt > $(objtree)/binkernel.spec
	+rpmbuild $(RPMOPTS) --define "_builddir $(objtree)" --target \
		$(UTS_MACHINE)-linux -bb $(objtree)/binkernel.spec
quiet_cmd_debianize = GEN     $@
      cmd_debianize = $(srctree)/scripts/package/mkdebian $(mkdebian-opts)

debian: FORCE
	$(call cmd,debianize)

PHONY += debian-orig
debian-orig: private source = $(shell dpkg-parsechangelog -S Source)
debian-orig: private version = $(shell dpkg-parsechangelog -S Version | sed 's/-[^-]*$$//')
debian-orig: private orig-name = $(source)_$(version).orig.tar.gz
debian-orig: mkdebian-opts = --need-source
debian-orig: linux.tar.gz debian
	$(Q)if [ "$(df  --output=target .. 2>/dev/null)" = "$(df --output=target $< 2>/dev/null)" ]; then \
		ln -f $< ../$(orig-name); \
	else \
		cp $< ../$(orig-name); \
	fi
	+dpkg-buildpackage -r$(KBUILD_PKG_ROOTCMD) -a$$(cat debian/arch) $(DPKG_FLAGS) \
		--build=source,binary -nc -us -uc
	+dpkg-buildpackage -r$(KBUILD_PKG_ROOTCMD) -a$$(cat debian/arch) $(DPKG_FLAGS) -b -nc -uc
	+$(CONFIG_SHELL) $(srctree)/scripts/package/builddeb
Linus Torvalds's avatar
Linus Torvalds committed

# snap-pkg
# ---------------------------------------------------------------------------
	rm -rf $(objtree)/snap
	mkdir $(objtree)/snap
	$(MAKE) clean
	$(call cmd,src_tar,$(KERNELPATH))
	sed "s@KERNELRELEASE@$(KERNELRELEASE)@; \
		s@SRCTREE@$(shell realpath $(KERNELPATH).tar.gz)@" \
		$(srctree)/scripts/package/snapcraft.template > \
		$(objtree)/snap/snapcraft.yaml
	cd $(objtree)/snap && \
	snapcraft --target-arch=$(UTS_MACHINE)

# dir-pkg tar*-pkg - tarball targets
# ---------------------------------------------------------------------------

tar-install: FORCE
	$(Q)$(MAKE) -f $(srctree)/Makefile
	+$(Q)$(srctree)/scripts/package/buildtar $@

quiet_cmd_tar = TAR     $@
      cmd_tar = cd $<; tar cf ../$@ --owner=root --group=root --sort=name *

linux-$(KERNELRELEASE)-$(ARCH).tar: tar-install
	$(call cmd,tar)

PHONY += dir-pkg
dir-pkg: tar-install
	@echo "Kernel tree successfully created in $<"

PHONY += tar-pkg
tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar
tar%-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar.% FORCE
	@:
# perf-tar*-src-pkg - generate a source tarball with perf source
# ---------------------------------------------------------------------------

.tmp_perf:
	$(Q)mkdir .tmp_perf
.tmp_perf/HEAD: .tmp_HEAD | .tmp_perf
	$(call cmd,copy)

quiet_cmd_perf_version_file = GEN     $@
      cmd_perf_version_file = cd $(srctree)/tools/perf; util/PERF-VERSION-GEN $(dir $(abspath $@))

# PERF-VERSION-FILE and .tmp_HEAD are independent, but this avoids updating the
# timestamp of PERF-VERSION-FILE.
# The best is to fix tools/perf/util/PERF-VERSION-GEN.
.tmp_perf/PERF-VERSION-FILE: .tmp_HEAD $(srctree)/tools/perf/util/PERF-VERSION-GEN | .tmp_perf
perf-archive-args = --add-file=$$(realpath $(word 2, $^)) \
	--add-file=$$(realpath $(word 3, $^)) \
	$$(cat $(word 2, $^))^{tree} $$(cat $<)

perf-tarballs := $(addprefix perf-$(KERNELVERSION), .tar .tar.gz .tar.bz2 .tar.xz .tar.zst)

targets += $(perf-tarballs)
$(perf-tarballs): archive-args = $(perf-archive-args)
$(perf-tarballs): tools/perf/MANIFEST .tmp_perf/HEAD .tmp_perf/PERF-VERSION-FILE FORCE
	$(call if_changed,archive)

PHONY += perf-tar-src-pkg
perf-tar-src-pkg: perf-$(KERNELVERSION).tar
	@:
perf-tar%-src-pkg: perf-$(KERNELVERSION).tar.% FORCE
	@:
Linus Torvalds's avatar
Linus Torvalds committed
# Help text displayed when executing 'make help'
# ---------------------------------------------------------------------------
	@echo '  rpm-pkg             - Build both source and binary RPM kernel packages'
	@echo '  srcrpm-pkg          - Build only the source kernel RPM package'
	@echo '  binrpm-pkg          - Build only the binary kernel RPM package'
	@echo '  deb-pkg             - Build both source and binary deb kernel packages'
	@echo '  bindeb-pkg          - Build only the binary kernel deb package'
	@echo '  snap-pkg            - Build only the binary kernel snap package'
	@echo '                        (will connect to external hosts)'
	@echo '  dir-pkg             - Build the kernel as a plain directory structure'
	@echo '  tar-pkg             - Build the kernel as an uncompressed tarball'
	@echo '  targz-pkg           - Build the kernel as a gzip compressed tarball'
	@echo '  tarbz2-pkg          - Build the kernel as a bzip2 compressed tarball'
	@echo '  tarxz-pkg           - Build the kernel as a xz compressed tarball'
	@echo '  tarzst-pkg          - Build the kernel as a zstd compressed tarball'
	@echo '  perf-tar-src-pkg    - Build the perf source tarball with no compression'
	@echo '  perf-targz-src-pkg  - Build the perf source tarball with gzip compression'
	@echo '  perf-tarbz2-src-pkg - Build the perf source tarball with bz2 compression'
	@echo '  perf-tarxz-src-pkg  - Build the perf source tarball with xz compression'
	@echo '  perf-tarzst-src-pkg - Build the perf source tarball with zst compression'
# Read all saved command lines and dependencies for the $(targets) we
# may be building above, using $(if_changed{,_dep}). As an
# optimization, we don't need to read them if the target does not
# exist, we will rebuild anyway in that case.

existing-targets := $(wildcard $(sort $(targets)))

-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)