linux内核版本号添加字符/为何有时会自动添加“+”号以及怎么去掉

最近在TI 的DVSDK下写驱动模块时老受linux内核svn版本号问题的困扰,如"2.6.37-svn41"、"2.6.37-svn51"等等,svn版本变一次,从上面取下的代码内核版本就要变一次,这样造成原来驱动模块ko文件必须重新拷贝到新的lib/modules/2.6.37-svn51下,非常麻烦且不利于发版本。

因此找到一篇博文《去掉SVN管理kernel编译后版本自动变化》,见我的转载。

按其说法将arch/arm/configs/omap3_evm_defconfig里面

CONFIG_LOCALVERSION_AUTO=y

修改为#CONFIG_LOCALVERSION_AUTO=y

或#CONFIG_LOCALVERSION_AUTO is not set 

清理内核源码根目录下的.config文件后,重新make omap3_evm_defconfig或者make linux_config,CONFIG_LOCALVERSION_AUTO相关项虽然未设置但依旧被内建。

后来发现将其设置为:

CONFIG_LOCALVERSION_AUTO=n

才使其默认不选上,其原理研究后再补上。

但是这样做了之后,编译linux内核并引导发内核版本变成了"2.6.37+",多了个“+”号

首先顶层Makefile有一段代码

kernelrelease:
        @echo"$(KERNELVERSION)$$($(CONFIG_SHELL)$(srctree)/scripts/setlocalversion $(srctree))"
 
kernelversion:
        @echo $(KERNELVERSION)

这个就需要修改scripts/setlocalversion的脚本了,先来看一段代码

scm_version()
{
    local short
    short=false

    cd "$srctree"
    if test -e .scmversion; then
    cat .scmversion
    return
    fi
    if test "$1" = "--short"; then
        short=true
    fi
    # Check for git and a git repo.
    if test -d .git && head=`git rev-parse --verify --short HEAD 2>/dev/null`;then
        # If we are at a tagged commit (like "v2.6.30-rc6"), we ignore
        # it, because this version is defined in the top level Makefile.
        if [ -z "`git describe --exact-match 2>/dev/null`" ]; then
            # If only the short version is requested, don't bother
            # running further git commands
                if $short; then
                    echo "+"
                    return
                fi
        ……
        fi
        ……
    fi
}

这里有个 echo "+",那“2.6.37+”里面的“+”到底是这条语句产生的吗?开始看到有些博客说修改这里, 注释掉 "echo "+"",但并未起作用。需要认真分析一下,看第3个f语句,意思是至少有.git目录存在的话才会执行这后面的,但我们用的svn,没有.git的,所以这里的echo "+"并未执行。再看一段代码:

# 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

这里的if就是说如果CONFIG_LOCALVERSION_AUTO定义的话,会加上svn版本号,我们已经设为n了,所以会执行else部分, scm=$(scm_version --short)虽然将--short传给scm_version使得short=true,但.git不存在,所以scm_version并未打印出“+”,再来看 res="$res${scm:++}"这句话什么意思呢?scm:++实际上是说如果scm未定义的话,将使用默认值"+"也就是说:+表示使用默认值的意思,而第二个“+”表示默认值。所以将第二个“+”去掉即可。也就是改成 res="$res${scm:+}",即可打印不带“+”的版本号“2.6.37”。

原文地址:https://www.cnblogs.com/Rainingday/p/13943130.html