Skip to content

Commit bc6209b

Browse files
mattsoulanilleMatthew Soulanille
andauthored
Support Bazel builds on Apple silicon (#978)
* Support Bazel builds on Apple silicon * Add sha_mac_arm64 hash for 3.1.3 * Configure node_repositories in emscripten_deps.bzl * Add mac arm64 to bazel workspace update script Co-authored-by: Matthew Soulanille <[email protected]>
1 parent c33c7be commit bc6209b

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

bazel/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ config_setting(
1616
],
1717
)
1818

19+
config_setting(
20+
name = "macos_arm64",
21+
constraint_values = [
22+
"@platforms//os:macos",
23+
"@platforms//cpu:arm64",
24+
],
25+
)
26+
1927
config_setting(
2028
name = "windows",
2129
constraint_values = [
@@ -31,6 +39,7 @@ alias(
3139
actual = select({
3240
":linux": "@emscripten_bin_linux//:all",
3341
":macos": "@emscripten_bin_mac//:all",
42+
":macos_arm64": "@emscripten_bin_mac_arm64//:all",
3443
":windows": "@emscripten_bin_win//:all",
3544
"//conditions:default": ":empty",
3645
}),
@@ -41,6 +50,7 @@ alias(
4150
actual = select({
4251
":linux": "@emscripten_npm_linux//:node_modules",
4352
":macos": "@emscripten_npm_mac//:node_modules",
53+
":macos_arm64": "@emscripten_npm_mac//:node_modules",
4454
":windows": "@emscripten_npm_win//:node_modules",
4555
"//conditions:default": ":empty",
4656
}),

bazel/emscripten_deps.bzl

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2-
load("@build_bazel_rules_nodejs//:index.bzl", "npm_install")
2+
load("@build_bazel_rules_nodejs//:index.bzl", "npm_install", "node_repositories")
33
load(":revisions.bzl", "EMSCRIPTEN_TAGS")
44

55
def _parse_version(v):
@@ -19,16 +19,22 @@ def emscripten_deps(emscripten_version = "latest"):
1919

2020
revision = EMSCRIPTEN_TAGS[version]
2121

22-
emscripten_url = "https://storage.googleapis.com/webassembly/emscripten-releases-builds/{}/{}/wasm-binaries.{}"
22+
emscripten_url = "https://storage.googleapis.com/webassembly/emscripten-releases-builds/{}/{}/wasm-binaries{}.{}"
2323

2424
# This could potentially backfire for projects with multiple emscripten
2525
# dependencies that use different emscripten versions
2626
excludes = native.existing_rules().keys()
27+
if "nodejs_toolchains" not in excludes:
28+
# Node 16 is the first version that supports darwin_arm64
29+
node_repositories(
30+
node_version = "16.6.2",
31+
)
32+
2733
if "emscripten_bin_linux" not in excludes:
2834
http_archive(
2935
name = "emscripten_bin_linux",
3036
strip_prefix = "install",
31-
url = emscripten_url.format("linux", revision.hash, "tbz2"),
37+
url = emscripten_url.format("linux", revision.hash, "", "tbz2"),
3238
sha256 = revision.sha_linux,
3339
build_file = "@emsdk//emscripten_toolchain:emscripten.BUILD",
3440
type = "tar.bz2",
@@ -38,17 +44,27 @@ def emscripten_deps(emscripten_version = "latest"):
3844
http_archive(
3945
name = "emscripten_bin_mac",
4046
strip_prefix = "install",
41-
url = emscripten_url.format("mac", revision.hash, "tbz2"),
47+
url = emscripten_url.format("mac", revision.hash, "", "tbz2"),
4248
sha256 = revision.sha_mac,
4349
build_file = "@emsdk//emscripten_toolchain:emscripten.BUILD",
4450
type = "tar.bz2",
4551
)
4652

53+
if "emscripten_bin_mac_arm64" not in excludes:
54+
http_archive(
55+
name = "emscripten_bin_mac_arm64",
56+
strip_prefix = "install",
57+
url = emscripten_url.format("mac", revision.hash, "-arm64", "tbz2"),
58+
sha256 = revision.sha_mac_arm64,
59+
build_file = "@emsdk//emscripten_toolchain:emscripten.BUILD",
60+
type = "tar.bz2",
61+
)
62+
4763
if "emscripten_bin_win" not in excludes:
4864
http_archive(
4965
name = "emscripten_bin_win",
5066
strip_prefix = "install",
51-
url = emscripten_url.format("win", revision.hash, "zip"),
67+
url = emscripten_url.format("win", revision.hash, "", "zip"),
5268
sha256 = revision.sha_win,
5369
build_file = "@emsdk//emscripten_toolchain:emscripten.BUILD",
5470
type = "zip",
@@ -68,6 +84,13 @@ def emscripten_deps(emscripten_version = "latest"):
6884
package_lock_json = "@emscripten_bin_mac//:emscripten/package-lock.json",
6985
)
7086

87+
if "emscripten_npm_mac_arm64" not in excludes:
88+
npm_install(
89+
name = "emscripten_npm_mac",
90+
package_json = "@emscripten_bin_mac_arm64//:emscripten/package.json",
91+
package_lock_json = "@emscripten_bin_mac_arm64//:emscripten/package-lock.json",
92+
)
93+
7194
if "emscripten_npm_win" not in excludes:
7295
npm_install(
7396
name = "emscripten_npm_win",

bazel/emscripten_toolchain/emscripten_config

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ LLVM_ROOT = BINARYEN_ROOT + "/bin"
88
FROZEN_CACHE = True
99

1010
system = platform.system()
11+
12+
machine = "arm64" if platform.machine() == "arm64" else "amd64"
1113
nodejs_binary = "bin/nodejs/node.exe" if(system =="Windows") else "bin/node"
12-
NODE_JS = ROOT_DIR + "/external/nodejs_{}_amd64/{}".format(system.lower(), nodejs_binary)
14+
NODE_JS = ROOT_DIR + "/external/nodejs_{}_{}/{}".format(system.lower(), machine, nodejs_binary)

bazel/revisions.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ EMSCRIPTEN_TAGS = {
66
hash = "2ddc66235392b37e5b33477fd86cbe01a14b8aa2",
77
sha_linux = "8b840819eb88f9178c11bad25859ce448a0559e485823a863a6add21380636ca",
88
sha_mac = "0cb3f9bfbcc744233eae9d20036155738409405eacf8a3d4f9beefc5919d809a",
9+
sha_mac_arm64 = "ee2772f380419df17d154e00388a16bcddc78c7af035c16a2ee534d6ecf099aa",
910
sha_win = "c0549e1dbaa581ae66934c38beebd4250cd450cc2778e9a602cd9431bc81bc37",
1011
),
1112
"3.1.2": struct(
1213
hash = "6626e25d6d866cf283147ca68d54ac9326fe399f",
1314
sha_linux = "4fb53364a2ba1de8978445aa26b2204bfd215b41da5d7df04f231040b197010a",
1415
sha_mac = "a8e347accb1ff402d96a128912ac8cda1731611c9f89095fee0ad39a6a18bbc3",
16+
sha_mac_arm64 = "4374f5c852d0403b0a3b0e9dc8a3856a340e9d82ecf0f20aa8b36c6179d31fc8",
1517
sha_win = "e96f6ab8252fefa42f461676311d4c4e2d96fdc2e876ece07d9d7a49ef31aef0",
1618
),
1719
"3.1.1": struct(
@@ -48,6 +50,7 @@ EMSCRIPTEN_TAGS = {
4850
hash = "cef8850d57278271766fb2163eebcb07354018e7",
4951
sha_linux = "958a0f4b1533e877c1a5ed3c13cb8baabc80e791d45858c2c94ac62325ada953",
5052
sha_mac = "8ecb248653d44c3748e23c089cb9f0e3d4eee7cda13fdec27ec0113b896e34c4",
53+
sha_mac_arm64 = "1ec6f3d7afa5e10f3af996e26d9c3a66f02ae49e48e512a4b5d6b7165c61290f",
5154
sha_win = "6b6b2831f8b338488f787b4a8c34700277bf3988358dbb54426f017155603ac9",
5255
),
5356
"2.0.32": struct(

scripts/update_bazel_workspace.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ if [[ $ERR = 1 ]]; then
3434
fi
3535

3636
URL1=https://storage.googleapis.com/webassembly/emscripten-releases-builds/
37-
URL2=/wasm-binaries.
37+
URL2=/wasm-binaries
3838

3939
# Get commit hash for $1 version
4040
get_hash () {
41-
echo $(grep "$1" emscripten-releases-tags.json | grep -v latest | cut -f4 -d\")
41+
echo $(grep "$1" emscripten-releases-tags.json | grep -v latest | grep -v asserts | cut -f4 -d\")
4242
}
4343

44-
# Get sha256 for $1 os $2 extname $3 hash
44+
# Get sha256 for $1 os $2 extname $3 hash $4 architecture
4545
get_sha () {
46-
echo $(curl "${URL1}$1/$3${URL2}$2" 2>/dev/null | sha256sum | awk '{print $1}')
46+
echo $(curl "${URL1}$1/$3${URL2}$4.$2" 2>/dev/null | sha256sum | awk '{print $1}')
4747
}
4848

4949
# Assemble dictionary line
@@ -54,6 +54,7 @@ revisions_item () {
5454
"\ hash = \"$(get_hash ${hash})\",\n" \
5555
"\ sha_linux = \"$(get_sha linux tbz2 ${hash})\",\n" \
5656
"\ sha_mac = \"$(get_sha mac tbz2 ${hash})\",\n" \
57+
"\ sha_mac_arm64 = \"$(get_sha mac tbz2 ${hash} -arm64)\",\n" \
5758
"\ sha_win = \"$(get_sha win zip ${hash})\",\n" \
5859
"\ ),"
5960
}

0 commit comments

Comments
 (0)