如何在 yocto .bb 配方中有条件地指定变量

How to conditionally specify a variable in yocto .bb recipe

我想做这样的事情:

if [ "${BB_NO_NETWORK}" == "1" ]; then
    SRCREV ?= "a2d1f0225692c7c054014ec4dfc4dd13a35245ee"
else
    SRCREV ?= "${AUTOREV}"
fi

但是我得到一个无法解析的错误

这是我的工作。

SRCREV_FORMAT = "${@bb.utils.contains('BB_NO_NETWORK', '1', "v16_pcmx", "${AUTOREV}", d)}"
SRCREV_d3v16 = "${@bb.utils.contains('BB_NO_NETWORK', '1', "a2d1f0225692c7c054014ec4dfc4dd13a35245ee", "${AUTOREV}", d)}"
SRCREV_v16 = "${@bb.utils.contains('BB_NO_NETWORK', '1', "a2d1f0225692c7c054014ec4dfc4dd13a35245ee", "${AUTOREV}", d)}"
SRCREV_pcmx-native = "${@bb.utils.contains('BB_NO_NETWORK', '1', "d09e0b91ae34d807d3df27908f045e58a52e0c4c", "${AUTOREV}", d)}"
SRCREV_pcmx-pn-pcmx = "${@bb.utils.contains('BB_NO_NETWORK', '1', "d09e0b91ae34d807d3df27908f045e58a52e0c4c", "${AUTOREV}", d)}"
SRCREV_pcmx = "${@bb.utils.contains('BB_NO_NETWORK', '1', "d09e0b91ae34d807d3df27908f045e58a52e0c4c", "${AUTOREV}", d)}"
SRCREV_pn-pcmx = "${@bb.utils.contains('BB_NO_NETWORK', '1', "d09e0b91ae34d807d3df27908f045e58a52e0c4c", "${AUTOREV}", d)}"
SRCREV = "${@bb.utils.contains('BB_NO_NETWORK', '1', "", "${AUTOREV}", d)}"

这次尝试失败:

SRCREV_FORMAT = "${@ "v16_pcmx" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK',TRUE)) else d.getVar('AUTOREV') }"
SRCREV_d3v16 = "${@ "a2d1f0225692c7c054014ec4dfc4dd13a35245ee" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK',TRUE)) else d.getVar('AUTOREV') }"
SRCREV_v16 = "${@ "a2d1f0225692c7c054014ec4dfc4dd13a35245ee" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK',TRUE)) else d.getVar('AUTOREV') }"
SRCREV_pcmx-native = "${@ "d09e0b91ae34d807d3df27908f045e58a52e0c4c" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK',TRUE)) else d.getVar('AUTOREV') }"
SRCREV_pcmx-pn-pcmx = "${@ "d09e0b91ae34d807d3df27908f045e58a52e0c4c" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK',TRUE)) else d.getVar('AUTOREV') }"
SRCREV_pcmx = "${@ "d09e0b91ae34d807d3df27908f045e58a52e0c4c" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK',TRUE)) else d.getVar('AUTOREV') }"
SRCREV_pn-pcmx = "${@ "d09e0b91ae34d807d3df27908f045e58a52e0c4c" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK',TRUE)) else d.getVar('AUTOREV') }"
SRCREV = "${@ "" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK',TRUE)) else d.getVar('AUTOREV') }"

这也失败了

SRCREV_FORMAT = "${@ "v16_pcmx" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK')) else d.getVar('AUTOREV') }"
SRCREV_d3v16 = "${@ "a2d1f0225692c7c054014ec4dfc4dd13a35245ee" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK')) else d.getVar('AUTOREV') }"
SRCREV_v16 = "${@ "a2d1f0225692c7c054014ec4dfc4dd13a35245ee" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK')) else d.getVar('AUTOREV') }"
SRCREV_pcmx-native = "${@ "d09e0b91ae34d807d3df27908f045e58a52e0c4c" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK')) else d.getVar('AUTOREV') }"
SRCREV_pcmx-pn-pcmx = "${@ "d09e0b91ae34d807d3df27908f045e58a52e0c4c" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK')) else d.getVar('AUTOREV') }"
SRCREV_pcmx = "${@ "d09e0b91ae34d807d3df27908f045e58a52e0c4c" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK')) else d.getVar('AUTOREV') }"
SRCREV_pn-pcmx = "${@ "d09e0b91ae34d807d3df27908f045e58a52e0c4c" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK')) else d.getVar('AUTOREV') }"
SRCREV = "${@ "" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK')) else d.getVar('AUTOREV') }"

这个错误看起来像这样:

bb.data_smart.ExpansionError: Failure expanding variable SRCREV_v16, expression was ${@ "a2d1f0225692c7c054014ec4dfc4dd13a35245ee" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK')) else d.getVar('AUTOREV') } which triggered exception TypeError: getVar() missing 1 required positional argument: 'expand'

最后,这也失败了:

SRCREV_FORMAT = "${@'v16_pcmx' if d.getVar('BB_NO_NETWORK') == '1' else '${AUTOREV}'}"
SRCREV_d3v16 = "${@'a2d1f0225692c7c054014ec4dfc4dd13a35245ee' if d.getVar('BB_NO_NETWORK') == '1' else '${AUTOREV}'}"
SRCREV_v16 = "${@'a2d1f0225692c7c054014ec4dfc4dd13a35245ee' if d.getVar('BB_NO_NETWORK') == '1' else '${AUTOREV}'}"
SRCREV_pcmx-native = "${@'d09e0b91ae34d807d3df27908f045e58a52e0c4c' if d.getVar('BB_NO_NETWORK') == '1' else '${AUTOREV}'}"
SRCREV_pcmx-pn-pcmx = "${@'d09e0b91ae34d807d3df27908f045e58a52e0c4c' if d.getVar('BB_NO_NETWORK') == '1' else '${AUTOREV}'}"
SRCREV_pcmx = "${@'d09e0b91ae34d807d3df27908f045e58a52e0c4c' if d.getVar('BB_NO_NETWORK') == '1' else '${AUTOREV}'}"
SRCREV_pn-pcmx = "${@'d09e0b91ae34d807d3df27908f045e58a52e0c4c' if d.getVar('BB_NO_NETWORK') == '1' else '${AUTOREV}'}"
SRCREV = "${@'' if d.getVar('BB_NO_NETWORK') == '1' else '${AUTOREV}'}"

我想在现代 OE 上它看起来像这样(未经测试):

SRCREV = "${@ "a2d1f0225692c7c054014ec4dfc4dd13a35245ee" if bb.utils.to_boolean(d.getVar('BB_NO_NETWORK')) else d.getVar('AUTOREV') }"

使用 inline python variable expansion。在较旧的 OE 上,您可能需要将第二个参数指定为 d.getVar,例如 d.getVar('BB_NO_NETWORK', True).

您还可以使用 utils.bbclass 中的 base_conditional:

SRCREV = "${@base_conditional('BB_NO_NETWORK', '1', 'a2d1f0225692c7c054014ec4dfc4dd13a35245ee', '${AUTOREV}', d)}"