From 6e77c94a35ce175a9bdc3143e3f52beb4e6c31e8 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Fri, 29 Jul 2022 16:59:56 -0700 Subject: [PATCH 1/2] fix: ensure native modules are built with config.gypi This works by patching node.h to check that two defines are set using the equivilant of an XNOR operation. One define "ELECTRON_ENSURE_CONFIG_GYPI" is set via common.gypi which is _already_ used to build native modules and has been since the dawn of time. Therefore this define will be set for all native module compilations targetting the Electron runtime. The second define "USING_ELECTRON_CONFIG_GYPI" is only defined when the gypi argument "using_electron_config_gypi" is set to 1 which is only done so via config.gypi. Only new enough versions of node-gyp correctly use the config.gypi file thus resulting in a compilation error on version of node-gyp that are too old. --- BUILD.gn | 3 + patches/node/.patches | 1 + ...compilation_fails_if_not_using_a_new.patch | 70 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 patches/node/build_ensure_native_module_compilation_fails_if_not_using_a_new.patch diff --git a/BUILD.gn b/BUILD.gn index ec385d1e8c8df..1b09bb1350747 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -233,6 +233,9 @@ action("electron_js2c") { action("generate_config_gypi") { outputs = [ "$root_gen_dir/config.gypi" ] script = "script/generate-config-gypi.py" + inputs = [ + "//third_party/electron_node/configure.py" + ] args = rebase_path(outputs) + [ target_cpu ] } diff --git a/patches/node/.patches b/patches/node/.patches index 17a4ed067136a..9e31afcb3458c 100644 --- a/patches/node/.patches +++ b/patches/node/.patches @@ -45,3 +45,4 @@ fix_add_v8_enable_reverse_jsargs_defines_in_common_gypi.patch json_parse_errors_made_user-friendly.patch support_v8_sandboxed_pointers.patch build_ensure_v8_pointer_compression_sandbox_is_enabled_on_64bit.patch +build_ensure_native_module_compilation_fails_if_not_using_a_new.patch diff --git a/patches/node/build_ensure_native_module_compilation_fails_if_not_using_a_new.patch b/patches/node/build_ensure_native_module_compilation_fails_if_not_using_a_new.patch new file mode 100644 index 0000000000000..7b7150085658a --- /dev/null +++ b/patches/node/build_ensure_native_module_compilation_fails_if_not_using_a_new.patch @@ -0,0 +1,70 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Samuel Attard +Date: Wed, 13 Jul 2022 13:56:12 -0700 +Subject: build: ensure native module compilation fails if not using a new + enough builder + +This should not be upstreamed, it is a quality-of-life patch for downstream module builders. + +diff --git a/common.gypi b/common.gypi +index ca08deeb19f5fe9ee399ad809b24579fdaa10036..c68cf1eb0f9cdd2a8fe314237c1e790fe3690272 100644 +--- a/common.gypi ++++ b/common.gypi +@@ -85,6 +85,8 @@ + + 'v8_enable_reverse_jsargs%': 1, + ++ 'using_electron_config_gypi%': 0, ++ + ##### end V8 defaults ##### + + # When building native modules using 'npm install' with the system npm, +@@ -290,6 +292,7 @@ + 'V8_DEPRECATION_WARNINGS', + 'V8_IMMINENT_DEPRECATION_WARNINGS', + '_GLIBCXX_USE_CXX11_ABI=1', ++ 'ELECTRON_ENSURE_CONFIG_GYPI', + ], + + # Forcibly disable -Werror. We support a wide range of compilers, it's +@@ -393,6 +396,11 @@ + }], + ], + }], ++ ['using_electron_config_gypi == 1', { ++ 'defines': [ ++ 'USING_ELECTRON_CONFIG_GYPI', ++ ], ++ }], + ['v8_enable_pointer_compression == 1', { + 'defines': [ + 'V8_COMPRESS_POINTERS', +diff --git a/configure.py b/configure.py +index 426afed7cd0a3a403d4b753af0b25f55024d0f71..6ab1469c07298c9083a77d0211727a6fff37267e 100755 +--- a/configure.py ++++ b/configure.py +@@ -1427,6 +1427,7 @@ def configure_library(lib, output, pkgname=None): + + + def configure_v8(o): ++ o['variables']['using_electron_config_gypi'] = 1 + o['variables']['v8_enable_webassembly'] = 1 + o['variables']['v8_enable_lite_mode'] = 1 if options.v8_lite_mode else 0 + o['variables']['v8_enable_gdbjit'] = 1 if options.gdb else 0 +diff --git a/src/node.h b/src/node.h +index 0b807cb25f9eb52b2100f0e2a7c25344790967cf..a41b09047d2c499a90225651a8324ad83a7712e3 100644 +--- a/src/node.h ++++ b/src/node.h +@@ -22,6 +22,12 @@ + #ifndef SRC_NODE_H_ + #define SRC_NODE_H_ + ++#ifdef ELECTRON_ENSURE_CONFIG_GYPI ++#ifndef USING_ELECTRON_CONFIG_GYPI ++#error "It looks like you are building this native module without using the right config.gypi. This normally means that you need to update electron-rebuild (>=3.2.8) or node-gyp (>=8.4.0) if you're building modules directly." ++#endif ++#endif ++ + #ifdef _WIN32 + # ifndef BUILDING_NODE_EXTENSION + # define NODE_EXTERN __declspec(dllexport) From 82921b8717e77de4df4ddf2a843a49f6c16d277f Mon Sep 17 00:00:00 2001 From: VerteDinde Date: Sat, 30 Jul 2022 06:00:25 -0700 Subject: [PATCH 2/2] chore: fix lint --- BUILD.gn | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 1b09bb1350747..6ef3b3be92f9e 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -233,9 +233,7 @@ action("electron_js2c") { action("generate_config_gypi") { outputs = [ "$root_gen_dir/config.gypi" ] script = "script/generate-config-gypi.py" - inputs = [ - "//third_party/electron_node/configure.py" - ] + inputs = [ "//third_party/electron_node/configure.py" ] args = rebase_path(outputs) + [ target_cpu ] }