diff --git a/build.zig b/build.zig index 19bf41dc607d3b93e96bf53598d9945f8bd5bcf4..c98dcc649ca0773a12490c2d43dddc1ff344b873 100644 --- a/build.zig +++ b/build.zig @@ -268,7 +268,7 @@ pub fn build(b: *std.Build) !void { } // Ensure git version changes get picked up. - b.dependOnFileContents(b.graph.path(.build_root, ".git/HEAD")); + b.dependOnFileContents(b.path(".git/HEAD")); const version_string = b.fmt("{d}.{d}.{d}", .{ zig_version.major, zig_version.minor, zig_version.patch }); diff --git a/lib/compiler/Maker.zig b/lib/compiler/Maker.zig index d35a9dd53751001fdcace80f7ece432baf4f185b..46653431cb447f1d773c6838681a06e7c1c26cf0 100644 --- a/lib/compiler/Maker.zig +++ b/lib/compiler/Maker.zig @@ -1318,7 +1318,11 @@ fn configure(graph: *Graph, options: ConfigureOptions) !ScannedConfig { } for (configuration.path_deps) |path_dep| { - try config_man.addPathPost(path_dep.toCachePath(&configuration, arena)); + switch (path_dep.flags.mode) { + .directory => {}, // TODO + .contents => try config_man.addPathPost(confPathDepToCachePath(graph, &configuration, path_dep)), + .metadata => {}, // TODO + } } // If it is poisoned, there is no point in moving it to cached @@ -1825,7 +1829,7 @@ fn cmdInit(gpa: Allocator, graph: *Graph, args: []const []const u8) !void { return process.cleanExit(io); }, .minimal => { - writeSimpleTemplateFile(io, Package.Manifest.basename, + Templates.writeSimpleFile(io, Package.Manifest.basename, \\.{{ \\ .name = .{s}, \\ .version = "0.0.1", @@ -1842,7 +1846,7 @@ fn cmdInit(gpa: Allocator, graph: *Graph, args: []const []const u8) !void { else => fatal("failed to create {q}: {t}", .{ Package.Manifest.basename, err }), error.PathAlreadyExists => fatal("refusing to overwrite {q}", .{Package.Manifest.basename}), }; - writeSimpleTemplateFile(io, default_build_zig_basename, + Templates.writeSimpleFile(io, default_build_zig_basename, \\const std = @import("std"); \\ \\pub fn build(b: *std.Build) void {{ @@ -3498,7 +3502,7 @@ fn loadManifest( 0, ) catch |err| switch (err) { error.FileNotFound => { - writeSimpleTemplateFile(io, Package.Manifest.basename, + Templates.writeSimpleFile(io, Package.Manifest.basename, \\.{{ \\ .name = .{s}, \\ .version = "{s}", @@ -3658,12 +3662,47 @@ const Templates = struct { .buffer = std.array_list.Managed(u8).init(gpa), }; } + + fn writeSimpleFile(io: Io, file_name: []const u8, comptime format: []const u8, args: anytype) !void { + const f = try Io.Dir.cwd().createFile(io, file_name, .{ .exclusive = true }); + defer f.close(io); + var buf: [4096]u8 = undefined; + var fw = f.writer(io, &buf); + try fw.interface.print(format, args); + try fw.interface.flush(); + } }; -fn writeSimpleTemplateFile(io: Io, file_name: []const u8, comptime format: []const u8, args: anytype) !void { - const f = try Io.Dir.cwd().createFile(io, file_name, .{ .exclusive = true }); - defer f.close(io); - var buf: [4096]u8 = undefined; - var fw = f.writer(io, &buf); - try fw.interface.print(format, args); - try fw.interface.flush(); + +fn confPathDepToCachePath(graph: *const Graph, c: *const Configuration, path_dep: Configuration.PathDep) Path { + const sub_path = path_dep.sub.slice(c); + return switch (path_dep.flags.base) { + .cwd => .{ + .root_dir = .cwd(), + .sub_path = sub_path, + }, + .local_cache => .{ + .root_dir = graph.local_cache_root, + .sub_path = sub_path, + }, + .global_cache => .{ + .root_dir = graph.global_cache_root, + .sub_path = sub_path, + }, + .build_root => .{ + .root_dir = switch (path_dep.pkg.unwrap().?) { + .root => graph.build_root_directory, + _ => @panic("TODO"), + }, + .sub_path = sub_path, + }, + .zig_lib => .{ + .root_dir = graph.zig_lib_directory, + .sub_path = sub_path, + }, + .zig_exe => @panic("TODO"), + .install_prefix => @panic("TODO"), + .install_lib => @panic("TODO"), + .install_bin => @panic("TODO"), + .install_include => @panic("TODO"), + }; } diff --git a/lib/compiler/Maker/Graph.zig b/lib/compiler/Maker/Graph.zig index 116132c614e740ed7159c276141759d2f0e8f1ab..fabe31d5367c5611bd69612b9c61669af1ec4d12 100644 --- a/lib/compiler/Maker/Graph.zig +++ b/lib/compiler/Maker/Graph.zig @@ -4,7 +4,6 @@ const Graph = @This(); const std = @import("std"); const Io = std.Io; const Allocator = std.mem.Allocator; -const Configuration = std.Build.Configuration; const Path = std.Build.Cache.Path; const Directory = std.Build.Cache.Directory; diff --git a/lib/compiler/configurer.zig b/lib/compiler/configurer.zig index a7b7ae44274356702ffe34923d39405d23715f11..809bb9e6263c3a9c0e128bb6a677589fef6054c9 100644 --- a/lib/compiler/configurer.zig +++ b/lib/compiler/configurer.zig @@ -633,8 +633,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void { var s: Serialize = .{ .wc = wc, .arena = arena }; try wc.path_deps.ensureTotalCapacityPrecise(gpa, graph.configure_dependencies.items.len); - // TODO remove this - if (false) for ( + for ( graph.configure_dependencies.items, wc.path_deps.addManyAsSliceAssumeCapacity(graph.configure_dependencies.items.len), ) |src, *dest| { @@ -662,7 +661,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void { .dependency => |d| .init(try s.builderToPackage(d.dependency.builder)), }, }; - }; + } // Starting from all top-level steps in `b`, traverse the entire step graph // and add all step dependencies implied by module graphs. diff --git a/lib/std/Build.zig b/lib/std/Build.zig index e349aba6c21549626289a837845a8c45092d0796..6d07c0fb49c6a056151a8fdc0a4adc13aa385dc1 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -177,6 +177,7 @@ pub const Graph = struct { /// A path whose components and contents are known at some point during /// `Step` resolution, relative to the provided base directory. pub fn path(graph: *Graph, base: Configuration.LazyPath.Relative.Base, sub_path: []const u8) LazyPath { + assert(base != .build_root); return .{ .relative = .{ .base = base, .sub_path = @This().dupePath(graph, sub_path), diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig index 54f0b8baad329214e94a37d56173f04d04c4d082..f731f5667c30b6406f0b3721d1249b1b3437fd77 100644 --- a/lib/std/Build/Cache.zig +++ b/lib/std/Build/Cache.zig @@ -70,6 +70,15 @@ pub const PrefixedPath = struct { } }; +fn findPrefixPath(cache: *const Cache, path: Path) !PrefixedPath { + const gpa = cache.gpa; + const resolved_path = try std.fs.path.resolve(gpa, &.{ + cache.cwd, path.root_dir.path orelse ".", path.subPathOrDot(), + }); + errdefer gpa.free(resolved_path); + return findPrefixResolved(cache, resolved_path); +} + fn findPrefix(cache: *const Cache, file_path: []const u8) !PrefixedPath { const gpa = cache.gpa; const resolved_path = try std.fs.path.resolve(gpa, &.{file_path}); @@ -998,13 +1007,22 @@ pub const Manifest = struct { /// This is useful for processes that don't know the all the files that are /// depended on ahead of time. For example, a source file that can import /// other files will need to be recompiled if the imported file is changed. - pub fn addFilePost(self: *Manifest, file_path: []const u8) !void { - assert(self.manifest_file != null); - const gpa = self.cache.gpa; - const prefixed_path = try self.cache.findPrefix(file_path); + pub fn addFilePost(man: *Manifest, file_path: []const u8) !void { + assert(man.manifest_file != null); + const gpa = man.cache.gpa; + const prefixed_path = try man.cache.findPrefix(file_path); var keep = false; defer if (!keep) gpa.free(prefixed_path.sub_path); - keep = try addPrefixedPathPost(self, prefixed_path); + keep = try addPrefixedPathPost(man, prefixed_path); + } + + pub fn addPathPost(man: *Manifest, path: Path) !void { + assert(man.manifest_file != null); + const gpa = man.cache.gpa; + const prefixed_path: PrefixedPath = try man.cache.findPrefixPath(path); + var keep = false; + defer if (!keep) gpa.free(prefixed_path.sub_path); + keep = try addPrefixedPathPost(man, prefixed_path); } /// Low level function. `prefixed_path` references cloned memory. Returns @@ -1034,12 +1052,6 @@ pub const Manifest = struct { return true; } - pub fn addPathPost(man: *Manifest, path: Path) !void { - _ = man; - _ = path; - std.log.err("TODO Build.Cache.addPathPost", .{}); - } - /// Like `addFilePost` but when the file contents have already been loaded from disk. pub fn addFilePostContents( self: *Manifest, diff --git a/lib/std/Build/Configuration.zig b/lib/std/Build/Configuration.zig index 4a0fc763e043705f120e6d317c59b406ba1a9afd..f10bf9055b58d63061dfa3b37ff7954474a35dcf 100644 --- a/lib/std/Build/Configuration.zig +++ b/lib/std/Build/Configuration.zig @@ -1557,6 +1557,7 @@ pub const LazyPath = union(@This().Tag) { cwd, local_cache, global_cache, + /// Must not be used with Relative since package index is missing. build_root, zig_exe, zig_lib, @@ -1876,13 +1877,6 @@ pub const PathDep = extern struct { }; pub const Mode = enum(u8) { directory, contents, metadata }; - - pub fn toCachePath(path: PathDep, c: *const Configuration, arena: Allocator) std.Build.Cache.Path { - _ = c; - _ = arena; - _ = path; - if (true) @panic("TODO Configuration.PathDep.toCachePath"); - } }; pub const InstallDestDir = enum(u32) {