From 62068ae828de91d6dafd425c60f47cbc43ac3df3 Mon Sep 17 00:00:00 2001 From: nullptrdevs <16590917+nullptrdevs@users.noreply.github.com> Date: Sat, 4 Feb 2023 23:15:42 -0800 Subject: [PATCH] zig build system changes (modules) (#976) --- build.zig | 39 ++++++++++++++---------------- src/special/build_runner.zig | 46 ++++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 25 deletions(-) diff --git a/build.zig b/build.zig index adddf0f..62aee8c 100644 --- a/build.zig +++ b/build.zig @@ -7,7 +7,7 @@ const zls_version = std.builtin.Version{ .major = 0, .minor = 11, .patch = 0 }; pub fn build(b: *std.build.Builder) !void { comptime { 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) { @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_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 }, - }); + const known_folders_module = b.createModule(.{ .source_file = .{ .path = known_folders_path } }); + exe.addModule("known-folders", known_folders_module); 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; - exe.addPackage(.{ - .name = "tres", - .source = .{ .path = tres_path }, - }); + const tres_module = b.createModule(.{ .source_file = .{ .path = tres_path } }); + exe.addModule("tres", tres_module); const check_submodules_step = CheckSubmodulesStep.init(b, &.{ known_folders_path, @@ -156,10 +152,7 @@ pub fn build(b: *std.build.Builder) !void { .name = "zls_gen", .root_source_file = .{ .path = "src/config_gen/config_gen.zig" }, }); - gen_exe.addPackage(.{ - .name = "tres", - .source = .{ .path = tres_path }, - }); + gen_exe.addModule("tres", tres_module); const gen_cmd = gen_exe.run(); gen_cmd.addArgs(&.{ @@ -201,15 +194,19 @@ pub fn build(b: *std.build.Builder) !void { }); } - tests.addPackage(.{ - .name = "zls", - .source = .{ .path = "src/zls.zig" }, - .dependencies = exe.packages.items, - }); - tests.addPackage(.{ - .name = "tres", - .source = .{ .path = tres_path }, + const build_options_module = exe_options.createModule(); + + const zls_module = b.createModule(.{ + .source_file = .{ .path = "src/zls.zig" }, + .dependencies = &.{ + .{ .name = "known-folders", .module = known_folders_module }, + .{ .name = "tres", .module = tres_module }, + .{ .name = "build_options", .module = build_options_module }, + }, }); + tests.addModule("zls", zls_module); + tests.addModule("tres", tres_module); + test_step.dependOn(&tests.step); } diff --git a/src/special/build_runner.zig b/src/special/build_runner.zig index dfdf97f..2b56d9b 100644 --- a/src/special/build_runner.zig +++ b/src/special/build_runner.zig @@ -164,8 +164,15 @@ fn processStep( try processIncludeDirs(allocator, include_dirs, install_exe.artifact.include_dirs.items); try processPkgConfig(allocator, include_dirs, install_exe.artifact); - for (install_exe.artifact.packages.items) |pkg| { - try processPackage(allocator, packages, pkg); + if (@hasField(LibExeObjStep, "modules")) { + 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| { if (exe.root_src) |src| { @@ -177,8 +184,15 @@ fn processStep( } try processIncludeDirs(allocator, include_dirs, exe.include_dirs.items); try processPkgConfig(allocator, include_dirs, exe); - for (exe.packages.items) |pkg| { - try processPackage(allocator, packages, pkg); + if (@hasField(LibExeObjStep, "modules")) { + 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 { 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( allocator: std.mem.Allocator, packages: *std.ArrayListUnmanaged(BuildConfig.Pkg),