2023-01-02 14:28:25 +00:00
|
|
|
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
|
|
|
|
|
2024-07-02 14:19:04 +00:00
|
|
|
_VERSION = "0.14.0"
|
2023-01-02 14:28:25 +00:00
|
|
|
|
|
|
|
|
_ARCH = {
|
|
|
|
|
"x86_64-linux": struct(
|
2024-07-02 14:19:04 +00:00
|
|
|
sha256 = "661f8d402ba3dc9b04b6e9bc3026495be7b838d2f18d148db2bd98bd699c1360",
|
2023-01-02 14:28:25 +00:00
|
|
|
exec_compatible_with = [
|
|
|
|
|
"@platforms//os:linux",
|
|
|
|
|
"@platforms//cpu:x86_64",
|
|
|
|
|
],
|
|
|
|
|
),
|
2024-07-25 15:58:14 +00:00
|
|
|
"x86_64-macos": struct(
|
|
|
|
|
sha256 = "4b63854d6b76810abd2563706e7d768efc7111e44dd8b371d49198e627697a13",
|
|
|
|
|
exec_compatible_with = [
|
|
|
|
|
"@platforms//os:macos",
|
|
|
|
|
"@platforms//cpu:x86_64",
|
|
|
|
|
],
|
|
|
|
|
),
|
2023-01-02 14:28:25 +00:00
|
|
|
"aarch64-macos": struct(
|
2024-07-02 14:19:04 +00:00
|
|
|
sha256 = "dfb627e1f9603583678f552d8035a12dce878215c0a507b32d6f1b9d074d6c4d",
|
2023-01-02 14:28:25 +00:00
|
|
|
exec_compatible_with = [
|
|
|
|
|
"@platforms//os:macos",
|
|
|
|
|
"@platforms//cpu:aarch64",
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ZlsInfo = provider(
|
|
|
|
|
fields = {
|
|
|
|
|
"bin": "ZLS binary",
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _zls_toolchain_impl(ctx):
|
|
|
|
|
default = DefaultInfo(
|
|
|
|
|
files = depset(direct = [ctx.file.zls]),
|
|
|
|
|
)
|
|
|
|
|
zlsinfo = ZlsInfo(
|
|
|
|
|
bin = ctx.file.zls,
|
|
|
|
|
)
|
|
|
|
|
toolchain_info = platform_common.ToolchainInfo(
|
|
|
|
|
default = default,
|
|
|
|
|
zlsinfo = zlsinfo,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
default,
|
|
|
|
|
zlsinfo,
|
|
|
|
|
toolchain_info,
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
zls_toolchain = rule(
|
|
|
|
|
implementation = _zls_toolchain_impl,
|
|
|
|
|
attrs = {
|
|
|
|
|
"zls": attr.label(
|
|
|
|
|
executable = True,
|
|
|
|
|
allow_single_file = True,
|
|
|
|
|
cfg = "exec",
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _repo_impl(mctx):
|
|
|
|
|
for arch, config in _ARCH.items():
|
|
|
|
|
http_archive(
|
|
|
|
|
name = "zls_{}".format(arch),
|
|
|
|
|
url = "https://github.com/zigtools/zls/releases/download/{version}/zls-{arch}.tar.xz".format(
|
|
|
|
|
version = _VERSION,
|
|
|
|
|
arch = arch,
|
|
|
|
|
),
|
|
|
|
|
sha256 = config.sha256,
|
|
|
|
|
build_file_content = """\
|
|
|
|
|
load("@zml//third_party/zls:zls.bzl", "zls_toolchain")
|
|
|
|
|
zls_toolchain(name = "toolchain", zls = "zls")
|
|
|
|
|
""",
|
|
|
|
|
)
|
|
|
|
|
return mctx.extension_metadata(
|
|
|
|
|
reproducible = True,
|
|
|
|
|
root_module_direct_deps = "all",
|
|
|
|
|
root_module_direct_dev_deps = [],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
repo = module_extension(
|
|
|
|
|
implementation = _repo_impl,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def targets():
|
|
|
|
|
for arch, config in _ARCH.items():
|
|
|
|
|
native.toolchain(
|
|
|
|
|
name = "toolchain_{}".format(arch),
|
|
|
|
|
exec_compatible_with = config.exec_compatible_with,
|
|
|
|
|
target_compatible_with = config.exec_compatible_with,
|
|
|
|
|
toolchain = "@zls_{}//:toolchain".format(arch),
|
|
|
|
|
toolchain_type = "@zml//third_party/zls:toolchain_type",
|
|
|
|
|
)
|
|
|
|
|
|
2023-11-20 15:29:01 +00:00
|
|
|
def build_runner_tpl(target):
|
|
|
|
|
return """\
|
|
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
|
|
pub fn main() !void {{
|
|
|
|
|
var gpa: std.heap.GeneralPurposeAllocator(.{{}}) = .{{}};
|
|
|
|
|
defer _ = gpa.deinit();
|
|
|
|
|
var arena_ = std.heap.ArenaAllocator.init(gpa.allocator());
|
|
|
|
|
defer arena_.deinit();
|
|
|
|
|
const arena = arena_.allocator();
|
|
|
|
|
|
|
|
|
|
const build_workspace_directory = try std.process.getEnvVarOwned(arena, "BUILD_WORKSPACE_DIRECTORY");
|
|
|
|
|
var child = std.process.Child.init(&.{{
|
|
|
|
|
"bazel",
|
|
|
|
|
"run",
|
|
|
|
|
{target},
|
|
|
|
|
}}, arena);
|
|
|
|
|
child.stdin_behavior = .Ignore;
|
|
|
|
|
child.stdout_behavior = .Inherit;
|
|
|
|
|
child.stderr_behavior = .Inherit;
|
|
|
|
|
child.cwd = build_workspace_directory;
|
|
|
|
|
_ = try child.spawnAndWait();
|
|
|
|
|
}}
|
|
|
|
|
""".format(target = repr(target))
|
2023-01-02 14:28:25 +00:00
|
|
|
|
2023-11-20 15:29:01 +00:00
|
|
|
def runner_tpl(zls, zig_exe_path, zig_lib_path, zig_cache, build_runner):
|
|
|
|
|
return """\
|
2023-01-02 14:28:25 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
set -eo pipefail
|
|
|
|
|
|
2023-11-20 15:29:01 +00:00
|
|
|
json_config="$(mktemp)"
|
|
|
|
|
cat <<EOF > ${{json_config}}
|
2023-01-02 14:28:25 +00:00
|
|
|
{{
|
2023-11-20 15:29:01 +00:00
|
|
|
"build_runner_path": "$(realpath {build_runner})",
|
|
|
|
|
"global_cache_path": "$(realpath {zig_cache})",
|
2023-01-02 14:28:25 +00:00
|
|
|
"zig_exe_path": "$(realpath {zig_exe_path})",
|
2023-11-20 15:29:01 +00:00
|
|
|
"zig_lib_path": "$(realpath {zig_lib_path})"
|
2023-01-02 14:28:25 +00:00
|
|
|
}}
|
|
|
|
|
EOF
|
|
|
|
|
|
2024-07-10 15:20:12 +00:00
|
|
|
exec {zls} "${{@}}" --config-path "${{json_config}}"
|
2023-11-20 15:29:01 +00:00
|
|
|
""".format(
|
|
|
|
|
zig_lib_path = zig_lib_path,
|
|
|
|
|
zig_exe_path = zig_exe_path,
|
|
|
|
|
zig_cache = zig_cache,
|
|
|
|
|
zls = zls,
|
|
|
|
|
build_runner = build_runner,
|
|
|
|
|
)
|
2023-01-02 14:28:25 +00:00
|
|
|
|
|
|
|
|
def _zls_runner_impl(ctx):
|
|
|
|
|
zigtoolchaininfo = ctx.toolchains["@rules_zig//zig:toolchain_type"].zigtoolchaininfo
|
|
|
|
|
zlsinfo = ctx.toolchains["@zml//third_party/zls:toolchain_type"].zlsinfo
|
|
|
|
|
|
2023-11-20 15:29:01 +00:00
|
|
|
build_runner = ctx.actions.declare_file(ctx.label.name + ".build_runner.zig")
|
|
|
|
|
ctx.actions.write(build_runner, build_runner_tpl(str(ctx.attr.target.label)))
|
|
|
|
|
|
2023-01-02 14:28:25 +00:00
|
|
|
zls_runner = ctx.actions.declare_file(ctx.label.name + ".zls_runner.sh")
|
2023-11-20 15:29:01 +00:00
|
|
|
ctx.actions.write(zls_runner, runner_tpl(
|
2023-01-02 14:28:25 +00:00
|
|
|
zig_cache = zigtoolchaininfo.zig_cache,
|
|
|
|
|
zig_exe_path = zigtoolchaininfo.zig_exe.short_path,
|
|
|
|
|
zig_lib_path = zigtoolchaininfo.zig_lib.short_path,
|
|
|
|
|
zls = zlsinfo.bin.short_path,
|
2023-11-20 15:29:01 +00:00
|
|
|
build_runner = build_runner.short_path,
|
2023-01-02 14:28:25 +00:00
|
|
|
))
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
DefaultInfo(
|
|
|
|
|
files = depset([zls_runner]),
|
|
|
|
|
executable = zls_runner,
|
|
|
|
|
runfiles = ctx.runfiles(
|
|
|
|
|
files = [
|
2023-11-20 15:29:01 +00:00
|
|
|
build_runner,
|
2023-01-02 14:28:25 +00:00
|
|
|
zlsinfo.bin,
|
|
|
|
|
],
|
|
|
|
|
transitive_files = zigtoolchaininfo.zig_files,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
zls_runner = rule(
|
|
|
|
|
implementation = _zls_runner_impl,
|
|
|
|
|
attrs = {
|
2023-11-20 15:29:01 +00:00
|
|
|
"target": attr.label(mandatory = True, executable = True, cfg = "exec"),
|
2023-01-02 14:28:25 +00:00
|
|
|
},
|
|
|
|
|
executable = True,
|
|
|
|
|
toolchains = [
|
|
|
|
|
"@rules_zig//zig:toolchain_type",
|
|
|
|
|
"@zml//third_party/zls:toolchain_type",
|
|
|
|
|
],
|
|
|
|
|
)
|