Mercurial
comparison third_party/emsdk/bazel/emscripten_cache.bzl @ 179:8d17f6e6e290
[ThirdParty] Added emsdk bazel rules that can be supported by bazel 9.0.0
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Thu, 22 Jan 2026 21:23:17 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 178:94705b5986b3 | 179:8d17f6e6e290 |
|---|---|
| 1 BUILD_FILE_CONTENT_TEMPLATE = """ | |
| 2 package(default_visibility = ['//visibility:public']) | |
| 3 exports_files(['emscripten_config']) | |
| 4 """ | |
| 5 | |
| 6 EMBUILDER_CONFIG_TEMPLATE = """ | |
| 7 CACHE = '{cache}' | |
| 8 BINARYEN_ROOT = '{binaryen_root}' | |
| 9 LLVM_ROOT = '{llvm_root}' | |
| 10 """ | |
| 11 | |
| 12 def get_root_and_script_ext(repository_ctx): | |
| 13 if repository_ctx.os.name.startswith("linux"): | |
| 14 if "amd64" in repository_ctx.os.arch or "x86_64" in repository_ctx.os.arch: | |
| 15 return (repository_ctx.path(Label("@emscripten_bin_linux//:BUILD.bazel")).dirname, "") | |
| 16 elif "aarch64" in repository_ctx.os.arch: | |
| 17 return (repository_ctx.path(Label("@emscripten_bin_linux_arm64//:BUILD.bazel")).dirname, "") | |
| 18 else: | |
| 19 fail("Unsupported architecture for Linux") | |
| 20 elif repository_ctx.os.name.startswith("mac"): | |
| 21 if "amd64" in repository_ctx.os.arch or "x86_64" in repository_ctx.os.arch: | |
| 22 return (repository_ctx.path(Label("@emscripten_bin_mac//:BUILD.bazel")).dirname, "") | |
| 23 elif "aarch64" in repository_ctx.os.arch: | |
| 24 return (repository_ctx.path(Label("@emscripten_bin_mac_arm64//:BUILD.bazel")).dirname, "") | |
| 25 else: | |
| 26 fail("Unsupported architecture for MacOS") | |
| 27 elif repository_ctx.os.name.startswith("windows"): | |
| 28 return (repository_ctx.path(Label("@emscripten_bin_win//:BUILD.bazel")).dirname, ".bat") | |
| 29 else: | |
| 30 fail("Unsupported operating system") | |
| 31 | |
| 32 def _emscripten_cache_repository_impl(repository_ctx): | |
| 33 # Read the default emscripten configuration file | |
| 34 default_config = repository_ctx.read( | |
| 35 repository_ctx.path( | |
| 36 Label("@emsdk//emscripten_toolchain:default_config"), | |
| 37 ), | |
| 38 ) | |
| 39 | |
| 40 if repository_ctx.attr.targets or repository_ctx.attr.configuration: | |
| 41 root, script_ext = get_root_and_script_ext(repository_ctx) | |
| 42 llvm_root = root.get_child("bin") | |
| 43 cache = repository_ctx.path("cache") | |
| 44 | |
| 45 # Create configuration file | |
| 46 embuilder_config_content = EMBUILDER_CONFIG_TEMPLATE.format( | |
| 47 cache = cache, | |
| 48 binaryen_root = root, | |
| 49 llvm_root = llvm_root, | |
| 50 ) | |
| 51 repository_ctx.file("embuilder_config", embuilder_config_content) | |
| 52 embuilder_config_path = repository_ctx.path("embuilder_config") | |
| 53 embuilder_path = "{}{}".format(root.get_child("emscripten").get_child("embuilder"), script_ext) | |
| 54 | |
| 55 # Prepare the command line | |
| 56 if repository_ctx.attr.targets: | |
| 57 targets = repository_ctx.attr.targets | |
| 58 else: | |
| 59 # If no targets are requested, build everything | |
| 60 targets = ["ALL"] | |
| 61 flags = ["--em-config", embuilder_config_path] + repository_ctx.attr.configuration | |
| 62 embuilder_args = [embuilder_path] + flags + ["build"] + targets | |
| 63 | |
| 64 # Run embuilder | |
| 65 repository_ctx.report_progress("Building secondary cache") | |
| 66 result = repository_ctx.execute( | |
| 67 embuilder_args, | |
| 68 quiet = True, | |
| 69 environment = { | |
| 70 "EM_IGNORE_SANITY": "1", | |
| 71 "EM_NODE_JS": "empty", | |
| 72 }, | |
| 73 ) | |
| 74 if result.return_code != 0: | |
| 75 fail("Embuilder exited with a non-zero return code") | |
| 76 | |
| 77 # Override Emscripten's cache with the secondary cache | |
| 78 default_config += "CACHE = '{}'\n".format(cache) | |
| 79 | |
| 80 # Create the configuration file for the toolchain and export | |
| 81 repository_ctx.file("emscripten_config", default_config) | |
| 82 repository_ctx.file("BUILD.bazel", BUILD_FILE_CONTENT_TEMPLATE) | |
| 83 | |
| 84 _emscripten_cache_repository = repository_rule( | |
| 85 implementation = _emscripten_cache_repository_impl, | |
| 86 attrs = { | |
| 87 "configuration": attr.string_list(), | |
| 88 "targets": attr.string_list(), | |
| 89 }, | |
| 90 ) | |
| 91 | |
| 92 def _emscripten_cache_impl(ctx): | |
| 93 all_configuration = [] | |
| 94 all_targets = [] | |
| 95 for mod in ctx.modules: | |
| 96 for configuration in mod.tags.configuration: | |
| 97 all_configuration += configuration.flags | |
| 98 for targets in mod.tags.targets: | |
| 99 all_targets += targets.targets | |
| 100 | |
| 101 _emscripten_cache_repository( | |
| 102 name = "emscripten_cache", | |
| 103 configuration = all_configuration, | |
| 104 targets = all_targets, | |
| 105 ) | |
| 106 | |
| 107 emscripten_cache = module_extension( | |
| 108 tag_classes = { | |
| 109 "configuration": tag_class(attrs = {"flags": attr.string_list()}), | |
| 110 "targets": tag_class(attrs = {"targets": attr.string_list()}), | |
| 111 }, | |
| 112 implementation = _emscripten_cache_impl, | |
| 113 ) |