Skip to content

Commit

Permalink
kbuild: use git-archive for source package creation
Browse files Browse the repository at this point in the history
Commit 5c3d1d0 ("kbuild: add a tool to list files ignored by git")
added a new tool, scripts/list-gitignored. My intention was to create
source packages without cleaning the source tree, without relying on git.

Linus strongly objected to it, and suggested using 'git archive' instead.
[1] [2] [3]

This commit goes in that direction - Remove scripts/list-gitignored.c
and rewrites Makefiles and scripts to use 'git archive' for building
Debian and RPM source packages. It also makes 'make perf-tar*-src-pkg'
use 'git archive' again.

Going forward, building source packages is only possible in a git-managed
tree. Building binary packages does not require git.

[1]: https://lore.kernel.org/lkml/CAHk-=wi49sMaC7vY1yMagk7eqLK=1jHeHQ=yZ_k45P=xBccnmA@mail.gmail.com/
[2]: https://lore.kernel.org/lkml/CAHk-=wh5AixGsLeT0qH2oZHKq0FLUTbyTw4qY921L=PwYgoGVw@mail.gmail.com/
[3]: https://lore.kernel.org/lkml/CAHk-=wgM-W6Fu==EoAVCabxyX8eYBz9kNC88-tm9ExRQwA79UQ@mail.gmail.com/

Fixes: 5c3d1d0 ("kbuild: add a tool to list files ignored by git")
Fixes: e0ca167 ("kbuild: make perf-tar*-src-pkg work without relying on git")
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
  • Loading branch information
Masahiro Yamada committed Mar 16, 2023
1 parent 81f59a2 commit 05e96e9
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 1,155 deletions.
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,7 @@ no-dot-config-targets := $(clean-targets) \
cscope gtags TAGS tags help% %docs check% coccicheck \
$(version_h) headers headers_% archheaders archscripts \
%asm-generic kernelversion %src-pkg dt_binding_check \
outputmakefile rustavailable rustfmt rustfmtcheck \
scripts_package
outputmakefile rustavailable rustfmt rustfmtcheck
# Installation targets should not require compiler. Unfortunately, vdso_install
# is an exception where build artifacts may be updated. This must be fixed.
no-compiler-targets := $(no-dot-config-targets) install dtbs_install \
Expand Down Expand Up @@ -1656,10 +1655,6 @@ distclean: mrproper
%pkg: include/config/kernel.release FORCE
$(Q)$(MAKE) -f $(srctree)/scripts/Makefile.package $@

PHONY += scripts_package
scripts_package: scripts_basic
$(Q)$(MAKE) $(build)=scripts scripts/list-gitignored

# Brief documentation of the typical targets used
# ---------------------------------------------------------------------------

Expand Down
1 change: 0 additions & 1 deletion scripts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/generate_rust_target
/insert-sys-cert
/kallsyms
/list-gitignored
/module.lds
/recordmcount
/sign-file
Expand Down
2 changes: 1 addition & 1 deletion scripts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ HOSTCFLAGS_sorttable.o += -DMCOUNT_SORT_ENABLED
endif

# The following programs are only built on demand
hostprogs += list-gitignored unifdef
hostprogs += unifdef

# The module linker script is preprocessed on demand
targets += module.lds
Expand Down
146 changes: 68 additions & 78 deletions scripts/Makefile.package
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Makefile for the different targets used to generate full packages of a kernel

include $(srctree)/scripts/Kbuild.include
include $(srctree)/scripts/Makefile.lib

KERNELPATH := kernel-$(subst -,_,$(KERNELRELEASE))
KBUILD_PKG_ROOTCMD ?="fakeroot -u"
Expand All @@ -26,54 +27,46 @@ fi ; \
tar -I $(KGZIP) -c $(RCS_TAR_IGNORE) -f $(2).tar.gz \
--transform 's:^:$(2)/:S' $(TAR_CONTENT) $(3)

# .tmp_filelist .tmp_filelist_exclude
# tarball compression
# ---------------------------------------------------------------------------

scripts/list-gitignored: FORCE
$(Q)$(MAKE) -f $(srctree)/Makefile scripts_package
%.tar.gz: %.tar
$(call cmd,gzip)

# 1f5d3a6b6532e25a5cdf1f311956b2b03d343a48 removed '*.rej' from .gitignore,
# but it is definitely a generated file.
filechk_filelist = \
$< --exclude='*.rej' --output=$@_exclude --prefix=./ --rootdir=$(srctree) --stat=-
%.tar.bz2: %.tar
$(call cmd,bzip2)

.tmp_filelist: scripts/list-gitignored FORCE
$(call filechk,filelist)
%.tar.xz: %.tar
$(call cmd,xzmisc)

# tarball
# ---------------------------------------------------------------------------

quiet_cmd_tar = TAR $@
cmd_tar = tar -c -f $@ $(tar-compress-opt) $(tar-exclude-opt) \
--owner=0 --group=0 --sort=name \
--transform 's:^\.:$*:S' -C $(tar-rootdir) .

tar-rootdir := $(srctree)
%.tar.zst: %.tar
$(call cmd,zstd)

%.tar:
$(call cmd,tar)

%.tar.gz: private tar-compress-opt := -I $(KGZIP)
%.tar.gz:
$(call cmd,tar)
# Git
# ---------------------------------------------------------------------------

%.tar.bz2: private tar-compress-opt := -I $(KBZIP2)
%.tar.bz2:
$(call cmd,tar)
filechk_HEAD = git -C $(srctree) rev-parse --verify HEAD 2>/dev/null

%.tar.xz: private tar-compress-opt := -I $(XZ)
%.tar.xz:
$(call cmd,tar)
.tmp_HEAD: check-git FORCE
$(call filechk,HEAD)

%.tar.zst: private tar-compress-opt := -I $(ZSTD)
%.tar.zst:
$(call cmd,tar)
PHONY += check-git
check-git:
@if ! $(srctree)/scripts/check-git; then \
echo >&2 "error: creating source package requires git repository"; \
false; \
fi

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

linux.tar.gz: tar-exclude-opt = --exclude=./$@ --exclude-from=$<_exclude
linux.tar.gz: .tmp_filelist
quiet_cmd_archive_linux = ARCHIVE $@
cmd_archive_linux = \
git -C $(srctree) archive --output=$$(realpath $@) --prefix=$(basename $@)/ $$(cat $<)

targets += linux.tar
linux.tar: .tmp_HEAD FORCE
$(call if_changed,archive_linux)

# rpm-pkg
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -148,74 +141,62 @@ snap-pkg:
# dir-pkg tar*-pkg - tarball targets
# ---------------------------------------------------------------------------

tar-pkg-tarball = linux-$(KERNELRELEASE)-$(ARCH).$(1)
tar-pkg-phony = $(subst .,,$(1))-pkg

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 $<"

define tar-pkg-rule
PHONY += $(tar-pkg-phony)
$(tar-pkg-phony): $(tar-pkg-tarball)
PHONY += tar-pkg
tar-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar
@:

$(tar-pkg-tarball): private tar-rootdir := tar-install
$(tar-pkg-tarball): tar-install
endef

$(foreach x, tar tar.gz tar.bz2 tar.xz tar.zst, $(eval $(call tar-pkg-rule,$(x))))
tar%-pkg: linux-$(KERNELRELEASE)-$(ARCH).tar.% FORCE
@:

# perf-tar*-src-pkg - generate a source tarball with perf source
# ---------------------------------------------------------------------------

perf-tar-src-pkg-tarball = perf-$(KERNELVERSION).$(1)
perf-tar-src-pkg-phony = perf-$(subst .,,$(1))-src-pkg

quiet_cmd_stage_perf_src = STAGE $@
cmd_stage_perf_src = \
rm -rf $@; \
mkdir -p $@; \
tar -c -f - --exclude-from=$<_exclude -C $(srctree) --files-from=$(srctree)/tools/perf/MANIFEST | \
tar -x -f - -C $@

.tmp_perf: .tmp_filelist
$(call cmd,stage_perf_src)

filechk_perf_head = \
if test -z "$(git -C $(srctree) rev-parse --show-cdup 2>/dev/null)" && \
head=$$(git -C $(srctree) rev-parse --verify HEAD 2>/dev/null); then \
echo $$head; \
else \
echo "not a git tree"; \
fi
.tmp_perf:
$(Q)mkdir .tmp_perf

.tmp_perf/HEAD: .tmp_perf FORCE
$(call filechk,perf_head)
.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 HEAD are independent, but this avoids updating the
# 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_perf/HEAD $(srctree)/tools/perf/util/PERF-VERSION-GEN
.tmp_perf/PERF-VERSION-FILE: .tmp_HEAD $(srctree)/tools/perf/util/PERF-VERSION-GEN | .tmp_perf
$(call cmd,perf_version_file)

define perf-tar-src-pkg-rule
PHONY += $(perf-tar-src-pkg-phony)
$(perf-tar-src-pkg-phony): $(perf-tar-src-pkg-tarball)
@:
quiet_cmd_archive_perf = ARCHIVE $@
cmd_archive_perf = \
git -C $(srctree) archive --output=$$(realpath $@) --prefix=$(basename $@)/ \
--add-file=$$(realpath $(word 2, $^)) \
--add-file=$$(realpath $(word 3, $^)) \
$$(cat $(word 2, $^))^{tree} $$(cat $<)

$(perf-tar-src-pkg-tarball): private tar-rootdir := .tmp_perf
$(perf-tar-src-pkg-tarball): .tmp_filelist .tmp_perf/HEAD .tmp_perf/PERF-VERSION-FILE
endef
targets += perf-$(KERNELVERSION).tar
perf-$(KERNELVERSION).tar: tools/perf/MANIFEST .tmp_perf/HEAD .tmp_perf/PERF-VERSION-FILE FORCE
$(call if_changed,archive_perf)

PHONY += perf-tar-src-pkg
perf-tar-src-pkg: perf-$(KERNELVERSION).tar
@:

$(foreach x, tar tar.gz tar.bz2 tar.xz tar.zst, $(eval $(call perf-tar-src-pkg-rule,$(x))))
perf-tar%-src-pkg: perf-$(KERNELVERSION).tar.% FORCE
@:

# Help text displayed when executing 'make help'
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -243,4 +224,13 @@ help:
PHONY += FORCE
FORCE:

# 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)

.PHONY: $(PHONY)
14 changes: 14 additions & 0 deletions scripts/check-git
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-only
#
# succeed if we are in a git repository

srctree="$(dirname $0)/.."

if ! git -C "${srctree}" rev-parse --verify HEAD >/dev/null 2>/dev/null; then
exit 1
fi

if ! test -z $(git -C "${srctree}" rev-parse --show-cdup 2>/dev/null); then
exit 1
fi
Loading

0 comments on commit 05e96e9

Please sign in to comment.