1

I'm trying to build a native Node.js module that links against a 3rd party shared library. This library is delivered as part of a bundle that includes pre-built versions for different OSes and Architectures in different directories.

e.g.

/opt/Foo/linux/x86/lib/libfoo.so
/opt/Foo/linux/x86/include/foo.h
/opt/Foo/linux/x86_64/lib/libfoo.so
/opt/Foo/linux/x86_64/include/foo.h
/opt/Foo/linux/arm/lib/libfoo.so
/opt/Foo/linux/arm/include/foo.h
/opt/Foo/mac/x86_64/lib/libfoo.so
/opt/Foo/mac/x86_64/include/foo.h

my binding.gyp currently looks like this:

{
  'targets': [
    {
      'target_name': 'foo',
      'sources': ['foo.cpp', 'foo.h'],
      'include_dirs': ["<!(node -e \"require('nan')\")"],
      'conditions': [
        ['OS=="mac"', {
            'include_dirs': ['/opt/Foo/mac/x86_64/include'],
            'libraries': ['-L/opt/Foo/mac/x86_64/lib', '-lfoo']
          }
        ],
        ['OS=="linux"', {
            'include_dirs': ['/opt/Foo/linux/x86_64/include'],
            'libraries': ['-L/opt/Foo/linux/x86_64/lib', '-lfoo']
          }
        ]
      ]
    }
  ]
}

I don't seem to be able to find the syntax for conditions to differentiate on the current platform architecture.

hardillb
  • 54,545
  • 11
  • 67
  • 105

1 Answers1

1

Having not found any other solutions I came up with the following:

...
['OS=="linux"', {
    'include_dirs': ["<!(node -e \"console.log('/opt/Foo/linux/%s/include',require('process').arch);\")"],
    'libraries': ["<!(node -e \"console.log('-L/opt/Foo/linux/%s/lib',require('process').arch);\")", '-lfoo']
  }
]
...

I had to rename the /opt/Foo/Linux/x86_64 to directory to /opt/Foo/Linux/x64 to match the output from process.arch.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • 1
    Never use process.arch to get the arch, it will get you the arch of the host and not that of the target, that one is available with the variable <(target_arch) – gabry Nov 17 '20 at 11:52
  • This can be shortened to `"<!(node -p 'process.arch')"`, but @gabry is correct that `target_arch` is what you want. – ZachB Jul 12 '22 at 19:28