Skip to content

Commit 8699124

Browse files
authored
refactor: dynamically search defines from node (electron#30563)
1 parent ec13a0b commit 8699124

File tree

8 files changed

+59
-109
lines changed

8 files changed

+59
-109
lines changed

BUILD.gn

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,23 @@ action("electron_fuses") {
307307
args = rebase_path(outputs)
308308
}
309309

310+
action("electron_generate_node_defines") {
311+
script = "build/generate_node_defines.py"
312+
313+
inputs = [
314+
"//third_party/electron_node/src/tracing/trace_event_common.h",
315+
"//third_party/electron_node/src/tracing/trace_event.h",
316+
"//third_party/electron_node/src/util.h",
317+
]
318+
319+
outputs = [
320+
"$target_gen_dir/push_and_undef_node_defines.h",
321+
"$target_gen_dir/pop_node_defines.h",
322+
]
323+
324+
args = [ rebase_path(target_gen_dir) ] + rebase_path(inputs)
325+
}
326+
310327
source_set("electron_lib") {
311328
configs += [ "//v8:external_startup_data" ]
312329
configs += [ "//third_party/electron_node:node_internals" ]
@@ -318,6 +335,7 @@ source_set("electron_lib") {
318335

319336
deps = [
320337
":electron_fuses",
338+
":electron_generate_node_defines",
321339
":electron_js2c",
322340
":electron_version_header",
323341
":resources",

build/generate_node_defines.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os
2+
import re
3+
import sys
4+
5+
DEFINE_EXTRACT_REGEX = re.compile('^ *# *define (\w*)', re.MULTILINE)
6+
7+
def main(outDir, headers):
8+
defines = []
9+
for filename in headers:
10+
with open(filename, 'r') as f:
11+
content = f.read()
12+
defines += read_defines(content)
13+
14+
push_and_undef = ''
15+
for define in defines:
16+
push_and_undef += '#pragma push_macro("%s")\n' % define
17+
push_and_undef += '#undef %s\n' % define
18+
with open(os.path.join(outDir, 'push_and_undef_node_defines.h'), 'w') as o:
19+
o.write(push_and_undef)
20+
21+
pop = ''
22+
for define in defines:
23+
pop += '#pragma pop_macro("%s")\n' % define
24+
with open(os.path.join(outDir, 'pop_node_defines.h'), 'w') as o:
25+
o.write(pop)
26+
27+
def read_defines(content):
28+
defines = []
29+
for match in DEFINE_EXTRACT_REGEX.finditer(content):
30+
defines.append(match.group(1))
31+
return defines
32+
33+
if __name__ == '__main__':
34+
main(sys.argv[1], sys.argv[2:])

patches/node/.patches

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ feat_add_new_built_with_electron_variable_to_config_gypi.patch
1010
feat_add_flags_for_low-level_hooks_and_exceptions.patch
1111
fix_expose_tracing_agent_and_use_tracing_tracingcontroller_instead.patch
1212
pass_all_globals_through_require.patch
13-
fixme_comment_trace_event_macro.patch
1413
build_modify_js2c_py_to_allow_injection_of_original-fs_and_custom_embedder_js.patch
1514
refactor_allow_embedder_overriding_of_internal_fs_calls.patch
1615
chore_allow_the_node_entrypoint_to_be_a_builtin_module.patch

patches/node/fixme_comment_trace_event_macro.patch

Lines changed: 0 additions & 26 deletions
This file was deleted.

shell/app/node_main.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ int NodeMain(int argc, char* argv[]) {
212212

213213
env = node::CreateEnvironment(isolate_data, gin_env.context(),
214214
result.args, result.exec_args);
215-
CHECK_NOT_NULL(env);
215+
CHECK_NE(nullptr, env);
216216

217217
node::IsolateSettings is;
218218
node::SetIsolateUpForNode(isolate, is);

shell/browser/javascript_environment.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "shell/browser/microtasks_runner.h"
2222
#include "shell/common/gin_helper/cleaned_up_at_exit.h"
2323
#include "shell/common/node_includes.h"
24-
#include "tracing/trace_event.h"
2524

2625
namespace {
2726
v8::Isolate* g_isolate;

shell/common/node_includes.h

Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,109 +5,35 @@
55
#ifndef SHELL_COMMON_NODE_INCLUDES_H_
66
#define SHELL_COMMON_NODE_INCLUDES_H_
77

8-
#include "base/check.h"
9-
108
// Include common headers for using node APIs.
119

1210
#ifdef NODE_SHARED_MODE
1311
#define BUILDING_NODE_EXTENSION
1412
#endif
1513

16-
// The following define makes sure that we do not include the macros
17-
// again. But we still need the tracing functions, so declaring them.
18-
#define SRC_TRACING_TRACE_EVENT_H_
19-
20-
#pragma push_macro("ASSERT")
21-
#pragma push_macro("CHECK")
22-
#pragma push_macro("CHECK_EQ")
23-
#pragma push_macro("CHECK_GE")
24-
#pragma push_macro("CHECK_GT")
25-
#pragma push_macro("CHECK_LE")
26-
#pragma push_macro("CHECK_LT")
27-
#pragma push_macro("CHECK_NE")
28-
#pragma push_macro("DCHECK")
29-
#pragma push_macro("DCHECK_EQ")
30-
#pragma push_macro("DCHECK_GE")
31-
#pragma push_macro("DCHECK_GT")
32-
#pragma push_macro("DCHECK_LE")
33-
#pragma push_macro("DCHECK_LT")
34-
#pragma push_macro("DCHECK_NE")
35-
#pragma push_macro("DISALLOW_COPY_AND_ASSIGN")
36-
#pragma push_macro("LIKELY")
37-
#pragma push_macro("NO_RETURN")
38-
#pragma push_macro("UNLIKELY")
39-
40-
#undef ASSERT
41-
#undef CHECK
42-
#undef CHECK_EQ
43-
#undef CHECK_GE
44-
#undef CHECK_GT
45-
#undef CHECK_LE
46-
#undef CHECK_LT
47-
#undef CHECK_NE
48-
#undef DCHECK
49-
#undef DCHECK_EQ
50-
#undef DCHECK_GE
51-
#undef DCHECK_GT
52-
#undef DCHECK_LE
53-
#undef DCHECK_LT
54-
#undef DCHECK_NE
55-
#undef DISALLOW_COPY_AND_ASSIGN
56-
#undef LIKELY
57-
#undef NO_RETURN
58-
#undef UNLIKELY
59-
6014
#undef debug_string // This is defined in macOS SDK in AssertMacros.h.
6115
#undef require_string // This is defined in macOS SDK in AssertMacros.h.
6216

17+
#include "electron/push_and_undef_node_defines.h"
18+
6319
#include "env-inl.h"
6420
#include "env.h"
6521
#include "node.h"
6622
#include "node_buffer.h"
6723
#include "node_errors.h"
6824
#include "node_internals.h"
25+
#include "node_native_module_env.h"
6926
#include "node_options-inl.h"
7027
#include "node_options.h"
7128
#include "node_platform.h"
7229
#include "tracing/agent.h"
7330

31+
#include "electron/pop_node_defines.h"
32+
7433
// Alternative to NODE_MODULE_CONTEXT_AWARE_X.
7534
// Allows to explicitly register builtin modules instead of using
7635
// __attribute__((constructor)).
7736
#define NODE_LINKED_MODULE_CONTEXT_AWARE(modname, regfunc) \
7837
NODE_MODULE_CONTEXT_AWARE_CPP(modname, regfunc, nullptr, NM_F_LINKED)
7938

80-
#pragma pop_macro("ASSERT")
81-
#pragma pop_macro("CHECK")
82-
#pragma pop_macro("CHECK_EQ")
83-
#pragma pop_macro("CHECK_GE")
84-
#pragma pop_macro("CHECK_GT")
85-
#pragma pop_macro("CHECK_LE")
86-
#pragma pop_macro("CHECK_LT")
87-
#pragma pop_macro("CHECK_NE")
88-
#pragma pop_macro("DCHECK")
89-
#pragma pop_macro("DCHECK_EQ")
90-
#pragma pop_macro("DCHECK_GE")
91-
#pragma pop_macro("DCHECK_GT")
92-
#pragma pop_macro("DCHECK_LE")
93-
#pragma pop_macro("DCHECK_LT")
94-
#pragma pop_macro("DCHECK_NE")
95-
#pragma pop_macro("DISALLOW_COPY_AND_ASSIGN")
96-
#pragma pop_macro("LIKELY")
97-
#pragma pop_macro("NO_RETURN")
98-
#pragma pop_macro("UNLIKELY")
99-
100-
namespace node {
101-
namespace tracing {
102-
103-
class TraceEventHelper {
104-
public:
105-
static v8::TracingController* GetTracingController();
106-
static node::tracing::Agent* GetAgent();
107-
static void SetAgent(node::tracing::Agent* agent);
108-
};
109-
110-
} // namespace tracing
111-
} // namespace node
112-
11339
#endif // SHELL_COMMON_NODE_INCLUDES_H_

shell/common/node_util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// found in the LICENSE file.
44

55
#include "shell/common/node_util.h"
6+
67
#include "base/logging.h"
78
#include "shell/common/node_includes.h"
8-
#include "third_party/electron_node/src/node_native_module_env.h"
99

1010
namespace electron {
1111

0 commit comments

Comments
 (0)