124 lines
3.3 KiB
Python
124 lines
3.3 KiB
Python
"""Implementation of the zls_toolchain rule."""
|
|
|
|
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
|
|
|
_VERSION = "0.15.0"
|
|
|
|
_ZLS_PLATFORMS = {
|
|
"x86_64-linux": struct(
|
|
compatible_with = [
|
|
"@platforms//os:linux",
|
|
"@platforms//cpu:x86_64",
|
|
],
|
|
),
|
|
"x86_64-macos": struct(
|
|
compatible_with = [
|
|
"@platforms//os:macos",
|
|
"@platforms//cpu:x86_64",
|
|
],
|
|
),
|
|
"aarch64-macos": struct(
|
|
compatible_with = [
|
|
"@platforms//os:macos",
|
|
"@platforms//cpu:aarch64",
|
|
],
|
|
),
|
|
}
|
|
|
|
_ZLS_VERSIONS = {
|
|
"x86_64-linux": "508bfe3fd637d2a02f07f3fc7da8900351f407116b03685c5dae26b4f01a30de",
|
|
"x86_64-macos": "46c31838bfef5adcc7fee82428c3ec2b9abbfae38242639afea5f242ee133d93",
|
|
"aarch64-macos": "76c7a23190f67e67970024065f689c2c49b3c7b0fc16876fb24ef199fb05fc2a",
|
|
}
|
|
|
|
ZlsToolchainInfo = provider(
|
|
doc = "Provide info for executing zls",
|
|
fields = {
|
|
"bin": "Executable ZLS binary",
|
|
},
|
|
)
|
|
|
|
def _zls_toolchain_impl(ctx):
|
|
binary = ctx.file.bin
|
|
|
|
default_info = DefaultInfo(
|
|
files = depset([binary]),
|
|
runfiles = ctx.runfiles(files = [binary]),
|
|
)
|
|
zlstoolchaininfo = ZlsToolchainInfo(
|
|
bin = binary,
|
|
)
|
|
toolchain_info = platform_common.ToolchainInfo(
|
|
default = default_info,
|
|
zlstoolchaininfo = zlstoolchaininfo,
|
|
)
|
|
|
|
return [
|
|
default_info,
|
|
zlstoolchaininfo,
|
|
toolchain_info,
|
|
]
|
|
|
|
zls_toolchain = rule(
|
|
implementation = _zls_toolchain_impl,
|
|
attrs = {
|
|
"bin": attr.label(
|
|
executable = True,
|
|
allow_single_file = True,
|
|
cfg = "exec",
|
|
),
|
|
},
|
|
)
|
|
|
|
def _zls_toolchains_repos_impl(mctx):
|
|
for platform, _ in _ZLS_PLATFORMS.items():
|
|
http_archive(
|
|
name = "zls_{}".format(platform),
|
|
url = "https://github.com/zigtools/zls/releases/download/{version}/zls-{platform}.tar.xz".format(
|
|
version = _VERSION,
|
|
platform = platform,
|
|
),
|
|
sha256 = _ZLS_VERSIONS[platform],
|
|
build_file_content = """\
|
|
load("@zml//third_party/zls:zls_toolchain.bzl", "zls_toolchain")
|
|
zls_toolchain(name = "toolchain", bin = "zls")
|
|
""",
|
|
)
|
|
|
|
zls_toolchains_repo(name = "zls_toolchains")
|
|
|
|
return mctx.extension_metadata(
|
|
reproducible = True,
|
|
root_module_direct_deps = ["zls_toolchains"],
|
|
root_module_direct_dev_deps = [],
|
|
)
|
|
|
|
zls_toolchains = module_extension(
|
|
implementation = _zls_toolchains_repos_impl,
|
|
)
|
|
|
|
def _zls_toolchains_repo_impl(rctx):
|
|
build_content = """# @generated by @zml//third_party/zls:zls_toolchain.bzl"""
|
|
|
|
for platform, config in _ZLS_PLATFORMS.items():
|
|
build_content += """
|
|
toolchain(
|
|
name = "{platform}_toolchain",
|
|
exec_compatible_with = {compatible_with},
|
|
target_compatible_with = {compatible_with},
|
|
toolchain = "@zls_{platform}//:toolchain",
|
|
toolchain_type = "@zml//third_party/zls:toolchain_type",
|
|
)
|
|
""".format(
|
|
platform = platform,
|
|
compatible_with = config.compatible_with,
|
|
)
|
|
|
|
rctx.file("BUILD.bazel", build_content)
|
|
|
|
zls_toolchains_repo = repository_rule(
|
|
_zls_toolchains_repo_impl,
|
|
doc = """Creates a repository with toolchain definitions for all known platforms
|
|
which can be registered or selected.""",
|
|
)
|