• 编译内核版本号带“+“问题


    编译内核后版本号中带"+"

    基于Linux-5.9.10版本。
    问题描述
    修改过内核,然后为了避免版本号与现有的一样,不好区分,因此加上了CONFIG_LOCALVERSION,然而发现编译后的版本号中总是带了"+“,对于某些python库比如disutils库中会对版本号读取,带了”+"可能会影响程序的正常运行。

    为何会带"+"号

    kernel源码路径下Makefile中:

    filechk_kernel.release = \
        echo "$(KERNELVERSION)$$($(CONFIG_SHELL) $(srctree)/scripts/setlocalversion $(srctree))"
    
    • 1
    • 2

    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"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    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
    
    # 省略一堆代码
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    那么一种粗暴的解法就是,修改顶层Makefile,添加–save-scmversion参数,这样就不用该config配置

    filechk_kernel.release = \
        echo "$(KERNELVERSION)$$($(CONFIG_SHELL) $(srctree)/scripts/setlocalversion --save-scmversion $(srctree))"
    
    • 1
    • 2
  • 相关阅读:
    如何在小程序首页设置标题栏文字
    Unity-CharacterController(角色控制器)
    栈(Stack)和队列(Queue)
    ceph分布式存储
    windows与Ubuntu实现文件夹共享
    Java常见面试题
    C++IO流详解
    [C++]IO流
    非常经典的电压掉电监测电路,你学废了吗?
    2023 年高教社杯全国大学生数学建模竞赛-E 题 黄河水沙监测数据分析详解+思路+Python代码
  • 原文地址:https://blog.csdn.net/qq_23662505/article/details/126500717