6

How do I include a depends line in a bitbake file with a condition ? I want something like below:

if (some env varible)
  DEPENDS += "recipe-1"
else
  DEPENDS += "recipe-2'

I have tried below in the .bb file:

DEPENDS += "${@ 'recipe-2' if '${ENV_VAR}' else 'recipe-1'}"

Before that I exported ENV_VAR to BB_ENV_EXTRAWHITE

export BB_ENV_EXTRAWHITE="$BB_ENV_EXTRAWHITE ENV_VAR"

This is working only when ENV_VAR is set:

env ENV_VAR="value" bitbake test-recipe

if ENV_VAR is not set, it is throwing an error while parsing the bitbake DEPENDS line

ExpansionError: Failure expanding variable DEPENDS, expression was
${@ 'recipe-2' if '${ENV_VAR}' else 'recipe-1'}  
which triggered exception SyntaxError: EOL while scanning string literal (DEPENDS, line 1)
seenu9333
  • 115
  • 1
  • 4
  • Does it deal with python ? – mvelay Feb 08 '16 at 11:32
  • I am using the python expressions in bitbake. DEPENDS += "${@ 'recipe-2' if '${ENV_VAR}' else 'recipe-1'}" Syntax:DEPENDS += "${@ python expression}" – seenu9333 Feb 08 '16 at 12:20
  • `recipe-2' if '${ENV_VAR}' else 'recipe-1` is not a correct python expression as `${ENV_VAR}` returns the value of `ENV_VAR` in a shell. Am I wrong ? – mvelay Feb 08 '16 at 12:50

2 Answers2

11

Try:

DEPENDS += "${@ 'recipe-2' if d.getVar('ENV_VAR') else 'recipe-1'}"

The reason why is that ${ENV_VAR} gets expanded to the value of the variable. If its unset, it doesn't get expanded and that triggers the error you see. By using getVar you get a result which the rest of the python expression can deal with None or a value.

Note that there are some proposed changes which might improve the behaviour to make this a bit more usable and understandable to people but the above would continue to work regardless.

Metacarpus
  • 15
  • 4
Richard Purdie
  • 2,028
  • 10
  • 16
1

Let's say you have recipes, recipe-main & recipe-test and based on the value of USE_TEST_RECIPE 0 or 1, you can do the following

DEPENDS_append += "${@base_conditional('USE_TEST_RECIPE', '1', 'recipe-test', 'recipe-main', d)}"

SD.
  • 1,432
  • 22
  • 38