0

Originally my binding file has only one target and that has been fine:

{
'targets': [
    {
        'target_name': 'target1',
        'sources': [ 'source1.cc', 'source2.cc' ],
        'cflags_cc!': [ '-fno-rtti', '-fno-exceptions' ],
        'cflags!': [ '-fno-exceptions' ],
        'conditions': [
            #A very, very long condition
        ]
    },
}

Now I need another target which is more or less the same, but build an executable file instead of link object. If I duplicate the original target, that will be OK, however I don't want to repeat the condition which is exactly the same. How could I do that?

E.g. My ideal bindin.gyp would look somewhat like this:

{
'conditions': [
    #A very, very long condition
]

'targets': [
    {
        'target_name': 'target1',
        'sources': [ 'source1.cc', 'source2.cc' ],
        'cflags_cc!': [ '-fno-rtti', '-fno-exceptions' ],
        'cflags!': [ '-fno-exceptions' ],
        'conditions' : #Refer to the conditions stated above
    },
    {
        'target_name': 'target2',
        'type' : 'executable'
        'sources': [ 'source1.cc', 'source3.cc' ],
        'cflags_cc!': [ '-fno-rtti', '-fno-exceptions' ],
        'cflags!': [ '-fno-exceptions' ],
        'conditions' : #Refer to the conditions stated above
    },
}

I tried using variables but node-gyp only allows variables of type string or list, while 'conditions' is an associative array

Max
  • 3,824
  • 8
  • 41
  • 62

2 Answers2

1

I'm not really sure, but at a guess, you might be able to stick the long condition array in a third target and depend on that condition target from each of the other two. Something like this:

{
'targets': [
    {
        'target_name': 'conditions_target',
        'conditions': [
            #A very, very long condition
        ]
    },
    {
        'target_name': 'target1',
        'sources': [ 'source1.cc', 'source2.cc' ],
        'cflags_cc!': [ '-fno-rtti', '-fno-exceptions' ],
        'cflags!': [ '-fno-exceptions' ],
        'dependencies': [
             'conditions_target',
        ],
    },
    {
        'target_name': 'target2',
        'type' : 'executable'
        'sources': [ 'source1.cc', 'source3.cc' ],
        'cflags_cc!': [ '-fno-rtti', '-fno-exceptions' ],
        'cflags!': [ '-fno-exceptions' ],
        'dependencies': [
             'conditions_target',
        ],
    },
}

But I'm not very experienced with node-gyp and that might blow things up. More on dependencies here: https://gyp.gsrc.io/docs/UserDocumentation.md#Dependencies-between-targets

Joshua Skrzypek
  • 448
  • 5
  • 9
0

Thanks for suggestion from Joshua Skrzypek. By my test, it can create a target with type 'none' for reusing build settings (including conditions, include_dirs, defines, ...) for not only other targets but also their direct or indirect dependents (using all_dependent_settings)! This is an example:

{
    'targets': [
        {
            'target_name': 'all-settings',
            'type': 'none',
            'all_dependent_settings': {
                'defines': [
                    'FOO',
                ],
                'conditions': [
                    ['OS=="mac"', {
                        'xcode_settings': {
                            'OTHER_LDFLAGS': [
                                '-undefined dynamic_lookup'
                            ],
                        },
                    }],
                    ['OS=="win"', {
                        'defines': [
                            'WIN32',
                        ],
                    }, {
                        'defines': [
                            'OTHER',
                        ],
                    }],
                ],
                'include_dirs': [
                    'include',
                ],
                'cflags': ['-Wall'],
                'xcode_settings': {
                    'OTHER_CFLAGS': ['-Wall']
                },
            }
        },
        {
            'target_name': 'mylib',
            'type': 'shared_library',
            'dependencies': [
                'all-settings'
            ],
            'sources': [
                'foo.cc',
                'bar.cc',
            ],
        }
    ]
}

This method is used in my npm pacakges. See this for reference: https://github.com/MrMYHuang/libxmljs/blob/ccbbea919e0718f45117481235e67185857c06e1/binding.gyp and its dependent package: https://github.com/MrMYHuang/node-libxslt/blob/49810b2269cb3c049a451bd3175eaad4d9de959c/binding.gyp#L10

Meng-Yuan Huang
  • 1,943
  • 3
  • 19
  • 23