prebuild / prebuildify

Create and package prebuilds for native modules
MIT License
198 stars 37 forks source link

Bundling a shared object and data in prebuilds #39

Closed gonzus closed 4 years ago

gonzus commented 4 years ago

More than an issue, this is a question.

I have a C library libfoo (that I wrote) that I am wrapping in a NodeJS wrapper FooNAPI. The C library is built and installed outside the NodeJS wrapper, via a Makefile. As part of the installation for the library, the Makefile puts header files in .../include/foo/, a shared object in .../lib/libfoo.so and some data files in .../share/foo/.

I can build FooNAPI by putting all the correct directories and flags in binding.gyp. Now, I would like to include libfoo.so and the files in .../share/foo/ in the FooNAPI prebuilt objects. Is there a way to do this with prebuildify? Or is this something I need to configure in binding.gyp?

If I later install the FooNAPI wrapper on another machine, and it brings in with it the shared object and the data files as part of the prebuilds, how do I get the dynamic linker on that machine to load the shared object from the node_modules/prebuilds directory?

Thanks for any hints!

vweevers commented 4 years ago

Have a look at sodium-native, does something similar.

You might also like the --artifacts option to copy additional files to prebuilds/.

gonzus commented 4 years ago

Thanks @vweevers -- good info. I see sodium-native ends up building these prebuilt objects:

$ tree prebuilds/
prebuilds/
└── linux-x64
    ├── libsodium.so.23
    └── node.napi.node

I guess, then, that when you install this module and load node.napi.node via a require, the dynamic linker finds and loads the libsodium.so.23 file?

Hum... I guess this is the glue to it all:

binding.gyp: 'libraries': [ "-Wl,-rpath=\\$$ORIGIN"]

Just for my education, do you happen to know what that ORIGIN variable means, and where that definition comes from?

Anyway, this makes sense:

$ ldd prebuilds/linux-x64/node.napi.node 
    linux-vdso.so.1
    libsodium.so.23 => .../sodium-native/prebuilds/linux-x64/libsodium.so.23
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0
    /lib64/ld-linux-x86-64.so.2
mafintosh commented 4 years ago

ORIGIN is a Linux feature where you make the link relative to where your binary is.

gonzus commented 4 years ago

Thanks for all the hints and pointers, it is working fine for me.