Bazel: Migrate to bzlmod (#1530)
This migrates the bazel integration to the new Bazel dependency system "bzlmod". bzlmod is becoming mandatory this year (see second sentence here: https://bazel.build/external/migration). This is a backwards incompatible migration, directly removing the old WORKSPACE based approach. Users will have to change how they depend on bzlmod, however I assume pretty much every user will be happy about it, because they are forced to use bzlmod anyway or add extra flags to continue to build with newer Bazel versions. Given that users normally depend on specific git commits in the old system, they won't be hit with this until they decide to upgrade emsdk. The basic principle here is simple: I took everything that WORKSPACE did and searched an alternative in bzlmod. Some interesting bits: - We have less worries about multiple versions and people depending on emscripten multiple times in different ways. This is resolved by the new system: Bazel first iterates through the MODULE.bazel files recursively, then finds the minimum version needed for each module and then executes the module extensions that define repos exactly once at that version. So no more ifs needed to detect multiple inclusions. - A bunch of nodejs stuff moves to MODULE.bazel, because that is how the nodejs module works now. As their module extension gets executed only once you need to declare everything that you could need before that in the MODULE.bazel file. A side effect of that is that we have to make a fake repository when emscripten doesn't have an arm64 binary for linux, because we can't actually figure that out in MODULE.bazel, so we have to declare that it always exists and then create one in all cases. There is a bunch of autoformatter changes in here as well, I could try to revert them if you prefer. Closes #1509
This commit is contained in:
@@ -1,210 +1,99 @@
|
||||
load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock")
|
||||
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
||||
load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")
|
||||
load(":remote_emscripten_repository.bzl", "remote_emscripten_repository")
|
||||
load(":revisions.bzl", "EMSCRIPTEN_TAGS")
|
||||
|
||||
def _parse_version(v):
|
||||
return [int(u) for u in v.split(".")]
|
||||
|
||||
BUILD_FILE_CONTENT_TEMPLATE = """
|
||||
package(default_visibility = ['//visibility:public'])
|
||||
def _empty_repository_impl(ctx):
|
||||
ctx.file("MODULE.bazel", """module(name = "{}")""".format(ctx.name))
|
||||
ctx.file("BUILD.bazel", "")
|
||||
|
||||
filegroup(
|
||||
name = "all",
|
||||
srcs = glob(["**"]),
|
||||
_empty_repository = repository_rule(
|
||||
implementation = _empty_repository_impl,
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "includes",
|
||||
srcs = glob([
|
||||
"emscripten/cache/sysroot/include/c++/v1/**",
|
||||
"emscripten/cache/sysroot/include/compat/**",
|
||||
"emscripten/cache/sysroot/include/**",
|
||||
"lib/clang/**/include/**",
|
||||
]),
|
||||
)
|
||||
def emscripten_repo_name(name):
|
||||
return "emscripten_bin_{}".format(name)
|
||||
|
||||
filegroup(
|
||||
name = "emcc_common",
|
||||
srcs = [
|
||||
"emscripten/emcc.py",
|
||||
"emscripten/embuilder.py",
|
||||
"emscripten/emscripten-version.txt",
|
||||
"emscripten/cache/sysroot_install.stamp",
|
||||
"emscripten/src/settings.js",
|
||||
"emscripten/src/settings_internal.js",
|
||||
] + glob(
|
||||
include = [
|
||||
"emscripten/third_party/**",
|
||||
"emscripten/tools/**",
|
||||
],
|
||||
exclude = [
|
||||
"**/__pycache__/**",
|
||||
],
|
||||
),
|
||||
)
|
||||
def _emscripten_deps_impl(ctx):
|
||||
version = None
|
||||
|
||||
filegroup(
|
||||
name = "compiler_files",
|
||||
srcs = [
|
||||
"bin/clang{bin_extension}",
|
||||
"bin/clang++{bin_extension}",
|
||||
":emcc_common",
|
||||
":includes",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "linker_files",
|
||||
srcs = [
|
||||
"bin/clang{bin_extension}",
|
||||
"bin/llvm-ar{bin_extension}",
|
||||
"bin/llvm-dwarfdump{bin_extension}",
|
||||
"bin/llvm-nm{bin_extension}",
|
||||
"bin/llvm-objcopy{bin_extension}",
|
||||
"bin/wasm-ctor-eval{bin_extension}",
|
||||
"bin/wasm-emscripten-finalize{bin_extension}",
|
||||
"bin/wasm-ld{bin_extension}",
|
||||
"bin/wasm-metadce{bin_extension}",
|
||||
"bin/wasm-opt{bin_extension}",
|
||||
"bin/wasm-split{bin_extension}",
|
||||
"bin/wasm2js{bin_extension}",
|
||||
":emcc_common",
|
||||
] + glob(
|
||||
include = [
|
||||
"emscripten/cache/sysroot/lib/**",
|
||||
"emscripten/node_modules/**",
|
||||
"emscripten/src/**",
|
||||
],
|
||||
),
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "ar_files",
|
||||
srcs = [
|
||||
"bin/llvm-ar{bin_extension}",
|
||||
"emscripten/emar.py",
|
||||
"emscripten/emscripten-version.txt",
|
||||
"emscripten/src/settings.js",
|
||||
"emscripten/src/settings_internal.js",
|
||||
] + glob(
|
||||
include = [
|
||||
"emscripten/tools/**",
|
||||
],
|
||||
exclude = [
|
||||
"**/__pycache__/**",
|
||||
],
|
||||
),
|
||||
)
|
||||
"""
|
||||
|
||||
def emscripten_deps(emscripten_version = "latest"):
|
||||
version = emscripten_version
|
||||
for mod in ctx.modules:
|
||||
for config in mod.tags.config:
|
||||
if config.version and version != None:
|
||||
fail("More than one emscripten version specified!")
|
||||
version = config.version
|
||||
if version == None:
|
||||
version = "latest"
|
||||
|
||||
if version == "latest":
|
||||
version = reversed(sorted(EMSCRIPTEN_TAGS.keys(), key = _parse_version))[0]
|
||||
|
||||
if version not in EMSCRIPTEN_TAGS.keys():
|
||||
error_msg = "Emscripten version {} not found.".format(version)
|
||||
error_msg += " Look at @emsdk//:revisions.bzl for the list "
|
||||
error_msg += "of currently supported versions."
|
||||
fail(error_msg)
|
||||
|
||||
revision = EMSCRIPTEN_TAGS[version]
|
||||
|
||||
emscripten_url = "https://storage.googleapis.com/webassembly/emscripten-releases-builds/{}/{}/wasm-binaries{}.{}"
|
||||
|
||||
# This could potentially backfire for projects with multiple emscripten
|
||||
# dependencies that use different emscripten versions
|
||||
excludes = native.existing_rules().keys()
|
||||
if "nodejs_toolchains" not in excludes:
|
||||
# Node 16 is the first version that supports darwin_arm64
|
||||
nodejs_register_toolchains(
|
||||
node_version = "20.18.0",
|
||||
)
|
||||
remote_emscripten_repository(
|
||||
name = emscripten_repo_name("linux"),
|
||||
bin_extension = "",
|
||||
sha256 = revision.sha_linux,
|
||||
strip_prefix = "install",
|
||||
type = "tar.xz",
|
||||
url = emscripten_url.format("linux", revision.hash, "", "tar.xz"),
|
||||
)
|
||||
|
||||
if "emscripten_bin_linux" not in excludes:
|
||||
http_archive(
|
||||
name = "emscripten_bin_linux",
|
||||
# Not all versions have a linux/arm64 release: https://github.com/emscripten-core/emsdk/issues/547
|
||||
if hasattr(revision, "sha_linux_arm64"):
|
||||
remote_emscripten_repository(
|
||||
name = emscripten_repo_name("linux_arm64"),
|
||||
bin_extension = "",
|
||||
sha256 = revision.sha_linux_arm64,
|
||||
strip_prefix = "install",
|
||||
url = emscripten_url.format("linux", revision.hash, "", "tar.xz"),
|
||||
sha256 = revision.sha_linux,
|
||||
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension = ""),
|
||||
type = "tar.xz",
|
||||
)
|
||||
|
||||
if "emscripten_bin_linux_arm64" not in excludes:
|
||||
http_archive(
|
||||
name = "emscripten_bin_linux_arm64",
|
||||
strip_prefix = "install",
|
||||
url = emscripten_url.format("linux", revision.hash, "-arm64", "tar.xz"),
|
||||
# Not all versions have a linux/arm64 release: https://github.com/emscripten-core/emsdk/issues/547
|
||||
sha256 = getattr(revision, "sha_linux_arm64", None),
|
||||
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension = ""),
|
||||
type = "tar.xz",
|
||||
)
|
||||
else:
|
||||
_empty_repository(
|
||||
name = emscripten_repo_name("linux_arm64"),
|
||||
)
|
||||
|
||||
if "emscripten_bin_mac" not in excludes:
|
||||
http_archive(
|
||||
name = "emscripten_bin_mac",
|
||||
strip_prefix = "install",
|
||||
url = emscripten_url.format("mac", revision.hash, "", "tar.xz"),
|
||||
sha256 = revision.sha_mac,
|
||||
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension = ""),
|
||||
type = "tar.xz",
|
||||
)
|
||||
remote_emscripten_repository(
|
||||
name = emscripten_repo_name("mac"),
|
||||
bin_extension = "",
|
||||
sha256 = revision.sha_mac,
|
||||
strip_prefix = "install",
|
||||
type = "tar.xz",
|
||||
url = emscripten_url.format("mac", revision.hash, "", "tar.xz"),
|
||||
)
|
||||
|
||||
if "emscripten_bin_mac_arm64" not in excludes:
|
||||
http_archive(
|
||||
name = "emscripten_bin_mac_arm64",
|
||||
strip_prefix = "install",
|
||||
url = emscripten_url.format("mac", revision.hash, "-arm64", "tar.xz"),
|
||||
sha256 = revision.sha_mac_arm64,
|
||||
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension = ""),
|
||||
type = "tar.xz",
|
||||
)
|
||||
remote_emscripten_repository(
|
||||
name = emscripten_repo_name("mac_arm64"),
|
||||
bin_extension = "",
|
||||
sha256 = revision.sha_mac_arm64,
|
||||
strip_prefix = "install",
|
||||
type = "tar.xz",
|
||||
url = emscripten_url.format("mac", revision.hash, "-arm64", "tar.xz"),
|
||||
)
|
||||
|
||||
if "emscripten_bin_win" not in excludes:
|
||||
http_archive(
|
||||
name = "emscripten_bin_win",
|
||||
strip_prefix = "install",
|
||||
url = emscripten_url.format("win", revision.hash, "", "zip"),
|
||||
sha256 = revision.sha_win,
|
||||
build_file_content = BUILD_FILE_CONTENT_TEMPLATE.format(bin_extension = ".exe"),
|
||||
type = "zip",
|
||||
)
|
||||
remote_emscripten_repository(
|
||||
name = emscripten_repo_name("win"),
|
||||
bin_extension = ".exe",
|
||||
sha256 = revision.sha_win,
|
||||
strip_prefix = "install",
|
||||
type = "zip",
|
||||
url = emscripten_url.format("win", revision.hash, "", "zip"),
|
||||
)
|
||||
|
||||
if "emscripten_npm_linux" not in excludes:
|
||||
npm_translate_lock(
|
||||
name = "emscripten_npm_linux",
|
||||
data = ["@emscripten_bin_linux//:emscripten/package.json"],
|
||||
npm_package_lock = "@emscripten_bin_linux//:emscripten/package-lock.json",
|
||||
)
|
||||
|
||||
if "emscripten_npm_linux_arm64" not in excludes:
|
||||
npm_translate_lock(
|
||||
name = "emscripten_npm_linux_arm64",
|
||||
data = ["@emscripten_bin_linux_arm64//:emscripten/package.json"],
|
||||
npm_package_lock = "@emscripten_bin_linux_arm64//:emscripten/package-lock.json",
|
||||
)
|
||||
|
||||
if "emscripten_npm_mac" not in excludes:
|
||||
npm_translate_lock(
|
||||
name = "emscripten_npm_mac",
|
||||
data = ["@emscripten_bin_mac//:emscripten/package.json"],
|
||||
npm_package_lock = "@emscripten_bin_mac//:emscripten/package-lock.json",
|
||||
)
|
||||
|
||||
if "emscripten_npm_mac_arm64" not in excludes:
|
||||
npm_translate_lock(
|
||||
name = "emscripten_npm_mac",
|
||||
data = ["@emscripten_bin_mac_arm64//:emscripten/package.json"],
|
||||
npm_package_lock = "@emscripten_bin_mac_arm64//:emscripten/package-lock.json",
|
||||
)
|
||||
|
||||
if "emscripten_npm_win" not in excludes:
|
||||
npm_translate_lock(
|
||||
name = "emscripten_npm_win",
|
||||
data = ["@emscripten_bin_win//:emscripten/package.json"],
|
||||
npm_package_lock = "@emscripten_bin_win//:emscripten/package-lock.json",
|
||||
)
|
||||
emscripten_deps = module_extension(
|
||||
tag_classes = {
|
||||
"config": tag_class(
|
||||
attrs = {
|
||||
"version": attr.string(
|
||||
doc = "Version to use. 'latest' to use latest.",
|
||||
values = ["latest"] + EMSCRIPTEN_TAGS.keys(),
|
||||
),
|
||||
},
|
||||
),
|
||||
},
|
||||
implementation = _emscripten_deps_impl,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user