Search code examples
node.jsrecursioncompilationnativenode-gyp

node-gyp: run binding.gyp in all subdirectories


I'm developing a big node.js project which also includes several native libraries.
To use these libraries in JavaScript I'm compiling them to node addons (.node) using node-gyp.

I'd like to run node-gyp once from the root directory to compile all the available binding.gyp recursively (in all the subdirectories).

Is there any way to do that?


Solution

  • GYP allows to set a list of dependencies for a target. You can create a target of type: none in the top-level bindings.gyp and list there dependencies from subdirectories:

    {
        'targets': [
            {
                'target_name': 'build_all',
                'type': 'none',
                'dependencies': ['subdir1/bindings.gyp:*', 'subdir/subdir2/bindings.gyp:*'],
                # or generate dependencies list with a command expansion
                'dependencies': ['<!@(find -mindepth 2 -name binding.gyp | sed -e s/$/:*/)'],
            }
        ]
    }
    

    This will compile all the dependencies and put them into build/ directory in the root.
    For putting each addon in its corresponding directory, add a postbuild target inside the addon's binding.gyp:

    {
      "targets": [
        {
          "target_name": "my-target",
          "sources": [ "example.cpp" ]      
        },      
        {
          "target_name": "action_after_build",
          "type": "none",
          "dependencies": [ "my-target" ],
          "copies": [
            {
              "files": [ "<(PRODUCT_DIR)/my-target.node" ],
              "destination": "."
            }
          ]
        }
      ]
    }