Skip to content

Commit c66b9bc

Browse files
andy-shevintel-lab-lkp
authored andcommitted
kbuild: Fix off-by-one error when generate a new version
When build on, for example, x86 using `make O=... -j64` the version in the built kernel comes from include/generated/compile.h, which is: #define UTS_VERSION "torvalds#351 SMP Fri Jan 24 18:46:34 EET 2020" While at the end the x86 specific Makefile prints the contents of the .version file: Kernel: arch/x86/boot/bzImage is ready (torvalds#352) Obviously the latter is not true. This happens because we first check compile.h and update it and then generate new version, which is incorrect flow: CHK include/generated/compile.h UPD include/generated/compile.h ... GEN .version In order to fix this, move the version generation from link-vmlinux.sh to scripts/version.sh and re-use it in init/Makefile. Additionally provide a unified way to get the current version of the build and use this in few callers. This will respect the KBUILD_BUILD_VERSION in case it's provided. Signed-off-by: Andy Shevchenko <[email protected]>
1 parent 6d5a828 commit c66b9bc

File tree

8 files changed

+60
-22
lines changed

8 files changed

+60
-22
lines changed

arch/microblaze/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ linux.bin.ub linux.bin.gz: linux.bin
8787
linux.bin: vmlinux
8888
linux.bin linux.bin.gz linux.bin.ub:
8989
$(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
90-
@echo 'Kernel: $(boot)/$@ is ready' ' (#'`cat .version`')'
90+
@echo 'Kernel: $(boot)/$@ is ready' ' (#'`$(srctree)/scripts/kversion.sh --show`')'
9191

9292
PHONY += simpleImage.$(DTB)
9393
simpleImage.$(DTB): vmlinux
9494
$(Q)$(MAKE) $(build)=$(boot) $(addprefix $(boot)/$@., ub unstrip strip)
95-
@echo 'Kernel: $(boot)/$@ is ready' ' (#'`cat .version`')'
95+
@echo 'Kernel: $(boot)/$@ is ready' ' (#'`$(srctree)/scripts/kversion.sh --show`')'
9696

9797
define archhelp
9898
echo '* linux.bin - Create raw binary'

arch/x86/boot/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ cmd_image = $(obj)/tools/build $(obj)/setup.bin $(obj)/vmlinux.bin \
8080

8181
$(obj)/bzImage: $(obj)/setup.bin $(obj)/vmlinux.bin $(obj)/tools/build FORCE
8282
$(call if_changed,image)
83-
@$(kecho) 'Kernel: $@ is ready' ' (#'`cat .version`')'
83+
@$(kecho) 'Kernel: $@ is ready' ' (#'`$(srctree)/scripts/kversion.sh --show`')'
8484

8585
OBJCOPYFLAGS_vmlinux.bin := -O binary -R .note -R .comment -S
8686
$(obj)/vmlinux.bin: $(obj)/compressed/vmlinux FORCE

init/Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ $(obj)/version.o: include/generated/compile.h
3131
chk_compile.h = :
3232
quiet_chk_compile.h = echo ' CHK $@'
3333
silent_chk_compile.h = :
34-
include/generated/compile.h: FORCE
34+
include/generated/compile.h: kversion FORCE
3535
@$($(quiet)chk_compile.h)
3636
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/mkcompile_h $@ \
3737
"$(UTS_MACHINE)" "$(CONFIG_SMP)" "$(CONFIG_PREEMPT)" \
3838
"$(CONFIG_PREEMPT_RT)" "$(CC) $(KBUILD_CFLAGS)"
39+
40+
quiet_cmd_kversion = CALL $<
41+
cmd_kversion = $(CONFIG_SHELL) $< --update
42+
43+
kversion: $(srctree)/scripts/kversion.sh FORCE
44+
$(call cmd,kversion)

scripts/kversion.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/sh -e
2+
3+
show_version() {
4+
local dotversion="$1"; shift
5+
6+
# Check if special build version is requested
7+
if [ -n "$KBUILD_BUILD_VERSION" ]; then
8+
echo "$KBUILD_BUILD_VERSION"
9+
else
10+
cat $dotversion 2>/dev/null || echo 1
11+
fi
12+
}
13+
14+
update_version() {
15+
local dotversion="$1"; shift
16+
17+
# Don't update local version if special build version is requested
18+
if [ -n "$KBUILD_BUILD_VERSION" ]; then return; fi
19+
20+
if [ -r $dotversion ]; then
21+
local version="$(expr 0$(cat $dotversion) + 1)"
22+
echo "$version" > $dotversion
23+
else
24+
rm -f $dotversion
25+
echo "1" > $dotversion
26+
fi
27+
}
28+
29+
VERSION_FILE_NAME=".version"
30+
31+
show=
32+
update=
33+
while [ "$#" -ge "1" ]; do
34+
case "$1" in
35+
--show) show=1 ;;
36+
--update) update=1 ;;
37+
--) break ;;
38+
esac
39+
shift
40+
done
41+
42+
if [ -n "$1" ]; then VERSION_FILE_NAME="$1"; fi
43+
44+
if [ -n "$show" ]; then show_version "$VERSION_FILE_NAME"; fi
45+
if [ -n "$update" ]; then update_version "$VERSION_FILE_NAME"; fi

scripts/link-vmlinux.sh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,6 @@ fi
228228
# We need access to CONFIG_ symbols
229229
. include/config/auto.conf
230230

231-
# Update version
232-
info GEN .version
233-
if [ -r .version ]; then
234-
VERSION=$(expr 0$(cat .version) + 1)
235-
echo $VERSION > .version
236-
else
237-
rm -f .version
238-
echo 1 > .version
239-
fi;
240-
241231
# final build of init/
242232
${MAKE} -f "${srctree}/scripts/Makefile.build" obj=init
243233

scripts/mkcompile_h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ set -f
2828
LC_ALL=C
2929
export LC_ALL
3030

31-
if [ -z "$KBUILD_BUILD_VERSION" ]; then
32-
VERSION=$(cat .version 2>/dev/null || echo 1)
33-
else
34-
VERSION=$KBUILD_BUILD_VERSION
35-
fi
36-
3731
if [ -z "$KBUILD_BUILD_TIMESTAMP" ]; then
3832
TIMESTAMP=`date`
3933
else
@@ -50,7 +44,10 @@ else
5044
LINUX_COMPILE_HOST=$KBUILD_BUILD_HOST
5145
fi
5246

47+
VERSION=$($srctree/scripts/kversion.sh --show)
48+
5349
UTS_VERSION="#$VERSION"
50+
5451
CONFIG_FLAGS=""
5552
if [ -n "$SMP" ] ; then CONFIG_FLAGS="SMP"; fi
5653
if [ -n "$PREEMPT" ] ; then CONFIG_FLAGS="$CONFIG_FLAGS PREEMPT"; fi

scripts/package/mkdebian

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ if [ -n "$KDEB_PKGVERSION" ]; then
9090
packageversion=$KDEB_PKGVERSION
9191
revision=${packageversion##*-}
9292
else
93-
revision=$(cat .version 2>/dev/null||echo 1)
93+
revision=$($srctree/scripts/kversion.sh --show)
9494
packageversion=$version-$revision
9595
fi
9696
sourcename=$KDEB_SOURCENAME

scripts/package/mkspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ sed -e '/^DEL/d' -e 's/^\t*//' <<EOF
4242
Name: kernel
4343
Summary: The Linux Kernel
4444
Version: $__KERNELRELEASE
45-
Release: $(cat .version 2>/dev/null || echo 1)
45+
Release: $($srctree/scripts/kversion.sh --show)
4646
License: GPL
4747
Group: System Environment/Kernel
4848
Vendor: The Linux Community

0 commit comments

Comments
 (0)