Skip to content

Commit 93f21c9

Browse files
authored
Optimize sandbox performance (#1045)
* Optimize sandbox performance Link just the files needed to compile, link, or archive, rather than the entire directory tree. This drastically improves build times on macOS, where the sandbox is known to be slow (bazelbuild/bazel#8230). * Linux wants clang to link * all_files not needed? * Linux wants llc * And llvm-ar * Templated build_file_content
1 parent 891b449 commit 93f21c9

File tree

4 files changed

+93
-43
lines changed

4 files changed

+93
-43
lines changed

bazel/BUILD

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,34 @@ config_setting(
3535
filegroup(name = "empty")
3636

3737
alias(
38-
name = "binaries",
38+
name = "compiler_files",
3939
actual = select({
40-
":linux": "@emscripten_bin_linux//:all",
41-
":macos": "@emscripten_bin_mac//:all",
42-
":macos_arm64": "@emscripten_bin_mac_arm64//:all",
43-
":windows": "@emscripten_bin_win//:all",
40+
":linux": "@emscripten_bin_linux//:compiler_files",
41+
":macos": "@emscripten_bin_mac//:compiler_files",
42+
":macos_arm64": "@emscripten_bin_mac_arm64//:compiler_files",
43+
":windows": "@emscripten_bin_win//:compiler_files",
4444
"//conditions:default": ":empty",
4545
}),
4646
)
4747

4848
alias(
49-
name = "node_modules",
49+
name = "linker_files",
5050
actual = select({
51-
":linux": "@emscripten_npm_linux//:node_modules",
52-
":macos": "@emscripten_npm_mac//:node_modules",
53-
":macos_arm64": "@emscripten_npm_mac//:node_modules",
54-
":windows": "@emscripten_npm_win//:node_modules",
51+
":linux": "@emscripten_bin_linux//:linker_files",
52+
":macos": "@emscripten_bin_mac//:linker_files",
53+
":macos_arm64": "@emscripten_bin_mac_arm64//:linker_files",
54+
":windows": "@emscripten_bin_win//:linker_files",
55+
"//conditions:default": ":empty",
56+
}),
57+
)
58+
59+
alias(
60+
name = "ar_files",
61+
actual = select({
62+
":linux": "@emscripten_bin_linux//:ar_files",
63+
":macos": "@emscripten_bin_mac//:ar_files",
64+
":macos_arm64": "@emscripten_bin_mac_arm64//:ar_files",
65+
":windows": "@emscripten_bin_win//:ar_files",
5566
"//conditions:default": ":empty",
5667
}),
5768
)

bazel/emscripten_deps.bzl

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,53 @@ load(":revisions.bzl", "EMSCRIPTEN_TAGS")
55
def _parse_version(v):
66
return [int(u) for u in v.split(".")]
77

8+
BUILD_FILE_CONTENT_TEMPLATE = """
9+
package(default_visibility = ['//visibility:public'])
10+
11+
filegroup(
12+
name = "includes",
13+
srcs = glob([
14+
"emscripten/cache/sysroot/include/c++/v1/**",
15+
"emscripten/cache/sysroot/include/compat/**",
16+
"emscripten/cache/sysroot/include/**",
17+
"lib/clang/15.0.0/include/**",
18+
]),
19+
)
20+
21+
filegroup(
22+
name = "compiler_files",
23+
srcs = [
24+
"emscripten/emcc.py",
25+
"bin/clang{bin_extension}",
26+
"bin/clang++{bin_extension}",
27+
":includes",
28+
],
29+
)
30+
31+
filegroup(
32+
name = "linker_files",
33+
srcs = [
34+
"emscripten/emcc.py",
35+
"bin/clang{bin_extension}",
36+
"bin/llc{bin_extension}",
37+
"bin/llvm-ar{bin_extension}",
38+
"bin/llvm-nm{bin_extension}",
39+
"bin/llvm-objcopy{bin_extension}",
40+
"bin/wasm-emscripten-finalize{bin_extension}",
41+
"bin/wasm-ld{bin_extension}",
42+
"bin/wasm-opt{bin_extension}",
43+
],
44+
)
45+
46+
filegroup(
47+
name = "ar_files",
48+
srcs = [
49+
"emscripten/emar.py",
50+
"bin/llvm-ar{bin_extension}",
51+
],
52+
)
53+
"""
54+
855
def emscripten_deps(emscripten_version = "latest"):
956
version = emscripten_version
1057

@@ -36,7 +83,7 @@ def emscripten_deps(emscripten_version = "latest"):
3683
strip_prefix = "install",
3784
url = emscripten_url.format("linux", revision.hash, "", "tbz2"),
3885
sha256 = revision.sha_linux,
39-
build_file = "@emsdk//emscripten_toolchain:emscripten.BUILD",
86+
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension=""),
4087
type = "tar.bz2",
4188
)
4289

@@ -46,7 +93,7 @@ def emscripten_deps(emscripten_version = "latest"):
4693
strip_prefix = "install",
4794
url = emscripten_url.format("mac", revision.hash, "", "tbz2"),
4895
sha256 = revision.sha_mac,
49-
build_file = "@emsdk//emscripten_toolchain:emscripten.BUILD",
96+
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension=""),
5097
type = "tar.bz2",
5198
)
5299

@@ -56,7 +103,7 @@ def emscripten_deps(emscripten_version = "latest"):
56103
strip_prefix = "install",
57104
url = emscripten_url.format("mac", revision.hash, "-arm64", "tbz2"),
58105
sha256 = revision.sha_mac_arm64,
59-
build_file = "@emsdk//emscripten_toolchain:emscripten.BUILD",
106+
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension=""),
60107
type = "tar.bz2",
61108
)
62109

@@ -66,7 +113,7 @@ def emscripten_deps(emscripten_version = "latest"):
66113
strip_prefix = "install",
67114
url = emscripten_url.format("win", revision.hash, "", "zip"),
68115
sha256 = revision.sha_win,
69-
build_file = "@emsdk//emscripten_toolchain:emscripten.BUILD",
116+
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension=".exe"),
70117
type = "zip",
71118
)
72119

bazel/emscripten_toolchain/BUILD.bazel

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,43 @@ package(default_visibility = ["//visibility:public"])
88
node_files = "@nodejs_host//:node_files" if existing_rule("@nodejs_host//:node_files") else "@nodejs//:node_files"
99

1010
filegroup(
11-
name = "common-script-includes",
11+
name = "common_files",
1212
srcs = [
13-
"emar.sh",
14-
"emar.bat",
15-
"emcc.sh",
16-
"emcc.bat",
1713
"emscripten_config",
1814
"env.sh",
1915
"env.bat",
20-
"@emsdk//:binaries",
21-
"@emsdk//:node_modules",
2216
node_files,
2317
],
2418
)
2519

2620
filegroup(
27-
name = "compile-emscripten",
28-
srcs = [":common-script-includes"],
21+
name = "compiler_files",
22+
srcs = [
23+
"emcc.sh",
24+
"emcc.bat",
25+
"@emsdk//:compiler_files",
26+
":common_files",
27+
],
2928
)
3029

3130
filegroup(
32-
name = "link-emscripten",
31+
name = "linker_files",
3332
srcs = [
3433
"emcc_link.sh",
3534
"emcc_link.bat",
3635
"link_wrapper.py",
37-
":common-script-includes",
38-
"@emsdk//:binaries",
39-
node_files,
36+
"@emsdk//:linker_files",
37+
":common_files",
4038
],
4139
)
4240

4341
filegroup(
44-
name = "every-file",
42+
name = "ar_files",
4543
srcs = [
46-
":compile-emscripten",
47-
":link-emscripten",
48-
"@emsdk//:binaries",
49-
node_files,
44+
"emar.sh",
45+
"emar.bat",
46+
"@emsdk//:ar_files",
47+
":common_files",
5048
],
5149
)
5250

@@ -59,7 +57,7 @@ emscripten_cc_toolchain_config_rule(
5957
name = "wasm",
6058
cpu = "wasm",
6159
em_config = "emscripten_config",
62-
emscripten_binaries = "@emsdk//:binaries",
60+
emscripten_binaries = "@emsdk//:compiler_files",
6361
script_extension = select({
6462
"@bazel_tools//src/conditions:host_windows": "bat",
6563
"//conditions:default": "sh",
@@ -68,12 +66,12 @@ emscripten_cc_toolchain_config_rule(
6866

6967
cc_toolchain(
7068
name = "cc-compiler-wasm",
71-
all_files = ":every-file",
72-
ar_files = ":common-script-includes",
69+
all_files = ":empty",
70+
ar_files = ":ar_files",
7371
as_files = ":empty",
74-
compiler_files = ":compile-emscripten",
72+
compiler_files = ":compiler_files",
7573
dwp_files = ":empty",
76-
linker_files = ":link-emscripten",
74+
linker_files = ":linker_files",
7775
objcopy_files = ":empty",
7876
strip_files = ":empty",
7977
toolchain_config = "wasm",

bazel/emscripten_toolchain/emscripten.BUILD

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

0 commit comments

Comments
 (0)