1

I have a node add on module using node-addon-api interface.

{
  "targets": [
    {
      "target_name": "mod",
      "sources": [
        "./src/index.cpp",
        ...
      ],
      "include_dirs": [
        ...,
        "<!@(node -p \"require('node-addon-api').include\")"
      ],
      "libraries": [
        ...,
        "/usr/lib/libgfortran.so.5"
      ],
      'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
    }
  ]
}

This will produce a module that I can load from NodeJS with require('./mod.node')

What are the step to do this compilation from a makefile?

Bob
  • 13,867
  • 1
  • 5
  • 27

2 Answers2

1

If your Node addon uses a 3rd party library that you have to build, there are three ways to do it:

  • Create a Makefile that launches the compilation of your 3rd party library and your Node addon (using the commands from my other answer), then link your addon with that library using the libraries section of the gyp file - this is the easiest choice
  • Completely convert the 3rd party library to gyp - this is the best choice - I a maintaining a number of Node addons and I have created gyp building systems for them (I see Fortran in your gyp so you probably know about netcdf and hdf5) - but it is quite complex
  • Convert your Node addon to classical Makefile or cmake - this is by far the hardest and it won't have any immediate benefits - you will have to reimplement node-gyp from scratch - something that even Node's core team is reluctant do to
mmomtchev
  • 2,497
  • 1
  • 8
  • 23
  • This is getting better * My approach is what you described in the first case. * Could you expand the second a little bit. The projects you are mentioning are open source? If so, could you please link them. * What are the reasons why Node's core team is reluctant, links again are wellcome. – Bob Jan 12 '22 at 10:40
  • https://github.com/mmomtchev/node-gdal-async - uses method 2 – mmomtchev Jan 12 '22 at 10:48
  • Then just create a `Makefile` with: `project/lib/binding/addon.node: project/src/*.cc node-gyp configure && node-gyp build` – mmomtchev Jan 12 '22 at 10:48
  • I didn't understand your last comment. But now I was able at least to include the headers, so I think I can continue with the initial approach. – Bob Jan 12 '22 at 15:01
  • I will add it to the other answer – mmomtchev Jan 12 '22 at 16:02
  • Please, don't. There is no point of keep adding multiple answers, it is better to write one good answer. The idea here is to summarize and write things in a way that is clearer and more concise than the documentation. I asked the question precisely because I was confused with multiple pages with similar text, and finding myself unable to find what I need. – Bob Jan 12 '22 at 16:36
  • @bob There are two questions here - how to build a node addon and how to integrate it with another build system - hence my two answers – mmomtchev Jan 12 '22 at 16:37
  • ahh sorry, I thought you were going to post a third answer. – Bob Jan 12 '22 at 16:39
0

Normally, you don't use a Makefile with a node-gyp since gyp is a full-blown make in itself.

To build this you should call npx node-gyp build or just node-gyp build if node_modules/.bin is in your path

You should also install it as a project dependency: npm i --save node-gyp

If you used yeoman to get this far, it already built it once for you automagically without requiring node-gyp - because it contains it in its package. You can call its copy from /usr/lib/node_modules/yo/node_modules/node-gyp/bin/node-gyp.js but normally you should install it as a dependency of your project.

If you want to build this module from a Makefile, simply add this:

project/lib/binding/addon.node: project/src/*.cc project/src/*.h
  project/node_modules/.bin/node-gyp configure
  project/node_modules/.bin/node-gyp build -j max

In this case all your users will have to build it after installing from npm.

If you want to distribute it as a binary via npm, I also suggest you take a look at @mapbox/node-pre-gyp which is a replacement for node-gyp and automatically downloads prebuilt binaries from Amazon S3. If you prefer using Github Actions / Packages, you should take a look at @mmomtchev/node-pre-gyp-github of which I am the current maintainer.

mmomtchev
  • 2,497
  • 1
  • 8
  • 23
  • This question is related to what [this](https://stackoverflow.com/q/70671045/12750353). The kaldi project generate some makefiles using ./configure and they work if I have my code as a subfolder of the `src/` module. If I can extend them to compile a library compatible with node I am done. – Bob Jan 12 '22 at 09:49
  • I will post another answer, I think this one could be useful to someone else :-) – mmomtchev Jan 12 '22 at 09:50