Work in Zig's breaking changes (build sys apis) (#955)

This commit is contained in:
nullptrdevs 2023-02-03 14:06:57 -08:00 committed by GitHub
parent 75f0617279
commit 6297536d7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 27 deletions

View File

@ -7,15 +7,22 @@ const zls_version = std.builtin.Version{ .major = 0, .minor = 11, .patch = 0 };
pub fn build(b: *std.build.Builder) !void { pub fn build(b: *std.build.Builder) !void {
comptime { comptime {
const current_zig = builtin.zig_version; const current_zig = builtin.zig_version;
const min_zig = std.SemanticVersion.parse("0.11.0-dev.1254+1f8f79cd5") catch return; // add helper functions to std.zig.Ast const min_zig = std.SemanticVersion.parse("0.11.0-dev.1524+efa25e7d5") catch return; // build API changes
if (current_zig.order(min_zig) == .lt) { if (current_zig.order(min_zig) == .lt) {
@compileError(std.fmt.comptimePrint("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig })); @compileError(std.fmt.comptimePrint("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig }));
} }
} }
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions(); const target = b.standardTargetOptions(.{});
const exe = b.addExecutable("zls", "src/main.zig"); const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "zls",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const exe_options = b.addOptions(); const exe_options = b.addOptions();
exe.addOptions("build_options", exe_options); exe.addOptions("build_options", exe_options);
@ -105,11 +112,17 @@ pub fn build(b: *std.build.Builder) !void {
const KNOWN_FOLDERS_DEFAULT_PATH = "src/known-folders/known-folders.zig"; const KNOWN_FOLDERS_DEFAULT_PATH = "src/known-folders/known-folders.zig";
const known_folders_path = b.option([]const u8, "known-folders", "Path to known-folders package (default: " ++ KNOWN_FOLDERS_DEFAULT_PATH ++ ")") orelse KNOWN_FOLDERS_DEFAULT_PATH; const known_folders_path = b.option([]const u8, "known-folders", "Path to known-folders package (default: " ++ KNOWN_FOLDERS_DEFAULT_PATH ++ ")") orelse KNOWN_FOLDERS_DEFAULT_PATH;
exe.addPackage(.{ .name = "known-folders", .source = .{ .path = known_folders_path } }); exe.addPackage(.{
.name = "known-folders",
.source = .{ .path = known_folders_path },
});
const TRES_DEFAULT_PATH = "src/tres/tres.zig"; const TRES_DEFAULT_PATH = "src/tres/tres.zig";
const tres_path = b.option([]const u8, "tres", "Path to tres package (default: " ++ TRES_DEFAULT_PATH ++ ")") orelse TRES_DEFAULT_PATH; const tres_path = b.option([]const u8, "tres", "Path to tres package (default: " ++ TRES_DEFAULT_PATH ++ ")") orelse TRES_DEFAULT_PATH;
exe.addPackage(.{ .name = "tres", .source = .{ .path = tres_path } }); exe.addPackage(.{
.name = "tres",
.source = .{ .path = tres_path },
});
const check_submodules_step = CheckSubmodulesStep.init(b, &.{ const check_submodules_step = CheckSubmodulesStep.init(b, &.{
known_folders_path, known_folders_path,
@ -137,12 +150,16 @@ pub fn build(b: *std.build.Builder) !void {
} }
} }
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install(); exe.install();
const gen_exe = b.addExecutable("zls_gen", "src/config_gen/config_gen.zig"); const gen_exe = b.addExecutable(.{
gen_exe.addPackage(.{ .name = "tres", .source = .{ .path = tres_path } }); .name = "zls_gen",
.root_source_file = .{ .path = "src/config_gen/config_gen.zig" },
});
gen_exe.addPackage(.{
.name = "tres",
.source = .{ .path = tres_path },
});
const gen_cmd = gen_exe.run(); const gen_cmd = gen_exe.run();
gen_cmd.addArgs(&.{ gen_cmd.addArgs(&.{
@ -160,7 +177,12 @@ pub fn build(b: *std.build.Builder) !void {
const test_step = b.step("test", "Run all the tests"); const test_step = b.step("test", "Run all the tests");
test_step.dependOn(b.getInstallStep()); test_step.dependOn(b.getInstallStep());
var tests = b.addTest("tests/tests.zig"); var tests = b.addTest(.{
.root_source_file = .{ .path = "tests/tests.zig" },
.target = target,
.optimize = .Debug,
});
tests.setFilter(b.option( tests.setFilter(b.option(
[]const u8, []const u8,
"test-filter", "test-filter",
@ -179,29 +201,34 @@ pub fn build(b: *std.build.Builder) !void {
}); });
} }
tests.addPackage(.{ .name = "zls", .source = .{ .path = "src/zls.zig" }, .dependencies = exe.packages.items }); tests.addPackage(.{
tests.addPackage(.{ .name = "tres", .source = .{ .path = tres_path } }); .name = "zls",
tests.setBuildMode(.Debug); .source = .{ .path = "src/zls.zig" },
tests.setTarget(target); .dependencies = exe.packages.items,
});
tests.addPackage(.{
.name = "tres",
.source = .{ .path = tres_path },
});
test_step.dependOn(&tests.step); test_step.dependOn(&tests.step);
} }
const CheckSubmodulesStep = struct { const CheckSubmodulesStep = struct {
step: std.build.Step, step: std.Build.Step,
builder: *std.build.Builder, builder: *std.Build,
submodules: []const []const u8, submodules: []const []const u8,
pub fn init(builder: *std.build.Builder, submodules: []const []const u8) *CheckSubmodulesStep { pub fn init(builder: *std.Build, submodules: []const []const u8) *CheckSubmodulesStep {
var self = builder.allocator.create(CheckSubmodulesStep) catch unreachable; var self = builder.allocator.create(CheckSubmodulesStep) catch unreachable;
self.* = CheckSubmodulesStep{ self.* = CheckSubmodulesStep{
.builder = builder, .builder = builder,
.step = std.build.Step.init(.custom, "Check Submodules", builder.allocator, make), .step = std.Build.Step.init(.custom, "Check Submodules", builder.allocator, make),
.submodules = builder.allocator.dupe([]const u8, submodules) catch unreachable, .submodules = builder.allocator.dupe([]const u8, submodules) catch unreachable,
}; };
return self; return self;
} }
fn make(step: *std.build.Step) anyerror!void { fn make(step: *std.Build.Step) anyerror!void {
const self = @fieldParentPtr(CheckSubmodulesStep, "step", step); const self = @fieldParentPtr(CheckSubmodulesStep, "step", step);
for (self.submodules) |path| { for (self.submodules) |path| {
const access = std.fs.accessAbsolute(self.builder.pathFromRoot(path), .{}); const access = std.fs.accessAbsolute(self.builder.pathFromRoot(path), .{});

View File

@ -48,13 +48,26 @@ pub fn main() !void {
return error.InvalidArgs; return error.InvalidArgs;
}; };
const builder = try Builder.create( const builder = blk: {
// Zig 0.11.0-dev.1524+
if (@hasDecl(std, "Build")) {
const host = try std.zig.system.NativeTargetInfo.detect(.{});
break :blk try Builder.create(
allocator,
zig_exe,
build_root,
cache_root,
global_cache_root,
host,
);
} else break :blk try Builder.create(
allocator, allocator,
zig_exe, zig_exe,
build_root, build_root,
cache_root, cache_root,
global_cache_root, global_cache_root,
); );
};
defer builder.destroy(); defer builder.destroy();