You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在local.conf中通过代码修改Bitbake变量?解析错误求助

解决Bitbake local.conf中动态生成PERSISTENT_BINDS的解析错误

问题场景

想要通过ENTRIES变量动态生成PERSISTENT_BINDS的内容,直接在local.conf里写Python函数时触发解析错误:

bb.parse.ParseError: ParseError at /home/user/workspace/yocto/build/conf/local.conf:173: unparsed line: 'def add_persistent_binds(d):'

原错误代码:

# 想要修改的变量
PERSISTENT_BINDS += "${@add_persistent_binds(d)}"

def add_persistent_binds(d):
    bb.plain("Path: edit persistent bind list")
    binds = []
    for entry in oe.data.typed_value("ENTRIES", d):
        if not entry:
            continue
        ta, tb = line.split(None, 1)
        binds.append("/data/sys/" + ta + " /" + tb + "\n")
    return " ".join(binds)

ENTRIES配置示例:

ENTRIES ??= ""
ENTRIES[type] = "list"
ENTRIES[separator] = "\n"

ENTRIES = "\
    foo bar\n\
    dead beef\n\
"

预期效果:

PERSISTENT_BINDS = " \
    ...
    /data/sys/foo /bar\n\
    /data/sys/dead /beef\n\
"

错误原因

Bitbake配置文件(如local.conf)不支持直接写裸Python函数定义,必须用python <函数名>()块包裹函数实现;另外原代码存在变量名错误:line.split应为entry.split。

解决方法

方法1:正确的Python函数写法

将Python函数放在python块中,修正变量错误并调整返回格式适配Bitbake变量规则:

# 定义Python函数
python add_persistent_binds() {
    bb.plain("Path: edit persistent bind list")
    binds = []
    for entry in oe.data.typed_value("ENTRIES", d):
        entry = entry.strip()
        if not entry:
            continue
        ta, tb = entry.split(None, 1)
        binds.append(f"/data/sys/{ta} /{tb}")
    # 用空格分隔,Bitbake自动处理换行转义
    return " ".join(binds)
}

# 动态追加内容
PERSISTENT_BINDS += "${@add_persistent_binds(d)}"

方法2:Shell函数替代方案

若更习惯Shell语法,可改用Shell函数实现:

# 定义Shell函数
add_persistent_binds() {
    echo "Path: edit persistent bind list"
    while read -r line; do
        line=$(echo "$line" | xargs)
        [ -z "$line" ] && continue
        ta=$(echo "$line" | awk '{print $1}')
        tb=$(echo "$line" | awk '{print $2}')
        echo -n "/data/sys/$ta /$tb "
    done <<EOF
${ENTRIES}
EOF
}

# 动态追加内容
PERSISTENT_BINDS += "${@add_persistent_binds()}"

验证效果

配置完成后,执行bitbake -e | grep -A5 PERSISTENT_BINDS,可看到生成内容符合预期,同时ENTRIES变量仍可在其他场景正常使用。

内容的提问来源于stack exchange,提问作者Jib

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 10:57:03