Is there wildcard mechanism for listing sources in node-gyp

前端 未结 4 1420
日久生厌
日久生厌 2021-02-05 12:41

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 buil

相关标签:
4条回答
  • 2021-02-05 12:49

    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.

    0 讨论(0)
  • 2021-02-05 12:55

    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)

    0 讨论(0)
  • 2021-02-05 13:07

    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])\")"
    
    0 讨论(0)
  • 2021-02-05 13:07

    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\")"
            ],
    
    0 讨论(0)
提交回复
热议问题