## 8.2.12

### variables in gyp

These are the ways to set variables you can reference in your gyp files.

Variables are referenced using the expansion character '<', like so '<(foo)'.
This happens in ExpandVariables, see [input.py]. There is also an "expand to list" variant '<@', which works if the context expects a list and if the string being expanded does not contain anything else.
Here is a toy .gyp file that references a variable foo
{
'targets': [
{
'target_name': 'foo_target',
'type': 'executable',
'sources': [ 'yo.c' ],
'actions': [
{
'action_name': 'greet',
'action': [ 'echo', 'hello world <(foo)', ],

'inputs': [],
'outputs': [ 'yo.c' ],
}]}]}


Now you could either add a variables section before 'targets' like so

'variables' : {
'foo': 'bar'
},


However, most of the time you will want to set these separately. You can do this in two ways.

1) include a separate file.
For this we would dreate a file some_vars.gypi that contains just this:
{
'variables' : {
'foo': 'bar'
},
}

and add an include to the original .gyp file
'includes': [ 'some_vars.gypi' ]


Here is the result:
$GYP_GENERATORS=ninja gyp --depth=0 --toplevel-dir=pwd \ simple_action.gyp$ ninja -C out/Default all
ninja: Entering directory out/Default'
[1/3] ACTION foo_target: greet
hello world bar
[...]


2) Set it on the command line.

gyp offers a "define" facility with the -D flag, which is also used to pass definitions down to the C/C++ compiler. The code says "-D is for default", so if the variable is already defined, that value is going to be used instead of your command line value.

Using the original .gyp file, we do

$GYP_GENERATORS=ninja gyp --depth=0 --toplevel-dir=pwd \ simple_action.gyp -Dfoo=baz$ ninja -C out/Default all
ninja: Entering directory out/Default'
[1/3] ACTION foo_target: greet
hello world baz


For an example with list expansion, check out the [Actions example] in the gyp language spec.