Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to new module system #1698

Merged
merged 4 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ const builtin = @import("builtin");
const zls_version = std.SemanticVersion{ .major = 0, .minor = 12, .patch = 0 };

/// document the latest breaking change that caused a change to the string below:
/// 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 }));
}
/// build/LazyPath: Add dirname (#18371)
const min_zig_string = "0.12.0-dev.2046+d3a163f86";

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
8 changes: 4 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

.dependencies = .{
.known_folders = .{
.url = "https://github.com/ziglibs/known-folders/archive/8bb4bf761bab20ffed74ffac83c3a9eb4456f28d.tar.gz",
.hash = "122002462f37b67a54fa1ab712d870d4637dae0d94437c0077b148e1fb75616c573d",
.url = "https://github.com/ziglibs/known-folders/archive/806ba01b872820004c7dec3117cb0db66b206af6.tar.gz",
.hash = "122094b48dea08e241b48d0ec32e3df6965e814091e3cabaff942e62d053f3ff9b5f",
},
.diffz = .{
.url = "https://github.com/ziglibs/diffz/archive/77ccba4b4ab2d5a89b2e1fe6cfe4ce7bb7361efb.tar.gz",
.hash = "122020da93490cefb22ff4b6a2a814d7572ef042e38b17f841d7195b92d69ff3be93",
.url = "https://github.com/ziglibs/diffz/archive/e10bf15962e45affb3fcd7d9a950977a69c901b3.tar.gz",
.hash = "12200d71e4b7029ea56a429e24260c6c0e85a3069b0d4ba85eace21a0fd75910aa64",
},
},
.paths = .{""},
Expand Down
12 changes: 6 additions & 6 deletions deps.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

linkFarm "zig-packages" [
{
name = "122002462f37b67a54fa1ab712d870d4637dae0d94437c0077b148e1fb75616c573d";
name = "12200d71e4b7029ea56a429e24260c6c0e85a3069b0d4ba85eace21a0fd75910aa64";
path = fetchzip {
url = "https://github.com/ziglibs/known-folders/archive/8bb4bf761bab20ffed74ffac83c3a9eb4456f28d.tar.gz";
hash = "sha256-t5nZi7HmBy0tLBYphlyrPHTSr5HStobgdZbISi5hjno=";
url = "https://github.com/ziglibs/diffz/archive/e10bf15962e45affb3fcd7d9a950977a69c901b3.tar.gz";
hash = "sha256-yVFPVn4jGfcoE2V4xdTqdThYPutshL6U4feDzetWgFw=";
};
}
{
name = "122020da93490cefb22ff4b6a2a814d7572ef042e38b17f841d7195b92d69ff3be93";
name = "122094b48dea08e241b48d0ec32e3df6965e814091e3cabaff942e62d053f3ff9b5f";
path = fetchzip {
url = "https://github.com/ziglibs/diffz/archive/77ccba4b4ab2d5a89b2e1fe6cfe4ce7bb7361efb.tar.gz";
hash = "sha256-11y9tETADYRPAbpsO2iL7zeV4n8FOz3hkpnEsl/x0xA=";
url = "https://github.com/ziglibs/known-folders/archive/806ba01b872820004c7dec3117cb0db66b206af6.tar.gz";
hash = "sha256-4sKYjNRTr2XAFoh3w/6zUnEqTtXgcf4omUlHZeV+tcU=";
};
}
]
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 @@
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(.{});

Check warning on line 1293 in src/DocumentStore.zig

View check run for this annotation

Codecov / codecov/patch

src/DocumentStore.zig#L1293

Added line #L1293 was not covered by tests
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
32 changes: 23 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 Expand Up @@ -384,6 +390,14 @@ const copied_from_zig = struct {
return null;
}
},
.generated_dirname => |gen| {
var dirname = getPath(.{ .generated = gen.generated }, builder) orelse return null;
var i: usize = 0;
while (i <= gen.up) : (i += 1) {
dirname = std.fs.path.dirname(dirname) orelse return null;
}
return dirname;
},
.dependency => |dep| return dep.dependency.builder.pathJoin(&[_][]const u8{
dep.dependency.builder.build_root.path.?,
dep.sub_path,
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