From c3a3f589be339d65832d48ce037f3543f689ea22 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 4 Mar 2026 02:54:12 +0000 Subject: [PATCH] feat: bootstrap bun toolchain skeleton and smoke test --- BUILD.bazel | 1 + MODULE.bazel | 6 ++ README.md | 11 ++- WORKSPACE | 5 ++ bun/BUILD.bazel | 103 ++++++++++++++++++++++ bun/defs.bzl | 9 ++ bun/repositories.bzl | 73 +++++++++++++++ bun/toolchain.bzl | 21 +++++ examples/basic/BUILD.bazel | 1 + examples/basic/README.md | 3 + internal/bun_binary.bzl | 2 + internal/bun_bundle.bzl | 2 + internal/bun_install.bzl | 2 + internal/bun_test.bzl | 2 + tests/BUILD.bazel | 1 + tests/toolchain_test/BUILD.bazel | 10 +++ tests/toolchain_test/toolchain_version.sh | 10 +++ 17 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 BUILD.bazel create mode 100644 MODULE.bazel create mode 100644 WORKSPACE create mode 100644 bun/BUILD.bazel create mode 100644 bun/defs.bzl create mode 100644 bun/repositories.bzl create mode 100644 bun/toolchain.bzl create mode 100644 examples/basic/BUILD.bazel create mode 100644 examples/basic/README.md create mode 100644 internal/bun_binary.bzl create mode 100644 internal/bun_bundle.bzl create mode 100644 internal/bun_install.bzl create mode 100644 internal/bun_test.bzl create mode 100644 tests/BUILD.bazel create mode 100644 tests/toolchain_test/BUILD.bazel create mode 100755 tests/toolchain_test/toolchain_version.sh diff --git a/BUILD.bazel b/BUILD.bazel new file mode 100644 index 0000000..ffd0fb0 --- /dev/null +++ b/BUILD.bazel @@ -0,0 +1 @@ +package(default_visibility = ["//visibility:public"]) diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000..1c77d2a --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,6 @@ +module( + name = "rules_bun", + version = "0.1.0", +) + +bazel_dep(name = "platforms", version = "1.0.0") diff --git a/README.md b/README.md index 8c174f5..7f3275d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,12 @@ # rules_bun -Bazel rules for bun \ No newline at end of file +Bazel rules for bun. + +## Current status + +Phase 1 bootstrap is in place: + +- Bun toolchain rule and provider (`/bun/toolchain.bzl`) +- Platform-specific Bun repository downloads (`/bun/repositories.bzl`) +- Toolchain declarations and registration targets (`/bun/BUILD.bazel`) +- Smoke test for `bun --version` (`//tests/toolchain_test:bun_version_test`) diff --git a/WORKSPACE b/WORKSPACE new file mode 100644 index 0000000..2f8ace5 --- /dev/null +++ b/WORKSPACE @@ -0,0 +1,5 @@ +workspace(name = "rules_bun") + +load("//bun:repositories.bzl", "bun_register_toolchains") + +bun_register_toolchains() diff --git a/bun/BUILD.bazel b/bun/BUILD.bazel new file mode 100644 index 0000000..c84f88c --- /dev/null +++ b/bun/BUILD.bazel @@ -0,0 +1,103 @@ +load(":toolchain.bzl", "bun_toolchain") + +toolchain_type(name = "toolchain_type") + +bun_toolchain( + name = "linux_x64_toolchain_impl", + bun = "@bun_linux_x64//:bun", + version = "1.1.38", +) + +bun_toolchain( + name = "linux_aarch64_toolchain_impl", + bun = "@bun_linux_aarch64//:bun", + version = "1.1.38", +) + +bun_toolchain( + name = "darwin_x64_toolchain_impl", + bun = "@bun_darwin_x64//:bun", + version = "1.1.38", +) + +bun_toolchain( + name = "darwin_aarch64_toolchain_impl", + bun = "@bun_darwin_aarch64//:bun", + version = "1.1.38", +) + +bun_toolchain( + name = "windows_x64_toolchain_impl", + bun = "@bun_windows_x64//:bun", + version = "1.1.38", +) + +toolchain( + name = "linux_x64_toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], + toolchain = ":linux_x64_toolchain_impl", + toolchain_type = ":toolchain_type", +) + +toolchain( + name = "linux_aarch64_toolchain", + exec_compatible_with = [ + "@platforms//cpu:arm64", + "@platforms//os:linux", + ], + target_compatible_with = [ + "@platforms//cpu:arm64", + "@platforms//os:linux", + ], + toolchain = ":linux_aarch64_toolchain_impl", + toolchain_type = ":toolchain_type", +) + +toolchain( + name = "darwin_x64_toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:macos", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:macos", + ], + toolchain = ":darwin_x64_toolchain_impl", + toolchain_type = ":toolchain_type", +) + +toolchain( + name = "darwin_aarch64_toolchain", + exec_compatible_with = [ + "@platforms//cpu:arm64", + "@platforms//os:macos", + ], + target_compatible_with = [ + "@platforms//cpu:arm64", + "@platforms//os:macos", + ], + toolchain = ":darwin_aarch64_toolchain_impl", + toolchain_type = ":toolchain_type", +) + +toolchain( + name = "windows_x64_toolchain", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:windows", + ], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:windows", + ], + toolchain = ":windows_x64_toolchain_impl", + toolchain_type = ":toolchain_type", +) diff --git a/bun/defs.bzl b/bun/defs.bzl new file mode 100644 index 0000000..bfca96a --- /dev/null +++ b/bun/defs.bzl @@ -0,0 +1,9 @@ +load(":repositories.bzl", "bun_register_toolchains", "bun_repositories") +load(":toolchain.bzl", "BunToolchainInfo", "bun_toolchain") + +__all__ = [ + "BunToolchainInfo", + "bun_register_toolchains", + "bun_repositories", + "bun_toolchain", +] diff --git a/bun/repositories.bzl b/bun/repositories.bzl new file mode 100644 index 0000000..665bcf3 --- /dev/null +++ b/bun/repositories.bzl @@ -0,0 +1,73 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +_BUN_VERSION = "1.1.38" + +_BUN_ARCHIVES = { + "bun_linux_x64": { + "sha256": "a61da5357e28d4977fccd4851fed62ff4da3ea33853005c7dd93dac80bc53932", + "asset": "bun-linux-x64.zip", + "binary": "bun-linux-x64/bun", + }, + "bun_linux_aarch64": { + "sha256": "3b08fd0b31f745509e1fed9c690c80d1a32ef2b3c8d059583f643f696639bd21", + "asset": "bun-linux-aarch64.zip", + "binary": "bun-linux-aarch64/bun", + }, + "bun_darwin_x64": { + "sha256": "4e9814c9b2e64f9166ed8fc2a48f905a2195ea599b7ceda7ac821688520428a5", + "asset": "bun-darwin-x64.zip", + "binary": "bun-darwin-x64/bun", + }, + "bun_darwin_aarch64": { + "sha256": "bbc6fb0e7bb99e7e95001ba05105cf09d0b79c06941d9f6ee3d0b34dc1541590", + "asset": "bun-darwin-aarch64.zip", + "binary": "bun-darwin-aarch64/bun", + }, + "bun_windows_x64": { + "sha256": "52d6c588237c5a1071839dc20dc96f19ca9f8021b7757fa096d22927b0a44a8b", + "asset": "bun-windows-x64.zip", + "binary": "bun-windows-x64/bun.exe", + }, +} + + +def _declare_bun_repo(name, asset, sha256, binary, version): + if native.existing_rule(name): + return + + http_archive( + name = name, + urls = ["https://github.com/oven-sh/bun/releases/download/bun-v{}/{}".format(version, asset)], + sha256 = sha256, + build_file_content = """ +exports_files(["{binary}"]) + +filegroup( + name = "bun", + srcs = ["{binary}"], + visibility = ["//visibility:public"], +) +""".format(binary = binary), + ) + + +def bun_repositories(version = _BUN_VERSION): + for name, metadata in _BUN_ARCHIVES.items(): + _declare_bun_repo( + name = name, + asset = metadata["asset"], + sha256 = metadata["sha256"], + binary = metadata["binary"], + version = version, + ) + + +def bun_register_toolchains(version = _BUN_VERSION): + bun_repositories(version = version) + native.register_toolchains( + "//bun:darwin_aarch64_toolchain", + "//bun:darwin_x64_toolchain", + "//bun:linux_aarch64_toolchain", + "//bun:linux_x64_toolchain", + "//bun:windows_x64_toolchain", + ) diff --git a/bun/toolchain.bzl b/bun/toolchain.bzl new file mode 100644 index 0000000..0b29a13 --- /dev/null +++ b/bun/toolchain.bzl @@ -0,0 +1,21 @@ +BunToolchainInfo = provider(fields = ["bun_bin", "version"]) + + +def _bun_toolchain_impl(ctx): + return [ + platform_common.ToolchainInfo( + bun = BunToolchainInfo( + bun_bin = ctx.executable.bun, + version = ctx.attr.version, + ), + ), + ] + + +bun_toolchain = rule( + implementation = _bun_toolchain_impl, + attrs = { + "bun": attr.label(allow_single_file = True, executable = True, cfg = "exec"), + "version": attr.string(mandatory = True), + }, +) diff --git a/examples/basic/BUILD.bazel b/examples/basic/BUILD.bazel new file mode 100644 index 0000000..ffd0fb0 --- /dev/null +++ b/examples/basic/BUILD.bazel @@ -0,0 +1 @@ +package(default_visibility = ["//visibility:public"]) diff --git a/examples/basic/README.md b/examples/basic/README.md new file mode 100644 index 0000000..cba1cbf --- /dev/null +++ b/examples/basic/README.md @@ -0,0 +1,3 @@ +# basic example + +Placeholder for end-to-end bun rules example. diff --git a/internal/bun_binary.bzl b/internal/bun_binary.bzl new file mode 100644 index 0000000..e9808f9 --- /dev/null +++ b/internal/bun_binary.bzl @@ -0,0 +1,2 @@ +def bun_binary(**_kwargs): + fail("bun_binary is not implemented yet") diff --git a/internal/bun_bundle.bzl b/internal/bun_bundle.bzl new file mode 100644 index 0000000..b430850 --- /dev/null +++ b/internal/bun_bundle.bzl @@ -0,0 +1,2 @@ +def bun_bundle(**_kwargs): + fail("bun_bundle is not implemented yet") diff --git a/internal/bun_install.bzl b/internal/bun_install.bzl new file mode 100644 index 0000000..b7e5080 --- /dev/null +++ b/internal/bun_install.bzl @@ -0,0 +1,2 @@ +def bun_install(**_kwargs): + fail("bun_install is not implemented yet") diff --git a/internal/bun_test.bzl b/internal/bun_test.bzl new file mode 100644 index 0000000..f013b9e --- /dev/null +++ b/internal/bun_test.bzl @@ -0,0 +1,2 @@ +def bun_test(**_kwargs): + fail("bun_test is not implemented yet") diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel new file mode 100644 index 0000000..ffd0fb0 --- /dev/null +++ b/tests/BUILD.bazel @@ -0,0 +1 @@ +package(default_visibility = ["//visibility:public"]) diff --git a/tests/toolchain_test/BUILD.bazel b/tests/toolchain_test/BUILD.bazel new file mode 100644 index 0000000..7d620d8 --- /dev/null +++ b/tests/toolchain_test/BUILD.bazel @@ -0,0 +1,10 @@ +sh_test( + name = "bun_version_test", + srcs = ["toolchain_version.sh"], + args = ["$(location @bun_linux_x64//:bun)"], + data = ["@bun_linux_x64//:bun"], + target_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], +) diff --git a/tests/toolchain_test/toolchain_version.sh b/tests/toolchain_test/toolchain_version.sh new file mode 100755 index 0000000..3024b9e --- /dev/null +++ b/tests/toolchain_test/toolchain_version.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -euo pipefail + +bun_path="$1" +version="$(${bun_path} --version)" + +if [[ ! "${version}" =~ ^[0-9]+\.[0-9]+\.[0-9]+ ]]; then + echo "Unexpected bun version output: ${version}" >&2 + exit 1 +fi