feat: bootstrap bun toolchain skeleton and smoke test

This commit is contained in:
Eric
2026-03-04 02:54:12 +00:00
parent b66339a33b
commit ebce24804c
17 changed files with 261 additions and 1 deletions

1
BUILD.bazel Normal file
View File

@@ -0,0 +1 @@
package(default_visibility = ["//visibility:public"])

6
MODULE.bazel Normal file
View File

@@ -0,0 +1,6 @@
module(
name = "rules_bun",
version = "0.1.0",
)
bazel_dep(name = "platforms", version = "1.0.0")

View File

@@ -1,3 +1,12 @@
# rules_bun # rules_bun
Bazel rules for bun 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`)

5
WORKSPACE Normal file
View File

@@ -0,0 +1,5 @@
workspace(name = "rules_bun")
load("//bun:repositories.bzl", "bun_register_toolchains")
bun_register_toolchains()

103
bun/BUILD.bazel Normal file
View File

@@ -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",
)

9
bun/defs.bzl Normal file
View File

@@ -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",
]

73
bun/repositories.bzl Normal file
View File

@@ -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",
)

21
bun/toolchain.bzl Normal file
View File

@@ -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),
},
)

View File

@@ -0,0 +1 @@
package(default_visibility = ["//visibility:public"])

3
examples/basic/README.md Normal file
View File

@@ -0,0 +1,3 @@
# basic example
Placeholder for end-to-end bun rules example.

2
internal/bun_binary.bzl Normal file
View File

@@ -0,0 +1,2 @@
def bun_binary(**_kwargs):
fail("bun_binary is not implemented yet")

2
internal/bun_bundle.bzl Normal file
View File

@@ -0,0 +1,2 @@
def bun_bundle(**_kwargs):
fail("bun_bundle is not implemented yet")

2
internal/bun_install.bzl Normal file
View File

@@ -0,0 +1,2 @@
def bun_install(**_kwargs):
fail("bun_install is not implemented yet")

2
internal/bun_test.bzl Normal file
View File

@@ -0,0 +1,2 @@
def bun_test(**_kwargs):
fail("bun_test is not implemented yet")

1
tests/BUILD.bazel Normal file
View File

@@ -0,0 +1 @@
package(default_visibility = ["//visibility:public"])

View File

@@ -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",
],
)

View File

@@ -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