From e4a965a968de497d366a361271de4b36d95e7cdd Mon Sep 17 00:00:00 2001 From: NickNaso Date: Thu, 23 May 2019 11:57:31 +0200 Subject: [PATCH 1/6] build: expose napi_build_version variable to native addons Expose `napi_build_version` to allow `node-gyp` to make it available for addons. Fixes: https://github.com/nodejs/node-gyp/issues/1745 Refs: https://github.com/nodejs/abi-stable-node/issues/371 --- common.gypi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/common.gypi b/common.gypi index 223043e55194c3..834c61d99c4b88 100644 --- a/common.gypi +++ b/common.gypi @@ -72,6 +72,9 @@ ##### end V8 defaults ##### + # This variable needs to be updated when N-API version change + 'napi_build_version': 4, + 'conditions': [ ['target_arch=="arm64"', { # Disabled pending https://github.com/nodejs/node/issues/23913. From 5e4f0bb5b203566dc078e10fe3d9fb4dbaca7ba1 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Tue, 4 Jun 2019 01:39:31 +0200 Subject: [PATCH 2/6] Retrieve napi_build_version in a dynamic way. --- common.gypi | 3 --- configure.py | 5 +++++ doc/api/process.md | 1 + test/parallel/test-process-versions.js | 3 +++ tools/getnapibuildversion.py | 26 ++++++++++++++++++++++++++ 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 tools/getnapibuildversion.py diff --git a/common.gypi b/common.gypi index 834c61d99c4b88..223043e55194c3 100644 --- a/common.gypi +++ b/common.gypi @@ -72,9 +72,6 @@ ##### end V8 defaults ##### - # This variable needs to be updated when N-API version change - 'napi_build_version': 4, - 'conditions': [ ['target_arch=="arm64"', { # Disabled pending https://github.com/nodejs/node/issues/23913. diff --git a/configure.py b/configure.py index 15ea5687cf1cd8..d5460e4f916978 100755 --- a/configure.py +++ b/configure.py @@ -34,6 +34,7 @@ # imports in tools/ sys.path.insert(0, 'tools') import getmoduleversion +import getnapibuildversion from gyp_node import run_gyp # imports in deps/v8/tools/node @@ -1097,6 +1098,9 @@ def configure_node(o): else: o['variables']['node_target_type'] = 'executable' +def configure_napi(output): + output['variables']['napi_build_version'] = getnapibuildversion.get_napi_version() + def configure_library(lib, output): shared_lib = 'shared_' + lib output['variables']['node_' + shared_lib] = b(getattr(options, shared_lib)) @@ -1576,6 +1580,7 @@ def make_bin_override(): flavor = GetFlavor(flavor_params) configure_node(output) +configure_napi(output) configure_library('zlib', output) configure_library('http_parser', output) configure_library('libuv', output) diff --git a/doc/api/process.md b/doc/api/process.md index c868f01fa1d3e8..3a7971be22b5a3 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -666,6 +666,7 @@ An example of the possible output looks like: variables: { host_arch: 'x64', + napi_build_version: 4, node_install_npm: 'true', node_prefix: '', node_shared_cares: 'false', diff --git a/test/parallel/test-process-versions.js b/test/parallel/test-process-versions.js index 18dffe0cadbe36..d3eaa745ba479e 100644 --- a/test/parallel/test-process-versions.js +++ b/test/parallel/test-process-versions.js @@ -45,3 +45,6 @@ for (let i = 0; i < expected_keys.length; i++) { const descriptor = Object.getOwnPropertyDescriptor(process.versions, key); assert.strictEqual(descriptor.writable, false); } + +assert.strictEqual(process.config.variables.napi_build_version, + process.versions.napi); diff --git a/tools/getnapibuildversion.py b/tools/getnapibuildversion.py new file mode 100644 index 00000000000000..c6275dcf38d0c5 --- /dev/null +++ b/tools/getnapibuildversion.py @@ -0,0 +1,26 @@ +from __future__ import print_function +import os +import re + + +def get_napi_version(): + napi_version_h = os.path.join( + os.path.dirname(__file__), + '..', + 'src', + 'js_native_api.h') + + f = open(napi_version_h) + + regex = '^#define NAPI_VERSION [0-9]+' + + for line in f: + if re.match(regex, line): + napi_version = line.split()[2] + return napi_version + + raise Exception('Could not find pattern matching %s' % regex) + + +if __name__ == '__main__': + print(get_napi_version()) From 875897374a5c07b7da766b0b3da86060bfd6d8f8 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Tue, 4 Jun 2019 11:28:56 +0200 Subject: [PATCH 3/6] Fixed lint problem on test and retrtieve N-API version from node_version.h --- test/parallel/test-process-versions.js | 2 +- tools/getnapibuildversion.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/parallel/test-process-versions.js b/test/parallel/test-process-versions.js index d3eaa745ba479e..83568a30d8074a 100644 --- a/test/parallel/test-process-versions.js +++ b/test/parallel/test-process-versions.js @@ -47,4 +47,4 @@ for (let i = 0; i < expected_keys.length; i++) { } assert.strictEqual(process.config.variables.napi_build_version, - process.versions.napi); + process.versions.napi); diff --git a/tools/getnapibuildversion.py b/tools/getnapibuildversion.py index c6275dcf38d0c5..de1de676d3763e 100644 --- a/tools/getnapibuildversion.py +++ b/tools/getnapibuildversion.py @@ -8,11 +8,11 @@ def get_napi_version(): os.path.dirname(__file__), '..', 'src', - 'js_native_api.h') + 'node_version.h') f = open(napi_version_h) - regex = '^#define NAPI_VERSION [0-9]+' + regex = '^#define NAPI_VERSION' for line in f: if re.match(regex, line): From cc8031b6bf2b1b3419da9a4ffb51fdd1ccca6bb3 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Mon, 10 Jun 2019 11:59:22 +0200 Subject: [PATCH 4/6] Added some comments about NAPI_VERSION --- src/js_native_api.h | 7 ++++++- src/node_version.h | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/js_native_api.h b/src/js_native_api.h index 0646a2d94cfd28..01af602ef70f07 100644 --- a/src/js_native_api.h +++ b/src/js_native_api.h @@ -12,7 +12,12 @@ #ifdef NAPI_EXPERIMENTAL #define NAPI_VERSION NAPI_VERSION_EXPERIMENTAL #else -// The baseline version for N-API +// The baseline version for N-API. +// The NAPI_VERSION controls which version will be used by default when +// compilling a native addon. If the addon developer specifically wants to use +// functions available in a new version of N-API that is not yet ported in all +// LTS version, they can set NAPI_VERSION knowing that they have specifically +// depended on that version. #define NAPI_VERSION 4 #endif #endif diff --git a/src/node_version.h b/src/node_version.h index 448fe507d053db..91ba66bab34a13 100644 --- a/src/node_version.h +++ b/src/node_version.h @@ -91,7 +91,8 @@ */ #define NODE_MODULE_VERSION 72 -// the NAPI_VERSION provided by this version of the runtime +// The NAPI_VERSION provided by this version of the runtime. This is the version +// which the Node binary being built supports. #define NAPI_VERSION 4 #endif // SRC_NODE_VERSION_H_ From a46ad002400e65f06f41e713814e77f7279ec7b7 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Thu, 13 Jun 2019 19:51:52 +0200 Subject: [PATCH 5/6] Fixed typo --- src/js_native_api.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js_native_api.h b/src/js_native_api.h index 01af602ef70f07..4f9345a4abf134 100644 --- a/src/js_native_api.h +++ b/src/js_native_api.h @@ -16,7 +16,7 @@ // The NAPI_VERSION controls which version will be used by default when // compilling a native addon. If the addon developer specifically wants to use // functions available in a new version of N-API that is not yet ported in all -// LTS version, they can set NAPI_VERSION knowing that they have specifically +// LTS versions, they can set NAPI_VERSION knowing that they have specifically // depended on that version. #define NAPI_VERSION 4 #endif From 0bf6549626cc8b93fe1e90e7a6044bf7def7e2a6 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Sun, 23 Jun 2019 13:16:07 +0200 Subject: [PATCH 6/6] Fixed style on configure_napi function. --- configure.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/configure.py b/configure.py index d5460e4f916978..ab7c00b7e49a00 100755 --- a/configure.py +++ b/configure.py @@ -1099,7 +1099,8 @@ def configure_node(o): o['variables']['node_target_type'] = 'executable' def configure_napi(output): - output['variables']['napi_build_version'] = getnapibuildversion.get_napi_version() + version = getnapibuildversion.get_napi_version() + output['variables']['napi_build_version'] = version def configure_library(lib, output): shared_lib = 'shared_' + lib