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( zls_completion(
name = "completion", name = "completion",
visibility = ["//visibility:public"],
deps = [ deps = [
"//async", "//async",
"//stdx", "//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") toolchain_type(name = "toolchain_type")
targets() targets()
zig_runner(
name = "zig",
)
zls_runner( zls_runner(
name = "zls", name = "zls",
zig = ":zig", target = "//:completion",
) )

View File

@ -88,98 +88,69 @@ def targets():
toolchain_type = "@zml//third_party/zls:toolchain_type", toolchain_type = "@zml//third_party/zls:toolchain_type",
) )
_ZIG_RUNNER_TPL = """\ def build_runner_tpl(target):
#!/bin/bash return """\
const std = @import("std");
if [[ "${{1}}" == "build" ]]; then pub fn main() !void {{
for arg in "${{@:2}}"; do var gpa: std.heap.GeneralPurposeAllocator(.{{}}) = .{{}};
if [[ "${{arg}}" == "-Dcmd="* ]]; then defer _ = gpa.deinit();
cd ${{BUILD_WORKSPACE_DIRECTORY}} var arena_ = std.heap.ArenaAllocator.init(gpa.allocator());
exec ${{arg/-Dcmd=/}} defer arena_.deinit();
fi const arena = arena_.allocator();
done
fi
export ZIG_GLOBAL_CACHE_DIR="$(realpath {zig_cache})" const build_workspace_directory = try std.process.getEnvVarOwned(arena, "BUILD_WORKSPACE_DIRECTORY");
export ZIG_LOCAL_CACHE_DIR="$(realpath {zig_cache})" var child = std.process.Child.init(&.{{
export ZIG_LIB_DIR="$(realpath {zig_lib_path})" "bazel",
exec {zig_exe_path} "${{@}}" "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 #!/bin/bash
set -eo pipefail 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)" json_config="$(mktemp)"
ZLS_ARGS=("--config-path" "${{json_config}}")
cat <<EOF > ${{json_config}} 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})", "zig_exe_path": "$(realpath {zig_exe_path})",
"global_cache_path": "$(realpath {zig_cache})" "zig_lib_path": "$(realpath {zig_lib_path})"
}} }}
EOF EOF
while ((${{#}})); do exec {zls} --config-path "${{json_config}}" "${{@}}"
case "${{1}}" in """.format(
--config-path) zig_lib_path = zig_lib_path,
cat "${{2}}" >> "${{json_config}}" zig_exe_path = zig_exe_path,
{jq} -s add "${{json_config}}" > "${{json_config}}.tmp" zig_cache = zig_cache,
mv "${{json_config}}.tmp" "${{json_config}}" zls = zls,
shift 2 build_runner = build_runner,
;; )
*)
ZLS_ARGS+=("${{1}}")
shift
;;
esac
done
exec {zls} "${{ZLS_ARGS[@]}}"
}}
case $1 in
zig)
shift
zig "${{@}}"
;;
zls)
shift
zls "${{@}}"
;;
esac
"""
def _zls_runner_impl(ctx): 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 zigtoolchaininfo = ctx.toolchains["@rules_zig//zig:toolchain_type"].zigtoolchaininfo
zlsinfo = ctx.toolchains["@zml//third_party/zls:toolchain_type"].zlsinfo 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") zls_runner = ctx.actions.declare_file(ctx.label.name + ".zls_runner.sh")
ctx.actions.write(zls_runner, _RUNNER_TPL.format( ctx.actions.write(zls_runner, runner_tpl(
jq = jqinfo.bin.short_path,
zig_cache = zigtoolchaininfo.zig_cache, zig_cache = zigtoolchaininfo.zig_cache,
zig_exe_path = zigtoolchaininfo.zig_exe.short_path, zig_exe_path = zigtoolchaininfo.zig_exe.short_path,
zig_lib_path = zigtoolchaininfo.zig_lib.short_path, zig_lib_path = zigtoolchaininfo.zig_lib.short_path,
zls = zlsinfo.bin.short_path, zls = zlsinfo.bin.short_path,
build_runner = build_runner.short_path,
)) ))
return [ return [
@ -188,8 +159,7 @@ def _zls_runner_impl(ctx):
executable = zls_runner, executable = zls_runner,
runfiles = ctx.runfiles( runfiles = ctx.runfiles(
files = [ files = [
ctx.executable.zig, build_runner,
jqinfo.bin,
zlsinfo.bin, zlsinfo.bin,
], ],
transitive_files = zigtoolchaininfo.zig_files, transitive_files = zigtoolchaininfo.zig_files,
@ -200,39 +170,11 @@ def _zls_runner_impl(ctx):
zls_runner = rule( zls_runner = rule(
implementation = _zls_runner_impl, implementation = _zls_runner_impl,
attrs = { attrs = {
"zig": attr.label(mandatory = True, executable = True, cfg = "exec"), "target": attr.label(mandatory = True, executable = True, cfg = "exec"),
}, },
executable = True, executable = True,
toolchains = [ toolchains = [
"@rules_zig//zig:toolchain_type", "@rules_zig//zig:toolchain_type",
"@aspect_bazel_lib//lib:jq_toolchain_type",
"@zml//third_party/zls: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 #!/bin/bash
cd "$(dirname "${BASH_SOURCE[0]}")" cd "$(dirname "${BASH_SOURCE[0]}")"
cd "$(bazel info workspace)"
exec bazel run -- @buildifier_prebuilt//:buildifier "$@" exec bazel run -- @buildifier_prebuilt//:buildifier "$@"

View File

@ -1,3 +1,4 @@
#!/bin/bash #!/bin/bash
cd "$(dirname "${BASH_SOURCE[0]}")" 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 "${@}"