perf: exclude lsp when not needed

This commit is contained in:
Nurul Huda (Apon) 2026-03-23 02:08:25 +06:00
parent 54ebc94c94
commit 6e8ce553f4
No known key found for this signature in database
GPG Key ID: 5D3F1DE2855A2F79
3 changed files with 27 additions and 26 deletions

View File

@ -17,6 +17,7 @@ pub const plugins = buildlib.plugins;
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exclude_lsp = b.option(bool, "exclude-lsp", "Exclude the LSP server to speed up builds") orelse false;
// --- ZX Meta Options --- //
const options = b.addOptions();
@ -68,10 +69,15 @@ pub fn build(b: *std.Build) !void {
.imports = &.{
.{ .name = "zx", .module = mod },
.{ .name = "zli", .module = zli_dep.module("zli") },
.{ .name = "zls", .module = zls_dep.module("zls") },
},
};
const exe_build_options = b.addOptions();
exe_build_options.addOption(bool, "exclude_lsp", exclude_lsp);
const exe = b.addExecutable(.{ .name = "zx", .root_module = b.createModule(exe_rootmod_opts) });
exe.root_module.addOptions("build_options", exe_build_options);
if (!exclude_lsp) exe.root_module.addImport("zls", zls_dep.module("zls"));
b.installArtifact(exe);
// --- Steps: Run --- //

View File

@ -21,6 +21,12 @@ pub fn init(b: *std.Build, exe: *std.Build.Step.Compile, options: InitOptions) !
const zx_host_dep = b.dependencyFromBuildZig(build_zig, .{
.optimize = options.cli.optimize, // Always in release mode for faster transpilation
// No target = host target, so zx CLI can execute during build
.@"exclude-lsp" = true, // Skip LSP for faster build-time transpilation
});
// Full CLI dep (includes LSP) for the `zig build zx` step
const zx_full_dep = b.dependencyFromBuildZig(build_zig, .{
.optimize = options.cli.optimize,
});
const zx_wasm_dep = b.dependencyFromBuildZig(build_zig, .{
@ -31,6 +37,7 @@ pub fn init(b: *std.Build, exe: *std.Build.Step.Compile, options: InitOptions) !
const zx_module = zx_dep.module("zx");
const zx_wasm_module = zx_wasm_dep.module("zx_wasm");
const zx_exe = zx_host_dep.artifact("zx");
const zx_full_exe = zx_full_dep.artifact("zx");
const ziex_js_dep = zx_dep.builder.dependency("ziex_js", .{});
var opts: InitInnerOptions = .{
@ -56,7 +63,7 @@ pub fn init(b: *std.Build, exe: *std.Build.Step.Compile, options: InitOptions) !
opts.steps = cli_steps;
}
return initInner(b, exe, zx_exe, zx_module, zx_wasm_module, opts);
return initInner(b, exe, zx_exe, zx_full_exe, zx_module, zx_wasm_module, opts);
}
const InitInnerOptions = struct {
@ -97,6 +104,7 @@ pub fn initInner(
b: *std.Build,
exe: *std.Build.Step.Compile,
zx_exe: *std.Build.Step.Compile,
zx_full_exe: *std.Build.Step.Compile,
zx_module: *std.Build.Module,
zx_wasm_module: *std.Build.Module,
opts: InitInnerOptions,
@ -274,21 +282,8 @@ pub fn initInner(
"main.wasm",
);
// b.installDirectory(.{
// .source_dir = wasm_exe.getEmittedBinDirectory(),
// .install_dir = .prefix,
// .install_subdir = "static/assets",
// });
install_wasm.step.name = b.fmt("install {s} - client", .{exe.name});
const post_transpile_cmd = getZxRun(b, zx_exe, opts);
post_transpile_cmd.addArgs(&.{"transpile"});
post_transpile_cmd.addFileArg(wasm_binpath);
post_transpile_cmd.addArgs(&.{ "--copy-only", "--outdir" });
post_transpile_cmd.addDirectoryArg(transpile_outdir.path(b, "assets"));
post_transpile_cmd.expectExitCode(0);
post_transpile_cmd.step.dependOn(&transpile_cmd.step);
post_transpile_cmd.step.dependOn(&wasm_exe.step);
b.default_step.dependOn(&post_transpile_cmd.step);
b.default_step.dependOn(&install_wasm.step);
// --- Steps: ZX (Root of ZX CLI) --- //
@ -297,7 +292,7 @@ pub fn initInner(
"zx",
b.fmt("ZX CLI - \x1b[2m{s}\x1b[0m", .{"zig build zx -- <args>"}),
);
const zx_cmd = b.addRunArtifact(zx_exe);
const zx_cmd = b.addRunArtifact(zx_full_exe);
zx_step.dependOn(&zx_cmd.step);
if (b.args) |args| zx_cmd.addArgs(args);
}

View File

@ -11,14 +11,13 @@ pub fn main() !void {
defer if (builtin.mode == .Debug) std.debug.assert(dbg.deinit() == .ok);
var args = try std.process.argsWithAllocator(allocator);
defer args.deinit();
if (comptime (!build_options.exclude_lsp)) {
var args = try std.process.argsWithAllocator(allocator);
defer args.deinit();
_ = args.next();
const subcmd = args.next();
if (std.mem.eql(u8, subcmd orelse "", "lsp")) {
try lsp.main();
return;
_ = args.next();
const subcmd = args.next();
if (std.mem.eql(u8, subcmd orelse "", "lsp")) return try lsp.main();
}
if (builtin.os.tag == .wasi) return try main_wasm();
@ -91,11 +90,12 @@ fn main_wasm() !void {
}
const std = @import("std");
const cli = @import("cli/root.zig");
const builtin = @import("builtin");
const build_options = @import("build_options");
const zx = @import("zx");
const cli = @import("cli/root.zig");
const tui = @import("tui/main.zig");
const lsp = @import("lsp/main.zig");
const lsp = if (build_options.exclude_lsp) void else @import("lsp/main.zig");
pub const std_options = std.Options{
.log_scope_levels = &[_]std.log.ScopeLevel{