381b4c83创建于 2025年6月12日历史提交
import("//v8/gni/v8.gni")
import("//electron/js2c_toolchain.gni")
import("electron_node.gni")

declare_args() {
  # Enable the V8 inspector protocol for use with node.
  node_enable_inspector = true

  # Build node with SSL support.
  # The variable is called "openssl" for parity with node's GYP build.
  node_use_openssl = true

  # Use the specified path to system CA (PEM format) in addition to
  # the BoringSSL supplied CA store or compiled-in Mozilla CA copy.
  node_openssl_system_ca_path = ""

  # Initialize v8 platform during node.js startup.
  # NB. this must be turned off in Electron, because Electron initializes the
  # v8 platform itself.
  node_use_v8_platform = false

  # Build with DTrace support.
  node_use_dtrace = false

  # Build with ETW support.
  node_use_etw = false

  # Build JavaScript in lib/ with DCHECK macros.
  node_debug_lib = false

  # Custom build tag.
  node_tag = ""

  # V8 options to pass, see `node --v8-options` for examples
  node_v8_options = ""

  # Provide a custom URL prefix for the `process.release` properties
  # `sourceUrl` and `headersUrl`. When compiling a release build, this will
  # default to https://nodejs.org/download/release/')
  node_release_urlbase = ""

  # Allows downstream packagers (eg. Linux distributions) to build Electron against system shared libraries.
  use_system_cares = false
  use_system_nghttp2 = false
  use_system_llhttp = false
  use_system_histogram = false
}

if (is_linux) {
 import("//build/config/linux/pkg_config.gni")
 if (use_system_cares) {
  pkg_config("cares") {
    packages = [ "libcares" ]
  }
 }
 if (use_system_nghttp2) {
  pkg_config("nghttp2") {
    packages = [ "libnghttp2" ]
  }
 }
}

assert(!node_use_dtrace, "node_use_dtrace not supported in GN")
assert(!node_use_etw, "node_use_etw not supported in GN")

assert(!node_enable_inspector || node_use_openssl,
       "node_enable_inspector requires node_use_openssl")

config("node_internals") {
  defines = [ "NODE_WANT_INTERNALS=1" ]
}

node_files = read_file("filenames.json", "json")
library_files = node_files.library_files
fs_files = node_files.fs_files
original_fs_files = []
foreach(file, fs_files) {
  original_fs_files += [string_replace(string_replace(string_replace(file, "internal/fs/", "internal/original-fs/"), "lib/fs.js", "lib/original-fs.js"), "lib/fs/", "lib/original-fs/")]
}

copy("node_js2c_inputs") {
  sources = library_files
  outputs = [
    "$target_gen_dir/js2c_inputs/{{source_target_relative}}",
  ]
}

action("node_js2c_original_fs") {
  script = "tools/generate_original_fs.py"
  inputs = fs_files
  outputs = []
  foreach(file, fs_files + original_fs_files) {
    outputs += ["$target_gen_dir/js2c_inputs/$file"]
  }

  args = [rebase_path("$target_gen_dir/js2c_inputs")] + fs_files
}

action("node_js2c_exec") {
  deps = [
    "//electron:generate_config_gypi",
    ":node_js2c_original_fs",
    ":node_js2c_inputs",
    ":node_js2c($electron_js2c_toolchain)"
  ]
  config_gypi = [ "$root_gen_dir/config.gypi" ]
  inputs = library_files + get_target_outputs(":node_js2c_original_fs") + config_gypi
  outputs = [
    "$target_gen_dir/node_javascript.cc",
  ]

  script = "//electron/build/run-in-dir.py"
  out_dir = get_label_info(":anything($electron_js2c_toolchain)", "root_out_dir")
  args = [ rebase_path("$target_gen_dir/js2c_inputs"), rebase_path("$out_dir/node_js2c") ] +
         rebase_path(outputs) + library_files + fs_files + original_fs_files + rebase_path(config_gypi)
}

config("node_features") {
  defines = []
  if (node_enable_inspector) {
    defines += [ "HAVE_INSPECTOR=1" ]
  } else {
    defines += [ "HAVE_INSPECTOR=0" ]
  }
  if (node_use_openssl) {
    defines += [ "HAVE_OPENSSL=1" ]
  } else {
    defines += [ "HAVE_OPENSSL=0" ]
  }
  if (v8_enable_i18n_support) {
    defines += [ "NODE_HAVE_I18N_SUPPORT=1" ]
  } else {
    defines += [ "NODE_HAVE_I18N_SUPPORT=0" ]
  }
  if (node_use_v8_platform) {
    defines += [ "NODE_USE_V8_PLATFORM=1" ]
  } else {
    defines += [ "NODE_USE_V8_PLATFORM=0" ]
  }
}

config("node_lib_config") {
  include_dirs = [ "src" ]

  cflags = [
    "-Wno-shadow",
    # FIXME(deepak1556): include paths should be corrected,
    # refer https://docs.google.com/presentation/d/1oxNHaVjA9Gn_rTzX6HIpJHP7nXRua_0URXxxJ3oYRq0/edit#slide=id.g71ecd450e_2_702
    "-Wno-microsoft-include",
  ]

  configs = [ ":node_features" ]

  if (is_debug) {
    defines = [ "DEBUG" ]
  }
}

config("node_internal_config") {
  visibility = [
    ":*",
    "src/inspector:*",
  ]
  defines = [
    "NODE_WANT_INTERNALS=1",
    "NODE_IMPLEMENTATION",
  ]
  if (node_module_version != "") {
    defines += [ "NODE_EMBEDDER_MODULE_VERSION=" + node_module_version ]
  }
  if (is_component_build) {
    defines += [
      "NODE_SHARED_MODE",
    ]
  }

  if (target_cpu == "x86") {
    node_arch = "ia32"
  } else {
    node_arch = target_cpu
  }
  defines += [ "NODE_ARCH=\"$node_arch\"" ]

  if (target_os == "win") {
    node_platform = "win32"
  } else if (target_os == "mac") {
    node_platform = "darwin"
  } else {
    node_platform = target_os
  }
  defines += [ "NODE_PLATFORM=\"$node_platform\"" ]

  if (is_win) {
    defines += [
      "NOMINMAX",
      "_UNICODE=1",
    ]
  } else {
    defines += [ "__POSIX__" ]
  }

  if (node_tag != "") {
    defines += [ "NODE_TAG=\"$node_tag\"" ]
  }
  if (node_v8_options != "") {
    defines += [ "NODE_V8_OPTIONS=\"$node_v8_options\"" ]
  }
  if (node_release_urlbase != "") {
    defines += [ "NODE_RELEASE_URLBASE=\"$node_release_urlbase\"" ]
  }

  if (node_use_openssl) {
    defines += [
      "NODE_OPENSSL_SYSTEM_CERT_PATH=\"$node_openssl_system_ca_path\"",
      "EVP_CTRL_CCM_SET_TAG=EVP_CTRL_GCM_SET_TAG",
    ]
  }
}

executable("overlapped-checker") {
  sources = []
  if (is_win) {
    sources += [ "test/overlapped-checker/main_win.c" ]
  } else {
    sources += [ "test/overlapped-checker/main_unix.c" ]
  }
}

if (current_toolchain == electron_js2c_toolchain) {
  executable("node_js2c") {
    defines = []
    sources = [
      "tools/js2c.cc",
      "tools/executable_wrapper.h",
      "src/embedded_data.cc",
      "src/embedded_data.h",
    ]
    include_dirs = [ "tools", "src" ]
    deps = [
      "deps/simdutf($electron_js2c_toolchain)",
      "deps/uv($electron_js2c_toolchain)",
      "//v8"
    ]

    if (!is_win) {
      defines += [ "NODE_JS2C_USE_STRING_LITERALS" ]
    }
    if (is_debug) {
      cflags_cc = [ "-g", "-O0" ]
      defines += [ "DEBUG" ]
    }
  }
}

component("node_lib") {
  deps = [
    ":node_js2c_exec",
    "deps/googletest:gtest",
    "deps/ada",
    "deps/base64",
    "deps/simdutf",
    "deps/uvwasi",
    "//third_party/zlib",
    "//third_party/brotli:dec",
    "//third_party/brotli:enc",
    "//v8:v8_libplatform",
  ]
  if (use_system_cares) {
    configs += [ ":cares" ]
  } else {
    deps += [ "deps/cares" ]
  }
  if (use_system_nghttp2) {
    configs += [ ":nghttp2" ]
  } else {
    deps += [ "deps/nghttp2" ]
  }
  public_deps = [
    "deps/uv",
    "//electron:electron_js2c",
    "//v8",
  ]
  configs += [ ":node_internal_config" ]
  public_configs = [ ":node_lib_config" ]
  include_dirs = [
    "src",
    "deps/postject"
  ]
  libs = []
  if (use_system_llhttp) {
    libs += [ "llhttp" ]
  } else {
    deps += [ "deps/llhttp" ]
  }
  if (use_system_histogram) {
    libs += [ "hdr_histogram" ]
    include_dirs += [ "/usr/include/hdr" ]
  } else {
    deps += [ "deps/histogram" ]
  }
  frameworks = []
  cflags_cc = [
    "-Wno-deprecated-declarations",
    "-Wno-implicit-fallthrough",
    "-Wno-return-type",
    "-Wno-sometimes-uninitialized",
    "-Wno-string-plus-int",
    "-Wno-unused-function",
    "-Wno-unused-label",
    "-Wno-unused-private-field",
    "-Wno-unused-variable",
  ]

  if (v8_enable_i18n_support) {
    deps += [ "//third_party/icu" ]
  }

  sources = node_files.node_sources
  sources += [
    "$root_gen_dir/electron_natives.cc",
    "$target_gen_dir/node_javascript.cc",
    "src/node_snapshot_stub.cc",
  ]

  if (is_win) {
    libs += [ "psapi.lib" ]
  }
  if (is_mac) {
    frameworks += [ "CoreFoundation.framework" ]
  }

  if (is_ohos) {
    libs += ["os_account_ndk"]
  }

  if (node_enable_inspector) {
    sources += [
      "src/inspector_agent.cc",
      "src/inspector_agent.h",
      "src/inspector_io.cc",
      "src/inspector_io.h",
      "src/inspector_js_api.cc",
      "src/inspector_profiler.cc",
      "src/inspector_socket.cc",
      "src/inspector_socket.h",
      "src/inspector_socket_server.cc",
      "src/inspector_socket_server.h",
    ]
    deps += [ "src/inspector" ]
  }

  if (node_use_openssl) {
    deps += [ "//third_party/boringssl" ]
    sources += [
      "src/crypto/crypto_aes.cc",
      "src/crypto/crypto_aes.h",
      "src/crypto/crypto_bio.cc",
      "src/crypto/crypto_bio.h",
      "src/crypto/crypto_cipher.cc",
      "src/crypto/crypto_cipher.h",
      "src/crypto/crypto_clienthello-inl.h",
      "src/crypto/crypto_clienthello.cc",
      "src/crypto/crypto_clienthello.h",
      "src/crypto/crypto_common.cc",
      "src/crypto/crypto_common.h",
      "src/crypto/crypto_context.cc",
      "src/crypto/crypto_context.h",
      "src/crypto/crypto_dh.cc",
      "src/crypto/crypto_dh.h",
      "src/crypto/crypto_dsa.cc",
      "src/crypto/crypto_dsa.h",
      "src/crypto/crypto_ec.cc",
      "src/crypto/crypto_ec.h",
      "src/crypto/crypto_groups.h",
      "src/crypto/crypto_hash.cc",
      "src/crypto/crypto_hash.h",
      "src/crypto/crypto_hkdf.cc",
      "src/crypto/crypto_hkdf.h",
      "src/crypto/crypto_hmac.cc",
      "src/crypto/crypto_hmac.h",
      "src/crypto/crypto_keygen.cc",
      "src/crypto/crypto_keygen.h",
      "src/crypto/crypto_keys.cc",
      "src/crypto/crypto_keys.h",
      "src/crypto/crypto_pbkdf2.cc",
      "src/crypto/crypto_pbkdf2.h",
      "src/crypto/crypto_random.cc",
      "src/crypto/crypto_random.h",
      "src/crypto/crypto_rsa.cc",
      "src/crypto/crypto_rsa.h",
      "src/crypto/crypto_scrypt.cc",
      "src/crypto/crypto_scrypt.h",
      "src/crypto/crypto_sig.cc",
      "src/crypto/crypto_sig.h",
      "src/crypto/crypto_spkac.cc",
      "src/crypto/crypto_spkac.h",
      "src/crypto/crypto_timing.cc",
      "src/crypto/crypto_timing.h",
      "src/crypto/crypto_tls.cc",
      "src/crypto/crypto_tls.h",
      "src/crypto/crypto_util.cc",
      "src/crypto/crypto_util.h",
      "src/crypto/crypto_x509.cc",
      "src/crypto/crypto_x509.h",
      "src/node_crypto.cc",
      "src/node_crypto.h",
    ]
    cflags_cc += [ "-Wno-sign-compare" ]
  }
}