Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport --shared to V4.x codebase to allow building as a shared library / DLL #9385

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
'msvs_multi_core_compile': '0', # we do enable multicore compiles, but not using the V8 way
'python%': 'python',

'node_shared%': 'false',
'force_dynamic_crt%': 0,
'node_use_v8_platform%': 'true',
'node_use_bundled_v8%': 'true',
'node_module_version%': '',

'node_tag%': '',
'uv_library%': 'static_library',

Expand Down Expand Up @@ -73,6 +79,20 @@
['OS == "android"', {
'cflags': [ '-fPIE' ],
'ldflags': [ '-fPIE', '-pie' ]
}],
['node_shared=="true"', {
'msvs_settings': {
'VCCLCompilerTool': {
'RuntimeLibrary': 3 # MultiThreadedDebugDLL (/MDd)
}
}
}],
['node_shared=="false"', {
'msvs_settings': {
'VCCLCompilerTool': {
'RuntimeLibrary': 1 # MultiThreadedDebug (/MTd)
}
}
}]
],
'msvs_settings': {
Expand Down Expand Up @@ -110,11 +130,24 @@
['OS == "android"', {
'cflags': [ '-fPIE' ],
'ldflags': [ '-fPIE', '-pie' ]
}],
['node_shared=="true"', {
'msvs_settings': {
'VCCLCompilerTool': {
'RuntimeLibrary': 2 # MultiThreadedDLL (/MD)
}
}
}],
['node_shared=="false"', {
'msvs_settings': {
'VCCLCompilerTool': {
'RuntimeLibrary': 0 # MultiThreaded (/MT)
}
}
}]
],
'msvs_settings': {
'VCCLCompilerTool': {
'RuntimeLibrary': 0, # static release
'Optimization': 3, # /Ox, full optimization
'FavorSizeOrSpeed': 1, # /Ot, favour speed over size
'InlineFunctionExpansion': 2, # /Ob2, inline anything eligible
Expand Down Expand Up @@ -291,6 +324,9 @@
],
'ldflags!': [ '-rdynamic' ],
}],
[ 'node_shared=="true"', {
'cflags': [ '-fPIC' ],
}]
],
}],
['OS=="android"', {
Expand Down
36 changes: 33 additions & 3 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ from gyp.common import GetFlavor
sys.path.insert(0, os.path.join(root_dir, 'tools', 'configure.d'))
import nodedownload

# imports in tools/
sys.path.insert(0, os.path.join(root_dir, 'tools'))
import getmoduleversion

# parse our options
parser = optparse.OptionParser()

Expand Down Expand Up @@ -385,6 +389,26 @@ parser.add_option('--enable-static',
dest='enable_static',
help='build as static library')

parser.add_option('--shared',
action='store_true',
dest='shared',
help='compile shared library for embedding node in another project. ' +
'(This mode is not officially supported for regular applications)')

parser.add_option('--without-v8-platform',
action='store_true',
dest='without_v8_platform',
default=False,
help='do not initialize v8 platform during node.js startup. ' +
'(This mode is not officially supported for regular applications)')

parser.add_option('--without-bundled-v8',
action='store_true',
dest='without_bundled_v8',
default=False,
help='do not use V8 includes from the bundled deps folder. ' +
'(This mode is not officially supported for regular applications)')

(options, args) = parser.parse_args()

# Expand ~ in the install prefix now, it gets written to multiple files.
Expand Down Expand Up @@ -774,7 +798,14 @@ def configure_node(o):
if options.enable_static:
o['variables']['node_target_type'] = 'static_library'

o['variables']['node_module_version'] = 46
o['variables']['node_shared'] = b(options.shared)
o['variables']['node_use_v8_platform'] = b(not options.without_v8_platform)
o['variables']['node_use_bundled_v8'] = b(not options.without_bundled_v8)
node_module_version = getmoduleversion.get_version()
shlib_suffix = '%s.dylib' if sys.platform == 'darwin' else 'so.%s'
shlib_suffix %= node_module_version
o['variables']['node_module_version'] = int(node_module_version)
o['variables']['shlib_suffix'] = shlib_suffix

if options.linked_module:
o['variables']['library_files'] = options.linked_module
Expand Down Expand Up @@ -820,8 +851,7 @@ def configure_v8(o):
o['variables']['v8_random_seed'] = 0 # Use a random seed for hash tables.
o['variables']['v8_use_snapshot'] = 'false' if options.without_snapshot else 'true'
o['variables']['node_enable_d8'] = b(options.enable_d8)


o['variables']['force_dynamic_crt'] = 1 if options.shared else 0
def configure_openssl(o):
o['variables']['node_use_openssl'] = b(not options.without_ssl)
o['variables']['node_shared_openssl'] = b(options.shared_openssl)
Expand Down
7 changes: 4 additions & 3 deletions deps/v8/build/toolchain.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'ubsan_vptr%': 0,
'v8_target_arch%': '<(target_arch)',
'v8_host_byteorder%': '<!(python -c "import sys; print sys.byteorder")',
'force_dynamic_crt%': 0,
# Native Client builds currently use the V8 ARM JIT and
# arm/simulator-arm.cc to defer the significant effort required
# for NaCl JIT support. The nacl_target_arch variable provides
Expand Down Expand Up @@ -995,7 +996,7 @@
'VCCLCompilerTool': {
'Optimization': '0',
'conditions': [
['component=="shared_library"', {
['component=="shared_library" or force_dynamic_crt==1', {
'RuntimeLibrary': '3', # /MDd
}, {
'RuntimeLibrary': '1', # /MTd
Expand Down Expand Up @@ -1047,7 +1048,7 @@
'StringPooling': 'true',
'BasicRuntimeChecks': '0',
'conditions': [
['component=="shared_library"', {
['component=="shared_library" or force_dynamic_crt==1', {
'RuntimeLibrary': '3', #/MDd
}, {
'RuntimeLibrary': '1', #/MTd
Expand Down Expand Up @@ -1235,7 +1236,7 @@
'FavorSizeOrSpeed': '0',
'StringPooling': 'true',
'conditions': [
['component=="shared_library"', {
['component=="shared_library" or force_dynamic_crt==1', {
'RuntimeLibrary': '2', #/MD
}, {
'RuntimeLibrary': '0', #/MT
Expand Down
2 changes: 1 addition & 1 deletion deps/v8/include/v8-version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define V8_MAJOR_VERSION 4
#define V8_MINOR_VERSION 5
#define V8_BUILD_NUMBER 103
#define V8_PATCH_LEVEL 42
#define V8_PATCH_LEVEL 43

// Use 1 for candidates and 0 otherwise.
// (Boolean macro values are not supported by all preprocessors.)
Expand Down
56 changes: 48 additions & 8 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
'node_use_lttng%': 'false',
'node_use_etw%': 'false',
'node_use_perfctr%': 'false',
'node_use_v8_platform%': 'true',
'node_use_bundled_v8%': 'true',
'node_shared%': 'false',
'force_dynamic_crt%': 0,
'node_module_version%': 'true',
'node_has_winsdk%': 'false',
'node_shared_zlib%': 'false',
'node_shared_http_parser%': 'false',
Expand Down Expand Up @@ -101,6 +106,11 @@
}, {
'use_openssl_def': 0,
}],
[ 'node_shared=="true"', {
'node_target_type%': 'shared_library',
}, {
'node_target_type%': 'executable',
}],
],
},

Expand Down Expand Up @@ -220,6 +230,42 @@


'conditions': [
[ 'node_shared=="false"', {
'msvs_settings': {
'VCManifestTool': {
'EmbedManifest': 'true',
'AdditionalManifestFiles': 'src/res/node.exe.extra.manifest'
}
},
}, {
'defines': [
'NODE_SHARED_MODE',
],
'conditions': [
[ 'node_module_version!="" and OS!="win"', {
'product_extension': '<(shlib_suffix)',
}]
],
}],
[ 'node_use_bundled_v8=="true"', {
'include_dirs': [
'deps/v8' # include/v8_platform.h
],

'dependencies': [
'deps/v8/tools/gyp/v8.gyp:v8',
'deps/v8/tools/gyp/v8.gyp:v8_libplatform'
],
}],
[ 'node_use_v8_platform=="true"', {
'defines': [
'NODE_USE_V8_PLATFORM=1',
],
}, {
'defines': [
'NODE_USE_V8_PLATFORM=0',
],
}],
[ 'node_enable_d8=="true"', {
'dependencies': [ 'deps/v8/src/d8.gyp:d8' ],
}],
Expand Down Expand Up @@ -292,7 +338,7 @@
],
},
'conditions': [
['OS in "linux freebsd"', {
['OS in "linux freebsd" and node_shared=="false"', {
'ldflags': [
'-Wl,--whole-archive,'
'<(PRODUCT_DIR)/obj.target/deps/openssl/'
Expand Down Expand Up @@ -460,7 +506,7 @@
'NODE_PLATFORM="sunos"',
],
}],
[ 'OS=="freebsd" or OS=="linux"', {
[ '(OS=="freebsd" or OS=="linux") and node_shared=="false"', {
'ldflags': [ '-Wl,-z,noexecstack',
'-Wl,--whole-archive <(V8_BASE)',
'-Wl,--no-whole-archive' ]
Expand All @@ -469,12 +515,6 @@
'ldflags': [ '-Wl,-M,/usr/lib/ld/map.noexstk' ],
}],
],
'msvs_settings': {
'VCManifestTool': {
'EmbedManifest': 'true',
'AdditionalManifestFiles': 'src/res/node.exe.extra.manifest'
}
},
},
{
'target_name': 'mkssldef',
Expand Down
37 changes: 31 additions & 6 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
#include "string_bytes.h"
#include "util.h"
#include "uv.h"
#if NODE_USE_V8_PLATFORM
#include "libplatform/libplatform.h"
#endif // NODE_USE_V8_PLATFORM
#include "v8-debug.h"
#include "v8-profiler.h"
#include "zlib.h"
Expand Down Expand Up @@ -167,6 +169,30 @@ static v8::Platform* default_platform;
static uv_sem_t debug_semaphore;
#endif

static struct {
#if NODE_USE_V8_PLATFORM
void Initialize(int thread_pool_size) {
platform_ = v8::platform::CreateDefaultPlatform(thread_pool_size);
V8::InitializePlatform(platform_);
}

void PumpMessageLoop(Isolate* isolate) {
v8::platform::PumpMessageLoop(platform_, isolate);
}

void Dispose() {
delete platform_;
platform_ = nullptr;
}

v8::Platform* platform_;
#else // !NODE_USE_V8_PLATFORM
void Initialize(int thread_pool_size) {}
void PumpMessageLoop(Isolate* isolate) {}
void Dispose() {}
#endif // !NODE_USE_V8_PLATFORM
} v8_platform;

static void PrintErrorString(const char* format, ...) {
va_list ap;
va_start(ap, format);
Expand Down Expand Up @@ -4226,11 +4252,11 @@ static void StartNodeInstance(void* arg) {
SealHandleScope seal(isolate);
bool more;
do {
v8::platform::PumpMessageLoop(default_platform, isolate);
v8_platform.PumpMessageLoop(isolate);
more = uv_run(env->event_loop(), UV_RUN_ONCE);

if (more == false) {
v8::platform::PumpMessageLoop(default_platform, isolate);
v8_platform.PumpMessageLoop(isolate);
EmitBeforeExit(env);

// Emit `beforeExit` if the loop became alive either after emitting
Expand Down Expand Up @@ -4291,8 +4317,8 @@ int Start(int argc, char** argv) {
#endif

const int thread_pool_size = 4;
default_platform = v8::platform::CreateDefaultPlatform(thread_pool_size);
V8::InitializePlatform(default_platform);

v8_platform.Initialize(thread_pool_size);
V8::Initialize();

int exit_code = 1;
Expand All @@ -4309,8 +4335,7 @@ int Start(int argc, char** argv) {
}
V8::Dispose();

delete default_platform;
default_platform = nullptr;
v8_platform.Dispose();

delete[] exec_argv;
exec_argv = nullptr;
Expand Down
14 changes: 10 additions & 4 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,17 +396,23 @@ extern "C" NODE_EXTERN void node_module_register(void* mod);
# define NODE_MODULE_EXPORT __attribute__((visibility("default")))
#endif

#ifdef NODE_SHARED_MODE
# define NODE_CTOR_PREFIX
#else
# define NODE_CTOR_PREFIX static
#endif

#if defined(_MSC_VER)
#pragma section(".CRT$XCU", read)
#define NODE_C_CTOR(fn) \
static void __cdecl fn(void); \
NODE_CTOR_PREFIX void __cdecl fn(void); \
__declspec(dllexport, allocate(".CRT$XCU")) \
void (__cdecl*fn ## _)(void) = fn; \
static void __cdecl fn(void)
NODE_CTOR_PREFIX void __cdecl fn(void)
#else
#define NODE_C_CTOR(fn) \
static void fn(void) __attribute__((constructor)); \
static void fn(void)
NODE_CTOR_PREFIX void fn(void) __attribute__((constructor)); \
NODE_CTOR_PREFIX void fn(void)
#endif

#define NODE_MODULE_X(modname, regfunc, priv, flags) \
Expand Down
Loading