Skip to content

Commit

Permalink
build_runner: Require some paths to be given (#369)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
maringuu authored Dec 11, 2021
1 parent 14528db commit abe82f6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
26 changes: 25 additions & 1 deletion src/DocumentStore.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,28 @@ 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;
self.build_files = .{};
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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
},
});

Expand Down Expand Up @@ -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 });
};
Expand Down Expand Up @@ -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 });
};
Expand Down
7 changes: 7 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
44 changes: 41 additions & 3 deletions src/special/build_runner.zig
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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{});
Expand Down Expand Up @@ -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);
Expand All @@ -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.*];
}

0 comments on commit abe82f6

Please sign in to comment.