Skip to content

Commit

Permalink
update to new module system
Browse files Browse the repository at this point in the history
  • Loading branch information
xdBronch authored and Techatrix committed Jan 5, 2024
1 parent 5c0bebe commit 7dd6ffd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 43 deletions.
50 changes: 26 additions & 24 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ const zls_version = std.SemanticVersion{ .major = 0, .minor = 12, .patch = 0 };
/// Remove all usages of `std.mem.copy` and remove `std.mem.set` (#18143)
const min_zig_string = "0.12.0-dev.1767+1e42a3de89";

pub fn build(b: *std.build.Builder) !void {
comptime {
const current_zig = builtin.zig_version;
const min_zig = std.SemanticVersion.parse(min_zig_string) catch unreachable;
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 }));
}
const Build = blk: {
const current_zig = builtin.zig_version;
const min_zig = std.SemanticVersion.parse(min_zig_string) catch unreachable;
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 }));
}
break :blk std.Build;
};

pub fn build(b: *Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

Expand Down Expand Up @@ -110,15 +111,15 @@ pub fn build(b: *std.build.Builder) !void {
exe.pie = pie;
b.installArtifact(exe);

exe.addModule("build_options", exe_options_module);
exe.addModule("known-folders", known_folders_module);
exe.addModule("diffz", diffz_module);
exe.root_module.addImport("build_options", exe_options_module);
exe.root_module.addImport("known-folders", known_folders_module);
exe.root_module.addImport("diffz", diffz_module);

if (enable_tracy) {
const client_cpp = "src/tracy/public/TracyClient.cpp";

// On mingw, we need to opt into windows 7+ to get some features required by tracy.
const tracy_c_flags: []const []const u8 = if (target.isWindows() and target.getAbi() == .gnu)
const tracy_c_flags: []const []const u8 = if (target.result.isMinGW())
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined", "-D_WIN32_WINNT=0x601" }
else
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" };
Expand All @@ -131,7 +132,7 @@ pub fn build(b: *std.build.Builder) !void {
exe.linkLibCpp();
exe.linkLibC();

if (target.isWindows()) {
if (target.result.os.tag == .windows) {
exe.linkSystemLibrary("dbghelp");
exe.linkSystemLibrary("ws2_32");
}
Expand All @@ -140,6 +141,7 @@ pub fn build(b: *std.build.Builder) !void {
const gen_exe = b.addExecutable(.{
.name = "zls_gen",
.root_source_file = .{ .path = "src/config_gen/config_gen.zig" },
.target = b.host,
.single_threaded = true,
});

Expand Down Expand Up @@ -172,16 +174,16 @@ pub fn build(b: *std.build.Builder) !void {
break :blk b.fmt("version_data_{s}_{d}.zig", .{ data_version, timestamp });
};
gen_version_data_cmd.addArg("--generate-version-data-path");
const version_data_path: std.build.LazyPath = if (override_version_data_file_path) |path|
const version_data_path: std.Build.LazyPath = if (override_version_data_file_path) |path|
.{ .cwd_relative = path }
else
gen_version_data_cmd.addOutputFileArg(version_data_file_name);
const version_data_module = b.addModule("version_data", .{ .source_file = version_data_path });
exe.addModule("version_data", version_data_module);
const version_data_module = b.addModule("version_data", .{ .root_source_file = version_data_path });
exe.root_module.addImport("version_data", version_data_module);

const zls_module = b.addModule("zls", .{
.source_file = .{ .path = "src/zls.zig" },
.dependencies = &.{
.root_source_file = .{ .path = "src/zls.zig" },
.imports = &.{
.{ .name = "known-folders", .module = known_folders_module },
.{ .name = "diffz", .module = diffz_module },
.{ .name = "build_options", .module = build_options_module },
Expand All @@ -202,9 +204,9 @@ pub fn build(b: *std.build.Builder) !void {
tests.use_llvm = use_llvm;
tests.use_lld = use_llvm;

tests.addModule("zls", zls_module);
tests.addModule("build_options", build_options_module);
tests.addModule("test_options", test_options_module);
tests.root_module.addImport("zls", zls_module);
tests.root_module.addImport("build_options", build_options_module);
tests.root_module.addImport("test_options", test_options_module);
test_step.dependOn(&b.addRunArtifact(tests).step);

var src_tests = b.addTest(.{
Expand All @@ -216,14 +218,14 @@ pub fn build(b: *std.build.Builder) !void {
});
src_tests.use_llvm = use_llvm;
src_tests.use_lld = use_llvm;
src_tests.addModule("build_options", build_options_module);
src_tests.addModule("test_options", test_options_module);
src_tests.root_module.addImport("build_options", build_options_module);
src_tests.root_module.addImport("test_options", test_options_module);
test_step.dependOn(&b.addRunArtifact(src_tests).step);

if (coverage) {
const include_pattern = b.fmt("--include-pattern=/src", .{});
const exclude_pattern = b.fmt("--exclude-pattern=/src/stage2", .{});
const args = &[_]std.build.RunStep.Arg{
const args = &[_]std.Build.Step.Run.Arg{
.{ .bytes = b.dupe("kcov") },
.{ .bytes = b.dupe("--collect-only") },
.{ .bytes = b.dupe(include_pattern) },
Expand All @@ -239,7 +241,7 @@ pub fn build(b: *std.build.Builder) !void {
tests_run.argv.insertSlice(0, args) catch @panic("OOM");
src_tests_run.argv.insertSlice(0, args) catch @panic("OOM");

var merge_step = std.build.RunStep.create(b, "merge kcov");
var merge_step = std.Build.Step.Run.create(b, "merge kcov");
merge_step.has_side_effects = true;
merge_step.addArgs(&.{
"kcov",
Expand Down
2 changes: 1 addition & 1 deletion src/DocumentStore.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ pub fn collectIncludeDirs(
var arena_allocator = std.heap.ArenaAllocator.init(allocator);
defer arena_allocator.deinit();

const target_info = try std.zig.system.NativeTargetInfo.detect(.{});
const target_info = try std.zig.system.resolveTargetQuery(.{});
const native_paths = try std.zig.system.NativePaths.detect(arena_allocator.allocator(), target_info);

try include_dirs.ensureUnusedCapacity(allocator, native_paths.include_dirs.items.len);
Expand Down
24 changes: 15 additions & 9 deletions src/build_runner/master.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ pub fn main() !void {
cache.addPrefix(global_cache_directory);
cache.hash.addBytes(builtin.zig_version_string);

const host = try std.zig.system.NativeTargetInfo.detect(.{});
const host: std.Build.ResolvedTarget = .{
.query = .{},
.result = try std.zig.system.resolveTargetQuery(.{}),
};

const builder = try Build.create(
allocator,
Expand Down Expand Up @@ -247,36 +250,39 @@ fn processStep(
return;
};

if (exe.root_src) |src| {
if (exe.root_module.root_source_file) |src| {
if (copied_from_zig.getPath(src, builder)) |path| {
_ = try packages.addPackage("root", path);
}
}
try processIncludeDirs(builder, include_dirs, exe.include_dirs.items);
try processIncludeDirs(builder, include_dirs, exe.root_module.include_dirs.items);
try processPkgConfig(builder.allocator, include_dirs, exe);
try processModules(builder, packages, exe.modules);
try processModules(builder, packages, exe.root_module.import_table);
}

fn processModules(
builder: *Build,
packages: *Packages,
modules: std.StringArrayHashMap(*Build.Module),
modules: std.StringArrayHashMapUnmanaged(*Build.Module),
) !void {
for (modules.keys(), modules.values()) |name, mod| {
const path = copied_from_zig.getPath(mod.source_file, mod.builder) orelse continue;
const path = copied_from_zig.getPath(
mod.root_source_file orelse continue,
mod.owner,
) orelse continue;

const already_added = try packages.addPackage(name, path);
// if the package has already been added short circuit here or recursive modules will ruin us
if (already_added) continue;

try processModules(builder, packages, mod.dependencies);
try processModules(builder, packages, mod.import_table);
}
}

fn processIncludeDirs(
builder: *Build,
include_dirs: *std.StringArrayHashMapUnmanaged(void),
dirs: []Build.Step.Compile.IncludeDir,
dirs: []Build.Module.IncludeDir,
) !void {
for (dirs) |dir| {
switch (dir) {
Expand Down Expand Up @@ -312,7 +318,7 @@ fn processPkgConfig(
include_dirs: *std.StringArrayHashMapUnmanaged(void),
exe: *Build.Step.Compile,
) !void {
for (exe.link_objects.items) |link_object| {
for (exe.root_module.link_objects.items) |link_object| {
if (link_object != .system_lib) continue;
const system_lib = link_object.system_lib;

Expand Down
19 changes: 10 additions & 9 deletions tests/utility/offsets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const zls = @import("zls");

const types = zls.types;
const offsets = zls.offsets;
const Loc = offsets.Loc;

const Ast = std.zig.Ast;

Expand Down Expand Up @@ -130,7 +131,7 @@ test "offsets - convertPositionEncoding" {
try testConvertPositionEncoding("a¶↉🠁\na¶↉🠁", 1, 6, .{ 6, 3, 3 });
}
test "offsets - locIntersect" {
const a = offsets.Loc{ .start = 2, .end = 5 };
const a = Loc{ .start = 2, .end = 5 };
try std.testing.expect(offsets.locIntersect(a, .{ .start = 0, .end = 2 }) == false);
try std.testing.expect(offsets.locIntersect(a, .{ .start = 1, .end = 3 }) == true);
try std.testing.expect(offsets.locIntersect(a, .{ .start = 2, .end = 4 }) == true);
Expand All @@ -140,7 +141,7 @@ test "offsets - locIntersect" {
}

test "offsets - locInside" {
const outer = offsets.Loc{ .start = 2, .end = 5 };
const outer = Loc{ .start = 2, .end = 5 };
try std.testing.expect(offsets.locInside(.{ .start = 0, .end = 2 }, outer) == false);
try std.testing.expect(offsets.locInside(.{ .start = 1, .end = 3 }, outer) == false);
try std.testing.expect(offsets.locInside(.{ .start = 2, .end = 4 }, outer) == true);
Expand All @@ -150,13 +151,13 @@ test "offsets - locInside" {
}

test "offsets - locMerge" {
const a = offsets.Loc{ .start = 2, .end = 5 };
try std.testing.expectEqualDeep(offsets.locMerge(a, .{ .start = 0, .end = 2 }), .{ .start = 0, .end = 5 });
try std.testing.expectEqualDeep(offsets.locMerge(a, .{ .start = 1, .end = 3 }), .{ .start = 1, .end = 5 });
try std.testing.expectEqualDeep(offsets.locMerge(a, .{ .start = 2, .end = 4 }), .{ .start = 2, .end = 5 });
try std.testing.expectEqualDeep(offsets.locMerge(a, .{ .start = 3, .end = 5 }), .{ .start = 2, .end = 5 });
try std.testing.expectEqualDeep(offsets.locMerge(a, .{ .start = 4, .end = 6 }), .{ .start = 2, .end = 6 });
try std.testing.expectEqualDeep(offsets.locMerge(a, .{ .start = 5, .end = 7 }), .{ .start = 2, .end = 7 });
const a = Loc{ .start = 2, .end = 5 };
try std.testing.expectEqualDeep(offsets.locMerge(a, .{ .start = 0, .end = 2 }), Loc{ .start = 0, .end = 5 });
try std.testing.expectEqualDeep(offsets.locMerge(a, .{ .start = 1, .end = 3 }), Loc{ .start = 1, .end = 5 });
try std.testing.expectEqualDeep(offsets.locMerge(a, .{ .start = 2, .end = 4 }), Loc{ .start = 2, .end = 5 });
try std.testing.expectEqualDeep(offsets.locMerge(a, .{ .start = 3, .end = 5 }), Loc{ .start = 2, .end = 5 });
try std.testing.expectEqualDeep(offsets.locMerge(a, .{ .start = 4, .end = 6 }), Loc{ .start = 2, .end = 6 });
try std.testing.expectEqualDeep(offsets.locMerge(a, .{ .start = 5, .end = 7 }), Loc{ .start = 2, .end = 7 });
}

test "offsets - advancePosition" {
Expand Down

0 comments on commit 7dd6ffd

Please sign in to comment.