Update Bazel build files and helper scripts to integrate the custom build runner for ZLS code completion.

This commit is contained in:
Tarry Singh 2023-11-20 15:29:01 +00:00
parent 6e4fef8844
commit ec37c8f731
6 changed files with 50 additions and 1876 deletions

View File

@ -2,6 +2,7 @@ load("@rules_zig//zig:defs.bzl", "zls_completion")
zls_completion(
name = "completion",
visibility = ["//visibility:public"],
deps = [
"//async",
"//stdx",

File diff suppressed because one or more lines are too long

View File

@ -1,14 +1,10 @@
load(":zls.bzl", "targets", "zig_runner", "zls_runner")
load(":zls.bzl", "targets", "zls_runner")
toolchain_type(name = "toolchain_type")
targets()
zig_runner(
name = "zig",
)
zls_runner(
name = "zls",
zig = ":zig",
target = "//:completion",
)

View File

@ -88,98 +88,69 @@ def targets():
toolchain_type = "@zml//third_party/zls:toolchain_type",
)
_ZIG_RUNNER_TPL = """\
#!/bin/bash
def build_runner_tpl(target):
return """\
const std = @import("std");
if [[ "${{1}}" == "build" ]]; then
for arg in "${{@:2}}"; do
if [[ "${{arg}}" == "-Dcmd="* ]]; then
cd ${{BUILD_WORKSPACE_DIRECTORY}}
exec ${{arg/-Dcmd=/}}
fi
done
fi
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();
export ZIG_GLOBAL_CACHE_DIR="$(realpath {zig_cache})"
export ZIG_LOCAL_CACHE_DIR="$(realpath {zig_cache})"
export ZIG_LIB_DIR="$(realpath {zig_lib_path})"
exec {zig_exe_path} "${{@}}"
"""
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))
_RUNNER_TPL = """\
def runner_tpl(zls, zig_exe_path, zig_lib_path, zig_cache, build_runner):
return """\
#!/bin/bash
set -eo pipefail
zig() {{
if [[ "${{1}}" == "build" ]]; then
for arg in "${{@:2}}"; do
if [[ "${{arg}}" == "-Dcmd="* ]]; then
cd ${{BUILD_WORKSPACE_DIRECTORY}}
exec ${{arg/-Dcmd=/}}
fi
done
fi
export ZIG_GLOBAL_CACHE_DIR="$(realpath {zig_cache})"
export ZIG_LOCAL_CACHE_DIR="$(realpath {zig_cache})"
export ZIG_LIB_DIR="$(realpath {zig_lib_path})"
exec {zig_exe_path} "${{@}}"
}}
zls() {{
json_config="$(mktemp)"
ZLS_ARGS=("--config-path" "${{json_config}}")
cat <<EOF > ${{json_config}}
json_config="$(mktemp)"
cat <<EOF > ${{json_config}}
{{
"zig_lib_path": "$(realpath {zig_lib_path})",
"build_runner_path": "$(realpath {build_runner})",
"global_cache_path": "$(realpath {zig_cache})",
"zig_exe_path": "$(realpath {zig_exe_path})",
"global_cache_path": "$(realpath {zig_cache})"
"zig_lib_path": "$(realpath {zig_lib_path})"
}}
EOF
while ((${{#}})); do
case "${{1}}" in
--config-path)
cat "${{2}}" >> "${{json_config}}"
{jq} -s add "${{json_config}}" > "${{json_config}}.tmp"
mv "${{json_config}}.tmp" "${{json_config}}"
shift 2
;;
*)
ZLS_ARGS+=("${{1}}")
shift
;;
esac
done
exec {zls} "${{ZLS_ARGS[@]}}"
}}
case $1 in
zig)
shift
zig "${{@}}"
;;
zls)
shift
zls "${{@}}"
;;
esac
"""
exec {zls} --config-path "${{json_config}}" "${{@}}"
""".format(
zig_lib_path = zig_lib_path,
zig_exe_path = zig_exe_path,
zig_cache = zig_cache,
zls = zls,
build_runner = build_runner,
)
def _zls_runner_impl(ctx):
jqinfo = ctx.toolchains["@aspect_bazel_lib//lib:jq_toolchain_type"].jqinfo
zigtoolchaininfo = ctx.toolchains["@rules_zig//zig:toolchain_type"].zigtoolchaininfo
zlsinfo = ctx.toolchains["@zml//third_party/zls:toolchain_type"].zlsinfo
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)))
zls_runner = ctx.actions.declare_file(ctx.label.name + ".zls_runner.sh")
ctx.actions.write(zls_runner, _RUNNER_TPL.format(
jq = jqinfo.bin.short_path,
ctx.actions.write(zls_runner, runner_tpl(
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,
build_runner = build_runner.short_path,
))
return [
@ -188,8 +159,7 @@ def _zls_runner_impl(ctx):
executable = zls_runner,
runfiles = ctx.runfiles(
files = [
ctx.executable.zig,
jqinfo.bin,
build_runner,
zlsinfo.bin,
],
transitive_files = zigtoolchaininfo.zig_files,
@ -200,39 +170,11 @@ def _zls_runner_impl(ctx):
zls_runner = rule(
implementation = _zls_runner_impl,
attrs = {
"zig": attr.label(mandatory = True, executable = True, cfg = "exec"),
"target": attr.label(mandatory = True, executable = True, cfg = "exec"),
},
executable = True,
toolchains = [
"@rules_zig//zig:toolchain_type",
"@aspect_bazel_lib//lib:jq_toolchain_type",
"@zml//third_party/zls:toolchain_type",
],
)
def _zig_runner_impl(ctx):
zigtoolchaininfo = ctx.toolchains["@rules_zig//zig:toolchain_type"].zigtoolchaininfo
zig_runner = ctx.actions.declare_file(ctx.label.name + ".zig_runner.sh")
ctx.actions.write(zig_runner, _ZIG_RUNNER_TPL.format(
zig_cache = zigtoolchaininfo.zig_cache,
zig_exe_path = zigtoolchaininfo.zig_exe.short_path,
zig_lib_path = zigtoolchaininfo.zig_lib.short_path,
))
return [
DefaultInfo(
files = depset([zig_runner]),
executable = zig_runner,
runfiles = ctx.runfiles(
files = [zig_runner],
transitive_files = zigtoolchaininfo.zig_files,
),
),
]
zig_runner = rule(
implementation = _zig_runner_impl,
executable = True,
toolchains = ["@rules_zig//zig:toolchain_type"],
)

View File

@ -1,3 +1,4 @@
#!/bin/bash
cd "$(dirname "${BASH_SOURCE[0]}")"
cd "$(bazel info workspace)"
exec bazel run -- @buildifier_prebuilt//:buildifier "$@"

View File

@ -1,3 +1,4 @@
#!/bin/bash
cd "$(dirname "${BASH_SOURCE[0]}")"
exec bazel run --config=silent @zml//third_party/zls:zls -- zls "${@}"
cd "$(bazel info workspace)"
exec bazel run -- @zml//third_party/zls:zls "${@}"