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 {
124124 }
125125 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
127135 try sf.end();
128136 }
129137 }
lib/std/Build/Configuration.zig+24-2
......@@ -1679,6 +1679,7 @@ pub const Package = extern struct {
16791679pub const PackageInstance = extern struct {
16801680 package: Package.Index,
16811681 user_input_options: UserInputOption.List.Index,
1682 modules: PublicModules,
16821683
16831684 pub const UserInputOption = struct {
16841685 flags: Flags,
......@@ -1753,6 +1754,11 @@ pub const PackageInstance = extern struct {
17531754 };
17541755 };
17551756
1757 pub const PublicModules = extern struct {
1758 keys: StringList,
1759 values: Module.List.Index,
1760 };
1761
17561762 pub const Index = enum(u32) {
17571763 root,
17581764 _,
......@@ -1862,8 +1868,6 @@ pub const Module = struct {
18621868 }
18631869 };
18641870
1865 pub const Index = IndexType(@This());
1866
18671871 pub const Flags = packed struct(u32) {
18681872 optimize: Optimize,
18691873 strip: DefaultingBool,
......@@ -1934,6 +1938,24 @@ pub const Module = struct {
19341938 _: u30 = 0,
19351939 };
19361940 };
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 };
19371959};
19381960
19391961pub 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
2828 // instances.
2929 try s.traversePackages(b);
3030
31 // Seed the package_instance_map, which is later used in calls to
31 // Next, seed the package_instance_map, which is later used in calls to
3232 // 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, {});
3443 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
3750 try wc.path_deps.ensureTotalCapacityPrecise(gpa, graph.configure_dependencies.items.len);
3851 for (
......@@ -763,13 +776,13 @@ fn makePackageDep(s: *Serialize, parent_dep_prefix: []const u8, name: []const u8
763776 .package = @fromBackingInt(@intCast(index + 1)),
764777 };
765778
779 const index: Configuration.Package.Index = @fromBackingInt(@intCast(wc.packages.items.len));
780 try s.package_map.put(arena, hash, {});
781
766782 const entry = std.Build.package_map.get(hash) orelse unreachable;
767783
768784 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
773786 try wc.packages.append(wc.gpa, .{
774787 .dep_prefix = try wc.addString(dep_prefix),
775788 .hash = try wc.addString(hash),
......@@ -799,32 +812,58 @@ fn addPackageInstance(s: *Serialize, b: *std.Build) Allocator.Error!void {
799812 const arena = s.arena;
800813 const wc = s.wc;
801814
815 const index = s.package_instance_map.getIndex(b).?;
816
802817 const options = try arena.alloc(
803818 Configuration.PackageInstance.UserInputOption.Index,
804819 b.user_input_options.count(),
805820 );
806821
807 var i: usize = 0;
808 var iter = b.user_input_options.valueIterator();
809 while (iter.next()) |option| : (i += 1) {
810 options[i] = try wc.addExtra(Configuration.PackageInstance.UserInputOption, .{
811 .flags = .{
812 .tag = .init(option.value),
813 .used = option.used,
814 },
815 .name = try wc.addString(option.name),
816 .value = .{ .u = try s.makeUserValue(&option.value) },
817 });
822 {
823 var i: usize = 0;
824 var iter = b.user_input_options.valueIterator();
825 while (iter.next()) |option| : (i += 1) {
826 options[i] = try wc.addExtra(Configuration.PackageInstance.UserInputOption, .{
827 .flags = .{
828 .tag = .init(option.value),
829 .used = option.used,
830 },
831 .name = try wc.addString(option.name),
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 }
818853 }
819854
820 try wc.package_instances.append(wc.gpa, .{
855 wc.package_instances.items[index] = .{
821856 .package = s.packageFromHash(b.pkg_hash),
822857 .user_input_options = try wc.addDeduped(Configuration.PackageInstance.UserInputOption.List, .{
823858 .options = .{ .slice = options },
824859 }),
825 });
826
827 try s.package_instance_map.put(arena, b, {});
860 .modules = .{
861 .keys = try wc.addStringList(modules_keys),
862 .values = try wc.addDeduped(Configuration.Module.List, .{
863 .modules = .{ .slice = modules_values },
864 }),
865 },
866 };
828867}
829868
830869fn 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 {
13361375 const c_macros = try initStringList(s, m.c_macros.items);
13371376 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, .{
13401379 .flags = .{
13411380 .optimize = .init(m.optimize),
13421381 .strip = .init(m.strip),