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.
This commit is contained in:
Marten Ringwelski 2021-12-11 18:42:18 +01:00 committed by GitHub
parent 14528db0b7
commit abe82f6069
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 4 deletions

View File

@ -52,8 +52,19 @@ build_files: std.ArrayListUnmanaged(*BuildFile),
build_runner_path: []const u8, build_runner_path: []const u8,
build_runner_cache_path: []const u8, build_runner_cache_path: []const u8,
std_uri: ?[]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.allocator = allocator;
self.handles = std.StringHashMap(*Handle).init(allocator); self.handles = std.StringHashMap(*Handle).init(allocator);
self.zig_exe_path = zig_exe_path; 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_path = build_runner_path;
self.build_runner_cache_path = build_runner_cache_path; self.build_runner_cache_path = build_runner_cache_path;
self.std_uri = try stdUriFromLibPath(allocator, zig_lib_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 { 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, build_runner_cache_path: []const u8,
zig_exe_path: []const u8, zig_exe_path: []const u8,
build_file_path: ?[]const u8 = null, build_file_path: ?[]const u8 = null,
cache_root: []const u8,
global_cache_root: []const u8,
}; };
fn loadPackages(context: LoadPackagesContext) !void { fn loadPackages(context: LoadPackagesContext) !void {
@ -123,6 +138,11 @@ fn loadPackages(context: LoadPackagesContext) !void {
"@build@", "@build@",
build_file_path, build_file_path,
"--pkg-end", "--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, .build_runner_cache_path = self.build_runner_cache_path,
.zig_exe_path = self.zig_exe_path.?, .zig_exe_path = self.zig_exe_path.?,
.build_file_path = build_file_path, .build_file_path = build_file_path,
.cache_root = self.zig_cache_root,
.global_cache_root = self.zig_global_cache_root,
}) catch |err| { }) catch |err| {
log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, 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_path = self.build_runner_path,
.build_runner_cache_path = self.build_runner_cache_path, .build_runner_cache_path = self.build_runner_cache_path,
.zig_exe_path = self.zig_exe_path.?, .zig_exe_path = self.zig_exe_path.?,
.cache_root = self.zig_cache_root,
.global_cache_root = self.zig_global_cache_root,
}) catch |err| { }) catch |err| {
log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err }); log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err });
}; };

View File

@ -1815,6 +1815,13 @@ pub fn main() anyerror!void {
build_runner_path, build_runner_path,
build_runner_cache_path, build_runner_cache_path,
config.zig_lib_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(); defer document_store.deinit();

View File

@ -1,7 +1,9 @@
const root = @import("@build@"); const root = @import("@build@");
const std = @import("std"); const std = @import("std");
const io = std.io;
const fmt = std.fmt; const fmt = std.fmt;
const io = std.io;
const log = std.log;
const process = std.process;
const Builder = std.build.Builder; const Builder = std.build.Builder;
const Pkg = std.build.Pkg; const Pkg = std.build.Pkg;
const InstallArtifactStep = std.build.InstallArtifactStep; const InstallArtifactStep = std.build.InstallArtifactStep;
@ -17,7 +19,37 @@ pub fn main() !void {
const allocator = arena.allocator(); 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(); defer builder.destroy();
builder.resolveInstallPrefix(null, Builder.DirList{}); 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 }), .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.? }), .generated => |generated| if (generated.path != null) try out_stream.print("{s}\x00{s}\n", .{ pkg.name, generated.path.? }),
} }
if (pkg.dependencies) |dependencies| { if (pkg.dependencies) |dependencies| {
for (dependencies) |dep| { for (dependencies) |dep| {
try processPackage(out_stream, 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'"), 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.*];
}