authorgravatar for hemisputnik@proton.mehemisputnik <hemisputnik@proton.me> 2026-08-10 07:26:08+03:00
committergravatar for hemisputnik@proton.mehemisputnik <hemisputnik@proton.me> 2026-08-10 11:20:46+03:00
loge98205bd17bb39ba501213f9b8ce2b0559b90035
tree6493341c8ca12259c62ffe5cb7abc25326f2ab13
parent3d50222f9b20fb2cb4ab5ae6e8bcb35f74adbda9
signaturebadge-check Signed by SSH key SHA256:iUK/EffQPyeXTXChJNV0UrxsscinNLxnk+0pu4OGU7Q

std.Build.Configuration: serialize package instance public modules


3 files changed, 93 insertions(+), 24 deletions(-)

lib/compiler/Maker/ScannedConfig.zig+8
...@@ -124,6 +124,14 @@ pub fn print(sc: *const ScannedConfig, w: *Writer) Writer.Error!void {...@@ -124,6 +124,14 @@ pub fn print(sc: *const ScannedConfig, w: *Writer) Writer.Error!void {
124 }124 }
125 try otf.end();125 try otf.end();
126126
127 var msf = try sf.beginStructField("modules", .{});
128 for (inst.modules.keys.slice(c), inst.modules.values.slice(c)) |key, value| {
129 var msf2 = try msf.beginStructField(key.slice(c), .{});
130 try sc.printStruct(&msf2, Configuration.Module, value.get(c));
131 try msf2.end();
132 }
133 try msf.end();
134
127 try sf.end();135 try sf.end();
128 }136 }
129 }137 }
lib/std/Build/Configuration.zig+24-2
...@@ -1679,6 +1679,7 @@ pub const Package = extern struct {...@@ -1679,6 +1679,7 @@ pub const Package = extern struct {
1679pub const PackageInstance = extern struct {1679pub const PackageInstance = extern struct {
1680 package: Package.Index,1680 package: Package.Index,
1681 user_input_options: UserInputOption.List.Index,1681 user_input_options: UserInputOption.List.Index,
1682 modules: PublicModules,
16821683
1683 pub const UserInputOption = struct {1684 pub const UserInputOption = struct {
1684 flags: Flags,1685 flags: Flags,
...@@ -1753,6 +1754,11 @@ pub const PackageInstance = extern struct {...@@ -1753,6 +1754,11 @@ pub const PackageInstance = extern struct {
1753 };1754 };
1754 };1755 };
17551756
1757 pub const PublicModules = extern struct {
1758 keys: StringList,
1759 values: Module.List.Index,
1760 };
1761
1756 pub const Index = enum(u32) {1762 pub const Index = enum(u32) {
1757 root,1763 root,
1758 _,1764 _,
...@@ -1862,8 +1868,6 @@ pub const Module = struct {...@@ -1862,8 +1868,6 @@ pub const Module = struct {
1862 }1868 }
1863 };1869 };
18641870
1865 pub const Index = IndexType(@This());
1866
1867 pub const Flags = packed struct(u32) {1871 pub const Flags = packed struct(u32) {
1868 optimize: Optimize,1872 optimize: Optimize,
1869 strip: DefaultingBool,1873 strip: DefaultingBool,
...@@ -1934,6 +1938,24 @@ pub const Module = struct {...@@ -1934,6 +1938,24 @@ pub const Module = struct {
1934 _: u30 = 0,1938 _: u30 = 0,
1935 };1939 };
1936 };1940 };
1941
1942 pub const Index = IndexType(@This());
1943
1944 pub const List = struct {
1945 modules: Storage.LengthPrefixedList(Module.Index),
1946
1947 pub const Index = enum(u32) {
1948 _,
1949
1950 pub fn get(this: @This(), c: *const Configuration) List {
1951 return extraData(c, List, @backingInt(this));
1952 }
1953
1954 pub fn slice(this: @This(), c: *const Configuration) []const Module.Index {
1955 return this.get(c).modules.slice;
1956 }
1957 };
1958 };
1937};1959};
19381960
1939pub const ImportTable = struct {1961pub const ImportTable = struct {
lib/std/Build/Serialize.zig+61-22
...@@ -28,11 +28,24 @@ pub fn write(b: *std.Build, wc: *Configuration.Wip, writer: *std.Io.Writer) !voi...@@ -28,11 +28,24 @@ pub fn write(b: *std.Build, wc: *Configuration.Wip, writer: *std.Io.Writer) !voi
28 // instances.28 // instances.
29 try s.traversePackages(b);29 try s.traversePackages(b);
3030
31 // Seed the package_instance_map, which is later used in calls to31 // Next, seed the package_instance_map, which is later used in calls to
32 // packageInstanceFromBuilder.32 // packageInstanceFromBuilder.
33 try s.addPackageInstance(b);33
34 _ = try wc.package_instances.addManyAsSlice(gpa, 1 + b.graph.dependency_cache.count());
35 try s.package_instance_map.ensureTotalCapacity(arena, 1 + b.graph.dependency_cache.count());
36
37 // As serializing package instances also requires serializing the public
38 // modules of each one, we must first allocate an index for each package
39 // instance. Otherwise, addModule may access a package instance that hasn't
40 // been created yet with packageInstanceFromBuilder.
41
42 s.package_instance_map.putAssumeCapacityNoClobber(b, {});
34 var iter = b.graph.dependency_cache.valueIterator();43 var iter = b.graph.dependency_cache.valueIterator();
35 while (iter.next()) |dep| try s.addPackageInstance(dep.*.builder);44 while (iter.next()) |dep| s.package_instance_map.putAssumeCapacityNoClobber(dep.*.builder, {});
45
46 try s.addPackageInstance(b);
47 var iter2 = b.graph.dependency_cache.valueIterator();
48 while (iter2.next()) |dep| try s.addPackageInstance(dep.*.builder);
3649
37 try wc.path_deps.ensureTotalCapacityPrecise(gpa, graph.configure_dependencies.items.len);50 try wc.path_deps.ensureTotalCapacityPrecise(gpa, graph.configure_dependencies.items.len);
38 for (51 for (
...@@ -763,13 +776,13 @@ fn makePackageDep(s: *Serialize, parent_dep_prefix: []const u8, name: []const u8...@@ -763,13 +776,13 @@ fn makePackageDep(s: *Serialize, parent_dep_prefix: []const u8, name: []const u8
763 .package = @fromBackingInt(@intCast(index + 1)),776 .package = @fromBackingInt(@intCast(index + 1)),
764 };777 };
765778
779 const index: Configuration.Package.Index = @fromBackingInt(@intCast(wc.packages.items.len));
780 try s.package_map.put(arena, hash, {});
781
766 const entry = std.Build.package_map.get(hash) orelse unreachable;782 const entry = std.Build.package_map.get(hash) orelse unreachable;
767783
768 const dep_prefix = try arena.print("{s}{s}.", .{ parent_dep_prefix, name });784 const dep_prefix = try arena.print("{s}{s}.", .{ parent_dep_prefix, name });
769785
770 const index: Configuration.Package.Index = @fromBackingInt(@intCast(wc.packages.items.len));
771 try s.package_map.put(arena, hash, {});
772
773 try wc.packages.append(wc.gpa, .{786 try wc.packages.append(wc.gpa, .{
774 .dep_prefix = try wc.addString(dep_prefix),787 .dep_prefix = try wc.addString(dep_prefix),
775 .hash = try wc.addString(hash),788 .hash = try wc.addString(hash),
...@@ -799,32 +812,58 @@ fn addPackageInstance(s: *Serialize, b: *std.Build) Allocator.Error!void {...@@ -799,32 +812,58 @@ fn addPackageInstance(s: *Serialize, b: *std.Build) Allocator.Error!void {
799 const arena = s.arena;812 const arena = s.arena;
800 const wc = s.wc;813 const wc = s.wc;
801814
815 const index = s.package_instance_map.getIndex(b).?;
816
802 const options = try arena.alloc(817 const options = try arena.alloc(
803 Configuration.PackageInstance.UserInputOption.Index,818 Configuration.PackageInstance.UserInputOption.Index,
804 b.user_input_options.count(),819 b.user_input_options.count(),
805 );820 );
806821
807 var i: usize = 0;822 {
808 var iter = b.user_input_options.valueIterator();823 var i: usize = 0;
809 while (iter.next()) |option| : (i += 1) {824 var iter = b.user_input_options.valueIterator();
810 options[i] = try wc.addExtra(Configuration.PackageInstance.UserInputOption, .{825 while (iter.next()) |option| : (i += 1) {
811 .flags = .{826 options[i] = try wc.addExtra(Configuration.PackageInstance.UserInputOption, .{
812 .tag = .init(option.value),827 .flags = .{
813 .used = option.used,828 .tag = .init(option.value),
814 },829 .used = option.used,
815 .name = try wc.addString(option.name),830 },
816 .value = .{ .u = try s.makeUserValue(&option.value) },831 .name = try wc.addString(option.name),
817 });832 .value = .{ .u = try s.makeUserValue(&option.value) },
833 });
834 }
835 }
836
837 const modules_keys = try arena.alloc(
838 []const u8,
839 b.modules.count(),
840 );
841 const modules_values = try arena.alloc(
842 Configuration.Module.Index,
843 b.modules.count(),
844 );
845
846 {
847 var i: usize = 0;
848 var iter = b.modules.iterator();
849 while (iter.next()) |entry| : (i += 1) {
850 modules_keys[i] = entry.key_ptr.*;
851 modules_values[i] = try s.addModule(entry.value_ptr.*);
852 }
818 }853 }
819854
820 try wc.package_instances.append(wc.gpa, .{855 wc.package_instances.items[index] = .{
821 .package = s.packageFromHash(b.pkg_hash),856 .package = s.packageFromHash(b.pkg_hash),
822 .user_input_options = try wc.addDeduped(Configuration.PackageInstance.UserInputOption.List, .{857 .user_input_options = try wc.addDeduped(Configuration.PackageInstance.UserInputOption.List, .{
823 .options = .{ .slice = options },858 .options = .{ .slice = options },
824 }),859 }),
825 });860 .modules = .{
826861 .keys = try wc.addStringList(modules_keys),
827 try s.package_instance_map.put(arena, b, {});862 .values = try wc.addDeduped(Configuration.Module.List, .{
863 .modules = .{ .slice = modules_values },
864 }),
865 },
866 };
828}867}
829868
830fn makeUserValue(s: *Serialize, user_value: *const std.Build.UserValue) Allocator.Error!Configuration.PackageInstance.UserValue {869fn makeUserValue(s: *Serialize, user_value: *const std.Build.UserValue) Allocator.Error!Configuration.PackageInstance.UserValue {
...@@ -1336,7 +1375,7 @@ fn addModule(s: *Serialize, m: *std.Build.Module) !Configuration.Module.Index {...@@ -1336,7 +1375,7 @@ fn addModule(s: *Serialize, m: *std.Build.Module) !Configuration.Module.Index {
1336 const c_macros = try initStringList(s, m.c_macros.items);1375 const c_macros = try initStringList(s, m.c_macros.items);
1337 const export_symbol_names = try initStringList(s, m.export_symbol_names);1376 const export_symbol_names = try initStringList(s, m.export_symbol_names);
13381377
1339 const module_index: Configuration.Module.Index = try wc.addExtra(Configuration.Module, .{1378 const module_index: Configuration.Module.Index = try wc.addDeduped(Configuration.Module, .{
1340 .flags = .{1379 .flags = .{
1341 .optimize = .init(m.optimize),1380 .optimize = .init(m.optimize),
1342 .strip = .init(m.strip),1381 .strip = .init(m.strip),