Skip to content

Commit 93b1bb8

Browse files
jschlighttargos
authored andcommitted
n-api,doc: add info about building n-api addons
Adds information about tools available for building N-API addons. Fixes: nodejs/abi-stable-node#384 PR-URL: #30032 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 1d9f427 commit 93b1bb8

File tree

1 file changed

+109
-10
lines changed

1 file changed

+109
-10
lines changed

doc/api/n-api.md

+109-10
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ changes in the underlying JavaScript engine and allow modules
1414
compiled for one major version to run on later major versions of Node.js without
1515
recompilation. The [ABI Stability][] guide provides a more in-depth explanation.
1616

17-
Addons are built/packaged with the same approach/tools
18-
outlined in the section titled [C++ Addons](addons.html).
19-
The only difference is the set of APIs that are used by the native code.
20-
Instead of using the V8 or [Native Abstractions for Node.js][] APIs,
21-
the functions available in the N-API are used.
17+
Addons are built/packaged with the same approach/tools outlined in the section
18+
titled [C++ Addons][]. The only difference is the set of APIs that are used by
19+
the native code. Instead of using the V8 or [Native Abstractions for Node.js][]
20+
APIs, the functions available in the N-API are used.
2221

2322
APIs exposed by N-API are generally used to create and manipulate
2423
JavaScript values. Concepts and operations generally map to ideas specified
@@ -76,8 +75,7 @@ if (status != napi_ok) {
7675
The end result is that the addon only uses the exported C APIs. As a result,
7776
it still gets the benefits of the ABI stability provided by the C API.
7877

79-
When using `node-addon-api` instead of the C APIs, start with the API
80-
[docs](https://github.com/nodejs/node-addon-api#api-documentation)
78+
When using `node-addon-api` instead of the C APIs, start with the API [docs][]
8179
for `node-addon-api`.
8280

8381
## Implications of ABI Stability
@@ -118,11 +116,95 @@ must make use exclusively of N-API by restricting itself to using
118116
and by checking, for all external libraries that it uses, that the external
119117
library makes ABI stability guarantees similar to N-API.
120118

119+
## Building
120+
121+
Unlike modules written in JavaScript, developing and deploying Node.js
122+
native addons using N-API requires an additional set of tools. Besides the
123+
basic tools required to develop for Node.js, the native addon developer
124+
requires a toolchain that can compile C and C++ code into a binary. In
125+
addition, depending upon how the native addon is deployed, the *user* of
126+
the native addon will also need to have a C/C++ toolchain installed.
127+
128+
For Linux developers, the necessary C/C++ toolchain packages are readily
129+
available. [GCC][] is widely used in the Node.js community to build and
130+
test across a variety of plarforms. For many developers, the [LLVM][]
131+
compiler infrastructure is also a good choice.
132+
133+
For Mac developers, [Xcode][] offers all the required compiler tools.
134+
However, it is not necessary to install the entire Xcode IDE. The following
135+
command installs the necessary toolchain:
136+
137+
```bash
138+
xcode-select --install
139+
```
140+
141+
For Windows developers, [Visual Studio][] offers all the required compiler
142+
tools. However, it is not necessary to install the entire Visual Studio
143+
IDE. The following command installs the necessary toolchain:
144+
145+
```bash
146+
npm install --global --production windows-build-tools
147+
```
148+
149+
The sections below describe the additional tools available for developing
150+
and deploying Node.js native addons.
151+
152+
### Build tools
153+
154+
Both the tools listed here require that *users* of the native
155+
addon have a C/C++ toolchain installed in order to successfully install
156+
the native addon.
157+
158+
#### node-gyp
159+
160+
[node-gyp][] is a build system based on Google's [GYP][] tool and comes
161+
bundled with npm. GYP, and therefore node-gyp, requires that Python be
162+
installed.
163+
164+
Historically, node-gyp has been the tool of choice for building native
165+
addons. It has widespread adoption and documentation. However, some
166+
developers have run into limitations in node-gyp.
167+
168+
#### CMake.js
169+
170+
[CMake.js][] is an alternative build system based on [CMake][].
171+
172+
CMake.js is a good choice for projects that already use CMake or for
173+
developers affected by limitations in node-gyp.
174+
175+
### Uploading precompiled binaries
176+
177+
The three tools listed here permit native addon developers and maintainers
178+
to create and upload binaries to public or private servers. These tools are
179+
typically integrated with CI/CD build systems like [Travis CI][] and
180+
[AppVeyor][] to build and upload binaries for a variety of platforms and
181+
architectures. These binaries are then available for download by users who
182+
do not need to have a C/C++ toolchain installed.
183+
184+
#### node-pre-gyp
185+
186+
[node-pre-gyp][] is a tool based on node-gyp that adds the ability to
187+
upload binaries to a server of the developer's choice. node-pre-gyp has
188+
particularly good support for uploading binaries to Amazon S3.
189+
190+
#### prebuild
191+
192+
[prebuild][] is a tool that supports builds using either node-gyp or
193+
CMake.js. Unlike node-pre-gyp which supports a variety of servers, prebuild
194+
uploads binaries only to [GitHub releases][]. prebuild is a good choice for
195+
GitHub projects using CMake.js.
196+
197+
#### prebuildify
198+
199+
[prebuildify][] is tool based on node-gyp. The advantage of prebuildify is
200+
that the built binaries are bundled with the native module when it's
201+
uploaded to npm. The binaries are downloaded from npm and are immediately
202+
available to the module user when the native module is installed.
203+
121204
## Usage
122205

123-
In order to use the N-API functions, include the file
124-
[`node_api.h`](https://github.com/nodejs/node/blob/master/src/node_api.h)
125-
which is located in the src directory in the node development tree:
206+
In order to use the N-API functions, include the file [`node_api.h`][] which is
207+
located in the src directory in the node development tree:
126208

127209
```C
128210
#include <node_api.h>
@@ -5222,3 +5304,20 @@ This API may only be called from the main thread.
52225304
[context-aware addons]: addons.html#addons_context_aware_addons
52235305
[node-addon-api]: https://github.com/nodejs/node-addon-api
52245306
[worker threads]: https://nodejs.org/api/worker_threads.html
5307+
[C++ Addons]: addons.html
5308+
[docs]: https://github.com/nodejs/node-addon-api#api-documentation
5309+
[GCC]: https://gcc.gnu.org
5310+
[LLVM]: https://llvm.org
5311+
[Xcode]: https://developer.apple.com/xcode/
5312+
[Visual Studio]: https://visualstudio.microsoft.com
5313+
[node-gyp]: https://github.com/nodejs/node-gyp
5314+
[GYP]: https://gyp.gsrc.io
5315+
[CMake.js]: https://github.com/cmake-js/cmake-js
5316+
[CMake]: https://cmake.org
5317+
[Travis CI]: https://travis-ci.org
5318+
[AppVeyor]: https://www.appveyor.com
5319+
[node-pre-gyp]: https://github.com/mapbox/node-pre-gyp
5320+
[prebuild]: https://github.com/prebuild/prebuild
5321+
[GitHub releases]: https://help.github.com/en/github/administering-a-repository/about-releases
5322+
[prebuildify]: https://github.com/prebuild/prebuildify
5323+
[`node_api.h`]: https://github.com/nodejs/node/blob/master/src/node_api.h

0 commit comments

Comments
 (0)