From 9b735b17e922ee304c5f2106f992df99a6055d52 Mon Sep 17 00:00:00 2001 From: NickNaso Date: Thu, 23 May 2019 11:57:31 +0200 Subject: [PATCH] build: expose napi_build_version variable Expose `napi_build_version` to allow `node-gyp` to make it available for building native addons. Fixes: https://github.com/nodejs/node-gyp/issues/1745 Refs: https://github.com/nodejs/abi-stable-node/issues/371 PR-URL: https://github.com/nodejs/node/pull/27835 Reviewed-By: Ben Noordhuis Reviewed-By: Anna Henningsen Reviewed-By: Gabriel Schulhof Reviewed-By: Luigi Pinca Reviewed-By: Richard Lau Reviewed-By: Michael Dawson --- configure.py | 6 ++++++ doc/api/process.md | 1 + src/js_native_api.h | 7 ++++++- src/node_version.h | 3 ++- test/parallel/test-process-versions.js | 3 +++ tools/getnapibuildversion.py | 26 ++++++++++++++++++++++++++ 6 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 tools/getnapibuildversion.py diff --git a/configure.py b/configure.py index 6ecc33174ba1cc..4985200b284bae 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 @@ -1147,6 +1148,10 @@ def configure_node(o): else: o['variables']['node_target_type'] = 'executable' +def configure_napi(output): + version = getnapibuildversion.get_napi_version() + output['variables']['napi_build_version'] = version + def configure_library(lib, output): shared_lib = 'shared_' + lib output['variables']['node_' + shared_lib] = b(getattr(options, shared_lib)) @@ -1626,6 +1631,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 8b7f0a67e0c538..e274d96f7838a7 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -662,6 +662,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/src/js_native_api.h b/src/js_native_api.h index 0646a2d94cfd28..4f9345a4abf134 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 versions, 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 97c136c7190c01..eaabb46f78b376 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_ diff --git a/test/parallel/test-process-versions.js b/test/parallel/test-process-versions.js index 18dffe0cadbe36..83568a30d8074a 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..de1de676d3763e --- /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', + 'node_version.h') + + f = open(napi_version_h) + + regex = '^#define NAPI_VERSION' + + 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())