简体   繁体   English

node-gyp:在所有子目录中运行 binding.gyp

[英]node-gyp: run binding.gyp in all subdirectories

I'm developing a big node.js project which also includes several native libraries.我正在开发一个大型node.js项目,其中还包括几个本机库。
To use these libraries in JavaScript I'm compiling them to node addons ( .node ) using node-gyp .为了在 JavaScript 中使用这些库,我正在使用node- .node将它们编译为节点插件( .node )。

I'd like to run node-gyp once from the root directory to compile all the available binding.gyp recursively (in all the subdirectories).我想从根目录运行一次node-gyp以递归编译所有可用的binding.gyp (在所有子目录中)。

Is there any way to do that?有没有办法做到这一点?

GYP allows to set a list of dependencies for a target. GYP 允许为目标设置依赖项列表。 You can create a target of type: none in the top-level bindings.gyp and list there dependencies from subdirectories:您可以在顶级bindings.gyp创建type: none的目标,并列出子目录中的依赖项:

{
    '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.这将编译所有依赖项并将它们放入根目录的build/目录中。
For putting each addon in its corresponding directory, add a postbuild target inside the addon's binding.gyp :对于将每个插件在其相应的目录下,添加一个postbuild附加组件的内部目标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": "."
        }
      ]
    }
  ]
}

I didn't find any option to do this with just node-gyp , but one of the possible solutions is doing this in a script.我没有找到任何选项可以仅使用node-gyp执行此操作,但可能的解决方案之一是在脚本中执行此操作。
For example, adding the following to the package.json in the root folder:例如,在根文件夹中的package.json中添加以下内容:

 "scripts": {
    "install": "find ./app/* -name binding.gyp -execdir node-gyp rebuild ;"
 }

This will cause all the native addons to compile when running npm install in the root folder.这将导致在根文件夹中运行npm install时编译所有本机插件。

An alternative to the other answers which seems to work so far (without ever having to update binding.gyp):到目前为止似乎有效的其他答案的替代方法(无需更新 binding.gyp):

{
  "targets": [
    {
      "target_name": "addon",
      "sources": [ 
            "<!@(node -p \"var fs=require('fs'),path=require('path'),walk=function(r){let t,e=[],n=null;try{t=fs.readdirSync(r)}catch(r){n=r.toString()}if(n)return n;var a=0;return function n(){var i=t[a++];if(!i)return e;let u=path.resolve(r,i);i=r+'/'+i;let c=fs.statSync(u);if(c&&c.isDirectory()){let r=walk(i);return e=e.concat(r),n()}return e.push(i),n()}()};walk('./sources').join(' ');\")"
        ]
    }
  ]
}

(from https://stackoverflow.com/a/60947528/2016831 ) (来自https://stackoverflow.com/a/60947528/2016831

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM