Skip to content

Commit 9cc969b

Browse files
committed
build: configure --embedded/--no-v8-platform
Add configure flag for building shared library that could be used to embed node.js in some application (like Electron). This commit is esentially a merged and refined version of: * atom/node@289649a * atom/node@f76669f * atom/node@0828dfa
1 parent a11d506 commit 9cc969b

5 files changed

Lines changed: 118 additions & 26 deletions

File tree

common.gypi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
'msvs_multi_core_compile': '0', # we do enable multicore compiles, but not using the V8 way
1212
'python%': 'python',
1313

14+
'node_shared%': 'false',
15+
'node_no_v8_platform%': 'false',
16+
1417
'node_tag%': '',
1518
'uv_library%': 'static_library',
1619

configure

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,18 @@ parser.add_option('--no-browser-globals',
400400
help='do not export browser globals like setTimeout, console, etc. ' +
401401
'(This mode is not officially supported for regular applications)')
402402

403+
parser.add_option('--shared',
404+
action='store_true',
405+
dest='shared',
406+
help='compile shared library for embedding node in another project. ' +
407+
'(This mode is not officially supported for regular applications)')
408+
409+
parser.add_option('--no-v8-platform',
410+
action='store_true',
411+
dest='no_v8_platform',
412+
help='do not initialize v8 platform during node.js startup. ' +
413+
'(This mode is not officially supported for regular applications)')
414+
403415
(options, args) = parser.parse_args()
404416

405417
# Expand ~ in the install prefix now, it gets written to multiple files.
@@ -793,6 +805,8 @@ def configure_node(o):
793805
o['variables']['node_target_type'] = 'static_library'
794806

795807
o['variables']['node_no_browser_globals'] = b(options.no_browser_globals)
808+
o['variables']['node_shared'] = b(options.shared)
809+
o['variables']['node_no_v8_platform'] = b(options.no_v8_platform)
796810

797811
if options.linked_module:
798812
o['variables']['library_files'] = options.linked_module

node.gyp

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
'node_use_etw%': 'false',
77
'node_use_perfctr%': 'false',
88
'node_no_browser_globals%': 'false',
9+
'node_no_v8_platform%': 'false',
10+
'node_shared%': 'false',
911
'node_shared_zlib%': 'false',
1012
'node_shared_http_parser%': 'false',
1113
'node_shared_cares%': 'false',
@@ -14,7 +16,6 @@
1416
'node_shared_openssl%': 'false',
1517
'node_v8_options%': '',
1618
'node_enable_v8_vtunejit%': 'false',
17-
'node_target_type%': 'executable',
1819
'node_core_target_name%': 'node',
1920
'library_files': [
2021
'lib/internal/bootstrap_node.js',
@@ -99,6 +100,14 @@
99100
'deps/v8/tools/SourceMap.js',
100101
'deps/v8/tools/tickprocessor-driver.js',
101102
],
103+
104+
'conditions': [
105+
[ 'node_shared=="true"', {
106+
'node_target_type%': 'shared_library',
107+
}, {
108+
'node_target_type%': 'executable',
109+
}],
110+
],
102111
},
103112

104113
'targets': [
@@ -108,16 +117,13 @@
108117

109118
'dependencies': [
110119
'node_js2c#host',
111-
'deps/v8/tools/gyp/v8.gyp:v8',
112-
'deps/v8/tools/gyp/v8.gyp:v8_libplatform'
113120
],
114121

115122
'include_dirs': [
116123
'src',
117124
'tools/msvs/genfiles',
118125
'deps/uv/src/ares',
119126
'<(SHARED_INTERMEDIATE_DIR)', # for node_natives.h
120-
'deps/v8' # include/v8_platform.h
121127
],
122128

123129
'sources': [
@@ -214,6 +220,39 @@
214220

215221

216222
'conditions': [
223+
[ 'node_shared=="false"', {
224+
'dependencies': [
225+
'deps/v8/tools/gyp/v8.gyp:v8',
226+
],
227+
228+
'msvs_settings': {
229+
'VCManifestTool': {
230+
'EmbedManifest': 'true',
231+
'AdditionalManifestFiles': 'src/res/node.exe.extra.manifest'
232+
}
233+
},
234+
}, {
235+
'defines': [
236+
'NODE_SHARED_MODE',
237+
],
238+
239+
'libraries': [
240+
'-lv8',
241+
],
242+
}],
243+
[ 'node_no_v8_platform=="false"', {
244+
'include_dirs': [
245+
'deps/v8', # include/v8_platform.h
246+
],
247+
248+
'dependencies': [
249+
'deps/v8/tools/gyp/v8.gyp:v8_libplatform',
250+
],
251+
}, {
252+
'defines': [
253+
'NODE_NO_V8_PLATFORM',
254+
],
255+
}],
217256
[ 'node_tag!=""', {
218257
'defines': [ 'NODE_TAG="<(node_tag)"' ],
219258
}],
@@ -242,7 +281,8 @@
242281
'defines': [ 'NODE_HAVE_SMALL_ICU=1' ],
243282
}]],
244283
}],
245-
[ 'node_enable_v8_vtunejit=="true" and (target_arch=="x64" or \
284+
[ 'node_shared=="false" and \
285+
node_enable_v8_vtunejit=="true" and (target_arch=="x64" or \
246286
target_arch=="ia32" or target_arch=="x32")', {
247287
'defines': [ 'NODE_ENABLE_VTUNE_PROFILING' ],
248288
'dependencies': [
@@ -370,7 +410,7 @@
370410
[ 'node_no_browser_globals=="true"', {
371411
'defines': [ 'NODE_NO_BROWSER_GLOBALS' ],
372412
} ],
373-
[ 'v8_postmortem_support=="true"', {
413+
[ 'node_shared=="false" and v8_postmortem_support=="true"', {
374414
'dependencies': [ 'deps/v8/tools/gyp/v8.gyp:postmortem-metadata' ],
375415
'conditions': [
376416
# -force_load is not applicable for the static library
@@ -462,12 +502,6 @@
462502
'ldflags': [ '-Wl,-M,/usr/lib/ld/map.noexstk' ],
463503
}],
464504
],
465-
'msvs_settings': {
466-
'VCManifestTool': {
467-
'EmbedManifest': 'true',
468-
'AdditionalManifestFiles': 'src/res/node.exe.extra.manifest'
469-
}
470-
},
471505
},
472506
# generate ETW header and resource files
473507
{
@@ -690,9 +724,21 @@
690724
'type': 'executable',
691725
'dependencies': [
692726
'deps/gtest/gtest.gyp:gtest',
693-
'deps/v8/tools/gyp/v8.gyp:v8',
694-
'deps/v8/tools/gyp/v8.gyp:v8_libplatform'
695727
],
728+
729+
'conditions': [
730+
[ 'node_shared=="false"', {
731+
'dependencies': [
732+
'deps/v8/tools/gyp/v8.gyp:v8',
733+
],
734+
}],
735+
[ 'node_no_v8_platform=="false"', {
736+
'dependencies': [
737+
'deps/v8/tools/gyp/v8.gyp:v8_libplatform',
738+
],
739+
}],
740+
],
741+
696742
'include_dirs': [
697743
'src',
698744
'deps/v8/include'

src/node.cc

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
#include "string_bytes.h"
4040
#include "util.h"
4141
#include "uv.h"
42-
#include "libplatform/libplatform.h"
42+
#ifndef NODE_NO_V8_PLATFORM
43+
# include "libplatform/libplatform.h"
44+
#endif // NODE_NO_V8_PLATFORM
4345
#include "v8-debug.h"
4446
#include "v8-profiler.h"
4547
#include "zlib.h"
@@ -183,7 +185,30 @@ static bool debugger_running;
183185
static uv_async_t dispatch_debug_messages_async;
184186

185187
static node::atomic<Isolate*> node_isolate;
186-
static v8::Platform* default_platform;
188+
189+
static struct {
190+
#ifdef NODE_NO_V8_PLATFORM
191+
void Initialize(int thread_pool_size) {}
192+
void PumpMessageLoop(Isolate* isolate) {}
193+
void Dispose() {}
194+
#else // !NODE_NO_V8_PLATFORM
195+
void Initialize(int thread_pool_size) {
196+
platform = v8::platform::CreateDefaultPlatform(thread_pool_size);
197+
V8::InitializePlatform(platform);
198+
}
199+
200+
void PumpMessageLoop(Isolate* isolate) {
201+
v8::platform::PumpMessageLoop(platform, isolate);
202+
}
203+
204+
void Dispose() {
205+
delete platform;
206+
platform = nullptr;
207+
}
208+
209+
static v8::Platform* platform;
210+
#endif // !NODE_NO_V8_PLATFORM
211+
} v8_platform;
187212

188213
static void PrintErrorString(const char* format, ...) {
189214
va_list ap;
@@ -4299,11 +4324,11 @@ static void StartNodeInstance(void* arg) {
42994324
SealHandleScope seal(isolate);
43004325
bool more;
43014326
do {
4302-
v8::platform::PumpMessageLoop(default_platform, isolate);
4327+
v8_platform.PumpMessageLoop(isolate);
43034328
more = uv_run(env->event_loop(), UV_RUN_ONCE);
43044329

43054330
if (more == false) {
4306-
v8::platform::PumpMessageLoop(default_platform, isolate);
4331+
v8_platform.PumpMessageLoop(isolate);
43074332
EmitBeforeExit(env);
43084333

43094334
// Emit `beforeExit` if the loop became alive either after emitting
@@ -4367,8 +4392,7 @@ int Start(int argc, char** argv) {
43674392
V8::SetEntropySource(crypto::EntropySource);
43684393
#endif
43694394

4370-
default_platform = v8::platform::CreateDefaultPlatform(v8_thread_pool_size);
4371-
V8::InitializePlatform(default_platform);
4395+
v8_platform.Initialize(v8_thread_pool_size);
43724396
V8::Initialize();
43734397

43744398
int exit_code = 1;
@@ -4385,8 +4409,7 @@ int Start(int argc, char** argv) {
43854409
}
43864410
V8::Dispose();
43874411

4388-
delete default_platform;
4389-
default_platform = nullptr;
4412+
v8_platform.Dispose();
43904413

43914414
delete[] exec_argv;
43924415
exec_argv = nullptr;

src/node.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,17 +403,23 @@ extern "C" NODE_EXTERN void node_module_register(void* mod);
403403
# define NODE_MODULE_EXPORT __attribute__((visibility("default")))
404404
#endif
405405

406+
#ifdef NODE_EMBEDDED_MODE
407+
# define NODE_CTOR_PREFIX
408+
#else
409+
# define NODE_CTOR_PREFIX static
410+
#endif
411+
406412
#if defined(_MSC_VER)
407413
#pragma section(".CRT$XCU", read)
408414
#define NODE_C_CTOR(fn) \
409-
static void __cdecl fn(void); \
415+
NODE_CTOR_PREFIX void __cdecl fn(void); \
410416
__declspec(dllexport, allocate(".CRT$XCU")) \
411417
void (__cdecl*fn ## _)(void) = fn; \
412-
static void __cdecl fn(void)
418+
NODE_CTOR_PREFIX void __cdecl fn(void)
413419
#else
414420
#define NODE_C_CTOR(fn) \
415-
static void fn(void) __attribute__((constructor)); \
416-
static void fn(void)
421+
NODE_CTOR_PREFIX void fn(void) __attribute__((constructor)); \
422+
NODE_CTOR_PREFIX void fn(void)
417423
#endif
418424

419425
#define NODE_MODULE_X(modname, regfunc, priv, flags) \

0 commit comments

Comments
 (0)