编译内核后版本号中带"+"
基于Linux-5.9.10版本。
问题描述
修改过内核,然后为了避免版本号与现有的一样,不好区分,因此加上了CONFIG_LOCALVERSION,然而发现编译后的版本号中总是带了"+“,对于某些python库比如disutils库中会对版本号读取,带了”+"可能会影响程序的正常运行。
为何会带"+"号
kernel源码路径下Makefile中:
filechk_kernel.release = \
echo "$(KERNELVERSION)$$($(CONFIG_SHELL) $(srctree)/scripts/setlocalversion $(srctree))"
include/config/kernel.release
文件由顶层Makefile决定生成,setlocalversion中:
# localversion* files in the build and source directory
res="$(collect_files localversion*)"
if test ! "$srctree" -ef .; then
res="$res$(collect_files "$srctree"/localversion*)"
fi
# CONFIG_LOCALVERSION and LOCALVERSION (if set)
res="${res}${CONFIG_LOCALVERSION}${LOCALVERSION}"
# scm version string if not at a tagged commit
if test "$CONFIG_LOCALVERSION_AUTO" = "y"; then
# full scm version string
res="$res$(scm_version)"
else
# append a plus sign if the repository is not in a clean
# annotated or signed tagged state (as git describe only
# looks at signed or annotated tags - git tag -a/-s) and
# LOCALVERSION= is not specified
if test "${LOCALVERSION+set}" != "set"; then
scm=$(scm_version --short)
res="$res${scm:++}"
fi
fi
echo "$res"
setlocalversion执行最终打印res变量,res变量由三部分构成:localversion,CONFIG_LOCALVERSION,LOCALVERSION;
而其中,如果定义了CONFIG_LOCALVERSION_AUTO,则res由localversion和scm_version构成,scm_version可以由.scmversion文件定义或git提交的commit记录生成。
如果没有定义CONFIG_LOCALVERSION_AUTO,则res变量会res = $res${scm:++}
执行,scm只要定义了且非空,就会用"+“替换掉scm,所以版本号中带了”+"。
解决
.config中定义
CONFIG_LOCALVERSION_AUTO=y
即可
如果还想自己添加内核版本号字符串呢
CONFIG_LOCALVERSION="xxxxxx"
然后make distclean, cp config .config , make prepare, cat include/config/kernel.release查看效果
另一个解决方法
看下setlocalversion用法及参数检查
usage() {
echo "Usage: $0 [--save-scmversion] [srctree]" >&2
exit 1
}
scm_only=false
srctree=.
if test "$1" = "--save-scmversion"; then
scm_only=true
shift
fi
if test $# -gt 0; then
srctree=$1
shift
fi
if test $# -gt 0 -o ! -d "$srctree"; then
usage
fi
# 省略一堆代码
if $scm_only; then
if test ! -e .scmversion; then
res=$(scm_version)
echo "$res" >.scmversion
fi
exit
fi
# 省略一堆代码
那么一种粗暴的解法就是,修改顶层Makefile,添加–save-scmversion参数,这样就不用该config配置
filechk_kernel.release = \
echo "$(KERNELVERSION)$$($(CONFIG_SHELL) $(srctree)/scripts/setlocalversion --save-scmversion $(srctree))"