From e880392eef0455fff9ee4b37ad49331f57c5fb8f Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Tue, 26 May 2026 12:55:45 -0400 Subject: [PATCH 1/4] build: build codecache and snapshot with libnode Signed-off-by: Chengzhong Wu --- configure.py | 6 +- node.gyp | 217 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 139 insertions(+), 84 deletions(-) diff --git a/configure.py b/configure.py index 714639bf03bf07..7ec71285a49e9a 100755 --- a/configure.py +++ b/configure.py @@ -1900,8 +1900,7 @@ def configure_node(o): if options.without_node_snapshot or options.node_builtin_modules_path: o['variables']['node_use_node_snapshot'] = 'false' else: - o['variables']['node_use_node_snapshot'] = b( - not cross_compiling and not options.shared) + o['variables']['node_use_node_snapshot'] = b(not cross_compiling) # Do not use code cache when Node.js is built for collecting coverage of itself, this allows more # precise coverage for the JS built-ins. @@ -1909,8 +1908,7 @@ def configure_node(o): o['variables']['node_use_node_code_cache'] = 'false' else: # TODO(refack): fix this when implementing embedded code-cache when cross-compiling. - o['variables']['node_use_node_code_cache'] = b( - not cross_compiling and not options.shared) + o['variables']['node_use_node_code_cache'] = b(not cross_compiling) if options.write_snapshot_as_array_literals is not None: o['variables']['node_write_snapshot_as_array_literals'] = b(options.write_snapshot_as_array_literals) diff --git a/node.gyp b/node.gyp index 28db2a955b74d6..1ae55e2b50ba05 100644 --- a/node.gyp +++ b/node.gyp @@ -6,9 +6,15 @@ 'v8_enable_31bit_smis_on_64bit_arch%': 0, 'force_dynamic_crt%': 0, 'node_builtin_modules_path%': '', + # `node` executable target name. 'node_core_target_name%': 'node', - 'node_enable_v8_vtunejit%': 'false', + # Derived flag from `node_shared`. + # On most platforms, this is `static_library` if `node_shared` is false and `shared_library` if `node_shared` is true. + # AIX needs to generate static library first and then link to shared library `node_aix_shared`. + # TODO(legendecas): move this to depend on target `node_base` in AIX build . 'node_intermediate_lib_type%': 'static_library', + # `libnode` target name, can be a `static_library` or `shared_library` based on `node_shared`. + # NOTE: Gyp will prefix this with `lib` if this name does not start with `lib`. 'node_lib_target_name%': 'libnode', 'node_module_version%': '', 'node_no_browser_globals%': 'false', @@ -41,6 +47,7 @@ 'node_use_sqlite%': 'true', 'node_use_ffi%': 'false', 'node_use_v8_platform%': 'true', + 'node_enable_v8_vtunejit%': 'false', 'node_v8_options%': '', 'node_write_snapshot_as_string_literals': 'true', 'ossfuzz' : 'false', @@ -670,16 +677,16 @@ }], ], }], - [ 'node_intermediate_lib_type=="static_library" and node_shared=="false"', { + [ 'node_shared=="false"', { 'xcode_settings': { 'OTHER_LDFLAGS': [ - '-Wl,-force_load,<(PRODUCT_DIR)/<(STATIC_LIB_PREFIX)<(node_core_target_name)<(STATIC_LIB_SUFFIX)', + '-Wl,-force_load,<(PRODUCT_DIR)/<(STATIC_LIB_PREFIX)node_base<(STATIC_LIB_SUFFIX)', ], }, 'msvs_settings': { 'VCLinkerTool': { 'AdditionalOptions': [ - '/WHOLEARCHIVE:<(PRODUCT_DIR)/lib/<(node_lib_target_name)<(STATIC_LIB_SUFFIX)', + '/WHOLEARCHIVE:<(PRODUCT_DIR)/lib/<(STATIC_LIB_PREFIX)node_base<(STATIC_LIB_SUFFIX)', '/WHOLEARCHIVE:<(PRODUCT_DIR)/lib/<(STATIC_LIB_PREFIX)v8_base_without_compiler<(STATIC_LIB_SUFFIX)', ], }, @@ -695,7 +702,7 @@ ['node_use_bundled_v8=="true" and OS != "aix" and OS != "os400" and OS != "mac" and OS != "ios"', { 'ldflags': [ '-Wl,--whole-archive', - '<(obj_dir)/<(STATIC_LIB_PREFIX)<(node_core_target_name)<(STATIC_LIB_SUFFIX)', + '<(obj_dir)/<(STATIC_LIB_PREFIX)node_base<(STATIC_LIB_SUFFIX)', '<(obj_dir)/tools/v8_gypfiles/<(STATIC_LIB_PREFIX)v8_base_without_compiler<(STATIC_LIB_SUFFIX)', '-Wl,--no-whole-archive', ], @@ -703,7 +710,7 @@ ['node_use_bundled_v8!="true" and OS != "aix" and OS != "os400" and OS != "mac" and OS != "ios"', { 'ldflags': [ '-Wl,--whole-archive', - '<(obj_dir)/<(STATIC_LIB_PREFIX)<(node_core_target_name)<(STATIC_LIB_SUFFIX)', + '<(obj_dir)/<(STATIC_LIB_PREFIX)node_base<(STATIC_LIB_SUFFIX)', '-Wl,--no-whole-archive', ], }], @@ -771,55 +778,6 @@ 'LinkIncremental': 2, # enable incremental linking }, }, - }], - ['node_use_node_snapshot=="true"', { - 'dependencies': [ - 'node_mksnapshot', - ], - 'conditions': [ - ['node_snapshot_main!=""', { - 'actions': [ - { - 'action_name': 'node_mksnapshot', - 'process_outputs_as_sources': 1, - 'inputs': [ - '<(node_mksnapshot_exec)', - '<(node_snapshot_main)', - ], - 'outputs': [ - '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', - ], - 'action': [ - '<(node_mksnapshot_exec)', - '--build-snapshot', - '<(node_snapshot_main)', - '<@(_outputs)', - ], - }, - ], - }, { - 'actions': [ - { - 'action_name': 'node_mksnapshot', - 'process_outputs_as_sources': 1, - 'inputs': [ - '<(node_mksnapshot_exec)', - ], - 'outputs': [ - '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', - ], - 'action': [ - '<@(_inputs)', - '<@(_outputs)', - ], - }, - ], - }], - ], - }, { - 'sources': [ - 'src/node_snapshot_stub.cc' - ], }], [ 'OS in "linux freebsd openharmony" and ' 'target_arch=="x64"', { @@ -913,8 +871,8 @@ ], }, # node_core_target_name { - 'target_name': '<(node_lib_target_name)', - 'type': '<(node_intermediate_lib_type)', + 'target_name': 'node_base', + 'type': 'static_library', 'includes': [ 'node.gypi', ], @@ -933,9 +891,6 @@ # Dependency headers 'deps/v8/include/v8.h', 'deps/postject/postject-api.h', - # javascript files to make for an even more pleasant IDE experience - '<@(library_files)', - '<@(deps_files)', # node.gyp is added by default, common.gypi is added for change detection 'common.gypi', ], @@ -979,17 +934,6 @@ }], [ 'node_builtin_modules_path!=""', { 'defines': [ 'NODE_BUILTIN_MODULES_PATH="<(node_builtin_modules_path)"' ], - # When loading builtins from disk, JS source files do not need to - # trigger rebuilds since the binary reads them at runtime. - 'sources!': [ - '<@(library_files)', - '<@(deps_files)', - ], - }], - [ 'node_shared=="true"', { - 'sources': [ - 'src/node_snapshot_stub.cc', - ] }], [ 'node_use_bundled_v8!="false"', { 'dependencies': [ 'tools/v8_gypfiles/abseil.gyp:abseil' ], @@ -1042,13 +986,6 @@ 'defines': [ 'HAVE_INSPECTOR=0' ] }], [ 'OS=="win"', { - 'conditions': [ - [ 'node_intermediate_lib_type!="static_library"', { - 'sources': [ - 'src/res/node.rc', - ], - }], - ], 'libraries': [ 'Dbghelp', 'Psapi', @@ -1155,7 +1092,7 @@ [ 'debug_nghttp2==1', { 'defines': [ 'NODE_DEBUG_NGHTTP2=1' ] }], - # Thin LTO for node sources (scoped to libnode, not global) + # Thin LTO for node sources (scoped to node_base, not global) ['node_with_ltcg=="true"', { 'msvs_settings': { 'VCCLCompilerTool': { @@ -1200,6 +1137,126 @@ ], }, ], + }, # node_base + { + 'target_name': '<(node_lib_target_name)', + 'type': '<(node_intermediate_lib_type)', + 'includes': [ + 'node.gypi', + ], + + 'include_dirs': [ + 'src', + 'deps/v8/include', + 'deps/uv/include', + ], + + 'dependencies': [ + 'node_base', + ], + + 'defines': [ + 'NODE_ARCH="<(target_arch)"', + 'NODE_PLATFORM="<(OS)"', + 'NODE_WANT_INTERNALS=1', + ], + + 'sources': [ + # javascript files to make for an even more pleasant IDE experience + '<@(library_files)', + '<@(deps_files)', + ], + + 'conditions': [ + [ 'node_builtin_modules_path!=""', { + # When loading builtins from disk, JS source files do not need to + # trigger rebuilds since the binary reads them at runtime. + 'sources!': [ + '<@(library_files)', + '<@(deps_files)', + ], + }], + ['node_use_node_snapshot=="true"', { + 'dependencies': [ + 'node_mksnapshot', + ], + 'conditions': [ + ['node_snapshot_main!=""', { + 'actions': [ + { + 'action_name': 'node_mksnapshot', + 'process_outputs_as_sources': 1, + 'inputs': [ + '<(node_mksnapshot_exec)', + '<(node_snapshot_main)', + ], + 'outputs': [ + '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', + ], + 'action': [ + '<(node_mksnapshot_exec)', + '--build-snapshot', + '<(node_snapshot_main)', + '<@(_outputs)', + ], + }, + ], + }, { + 'actions': [ + { + 'action_name': 'node_mksnapshot', + 'process_outputs_as_sources': 1, + 'inputs': [ + '<(node_mksnapshot_exec)', + ], + 'outputs': [ + '<(SHARED_INTERMEDIATE_DIR)/node_snapshot.cc', + ], + 'action': [ + '<@(_inputs)', + '<@(_outputs)', + ], + }, + ], + }], + ], + }, { + 'sources': [ + 'src/node_snapshot_stub.cc' + ], + }], + [ 'node_shared=="true"', { + 'xcode_settings': { + 'OTHER_LDFLAGS': [ + '-Wl,-force_load,<(PRODUCT_DIR)/<(STATIC_LIB_PREFIX)node_base<(STATIC_LIB_SUFFIX)', + ], + }, + 'conditions': [ + ['OS!="aix" and OS!="os400" and OS!="mac" and OS!="ios"', { + 'ldflags': [ + '-Wl,--whole-archive', + '<(obj_dir)/<(STATIC_LIB_PREFIX)node_base<(STATIC_LIB_SUFFIX)', + '-Wl,--no-whole-archive', + ], + }], + ], + }], + [ 'node_shared=="true" and node_module_version!="" and OS!="win"', { + 'product_extension': '<(shlib_suffix)', + 'xcode_settings': { + 'LD_DYLIB_INSTALL_NAME': + '@rpath/lib<(node_core_target_name).<(shlib_suffix)' + }, + }], + ['node_shared=="true" and OS in "aix os400"', { + 'product_name': 'node_base', + }], + [ 'node_shared=="true" and OS=="win"', { + 'sources': [ + 'src/res/node.rc', + ], + }], + ], }, # node_lib_target_name { # fuzz_env 'target_name': 'fuzz_env', @@ -1692,7 +1749,7 @@ 'type': 'executable', 'dependencies': [ - '<(node_lib_target_name)', + 'node_base', ], 'includes': [ From 1b0519f196c05ab7f5a41b7a0df225f41c1462f3 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Thu, 28 May 2026 17:12:06 -0400 Subject: [PATCH 2/4] fixup! build: build codecache and snapshot with libnode --- node.gyp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/node.gyp b/node.gyp index 1ae55e2b50ba05..34374324bac6ad 100644 --- a/node.gyp +++ b/node.gyp @@ -970,16 +970,6 @@ }], ], }], - [ 'node_shared=="true" and node_module_version!="" and OS!="win"', { - 'product_extension': '<(shlib_suffix)', - 'xcode_settings': { - 'LD_DYLIB_INSTALL_NAME': - '@rpath/lib<(node_core_target_name).<(shlib_suffix)' - }, - }], - ['node_shared=="true" and OS in "aix os400"', { - 'product_name': 'node_base', - }], [ 'v8_enable_inspector==1', { 'includes' : [ 'src/inspector/node_inspector.gypi' ], }, { From da0cc2ffd792ef7a0932e4c30b699a7a3b69a383 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Fri, 29 May 2026 16:32:48 -0400 Subject: [PATCH 3/4] fixup! build: build codecache and snapshot with libnode --- node.gyp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/node.gyp b/node.gyp index 34374324bac6ad..080581913790f8 100644 --- a/node.gyp +++ b/node.gyp @@ -678,6 +678,7 @@ ], }], [ 'node_shared=="false"', { + # Keep this whole-archive section in sync with the `node_lib` target below. 'xcode_settings': { 'OTHER_LDFLAGS': [ '-Wl,-force_load,<(PRODUCT_DIR)/<(STATIC_LIB_PREFIX)node_base<(STATIC_LIB_SUFFIX)', @@ -1216,19 +1217,30 @@ ], }], [ 'node_shared=="true"', { + # Keep this whole-archive section in sync with the `node_exe` target above. 'xcode_settings': { 'OTHER_LDFLAGS': [ '-Wl,-force_load,<(PRODUCT_DIR)/<(STATIC_LIB_PREFIX)node_base<(STATIC_LIB_SUFFIX)', ], }, - 'conditions': [ - ['OS!="aix" and OS!="os400" and OS!="mac" and OS!="ios"', { - 'ldflags': [ - '-Wl,--whole-archive', - '<(obj_dir)/<(STATIC_LIB_PREFIX)node_base<(STATIC_LIB_SUFFIX)', - '-Wl,--no-whole-archive', + 'msvs_settings': { + 'VCLinkerTool': { + 'AdditionalOptions': [ + '/WHOLEARCHIVE:<(PRODUCT_DIR)/lib/<(STATIC_LIB_PREFIX)node_base<(STATIC_LIB_SUFFIX)', + '/WHOLEARCHIVE:<(PRODUCT_DIR)/lib/<(STATIC_LIB_PREFIX)v8_base_without_compiler<(STATIC_LIB_SUFFIX)', ], + }, + }, + 'conditions': [ + ['node_use_bundled_v8=="true"', { + 'xcode_settings': { + 'OTHER_LDFLAGS': [ + '-Wl,-force_load,<(PRODUCT_DIR)/<(STATIC_LIB_PREFIX)v8_base_without_compiler<(STATIC_LIB_SUFFIX)', + ], + }, }], + # gyp automatically applies `--whole-archive` to static dependencies of `shared_library` targets. + # No need to add the flags again here. ], }], [ 'node_shared=="true" and node_module_version!="" and OS!="win"', { From 1fabe7af9115b23171d7f34c5cdbc0bb28ad6e3c Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Fri, 29 May 2026 16:34:05 -0400 Subject: [PATCH 4/4] fixup! build: build codecache and snapshot with libnode --- configure.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/configure.py b/configure.py index 7ec71285a49e9a..13da256ad66329 100755 --- a/configure.py +++ b/configure.py @@ -1886,10 +1886,6 @@ def configure_node(o): o['variables']['arm_fpu'] = options.arm_fpu or 'neon' if options.node_snapshot_main is not None: - if options.shared: - # This should be possible to fix, but we will need to refactor the - # libnode target to avoid building it twice. - error('--node-snapshot-main is incompatible with --shared') if options.without_node_snapshot: error('--node-snapshot-main is incompatible with ' + '--without-node-snapshot')