Skip to content

Commit abe82f6

Browse files
authored
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.
1 parent 14528db commit abe82f6

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

src/DocumentStore.zig

+25-1
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,28 @@ build_files: std.ArrayListUnmanaged(*BuildFile),
5252
build_runner_path: []const u8,
5353
build_runner_cache_path: []const u8,
5454
std_uri: ?[]const u8,
55+
zig_cache_root: []const u8,
56+
zig_global_cache_root: []const u8,
5557

56-
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 {
58+
pub fn init(
59+
self: *DocumentStore,
60+
allocator: std.mem.Allocator,
61+
zig_exe_path: ?[]const u8,
62+
build_runner_path: []const u8,
63+
build_runner_cache_path: []const u8,
64+
zig_lib_path: ?[]const u8,
65+
zig_cache_root: []const u8,
66+
zig_global_cache_root: []const u8,
67+
) !void {
5768
self.allocator = allocator;
5869
self.handles = std.StringHashMap(*Handle).init(allocator);
5970
self.zig_exe_path = zig_exe_path;
6071
self.build_files = .{};
6172
self.build_runner_path = build_runner_path;
6273
self.build_runner_cache_path = build_runner_cache_path;
6374
self.std_uri = try stdUriFromLibPath(allocator, zig_lib_path);
75+
self.zig_cache_root = zig_cache_root;
76+
self.zig_global_cache_root = zig_global_cache_root;
6477
}
6578

6679
fn loadBuildAssociatedConfiguration(allocator: std.mem.Allocator, build_file: *BuildFile, build_file_path: []const u8) !void {
@@ -98,6 +111,8 @@ const LoadPackagesContext = struct {
98111
build_runner_cache_path: []const u8,
99112
zig_exe_path: []const u8,
100113
build_file_path: ?[]const u8 = null,
114+
cache_root: []const u8,
115+
global_cache_root: []const u8,
101116
};
102117

103118
fn loadPackages(context: LoadPackagesContext) !void {
@@ -123,6 +138,11 @@ fn loadPackages(context: LoadPackagesContext) !void {
123138
"@build@",
124139
build_file_path,
125140
"--pkg-end",
141+
"--",
142+
zig_exe_path,
143+
directory_path,
144+
context.cache_root,
145+
context.global_cache_root,
126146
},
127147
});
128148

@@ -229,6 +249,8 @@ fn newDocument(self: *DocumentStore, uri: []const u8, text: [:0]u8) anyerror!*Ha
229249
.build_runner_cache_path = self.build_runner_cache_path,
230250
.zig_exe_path = self.zig_exe_path.?,
231251
.build_file_path = build_file_path,
252+
.cache_root = self.zig_cache_root,
253+
.global_cache_root = self.zig_global_cache_root,
232254
}) catch |err| {
233255
log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err });
234256
};
@@ -496,6 +518,8 @@ pub fn applySave(self: *DocumentStore, handle: *Handle) !void {
496518
.build_runner_path = self.build_runner_path,
497519
.build_runner_cache_path = self.build_runner_cache_path,
498520
.zig_exe_path = self.zig_exe_path.?,
521+
.cache_root = self.zig_cache_root,
522+
.global_cache_root = self.zig_global_cache_root,
499523
}) catch |err| {
500524
log.debug("Failed to load packages of build file {s} (error: {})", .{ build_file.uri, err });
501525
};

src/main.zig

+7
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,13 @@ pub fn main() anyerror!void {
18151815
build_runner_path,
18161816
build_runner_cache_path,
18171817
config.zig_lib_path,
1818+
// TODO make this configurable
1819+
// We can't figure it out ourselves since we don't know what arguments
1820+
// the user will use to run "zig build"
1821+
"zig-cache",
1822+
// Since we don't compile anything and no packages should put their
1823+
// files there this path can be ignored
1824+
"ZLS_DONT_CARE",
18181825
);
18191826
defer document_store.deinit();
18201827

src/special/build_runner.zig

+41-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const root = @import("@build@");
22
const std = @import("std");
3-
const io = std.io;
43
const fmt = std.fmt;
4+
const io = std.io;
5+
const log = std.log;
6+
const process = std.process;
57
const Builder = std.build.Builder;
68
const Pkg = std.build.Pkg;
79
const InstallArtifactStep = std.build.InstallArtifactStep;
@@ -17,7 +19,37 @@ pub fn main() !void {
1719

1820
const allocator = arena.allocator();
1921

20-
const builder = try Builder.create(allocator, "", "", "", "");
22+
var args = try process.argsAlloc(allocator);
23+
defer process.argsFree(allocator, args);
24+
25+
// skip my own exe name
26+
var arg_idx: usize = 1;
27+
28+
const zig_exe = nextArg(args, &arg_idx) orelse {
29+
log.warn("Expected first argument to be path to zig compiler\n", .{});
30+
return error.InvalidArgs;
31+
};
32+
const build_root = nextArg(args, &arg_idx) orelse {
33+
log.warn("Expected second argument to be build root directory path\n", .{});
34+
return error.InvalidArgs;
35+
};
36+
const cache_root = nextArg(args, &arg_idx) orelse {
37+
log.warn("Expected third argument to be cache root directory path\n", .{});
38+
return error.InvalidArgs;
39+
};
40+
const global_cache_root = nextArg(args, &arg_idx) orelse {
41+
log.warn("Expected third argument to be global cache root directory path\n", .{});
42+
return error.InvalidArgs;
43+
};
44+
45+
const builder = try Builder.create(
46+
allocator,
47+
zig_exe,
48+
build_root,
49+
cache_root,
50+
global_cache_root,
51+
);
52+
2153
defer builder.destroy();
2254

2355
builder.resolveInstallPrefix(null, Builder.DirList{});
@@ -56,7 +88,7 @@ fn processPackage(out_stream: anytype, pkg: Pkg) anyerror!void {
5688
.path => |path| try out_stream.print("{s}\x00{s}\n", .{ pkg.name, path }),
5789
.generated => |generated| if (generated.path != null) try out_stream.print("{s}\x00{s}\n", .{ pkg.name, generated.path.? }),
5890
}
59-
91+
6092
if (pkg.dependencies) |dependencies| {
6193
for (dependencies) |dep| {
6294
try processPackage(out_stream, dep);
@@ -71,3 +103,9 @@ fn runBuild(builder: *Builder) anyerror!void {
71103
else => @compileError("expected return type of build to be 'void' or '!void'"),
72104
}
73105
}
106+
107+
fn nextArg(args: [][]const u8, idx: *usize) ?[]const u8 {
108+
if (idx.* >= args.len) return null;
109+
defer idx.* += 1;
110+
return args[idx.*];
111+
}

0 commit comments

Comments
 (0)