From abe82f60695a4203c4bf4f1afb1236a36815fc11 Mon Sep 17 00:00:00 2001 From: Marten Ringwelski Date: Sat, 11 Dec 2021 18:42:18 +0100 Subject: [PATCH] build_runner: Require some paths to be given (#369) We now require the following to ge given in the cli args: - zig_exe - build_root - cache_root - global_cache_root This fixes the path for packages that use one or more from the above to place their files. --- src/DocumentStore.zig | 26 ++++++++++++++++++++- src/main.zig | 7 ++++++ src/special/build_runner.zig | 44 +++++++++++++++++++++++++++++++++--- 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/DocumentStore.zig b/src/DocumentStore.zig index 9fd18ba..04addc3 100644 --- a/src/DocumentStore.zig +++ b/src/DocumentStore.zig @@ -52,8 +52,19 @@ build_files: std.ArrayListUnmanaged(*BuildFile), build_runner_path: []const u8, build_runner_cache_path: []const u8, std_uri: ?[]const u8, +zig_cache_root: []const u8, +zig_global_cache_root: []const u8, -pub fn init(self: *DocumentStore, allocator: std.mem.Allocator, zig_exe_path: ?[]const u8, build_runner_path: []const u8, build_runner_cache_path: []const u8, zig_lib_path: ?[]const u8) !void { +pub fn init( + self: *DocumentStore, + allocator: std.mem.Allocator, + zig_exe_path: ?[]const u8, + build_runner_path: []const u8, + build_runner_cache_path: []const u8, + zig_lib_path: ?[]const u8, + zig_cache_root: []const u8, + zig_global_cache_root: []const u8, +) !void { self.allocator = allocator; self.handles = std.StringHashMap(*Handle).init(allocator); self.zig_exe_path = zig_exe_path; @@ -61,6 +72,8 @@ pub fn init(self: *DocumentStore, allocator: std.mem.Allocator, zig_exe_path: ?[ self.build_runner_path = build_runner_path; self.build_runner_cache_path = build_runner_cache_path; self.std_uri = try stdUriFromLibPath(allocator, zig_lib_path); + self.zig_cache_root = zig_cache_root; + self.zig_global_cache_root = zig_global_cache_root; } fn loadBuildAssociatedConfiguration(allocator: std.mem.Allocator, build_file: *BuildFile, build_file_path: []const u8) !void { @@ -98,6 +111,8 @@ const LoadPackagesContext = struct { build_runner_cache_path: []const u8, zig_exe_path: []const u8, build_file_path: ?[]const u8 = null, + cache_root: []const u8, + global_cache_root: []const u8, }; fn loadPackages(context: LoadPackagesContext) !void { @@ -123,6 +138,11 @@ fn loadPackages(context: LoadPackagesContext) !void { "@build@", build_file_path, "--pkg-end", + "--", + zig_exe_path, + directory_path, + context.cache_root, + context.global_cache_root, }, }); @@ -229,6 +249,8 @@ fn newDocument(self: *DocumentStore, uri: []const u8, text: [:0]u8) anyerror!*Ha .build_runner_cache_path = self.build_runner_cache_path, .zig_exe_path = self.zig_exe_path.?, .build_file_path = build_file_path, + .cache_root = self.zig_cache_root, + .global_cache_root = self.zig_global_cache_root, }) catch |err| { log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err }); }; @@ -496,6 +518,8 @@ pub fn applySave(self: *DocumentStore, handle: *Handle) !void { .build_runner_path = self.build_runner_path, .build_runner_cache_path = self.build_runner_cache_path, .zig_exe_path = self.zig_exe_path.?, + .cache_root = self.zig_cache_root, + .global_cache_root = self.zig_global_cache_root, }) catch |err| { log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err }); }; diff --git a/src/main.zig b/src/main.zig index c9cb57c..fde4e69 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1815,6 +1815,13 @@ pub fn main() anyerror!void { build_runner_path, build_runner_cache_path, config.zig_lib_path, + // TODO make this configurable + // We can't figure it out ourselves since we don't know what arguments + // the user will use to run "zig build" + "zig-cache", + // Since we don't compile anything and no packages should put their + // files there this path can be ignored + "ZLS_DONT_CARE", ); defer document_store.deinit(); diff --git a/src/special/build_runner.zig b/src/special/build_runner.zig index 36c0ae5..3b023b0 100644 --- a/src/special/build_runner.zig +++ b/src/special/build_runner.zig @@ -1,7 +1,9 @@ const root = @import("@build@"); const std = @import("std"); -const io = std.io; const fmt = std.fmt; +const io = std.io; +const log = std.log; +const process = std.process; const Builder = std.build.Builder; const Pkg = std.build.Pkg; const InstallArtifactStep = std.build.InstallArtifactStep; @@ -17,7 +19,37 @@ pub fn main() !void { const allocator = arena.allocator(); - const builder = try Builder.create(allocator, "", "", "", ""); + var args = try process.argsAlloc(allocator); + defer process.argsFree(allocator, args); + + // skip my own exe name + var arg_idx: usize = 1; + + const zig_exe = nextArg(args, &arg_idx) orelse { + log.warn("Expected first argument to be path to zig compiler\n", .{}); + return error.InvalidArgs; + }; + const build_root = nextArg(args, &arg_idx) orelse { + log.warn("Expected second argument to be build root directory path\n", .{}); + return error.InvalidArgs; + }; + const cache_root = nextArg(args, &arg_idx) orelse { + log.warn("Expected third argument to be cache root directory path\n", .{}); + return error.InvalidArgs; + }; + const global_cache_root = nextArg(args, &arg_idx) orelse { + log.warn("Expected third argument to be global cache root directory path\n", .{}); + return error.InvalidArgs; + }; + + const builder = try Builder.create( + allocator, + zig_exe, + build_root, + cache_root, + global_cache_root, + ); + defer builder.destroy(); builder.resolveInstallPrefix(null, Builder.DirList{}); @@ -56,7 +88,7 @@ fn processPackage(out_stream: anytype, pkg: Pkg) anyerror!void { .path => |path| try out_stream.print("{s}\x00{s}\n", .{ pkg.name, path }), .generated => |generated| if (generated.path != null) try out_stream.print("{s}\x00{s}\n", .{ pkg.name, generated.path.? }), } - + if (pkg.dependencies) |dependencies| { for (dependencies) |dep| { try processPackage(out_stream, dep); @@ -71,3 +103,9 @@ fn runBuild(builder: *Builder) anyerror!void { else => @compileError("expected return type of build to be 'void' or '!void'"), } } + +fn nextArg(args: [][]const u8, idx: *usize) ?[]const u8 { + if (idx.* >= args.len) return null; + defer idx.* += 1; + return args[idx.*]; +}