mapbox / cpp

C++ @ Mapbox
108 stars 17 forks source link

Docs on publishing of header-only C++ libs (include in node c++ modules) #20

Open springmeyer opened 7 years ago

springmeyer commented 7 years ago

rational

We are increasingly working on small node C++ modules that depend on header-only C++ libs. Node.js's Nan module has pioneered a best practice for how to include header only C++ libs in node C++ addons by simply adding a package.json and include_dirs.js file to the root of the project. I think we should follow the lead of howNan does this and package all our C++ header only libs in npm as well as mason. I think publishing in two places is worth it because it makes including the libraries from node modules very easy and consistent with how JS modules work.

method

Add:

Example:

{
    "name": "@mapbox/geometry-hpp",
    "version": "0.9.0",
    "description": "A C++ header only geometry type library",
    "main": "./include_dirs.js",
    "repository"   :  {
      "type" : "git",
      "url"  : "git://github.com/mapbox/geometry.hpp.git"
    },
    "files": [
         "include"
    ]
}

Then test that only the right files are going to be packaged:

npm pack
tar -ztvf *tgz

^^ that should list just the headers and the default files npm includes like README.md and package.json etc

Examples:

Example use case:

springmeyer commented 7 years ago

The drawbacks of publishing C++ header-only libs in npm are:

These are downsides enough that npm publishing of C++ headers is not the ideal method / best practice at Mapbox.

Instead we'll need to come up with some other method to recommend and consistently use via node c++ modules, like using a shell script + mason like carmen-cache does to install rocksdb (https://github.com/mapbox/carmen-cache/blob/1aaf88413be2243e3e3c772b1a9c777ea527578c/binding.gyp#L3-L16 + https://github.com/mapbox/carmen-cache/blob/4182501f748e2ca2635485cbd51e17b6c49329f1/install_mason.sh. This would have the added benefit that it would work for both header only libs and dependencies that are precompiled libs.

mapsam commented 7 years ago

Nice @springmeyer! You may have a look over at hpp-skel as well, which implements some of the features you mention above. The README has a brief description of publishing as well.

https://github.com/mapbox/hpp-skel

springmeyer commented 7 years ago

Instead we'll need to come up with some other method to recommend and consistently use via node c++ modules, like using a shell script + mason like carmen-cache does

This is now implemented in node-cpp-skel. To recap the idea is:

Now that the best practice is in place the remaining actions are: