From 6d9eb3f52f185532e9270a0917dda814e8b8d41d Mon Sep 17 00:00:00 2001
From: Yang Guo <yangguo@chromium.org>
Date: Thu, 11 Jan 2018 13:25:09 +0100
Subject: [PATCH] build: add option to build v8 with GN

---
 common.gypi | 18 +++++++++++++++---
 configure   | 18 ++++++++++++++++++
 node.gypi   | 17 +++++++++++++----
 3 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/common.gypi b/common.gypi
index 84914640271fb1..cdc11bd909f740 100644
--- a/common.gypi
+++ b/common.gypi
@@ -48,7 +48,13 @@
         'V8_BASE': '<(PRODUCT_DIR)/obj/deps/v8/src/libv8_base.a',
        }, {
          'OBJ_DIR%': '<(PRODUCT_DIR)/obj.target',
-         'V8_BASE%': '<(PRODUCT_DIR)/obj.target/deps/v8/src/libv8_base.a',
+         'conditions': [
+           [ 'build_v8_with_gn=="true"', {
+             'V8_BASE': '<(PRODUCT_DIR)/obj.target/v8_monolith/geni/gn/obj/libv8_monolith.a',
+           }, {
+             'V8_BASE': '<(PRODUCT_DIR)/obj.target/deps/v8/src/libv8_base.a',
+           }],
+         ],
       }],
       ['OS == "win"', {
         'os_posix': 0,
@@ -59,9 +65,15 @@
         'os_posix': 1,
         'v8_postmortem_support%': 'true',
       }],
-      ['OS== "mac"', {
+      ['OS == "mac"', {
         'OBJ_DIR%': '<(PRODUCT_DIR)/obj.target',
-        'V8_BASE': '<(PRODUCT_DIR)/libv8_base.a',
+        'conditions': [
+          [ 'build_v8_with_gn=="true"', {
+            'V8_BASE': '<(PRODUCT_DIR)/obj.target/v8_monolith/geni/gn/obj/libv8_monolith.a',
+          }, {
+            'V8_BASE': '<(PRODUCT_DIR)/libv8_base.a',
+          }],
+        ],
       }],
       ['openssl_fips != ""', {
         'OPENSSL_PRODUCT': '<(STATIC_LIB_PREFIX)crypto<(STATIC_LIB_SUFFIX)',
diff --git a/configure b/configure
index db50ce0eedc863..c074f19974372d 100755
--- a/configure
+++ b/configure
@@ -55,6 +55,10 @@ sys.path.insert(0, 'tools')
 import getmoduleversion
 from gyp_node import run_gyp
 
+# imports in deps/v8/tools/node
+sys.path.insert(0, os.path.join('deps', 'v8', 'tools', 'node'))
+from fetch_deps import FetchDeps
+
 # parse our options
 parser = optparse.OptionParser()
 
@@ -548,6 +552,12 @@ parser.add_option('--without-bundled-v8',
     help='do not use V8 includes from the bundled deps folder. ' +
          '(This mode is not officially supported for regular applications)')
 
+parser.add_option('--build-v8-with-gn',
+    action='store_true',
+    dest='build_v8_with_gn',
+    default=False,
+    help='build V8 using GN instead of gyp')
+
 # Create compile_commands.json in out/Debug and out/Release.
 parser.add_option('-C',
     action='store_true',
@@ -1040,6 +1050,14 @@ def configure_v8(o):
     o['variables']['test_isolation_mode'] = 'noop'  # Needed by d8.gyp.
   if options.without_bundled_v8 and options.enable_d8:
     raise Exception('--enable-d8 is incompatible with --without-bundled-v8.')
+  if options.without_bundled_v8 and options.build_v8_with_gn:
+    raise Exception(
+        '--build-v8-with-gn is incompatible with --without-bundled-v8.')
+  if options.build_v8_with_gn:
+    v8_path = os.path.join('deps', 'v8')
+    print('Fetching dependencies to build V8 with GN')
+    options.build_v8_with_gn = FetchDeps(v8_path)
+  o['variables']['build_v8_with_gn'] = b(options.build_v8_with_gn)
 
 
 def configure_openssl(o):
diff --git a/node.gypi b/node.gypi
index 386601906fbe4a..8127b885152e53 100644
--- a/node.gypi
+++ b/node.gypi
@@ -41,9 +41,15 @@
       'dependencies': [ 'deps/v8/src/d8.gyp:d8' ],
     }],
     [ 'node_use_bundled_v8=="true"', {
-      'dependencies': [
-        'deps/v8/src/v8.gyp:v8',
-        'deps/v8/src/v8.gyp:v8_libplatform'
+      'conditions': [
+        [ 'build_v8_with_gn=="true"', {
+          'dependencies': ['deps/v8/src/v8.gyp:v8_monolith'],
+        }, {
+          'dependencies': [
+            'deps/v8/src/v8.gyp:v8',
+            'deps/v8/src/v8.gyp:v8_libplatform',
+          ],
+        }],
       ],
     }],
     [ 'node_use_v8_platform=="true"', {
@@ -93,7 +99,6 @@
       'defines': [ 'NODE_NO_BROWSER_GLOBALS' ],
     } ],
     [ 'node_use_bundled_v8=="true" and v8_postmortem_support=="true"', {
-      'dependencies': [ 'deps/v8/src/v8.gyp:postmortem-metadata' ],
       'conditions': [
         # -force_load is not applicable for the static library
         [ 'force_load=="true"', {
@@ -103,6 +108,10 @@
             ],
           },
         }],
+        # when building with GN, the v8_monolith target already includes postmortem metadata
+        [ 'build_v8_with_gn=="false"', {
+          'dependencies': [ 'deps/v8/src/v8.gyp:postmortem-metadata' ],
+        }],
       ],
     }],
     [ 'node_shared_zlib=="false"', {