Is there wildcard mechanism for listing sources in node-gyp

徘徊边缘 提交于 2020-05-09 21:47:53

问题


I'm writing binding.gyp file for my new node.js module. I have all my source files under src/ subdirectory. I would like to use all of them while building the module. Instead of modifying binding.gyp each time I add a new cpp file, I would like to list all cpp files through some wildcard mechanism. Does node-gyp support that? Something like following (which doesn't work

{
  'targets' : [
      {
          'target_name' : 'mymod',
          'sources' : 'src/*.cpp'
      }
   ]
}

I looked at https://code.google.com/p/gyp/wiki/InputFormatReference , but didn't find anything readily useful.


回答1:


Figured it out

{
  'targets' : [
      {
          'target_name' : 'mymod',
          'sources' : [ '<!@(ls -1 src/*.cpp)' ],
      }
   ]
}

Check out this link

Update

The solution above is not portable across platforms. Here's a portable version:

{
  'targets' : [
      {
          'target_name' : 'mymod',
          'sources' : [  "<!@(node -p \"require('fs').readdirSync('./src').map(f=>'src/'+f).join(' ')\")" ],
      }
   ]
}

Essentially it replaces the platform specific directory listing command (ls), by Javascript code that uses node's fs module to list the directory contents.




回答2:


An even more portable version (that doesn't depend on node, but rather python):

"<!@(python -c \"import os; print '\n'.join(['%s' % x for x in os.listdir('.') if x[-3:] == '.cc' and 'test' not in x])\")"



回答3:


To filter specific file extensions like cpp and to support also pre-compiled libraries .a files, I have slightly modified the accepted solution to be:

'sources': [
        'jamspell.cpp',
        "<!@(node -p \"require('fs').readdirSync('./src').filter(f=>f.endsWith('.cpp')).map(f=>'src/'+f).join(' ')\")",
        "<!@(node -p \"require('fs').readdirSync('./src/jamspell').filter(f=>f.endsWith('.cpp')).map(f=>'src/jamspell/'+f).join(' ')\")"
        ],
        'include_dirs': [
            "<!@(node -p \"require('node-addon-api').include\")"
        ],
        'libraries': [
          "<!@(node -p \"require('fs').readdirSync('./lib/contrib').filter(f=>f.endsWith('.a')).map(f=>'lib/contrib/'+f).join(' ')\")"
        ],
        'dependencies': [
            "<!(node -p \"require('node-addon-api').gyp\")"
        ],



回答4:


If anyone wants to include all sub files and folders within a certain folder (defined at end of line, here as "sources"):

{
  "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(' ');\")"
        ]
    }
  ]
}

(based off, but not exactly: node.js fs.readdir recursive directory search)



来源:https://stackoverflow.com/questions/15397582/is-there-wildcard-mechanism-for-listing-sources-in-node-gyp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!