Skip to content

Commit

Permalink
refactor: dynamically search defines from node (#30563)
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Aug 18, 2021
1 parent ec13a0b commit 8699124
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 109 deletions.
18 changes: 18 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,23 @@ action("electron_fuses") {
args = rebase_path(outputs)
}

action("electron_generate_node_defines") {
script = "build/generate_node_defines.py"

inputs = [
"//third_party/electron_node/src/tracing/trace_event_common.h",
"//third_party/electron_node/src/tracing/trace_event.h",
"//third_party/electron_node/src/util.h",
]

outputs = [
"$target_gen_dir/push_and_undef_node_defines.h",
"$target_gen_dir/pop_node_defines.h",
]

args = [ rebase_path(target_gen_dir) ] + rebase_path(inputs)
}

source_set("electron_lib") {
configs += [ "//v8:external_startup_data" ]
configs += [ "//third_party/electron_node:node_internals" ]
Expand All @@ -318,6 +335,7 @@ source_set("electron_lib") {

deps = [
":electron_fuses",
":electron_generate_node_defines",
":electron_js2c",
":electron_version_header",
":resources",
Expand Down
34 changes: 34 additions & 0 deletions build/generate_node_defines.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import re
import sys

DEFINE_EXTRACT_REGEX = re.compile('^ *# *define (\w*)', re.MULTILINE)

def main(outDir, headers):
defines = []
for filename in headers:
with open(filename, 'r') as f:
content = f.read()
defines += read_defines(content)

push_and_undef = ''
for define in defines:
push_and_undef += '#pragma push_macro("%s")\n' % define
push_and_undef += '#undef %s\n' % define
with open(os.path.join(outDir, 'push_and_undef_node_defines.h'), 'w') as o:
o.write(push_and_undef)

pop = ''
for define in defines:
pop += '#pragma pop_macro("%s")\n' % define
with open(os.path.join(outDir, 'pop_node_defines.h'), 'w') as o:
o.write(pop)

def read_defines(content):
defines = []
for match in DEFINE_EXTRACT_REGEX.finditer(content):
defines.append(match.group(1))
return defines

if __name__ == '__main__':
main(sys.argv[1], sys.argv[2:])
1 change: 0 additions & 1 deletion patches/node/.patches
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ feat_add_new_built_with_electron_variable_to_config_gypi.patch
feat_add_flags_for_low-level_hooks_and_exceptions.patch
fix_expose_tracing_agent_and_use_tracing_tracingcontroller_instead.patch
pass_all_globals_through_require.patch
fixme_comment_trace_event_macro.patch
build_modify_js2c_py_to_allow_injection_of_original-fs_and_custom_embedder_js.patch
refactor_allow_embedder_overriding_of_internal_fs_calls.patch
chore_allow_the_node_entrypoint_to_be_a_builtin_module.patch
Expand Down
26 changes: 0 additions & 26 deletions patches/node/fixme_comment_trace_event_macro.patch

This file was deleted.

2 changes: 1 addition & 1 deletion shell/app/node_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ int NodeMain(int argc, char* argv[]) {

env = node::CreateEnvironment(isolate_data, gin_env.context(),
result.args, result.exec_args);
CHECK_NOT_NULL(env);
CHECK_NE(nullptr, env);

node::IsolateSettings is;
node::SetIsolateUpForNode(isolate, is);
Expand Down
1 change: 0 additions & 1 deletion shell/browser/javascript_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "shell/browser/microtasks_runner.h"
#include "shell/common/gin_helper/cleaned_up_at_exit.h"
#include "shell/common/node_includes.h"
#include "tracing/trace_event.h"

namespace {
v8::Isolate* g_isolate;
Expand Down
84 changes: 5 additions & 79 deletions shell/common/node_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,109 +5,35 @@
#ifndef SHELL_COMMON_NODE_INCLUDES_H_
#define SHELL_COMMON_NODE_INCLUDES_H_

#include "base/check.h"

// Include common headers for using node APIs.

#ifdef NODE_SHARED_MODE
#define BUILDING_NODE_EXTENSION
#endif

// The following define makes sure that we do not include the macros
// again. But we still need the tracing functions, so declaring them.
#define SRC_TRACING_TRACE_EVENT_H_

#pragma push_macro("ASSERT")
#pragma push_macro("CHECK")
#pragma push_macro("CHECK_EQ")
#pragma push_macro("CHECK_GE")
#pragma push_macro("CHECK_GT")
#pragma push_macro("CHECK_LE")
#pragma push_macro("CHECK_LT")
#pragma push_macro("CHECK_NE")
#pragma push_macro("DCHECK")
#pragma push_macro("DCHECK_EQ")
#pragma push_macro("DCHECK_GE")
#pragma push_macro("DCHECK_GT")
#pragma push_macro("DCHECK_LE")
#pragma push_macro("DCHECK_LT")
#pragma push_macro("DCHECK_NE")
#pragma push_macro("DISALLOW_COPY_AND_ASSIGN")
#pragma push_macro("LIKELY")
#pragma push_macro("NO_RETURN")
#pragma push_macro("UNLIKELY")

#undef ASSERT
#undef CHECK
#undef CHECK_EQ
#undef CHECK_GE
#undef CHECK_GT
#undef CHECK_LE
#undef CHECK_LT
#undef CHECK_NE
#undef DCHECK
#undef DCHECK_EQ
#undef DCHECK_GE
#undef DCHECK_GT
#undef DCHECK_LE
#undef DCHECK_LT
#undef DCHECK_NE
#undef DISALLOW_COPY_AND_ASSIGN
#undef LIKELY
#undef NO_RETURN
#undef UNLIKELY

#undef debug_string // This is defined in macOS SDK in AssertMacros.h.
#undef require_string // This is defined in macOS SDK in AssertMacros.h.

#include "electron/push_and_undef_node_defines.h"

#include "env-inl.h"
#include "env.h"
#include "node.h"
#include "node_buffer.h"
#include "node_errors.h"
#include "node_internals.h"
#include "node_native_module_env.h"
#include "node_options-inl.h"
#include "node_options.h"
#include "node_platform.h"
#include "tracing/agent.h"

#include "electron/pop_node_defines.h"

// Alternative to NODE_MODULE_CONTEXT_AWARE_X.
// Allows to explicitly register builtin modules instead of using
// __attribute__((constructor)).
#define NODE_LINKED_MODULE_CONTEXT_AWARE(modname, regfunc) \
NODE_MODULE_CONTEXT_AWARE_CPP(modname, regfunc, nullptr, NM_F_LINKED)

#pragma pop_macro("ASSERT")
#pragma pop_macro("CHECK")
#pragma pop_macro("CHECK_EQ")
#pragma pop_macro("CHECK_GE")
#pragma pop_macro("CHECK_GT")
#pragma pop_macro("CHECK_LE")
#pragma pop_macro("CHECK_LT")
#pragma pop_macro("CHECK_NE")
#pragma pop_macro("DCHECK")
#pragma pop_macro("DCHECK_EQ")
#pragma pop_macro("DCHECK_GE")
#pragma pop_macro("DCHECK_GT")
#pragma pop_macro("DCHECK_LE")
#pragma pop_macro("DCHECK_LT")
#pragma pop_macro("DCHECK_NE")
#pragma pop_macro("DISALLOW_COPY_AND_ASSIGN")
#pragma pop_macro("LIKELY")
#pragma pop_macro("NO_RETURN")
#pragma pop_macro("UNLIKELY")

namespace node {
namespace tracing {

class TraceEventHelper {
public:
static v8::TracingController* GetTracingController();
static node::tracing::Agent* GetAgent();
static void SetAgent(node::tracing::Agent* agent);
};

} // namespace tracing
} // namespace node

#endif // SHELL_COMMON_NODE_INCLUDES_H_
2 changes: 1 addition & 1 deletion shell/common/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// found in the LICENSE file.

#include "shell/common/node_util.h"

#include "base/logging.h"
#include "shell/common/node_includes.h"
#include "third_party/electron_node/src/node_native_module_env.h"

namespace electron {

Expand Down

0 comments on commit 8699124

Please sign in to comment.