CLI: fix only_core_functionality logic

This commit is contained in:
Andrew Kelley 2023-10-08 11:23:20 -07:00
parent a605bd9887
commit 29156de120
2 changed files with 67 additions and 42 deletions

View File

@ -105,13 +105,13 @@ pub const JobQueue = struct {
}
}
/// Creates the dependencies.zig file and corresponding `Module` for the
/// build runner to obtain via `@import("@dependencies")`.
pub fn createDependenciesModule(jq: *JobQueue, buf: *std.ArrayList(u8)) Allocator.Error!void {
/// Creates the dependencies.zig source code for the build runner to obtain
/// via `@import("@dependencies")`.
pub fn createDependenciesSource(jq: *JobQueue, buf: *std.ArrayList(u8)) Allocator.Error!void {
const keys = jq.table.keys();
if (keys.len == 0)
return createEmptyDependenciesModule(buf);
return createEmptyDependenciesSource(buf);
try buf.appendSlice("pub const packages = struct {\n");
@ -188,7 +188,7 @@ pub const JobQueue = struct {
try buf.appendSlice("};\n");
}
pub fn createEmptyDependenciesModule(buf: *std.ArrayList(u8)) Allocator.Error!void {
pub fn createEmptyDependenciesSource(buf: *std.ArrayList(u8)) Allocator.Error!void {
try buf.appendSlice(
\\pub const packages = struct {};
\\pub const root_deps: []const struct { []const u8, []const u8 } = &.{};

View File

@ -4831,10 +4831,8 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
.root = .{ .root_dir = build_root },
.root_src_path = build_zig_basename,
};
var dependencies_zig_src = std.ArrayList(u8).init(gpa);
defer dependencies_zig_src.deinit();
if (build_options.only_core_functionality) {
try Package.Fetch.createEmptyDependenciesModule(&dependencies_zig_src);
try createEmptyDependenciesModule(arena, &main_mod, local_cache_directory);
} else {
var http_client: std.http.Client = .{ .allocator = gpa };
defer http_client.deinit();
@ -4890,40 +4888,16 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
if (fetch_only) return cleanExit();
try job_queue.createDependenciesModule(&dependencies_zig_src);
var source_buf = std.ArrayList(u8).init(gpa);
defer source_buf.deinit();
try job_queue.createDependenciesSource(&source_buf);
const deps_mod = try createDependenciesModule(
arena,
source_buf.items,
&main_mod,
local_cache_directory,
);
const deps_mod = m: {
// Atomically create the file in a directory named after the hash of its contents.
const basename = "dependencies.zig";
const rand_int = std.crypto.random.int(u64);
const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++
Package.Manifest.hex64(rand_int);
{
var tmp_dir = try local_cache_directory.handle.makeOpenPath(tmp_dir_sub_path, .{});
defer tmp_dir.close();
try tmp_dir.writeFile(basename, dependencies_zig_src.items);
}
var hh: Cache.HashHelper = .{};
hh.addBytes(build_options.version);
hh.addBytes(dependencies_zig_src.items);
const hex_digest = hh.final();
const o_dir_sub_path = try arena.dupe(u8, "o" ++ fs.path.sep_str ++ hex_digest);
try Package.Fetch.renameTmpIntoCache(
local_cache_directory.handle,
tmp_dir_sub_path,
o_dir_sub_path,
);
break :m try Package.Module.create(arena, .{
.root = .{
.root_dir = local_cache_directory,
.sub_path = o_dir_sub_path,
},
.root_src_path = basename,
});
};
{
// We need a Module for each package's build.zig.
const hashes = job_queue.table.keys();
@ -4944,7 +4918,6 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
deps_mod.deps.putAssumeCapacityNoClobber(hash_cloned, m);
}
}
try main_mod.deps.put(arena, "@dependencies", deps_mod);
}
try main_mod.deps.put(arena, "@build", &build_mod);
@ -6795,3 +6768,55 @@ fn cmdFetch(
return cleanExit();
}
fn createEmptyDependenciesModule(
arena: Allocator,
main_mod: *Package.Module,
local_cache_directory: Cache.Directory,
) !void {
var source = std.ArrayList(u8).init(arena);
try Package.Fetch.JobQueue.createEmptyDependenciesSource(&source);
_ = try createDependenciesModule(arena, source.items, main_mod, local_cache_directory);
}
/// Creates the dependencies.zig file and corresponding `Package.Module` for the
/// build runner to obtain via `@import("@dependencies")`.
fn createDependenciesModule(
arena: Allocator,
source: []const u8,
main_mod: *Package.Module,
local_cache_directory: Cache.Directory,
) !*Package.Module {
// Atomically create the file in a directory named after the hash of its contents.
const basename = "dependencies.zig";
const rand_int = std.crypto.random.int(u64);
const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++
Package.Manifest.hex64(rand_int);
{
var tmp_dir = try local_cache_directory.handle.makeOpenPath(tmp_dir_sub_path, .{});
defer tmp_dir.close();
try tmp_dir.writeFile(basename, source);
}
var hh: Cache.HashHelper = .{};
hh.addBytes(build_options.version);
hh.addBytes(source);
const hex_digest = hh.final();
const o_dir_sub_path = try arena.dupe(u8, "o" ++ fs.path.sep_str ++ hex_digest);
try Package.Fetch.renameTmpIntoCache(
local_cache_directory.handle,
tmp_dir_sub_path,
o_dir_sub_path,
);
const deps_mod = try Package.Module.create(arena, .{
.root = .{
.root_dir = local_cache_directory,
.sub_path = o_dir_sub_path,
},
.root_src_path = basename,
});
try main_mod.deps.put(arena, "@dependencies", deps_mod);
return deps_mod;
}