@@ -61,6 +61,8 @@ oom_flag: bool,
61
61
/// Contains shared state among all `Fetch` tasks.
62
62
pub const JobQueue = struct {
63
63
mutex : std.Thread.Mutex = .{},
64
+ /// It's an array hash map so that it can be sorted before rendering the
65
+ /// dependencies.zig source file.
64
66
/// Protected by `mutex`.
65
67
table : Table = .{},
66
68
/// `table` may be missing some tasks such as ones that failed, so this
@@ -75,7 +77,7 @@ pub const JobQueue = struct {
75
77
recursive : bool ,
76
78
work_around_btrfs_bug : bool ,
77
79
78
- pub const Table = std .AutoHashMapUnmanaged (Manifest .MultiHashHexDigest , * Fetch );
80
+ pub const Table = std .AutoArrayHashMapUnmanaged (Manifest .MultiHashHexDigest , * Fetch );
79
81
80
82
pub fn deinit (jq : * JobQueue ) void {
81
83
if (jq .all_fetches .items .len == 0 ) return ;
@@ -105,17 +107,74 @@ pub const JobQueue = struct {
105
107
106
108
/// Creates the dependencies.zig file and corresponding `Module` for the
107
109
/// build runner to obtain via `@import("@dependencies")`.
108
- pub fn createDependenciesModule (
109
- jq : * JobQueue ,
110
- arena : Allocator ,
111
- local_cache_directory : Cache.Directory ,
112
- basename : []const u8 ,
113
- ) ! * Package.Module {
114
- _ = jq ;
115
- _ = arena ;
116
- _ = local_cache_directory ;
117
- _ = basename ;
118
- @panic ("TODO: createDependenciesModule" );
110
+ pub fn createDependenciesModule (jq : * JobQueue , buf : * std .ArrayList (u8 )) Allocator.Error ! void {
111
+ try buf .appendSlice ("pub const packages = struct {\n " );
112
+
113
+ // Ensure the generated .zig file is deterministic.
114
+ jq .table .sortUnstable (@as (struct {
115
+ keys : []const Manifest.MultiHashHexDigest ,
116
+ pub fn lessThan (ctx : @This (), a_index : usize , b_index : usize ) bool {
117
+ return std .mem .lessThan (u8 , & ctx .keys [a_index ], & ctx .keys [b_index ]);
118
+ }
119
+ }, .{ .keys = jq .table .keys () }));
120
+
121
+ for (jq .table .keys ()[1.. ], jq .table .values ()[1.. ]) | hash , fetch | {
122
+ try buf .writer ().print (
123
+ \\ pub const {} = struct {{
124
+ \\ pub const build_root = "{q}";
125
+ \\
126
+ , .{ std .zig .fmtId (& hash ), fetch .package_root });
127
+
128
+ if (fetch .has_build_zig ) {
129
+ try buf .writer ().print (
130
+ \\ pub const build_zig = @import("{}");
131
+ \\
132
+ , .{std .zig .fmtEscapes (& hash )});
133
+ }
134
+
135
+ if (fetch .manifest ) | * manifest | {
136
+ try buf .appendSlice (
137
+ \\ pub const deps: []const struct { []const u8, []const u8 } = &.{
138
+ \\
139
+ );
140
+ for (manifest .dependencies .keys (), manifest .dependencies .values ()) | name , dep | {
141
+ try buf .writer ().print (
142
+ " .{{ \" {}\" , \" {}\" }},\n " ,
143
+ .{ std .zig .fmtEscapes (name ), std .zig .fmtEscapes (dep .hash .? ) },
144
+ );
145
+ }
146
+
147
+ try buf .appendSlice (
148
+ \\ };
149
+ \\ };
150
+ \\
151
+ );
152
+ } else {
153
+ try buf .appendSlice (
154
+ \\ pub const deps: []const struct { []const u8, []const u8 } = &.{};
155
+ \\ };
156
+ \\
157
+ );
158
+ }
159
+ }
160
+
161
+ try buf .appendSlice (
162
+ \\};
163
+ \\
164
+ \\pub const root_deps: []const struct { []const u8, []const u8 } = &.{
165
+ \\
166
+ );
167
+
168
+ const root_fetch = jq .all_fetches .items [0 ];
169
+ const root_manifest = & root_fetch .manifest .? ;
170
+
171
+ for (root_manifest .dependencies .keys (), root_manifest .dependencies .values ()) | name , dep | {
172
+ try buf .writer ().print (
173
+ " .{{ \" {}\" , \" {}\" }},\n " ,
174
+ .{ std .zig .fmtEscapes (name ), std .zig .fmtEscapes (dep .hash .? ) },
175
+ );
176
+ }
177
+ try buf .appendSlice ("};\n " );
119
178
}
120
179
};
121
180
0 commit comments