Skip to content

Commit f83d486

Browse files
committed
[Bazel] Support for feature debug fission in emsdk-bazel-toolchain emscripten-core#1479
1 parent 85390ce commit f83d486

11 files changed

+128
-10
lines changed

bazel/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ alias(
7878
}),
7979
)
8080

81+
alias(
82+
name = "dwp_files",
83+
actual = select({
84+
":linux": "@emscripten_bin_linux//:dwp_files",
85+
":linux_arm64": "@emscripten_bin_linux_arm64//:dwp_files",
86+
":macos": "@emscripten_bin_mac//:dwp_files",
87+
":macos_arm64": "@emscripten_bin_mac_arm64//:dwp_files",
88+
":windows": "@emscripten_bin_win//:dwp_files",
89+
"//conditions:default": ":empty",
90+
}),
91+
)
92+
8193
platform(
8294
name = "platform_wasm",
8395
constraint_values = [

bazel/emscripten_deps.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ filegroup(
9696
],
9797
),
9898
)
99+
100+
filegroup(
101+
name = "dwp_files",
102+
srcs = [
103+
"bin/llvm-dwp{bin_extension}",
104+
],
105+
)
99106
"""
100107

101108
def emscripten_deps(emscripten_version = "latest"):

bazel/emscripten_toolchain/BUILD.bazel

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,25 @@ filegroup(
4343
],
4444
)
4545

46+
filegroup(
47+
name = "dwp_files",
48+
srcs = [
49+
"emdwp-emscripten_bin_linux_arm64.sh",
50+
"emdwp-emscripten_bin_linux.sh",
51+
"emdwp-emscripten_bin_mac_arm64.sh",
52+
"emdwp-emscripten_bin_mac.sh",
53+
"emdwp-emscripten_bin_win.bat",
54+
"@emsdk//:dwp_files",
55+
],
56+
)
57+
4658
filegroup(
4759
name = "all_files",
4860
srcs = [
4961
":ar_files",
5062
":compiler_files",
5163
":linker_files",
64+
":dwp_files",
5265
],
5366
)
5467

@@ -75,7 +88,7 @@ cc_toolchain(
7588
ar_files = ":ar_files",
7689
as_files = ":empty",
7790
compiler_files = ":compiler_files",
78-
dwp_files = ":empty",
91+
dwp_files = ":dwp_files",
7992
linker_files = ":linker_files",
8093
objcopy_files = ":empty",
8194
strip_files = ":empty",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
exec external/emscripten_bin_linux/bin/llvm-dwp "$@"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
exec external/emscripten_bin_linux_arm64/bin/llvm-dwp "$@"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
exec external/emscripten_bin_mac/bin/llvm-dwp "$@"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
exec external/emscripten_bin_mac_arm64/bin/llvm-dwp "$@"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@ECHO OFF
2+
3+
call external\emscripten_bin_win\bin\llvm-dwp %*

bazel/emscripten_toolchain/toolchain.bzl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ def _impl(ctx):
7272

7373
emscripten_dir = ctx.attr.emscripten_binaries.label.workspace_root
7474
nodejs_path = ctx.file.nodejs_bin.path
75+
emscripten_name = ctx.attr.emscripten_binaries.label.repo_name
7576

7677
builtin_sysroot = emscripten_dir + "/emscripten/cache/sysroot"
7778

7879
emcc_script = "emcc.%s" % ctx.attr.script_extension
7980
emcc_link_script = "emcc_link.%s" % ctx.attr.script_extension
8081
emar_script = "emar.%s" % ctx.attr.script_extension
82+
emdwp_script = "emdwp-%s.%s" % (emscripten_name, ctx.attr.script_extension)
8183

8284
################################################################
8385
# Tools
@@ -99,6 +101,7 @@ def _impl(ctx):
99101
tool_path(name = "nm", path = "NOT_USED"),
100102
tool_path(name = "objdump", path = "/bin/false"),
101103
tool_path(name = "strip", path = "NOT_USED"),
104+
tool_path(name = "dwp", path = emdwp_script),
102105
]
103106

104107
################################################################
@@ -460,6 +463,49 @@ def _impl(ctx):
460463
feature(
461464
name = "wasm_standalone",
462465
),
466+
# Support for debug fission. In short, debugging fission should:
467+
# * reduce linking time, RAM usage and disk usage
468+
# * speed up incremental builds
469+
# * speed up debugger work (reduce startup and breakpoint time)
470+
# (to use this, follow the --fission=yes flag)
471+
# https://developer.chrome.com/blog/faster-wasm-debugging
472+
# https://bazel.build/docs/user-manual#fission
473+
feature(
474+
name = "per_object_debug_info",
475+
flag_sets = [
476+
flag_set(
477+
actions = [
478+
ACTION_NAMES.c_compile,
479+
ACTION_NAMES.cpp_compile,
480+
ACTION_NAMES.cpp_module_codegen,
481+
ACTION_NAMES.assemble,
482+
ACTION_NAMES.preprocess_assemble,
483+
],
484+
flag_groups = [
485+
flag_group(
486+
flags = ["-g", "-gsplit-dwarf", "-gdwarf-5", "-gpubnames"],
487+
expand_if_available = "per_object_debug_info_file",
488+
),
489+
],
490+
),
491+
],
492+
enabled = True,
493+
),
494+
feature(
495+
name = "fission_support",
496+
flag_sets = [
497+
flag_set(
498+
actions = all_link_actions,
499+
flag_groups = [
500+
flag_group(
501+
flags = ["-sWASM_BIGINT"], # WASM_BIGINT required to support dwarf-5
502+
expand_if_available = "is_using_fission",
503+
),
504+
],
505+
),
506+
],
507+
enabled = True,
508+
)
463509
]
464510

465511
crosstool_default_flag_sets = [

bazel/emscripten_toolchain/wasm_binary.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import argparse
1515
import os
1616
import tarfile
17-
17+
import shutil
1818

1919
def ensure(f):
2020
if not os.path.exists(f):
@@ -26,11 +26,20 @@ def main():
2626
parser = argparse.ArgumentParser()
2727
parser.add_argument('--archive', help='The archive to extract from.')
2828
parser.add_argument('--outputs', help='Comma separated list of files that should be extracted from the archive. Only the extname has to match a file in the archive.')
29+
parser.add_argument('--dwp_file', help='Optional dwp input file, generated when fission flags set.')
2930
parser.add_argument('--allow_empty_outputs', help='If an output listed in --outputs does not exist, create it anyways.', action='store_true')
3031
args = parser.parse_args()
3132

3233
args.archive = os.path.normpath(args.archive)
3334
args.outputs = args.outputs.split(",")
35+
args.dwp_file = os.path.normpath(args.dwp_file) if args.dwp_file else None
36+
37+
if args.dwp_file:
38+
for idx, output in enumerate(args.outputs):
39+
if output.endswith(".dwp"): # also update extension 'binary.dwp' to 'binary.wasm.dwp'
40+
shutil.copy2(args.dwp_file, output)
41+
args.outputs.pop(idx)
42+
break
3443

3544
tar = tarfile.open(args.archive)
3645

bazel/emscripten_toolchain/wasm_cc_binary.bzl

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ _ALLOW_OUTPUT_EXTNAMES = [
6969
".fetch.js",
7070
".js.symbols",
7171
".wasm.debug.wasm",
72+
".wasm.dwp",
7273
".html",
7374
".aw.js",
7475
]
@@ -107,10 +108,11 @@ _WASM_BINARY_COMMON_ATTRS = {
107108
}
108109

109110
def _wasm_cc_binary_impl(ctx):
110-
args = ctx.actions.args()
111111
cc_target = ctx.attr.cc_target[0]
112+
dwp_file = cc_target[DebugPackageInfo].dwp_file if DebugPackageInfo in cc_target else None
113+
outputs = ctx.outputs.outputs
112114

113-
for output in ctx.outputs.outputs:
115+
for output in outputs:
114116
valid_extname = False
115117
for allowed_extname in _ALLOW_OUTPUT_EXTNAMES:
116118
if output.path.endswith(allowed_extname):
@@ -119,28 +121,35 @@ def _wasm_cc_binary_impl(ctx):
119121
if not valid_extname:
120122
fail("Invalid output '{}'. Allowed extnames: {}".format(output.basename, ", ".join(_ALLOW_OUTPUT_EXTNAMES)))
121123

124+
inputs = ctx.files.cc_target
125+
args = ctx.actions.args()
122126
args.add_all("--archive", ctx.files.cc_target)
123-
args.add_joined("--outputs", ctx.outputs.outputs, join_with = ",")
127+
args.add_joined("--outputs", outputs, join_with = ",")
128+
129+
if dwp_file:
130+
args.add("--dwp_file", dwp_file)
131+
inputs = inputs + [dwp_file]
124132

125133
ctx.actions.run(
126-
inputs = ctx.files.cc_target,
127-
outputs = ctx.outputs.outputs,
134+
inputs = inputs,
135+
outputs = outputs,
128136
arguments = [args],
129137
executable = ctx.executable._wasm_binary_extractor,
130138
)
131139

132140
return [
133141
DefaultInfo(
134-
files = depset(ctx.outputs.outputs),
142+
files = depset(outputs),
135143
# This is needed since rules like web_test usually have a data
136144
# dependency on this target.
137-
data_runfiles = ctx.runfiles(transitive_files = depset(ctx.outputs.outputs)),
145+
data_runfiles = ctx.runfiles(transitive_files = depset(outputs)),
138146
),
139147
OutputGroupInfo(_wasm_tar = cc_target.files),
140148
]
141149

142150
def _wasm_cc_binary_legacy_impl(ctx):
143151
cc_target = ctx.attr.cc_target[0]
152+
dwp_file = cc_target[DebugPackageInfo].dwp_file if DebugPackageInfo in cc_target else None
144153
outputs = [
145154
ctx.outputs.loader,
146155
ctx.outputs.wasm,
@@ -151,17 +160,23 @@ def _wasm_cc_binary_legacy_impl(ctx):
151160
ctx.outputs.data,
152161
ctx.outputs.symbols,
153162
ctx.outputs.dwarf,
163+
ctx.outputs.dwp,
154164
ctx.outputs.html,
155165
ctx.outputs.audio_worklet,
156166
]
157167

168+
inputs = ctx.files.cc_target
158169
args = ctx.actions.args()
159170
args.add("--allow_empty_outputs")
160171
args.add_all("--archive", ctx.files.cc_target)
161172
args.add_joined("--outputs", outputs, join_with = ",")
162173

174+
if dwp_file:
175+
args.add("--dwp_file", dwp_file)
176+
inputs = inputs + [dwp_file]
177+
163178
ctx.actions.run(
164-
inputs = ctx.files.cc_target,
179+
inputs = inputs,
165180
outputs = outputs,
166181
arguments = [args],
167182
executable = ctx.executable._wasm_binary_extractor,
@@ -202,6 +217,7 @@ def _wasm_binary_legacy_outputs(name, cc_target):
202217
"data": "{}/{}.data".format(name, basename),
203218
"symbols": "{}/{}.js.symbols".format(name, basename),
204219
"dwarf": "{}/{}.wasm.debug.wasm".format(name, basename),
220+
"dwp": "{}/{}.wasm.dwp".format(name, basename),
205221
"html": "{}/{}.html".format(name, basename),
206222
"audio_worklet": "{}/{}.aw.js".format(name, basename)
207223
}

0 commit comments

Comments
 (0)