diff --git a/.gitignore b/.gitignore index b1233109f..bcded4821 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,7 @@ /benchmark/src /test/addon_build/addons /.vscode + +# ignore node-gyp generated files outside its build directory +/test/*.Makefile +/test/*.mk diff --git a/benchmark/binding.gyp b/benchmark/binding.gyp index 72f68a13e..879d4a569 100644 --- a/benchmark/binding.gyp +++ b/benchmark/binding.gyp @@ -4,22 +4,22 @@ { 'target_name': 'function_args', 'sources': [ 'function_args.cc' ], - 'includes': [ '../except.gypi' ], + 'dependencies': ['../node_addon_api.gyp:node_addon_api_except'], }, { 'target_name': 'function_args_noexcept', 'sources': [ 'function_args.cc' ], - 'includes': [ '../noexcept.gypi' ], + 'dependencies': ['../node_addon_api.gyp:node_addon_api'], }, { 'target_name': 'property_descriptor', 'sources': [ 'property_descriptor.cc' ], - 'includes': [ '../except.gypi' ], + 'dependencies': ['../node_addon_api.gyp:node_addon_api_except'], }, { 'target_name': 'property_descriptor_noexcept', 'sources': [ 'property_descriptor.cc' ], - 'includes': [ '../noexcept.gypi' ], + 'dependencies': ['../node_addon_api.gyp:node_addon_api'], }, ] } diff --git a/common.gypi b/common.gypi index 9be254f0b..e80a2b06a 100644 --- a/common.gypi +++ b/common.gypi @@ -15,7 +15,6 @@ } }] ], - 'include_dirs': ["<!(node -p \"require('../').include_dir\")"], 'cflags': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ], 'cflags_cc': [ '-Werror', '-Wall', '-Wextra', '-Wpedantic', '-Wunused-parameter' ] } diff --git a/doc/setup.md b/doc/setup.md index 49e039c8f..d716a0326 100644 --- a/doc/setup.md +++ b/doc/setup.md @@ -23,47 +23,27 @@ To use **Node-API** in a native module: } ``` - 2. Reference this package's include directory and gyp file in `binding.gyp`: - -```gyp - 'include_dirs': ["<!(node -p \"require('node-addon-api').include_dir\")"], -``` - - 3. Decide whether the package will enable C++ exceptions in the Node-API wrapper. + 2. Decide whether the package will enable C++ exceptions in the Node-API + wrapper, and reference this package as a dependency in `binding.gyp`. The base ABI-stable C APIs do not throw or handle C++ exceptions, but the Node-API C++ wrapper classes may _optionally_ [integrate C++ and JavaScript exception-handling ](https://github.com/nodejs/node-addon-api/blob/HEAD/doc/error_handling.md). - To enable that capability, C++ exceptions must be enabled in `binding.gyp`: + + To use without C++ exceptions, add the following to `binding.gyp`: ```gyp - 'cflags!': [ '-fno-exceptions' ], - 'cflags_cc!': [ '-fno-exceptions' ], - 'conditions': [ - ["OS=='win'", { - "defines": [ - "_HAS_EXCEPTIONS=1" - ], - "msvs_settings": { - "VCCLCompilerTool": { - "ExceptionHandling": 1 - }, - }, - }], - ["OS=='mac'", { - 'xcode_settings': { - 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES', - 'CLANG_CXX_LIBRARY': 'libc++', - 'MACOSX_DEPLOYMENT_TARGET': '10.7', - }, - }], + 'dependencies': [ + "<!(node -p \"require('node-addon-api').targets\"):node_addon_api", ], ``` - Alternatively, disable use of C++ exceptions in Node-API: + To enable that capability, add an alternative dependency in `binding.gyp`: ```gyp - 'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ], + 'dependencies': [ + "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except", + ], ``` If you decide to use node-addon-api without C++ exceptions enabled, please @@ -71,7 +51,9 @@ To use **Node-API** in a native module: exception handling pattern: ```gyp - 'defines': [ 'NODE_ADDON_API_ENABLE_MAYBE' ], + 'dependencies': [ + "<!(node -p \"require('node-addon-api').targets\"):node_addon_api_maybe", + ], ``` 4. If you would like your native addon to support OSX, please also add the diff --git a/index.js b/index.js index 52f53e3c2..4877abc97 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,8 @@ const includeDir = path.relative('.', __dirname); module.exports = { include: `"${__dirname}"`, // deprecated, can be removed as part of 4.0.0 include_dir: includeDir, - gyp: path.join(includeDir, 'node_api.gyp:nothing'), + gyp: path.join(includeDir, 'node_api.gyp:nothing'), // deprecated. + targets: path.join(includeDir, 'node_addon_api.gyp'), isNodeApiBuiltin: true, needsFlag: false }; diff --git a/node_addon_api.gyp b/node_addon_api.gyp new file mode 100644 index 000000000..29905ed47 --- /dev/null +++ b/node_addon_api.gyp @@ -0,0 +1,32 @@ +{ + 'targets': [ + { + 'target_name': 'node_addon_api', + 'type': 'none', + 'sources': [ 'napi.h', 'napi-inl.h' ], + 'direct_dependent_settings': { + 'include_dirs': [ '.' ], + 'includes': ['noexcept.gypi'], + } + }, + { + 'target_name': 'node_addon_api_except', + 'type': 'none', + 'sources': [ 'napi.h', 'napi-inl.h' ], + 'direct_dependent_settings': { + 'include_dirs': [ '.' ], + 'includes': ['except.gypi'], + } + }, + { + 'target_name': 'node_addon_api_maybe', + 'type': 'none', + 'sources': [ 'napi.h', 'napi-inl.h' ], + 'direct_dependent_settings': { + 'include_dirs': [ '.' ], + 'includes': ['noexcept.gypi'], + 'defines': ['NODE_ADDON_API_ENABLE_MAYBE'] + } + }, + ] +} diff --git a/test/binding.gyp b/test/binding.gyp index 0dcfda61d..08c51885b 100644 --- a/test/binding.gyp +++ b/test/binding.gyp @@ -97,41 +97,40 @@ 'targets': [ { 'target_name': 'binding', - 'includes': ['../except.gypi'], + 'dependencies': ['../node_addon_api.gyp:node_addon_api_except'], 'sources': ['>@(build_sources)'] }, { 'target_name': 'binding_noexcept', - 'includes': ['../noexcept.gypi'], + 'dependencies': ['../node_addon_api.gyp:node_addon_api'], 'sources': ['>@(build_sources)'] }, { 'target_name': 'binding_noexcept_maybe', - 'includes': ['../noexcept.gypi'], + 'dependencies': ['../node_addon_api.gyp:node_addon_api_maybe'], 'sources': ['>@(build_sources)'], - 'defines': ['NODE_ADDON_API_ENABLE_MAYBE'] }, { 'target_name': 'binding_swallowexcept', - 'includes': ['../except.gypi'], + 'dependencies': ['../node_addon_api.gyp:node_addon_api_except'], 'sources': [ '>@(build_sources_swallowexcept)'], 'defines': ['NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS'] }, { 'target_name': 'binding_swallowexcept_noexcept', - 'includes': ['../noexcept.gypi'], + 'dependencies': ['../node_addon_api.gyp:node_addon_api'], 'sources': ['>@(build_sources_swallowexcept)'], 'defines': ['NODE_API_SWALLOW_UNTHROWABLE_EXCEPTIONS'] }, { 'target_name': 'binding_type_check', - 'includes': ['../noexcept.gypi'], + 'dependencies': ['../node_addon_api.gyp:node_addon_api'], 'sources': ['>@(build_sources_type_check)'], 'defines': ['NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS'] }, { 'target_name': 'binding_custom_namespace', - 'includes': ['../noexcept.gypi'], + 'dependencies': ['../node_addon_api.gyp:node_addon_api'], 'sources': ['>@(build_sources)'], 'defines': ['NAPI_CPP_CUSTOM_NAMESPACE=cstm'] },