zig build system changes (modules) (#976)

This commit is contained in:
nullptrdevs 2023-02-04 23:15:42 -08:00 committed by GitHub
parent 0ad2009b00
commit 62068ae828
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 25 deletions

View File

@ -7,7 +7,7 @@ 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.1567+60935decd") catch return; // std.zig.parse(alloc, src) -> std.zig.Ast.parse(alloc, src, Ast.Mode) const min_zig = std.SemanticVersion.parse("0.11.0-dev.1570+693b12f8e") catch return; // addPackage -> addModule
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 }));
} }
@ -112,17 +112,13 @@ 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(.{ const known_folders_module = b.createModule(.{ .source_file = .{ .path = known_folders_path } });
.name = "known-folders", exe.addModule("known-folders", known_folders_module);
.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(.{ const tres_module = b.createModule(.{ .source_file = .{ .path = tres_path } });
.name = "tres", exe.addModule("tres", tres_module);
.source = .{ .path = tres_path },
});
const check_submodules_step = CheckSubmodulesStep.init(b, &.{ const check_submodules_step = CheckSubmodulesStep.init(b, &.{
known_folders_path, known_folders_path,
@ -156,10 +152,7 @@ pub fn build(b: *std.build.Builder) !void {
.name = "zls_gen", .name = "zls_gen",
.root_source_file = .{ .path = "src/config_gen/config_gen.zig" }, .root_source_file = .{ .path = "src/config_gen/config_gen.zig" },
}); });
gen_exe.addPackage(.{ gen_exe.addModule("tres", tres_module);
.name = "tres",
.source = .{ .path = tres_path },
});
const gen_cmd = gen_exe.run(); const gen_cmd = gen_exe.run();
gen_cmd.addArgs(&.{ gen_cmd.addArgs(&.{
@ -201,15 +194,19 @@ pub fn build(b: *std.build.Builder) !void {
}); });
} }
tests.addPackage(.{ const build_options_module = exe_options.createModule();
.name = "zls",
.source = .{ .path = "src/zls.zig" }, const zls_module = b.createModule(.{
.dependencies = exe.packages.items, .source_file = .{ .path = "src/zls.zig" },
}); .dependencies = &.{
tests.addPackage(.{ .{ .name = "known-folders", .module = known_folders_module },
.name = "tres", .{ .name = "tres", .module = tres_module },
.source = .{ .path = tres_path }, .{ .name = "build_options", .module = build_options_module },
},
}); });
tests.addModule("zls", zls_module);
tests.addModule("tres", tres_module);
test_step.dependOn(&tests.step); test_step.dependOn(&tests.step);
} }

View File

@ -164,8 +164,15 @@ fn processStep(
try processIncludeDirs(allocator, include_dirs, install_exe.artifact.include_dirs.items); try processIncludeDirs(allocator, include_dirs, install_exe.artifact.include_dirs.items);
try processPkgConfig(allocator, include_dirs, install_exe.artifact); try processPkgConfig(allocator, include_dirs, install_exe.artifact);
for (install_exe.artifact.packages.items) |pkg| { if (@hasField(LibExeObjStep, "modules")) {
try processPackage(allocator, packages, pkg); var modules_it = install_exe.artifact.modules.iterator();
while (modules_it.next()) |module_entry| {
try processModule(allocator, packages, module_entry);
}
} else { // assuming @hasField(LibExeObjStep, "packages")
for (install_exe.artifact.packages.items) |pkg| {
try processPackage(allocator, packages, pkg);
}
} }
} else if (step.cast(LibExeObjStep)) |exe| { } else if (step.cast(LibExeObjStep)) |exe| {
if (exe.root_src) |src| { if (exe.root_src) |src| {
@ -177,8 +184,15 @@ fn processStep(
} }
try processIncludeDirs(allocator, include_dirs, exe.include_dirs.items); try processIncludeDirs(allocator, include_dirs, exe.include_dirs.items);
try processPkgConfig(allocator, include_dirs, exe); try processPkgConfig(allocator, include_dirs, exe);
for (exe.packages.items) |pkg| { if (@hasField(LibExeObjStep, "modules")) {
try processPackage(allocator, packages, pkg); var modules_it = exe.modules.iterator();
while (modules_it.next()) |module_entry| {
try processModule(allocator, packages, module_entry);
}
} else { // assuming @hasField(LibExeObjStep, "packages")
for (exe.packages.items) |pkg| {
try processPackage(allocator, packages, pkg);
}
} }
} else { } else {
for (step.dependencies.items) |unknown_step| { for (step.dependencies.items) |unknown_step| {
@ -187,6 +201,30 @@ fn processStep(
} }
} }
fn processModule(
allocator: std.mem.Allocator,
packages: *std.ArrayListUnmanaged(BuildConfig.Pkg),
module: std.StringArrayHashMap(*std.Build.Module).Entry,
) !void {
for (packages.items) |package| {
if (std.mem.eql(u8, package.name, module.key_ptr.*)) return;
}
const maybe_path = switch (module.value_ptr.*.source_file) {
.path => |path| path,
.generated => |generated| generated.path,
};
if (maybe_path) |path| {
try packages.append(allocator, .{ .name = module.key_ptr.*, .path = path });
}
var deps_it = module.value_ptr.*.dependencies.iterator();
while (deps_it.next()) |module_dep| {
try processModule(allocator, packages, module_dep);
}
}
fn processPackage( fn processPackage(
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
packages: *std.ArrayListUnmanaged(BuildConfig.Pkg), packages: *std.ArrayListUnmanaged(BuildConfig.Pkg),