Skip to content

Commit ab14347

Browse files
authored
gyp: add common targets (#1389)
1 parent dc211eb commit ab14347

File tree

7 files changed

+62
-45
lines changed

7 files changed

+62
-45
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
/benchmark/src
55
/test/addon_build/addons
66
/.vscode
7+
8+
# ignore node-gyp generated files outside its build directory
9+
/test/*.Makefile
10+
/test/*.mk

benchmark/binding.gyp

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
{
55
'target_name': 'function_args',
66
'sources': [ 'function_args.cc' ],
7-
'includes': [ '../except.gypi' ],
7+
'dependencies': ['../node_addon_api.gyp:node_addon_api_except'],
88
},
99
{
1010
'target_name': 'function_args_noexcept',
1111
'sources': [ 'function_args.cc' ],
12-
'includes': [ '../noexcept.gypi' ],
12+
'dependencies': ['../node_addon_api.gyp:node_addon_api'],
1313
},
1414
{
1515
'target_name': 'property_descriptor',
1616
'sources': [ 'property_descriptor.cc' ],
17-
'includes': [ '../except.gypi' ],
17+
'dependencies': ['../node_addon_api.gyp:node_addon_api_except'],
1818
},
1919
{
2020
'target_name': 'property_descriptor_noexcept',
2121
'sources': [ 'property_descriptor.cc' ],
22-
'includes': [ '../noexcept.gypi' ],
22+
'dependencies': ['../node_addon_api.gyp:node_addon_api'],
2323
},
2424
]
2525
}

common.gypi

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
}
1616
}]
1717
],
18-
'include_dirs': ["<!(node -p \"require('../').include_dir\")"],
1918
'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ],
2019
'cflags_cc': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ]
2120
}

doc/setup.md

+13-31
Original file line numberDiff line numberDiff line change
@@ -23,55 +23,37 @@ To use **Node-API** in a native module:
2323
}
2424
```
2525

26-
2. Reference this package's include directory and gyp file in `binding.gyp`:
27-
28-
```gyp
29-
'include_dirs': ["<!(node -p \"require('node-addon-api').include_dir\")"],
30-
```
31-
32-
3. Decide whether the package will enable C++ exceptions in the Node-API wrapper.
26+
2. Decide whether the package will enable C++ exceptions in the Node-API
27+
wrapper, and reference this package as a dependency in `binding.gyp`.
3328
The base ABI-stable C APIs do not throw or handle C++ exceptions, but the
3429
Node-API C++ wrapper classes may _optionally_
3530
[integrate C++ and JavaScript exception-handling
3631
](https://github.com/nodejs/node-addon-api/blob/HEAD/doc/error_handling.md).
37-
To enable that capability, C++ exceptions must be enabled in `binding.gyp`:
32+
33+
To use without C++ exceptions, add the following to `binding.gyp`:
3834

3935
```gyp
40-
'cflags!': [ '-fno-exceptions' ],
41-
'cflags_cc!': [ '-fno-exceptions' ],
42-
'conditions': [
43-
["OS=='win'", {
44-
"defines": [
45-
"_HAS_EXCEPTIONS=1"
46-
],
47-
"msvs_settings": {
48-
"VCCLCompilerTool": {
49-
"ExceptionHandling": 1
50-
},
51-
},
52-
}],
53-
["OS=='mac'", {
54-
'xcode_settings': {
55-
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
56-
'CLANG_CXX_LIBRARY': 'libc++',
57-
'MACOSX_DEPLOYMENT_TARGET': '10.7',
58-
},
59-
}],
36+
'dependencies': [
37+
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api",
6038
],
6139
```
6240

63-
Alternatively, disable use of C++ exceptions in Node-API:
41+
To enable that capability, add an alternative dependency in `binding.gyp`:
6442

6543
```gyp
66-
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
44+
'dependencies': [
45+
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
46+
],
6747
```
6848

6949
If you decide to use node-addon-api without C++ exceptions enabled, please
7050
consider enabling node-addon-api safe API type guards to ensure the proper
7151
exception handling pattern:
7252

7353
```gyp
74-
'defines': [ 'NODE_ADDON_API_ENABLE_MAYBE' ],
54+
'dependencies': [
55+
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_maybe",
56+
],
7557
```
7658

7759
4. If you would like your native addon to support OSX, please also add the

index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const includeDir = path.relative('.', __dirname);
55
module.exports = {
66
include: `"${__dirname}"`, // deprecated, can be removed as part of 4.0.0
77
include_dir: includeDir,
8-
gyp: path.join(includeDir, 'node_api.gyp:nothing'),
8+
gyp: path.join(includeDir, 'node_api.gyp:nothing'), // deprecated.
9+
targets: path.join(includeDir, 'node_addon_api.gyp'),
910
isNodeApiBuiltin: true,
1011
needsFlag: false
1112
};

node_addon_api.gyp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'node_addon_api',
5+
'type': 'none',
6+
'sources': [ 'napi.h', 'napi-inl.h' ],
7+
'direct_dependent_settings': {
8+
'include_dirs': [ '.' ],
9+
'includes': ['noexcept.gypi'],
10+
}
11+
},
12+
{
13+
'target_name': 'node_addon_api_except',
14+
'type': 'none',
15+
'sources': [ 'napi.h', 'napi-inl.h' ],
16+
'direct_dependent_settings': {
17+
'include_dirs': [ '.' ],
18+
'includes': ['except.gypi'],
19+
}
20+
},
21+
{
22+
'target_name': 'node_addon_api_maybe',
23+
'type': 'none',
24+
'sources': [ 'napi.h', 'napi-inl.h' ],
25+
'direct_dependent_settings': {
26+
'include_dirs': [ '.' ],
27+
'includes': ['noexcept.gypi'],
28+
'defines': ['NODE_ADDON_API_ENABLE_MAYBE']
29+
}
30+
},
31+
]
32+
}

test/binding.gyp

+7-8
Original file line numberDiff line numberDiff line change
@@ -97,41 +97,40 @@
9797
'targets': [
9898
{
9999
'target_name': 'binding',
100-
'includes': ['../except.gypi'],
100+
'dependencies': ['../node_addon_api.gyp:node_addon_api_except'],
101101
'sources': ['>@(build_sources)']
102102
},
103103
{
104104
'target_name': 'binding_noexcept',
105-
'includes': ['../noexcept.gypi'],
105+
'dependencies': ['../node_addon_api.gyp:node_addon_api'],
106106
'sources': ['>@(build_sources)']
107107
},
108108
{
109109
'target_name': 'binding_noexcept_maybe',
110-
'includes': ['../noexcept.gypi'],
110+
'dependencies': ['../node_addon_api.gyp:node_addon_api_maybe'],
111111
'sources': ['>@(build_sources)'],
112-
'defines': ['NODE_ADDON_API_ENABLE_MAYBE']
113112
},
114113
{
115114
'target_name': 'binding_swallowexcept',
116-
'includes': ['../except.gypi'],
115+
'dependencies': ['../node_addon_api.gyp:node_addon_api_except'],
117116
'sources': [ '>@(build_sources_swallowexcept)'],
118117
'defines': ['NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS']
119118
},
120119
{
121120
'target_name': 'binding_swallowexcept_noexcept',
122-
'includes': ['../noexcept.gypi'],
121+
'dependencies': ['../node_addon_api.gyp:node_addon_api'],
123122
'sources': ['>@(build_sources_swallowexcept)'],
124123
'defines': ['NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS']
125124
},
126125
{
127126
'target_name': 'binding_type_check',
128-
'includes': ['../noexcept.gypi'],
127+
'dependencies': ['../node_addon_api.gyp:node_addon_api'],
129128
'sources': ['>@(build_sources_type_check)'],
130129
'defines': ['NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS']
131130
},
132131
{
133132
'target_name': 'binding_custom_namespace',
134-
'includes': ['../noexcept.gypi'],
133+
'dependencies': ['../node_addon_api.gyp:node_addon_api'],
135134
'sources': ['>@(build_sources)'],
136135
'defines': ['NAPI_CPP_CUSTOM_NAMESPACE=cstm']
137136
},

0 commit comments

Comments
 (0)