Tell if a variable is defined in boost build/b2/bjam

131 views Asked by At

For debug purposes, how can I see if a variable is set in b2 ?

aka. test if a variable is defined in b2

3

There are 3 answers

0
Gabriel Devillers On BEST ANSWER

Simply test the value of $(variable_name)-is-not-empty:

if $(variable_name)-not-empty
{
    echo "not empty" ;
}
else
{
    echo "empty" ;
}

this is how assert.variable-not-empty is implemented. It works because an undefined variable is the same as an empty list and because concatenating the string "-not-empty" to an empty list with variable expansion gives an empty list, which is equivalent to false.

0
Gabriel Devillers On

This is not perfect, but it is possible to tell if a variable is defined using an echo and Variable expansion:

:E=value
    Assign value to the variable if it is unset.

Example:

echo "Variable FOO has value $(FOO:E=was_not_previously_set)" ;

Will display: Variable FOO has value was_not_previously_set if FOO was unset before the call to echo.

0
Gabriel Devillers On

If you simply want to assert that the variable is defined, you can do:

import assert ;
assert.variable-not-empty variable_name ;

note: I did not write $(variable_name) (it is evaluated).

If the assert fails it displays:

jamfile:line: in modules.load from module Jamfile<C:\path\to\jamfile\dir>
error: assertion failure: Expected variable "variable_name" not to be an empty list

This works because in b2, as the doc says, "An undefined variable is indistinguishable from a variable with an empty list".

This kind of assert is useful for example if you want to produce a target name by concatenating: $(ModuleName_TOP)/project//target because this would become the empty list if ModuleName_TOP was not defined, causing build errors instead of b2 errors.