Skip to content

Commit f7bc55c

Browse files
authored
Merge pull request #17392 from ziglang/fetch
rework package manager
2 parents 75b48ef + 95907cb commit f7bc55c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2654
-2100
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ set(ZIG_STAGE2_SOURCES
528528
"${CMAKE_SOURCE_DIR}/src/Liveness.zig"
529529
"${CMAKE_SOURCE_DIR}/src/Module.zig"
530530
"${CMAKE_SOURCE_DIR}/src/Package.zig"
531-
"${CMAKE_SOURCE_DIR}/src/Package/hash.zig"
531+
"${CMAKE_SOURCE_DIR}/src/Package/Fetch.zig"
532532
"${CMAKE_SOURCE_DIR}/src/RangeSet.zig"
533533
"${CMAKE_SOURCE_DIR}/src/Sema.zig"
534534
"${CMAKE_SOURCE_DIR}/src/TypedValue.zig"

build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub fn build(b: *std.Build) !void {
8888
.name = "check-case",
8989
.root_source_file = .{ .path = "test/src/Cases.zig" },
9090
.optimize = optimize,
91-
.main_pkg_path = .{ .path = "." },
91+
.main_mod_path = .{ .path = "." },
9292
});
9393
check_case_exe.stack_size = stack_size;
9494
check_case_exe.single_threaded = single_threaded;

doc/build.zig.zon.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# build.zig.zon Documentation
2+
3+
This is the manifest file for build.zig scripts. It is named build.zig.zon in
4+
order to make it clear that it is metadata specifically pertaining to
5+
build.zig.
6+
7+
- **build root** - the directory that contains `build.zig`
8+
9+
## Top-Level Fields
10+
11+
### `name`
12+
13+
String. Required.
14+
15+
### `version`
16+
17+
String. Required.
18+
19+
[semver](https://semver.org/)
20+
21+
### `dependencies`
22+
23+
Struct.
24+
25+
Each dependency must either provide a `url` and `hash`, or a `path`.
26+
27+
#### `url`
28+
29+
String.
30+
31+
When updating this field to a new URL, be sure to delete the corresponding
32+
`hash`, otherwise you are communicating that you expect to find the old hash at
33+
the new URL.
34+
35+
#### `hash`
36+
37+
String.
38+
39+
[multihash](https://multiformats.io/multihash/)
40+
41+
This is computed from the file contents of the directory of files that is
42+
obtained after fetching `url` and applying the inclusion rules given by
43+
`paths`.
44+
45+
This field is the source of truth; packages do not come from an `url`; they
46+
come from a `hash`. `url` is just one of many possible mirrors for how to
47+
obtain a package matching this `hash`.
48+
49+
#### `path`
50+
51+
String.
52+
53+
When this is provided, the package is found in a directory relative to the
54+
build root. In this case the package's hash is irrelevant and therefore not
55+
computed.
56+
57+
### `paths`
58+
59+
List. Required.
60+
61+
Specifies the set of files and directories that are included in this package.
62+
Paths are relative to the build root. Use the empty string (`""`) to refer to
63+
the build root itself.
64+
65+
Only files included in the package are used to compute a package's `hash`.

lib/build_runner.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ fn usage(builder: *std.Build, already_ran_build: bool, out_stream: anytype) !voi
997997
\\ -j<N> Limit concurrent jobs (default is to use all CPU cores)
998998
\\ --maxrss <bytes> Limit memory usage (default is to use available memory)
999999
\\ --skip-oom-steps Instead of failing, skip steps that would exceed --maxrss
1000+
\\ --fetch Exit after fetching dependency tree
10001001
\\
10011002
\\Project-Specific Options:
10021003
\\

lib/std/Build.zig

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,9 @@ pub const ExecutableOptions = struct {
634634
use_llvm: ?bool = null,
635635
use_lld: ?bool = null,
636636
zig_lib_dir: ?LazyPath = null,
637+
main_mod_path: ?LazyPath = null,
638+
639+
/// Deprecated; use `main_mod_path`.
637640
main_pkg_path: ?LazyPath = null,
638641
};
639642

@@ -652,7 +655,7 @@ pub fn addExecutable(b: *Build, options: ExecutableOptions) *Step.Compile {
652655
.use_llvm = options.use_llvm,
653656
.use_lld = options.use_lld,
654657
.zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir,
655-
.main_pkg_path = options.main_pkg_path,
658+
.main_mod_path = options.main_mod_path orelse options.main_pkg_path,
656659
});
657660
}
658661

@@ -667,6 +670,9 @@ pub const ObjectOptions = struct {
667670
use_llvm: ?bool = null,
668671
use_lld: ?bool = null,
669672
zig_lib_dir: ?LazyPath = null,
673+
main_mod_path: ?LazyPath = null,
674+
675+
/// Deprecated; use `main_mod_path`.
670676
main_pkg_path: ?LazyPath = null,
671677
};
672678

@@ -683,7 +689,7 @@ pub fn addObject(b: *Build, options: ObjectOptions) *Step.Compile {
683689
.use_llvm = options.use_llvm,
684690
.use_lld = options.use_lld,
685691
.zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir,
686-
.main_pkg_path = options.main_pkg_path,
692+
.main_mod_path = options.main_mod_path orelse options.main_pkg_path,
687693
});
688694
}
689695

@@ -699,6 +705,9 @@ pub const SharedLibraryOptions = struct {
699705
use_llvm: ?bool = null,
700706
use_lld: ?bool = null,
701707
zig_lib_dir: ?LazyPath = null,
708+
main_mod_path: ?LazyPath = null,
709+
710+
/// Deprecated; use `main_mod_path`.
702711
main_pkg_path: ?LazyPath = null,
703712
};
704713

@@ -717,7 +726,7 @@ pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile
717726
.use_llvm = options.use_llvm,
718727
.use_lld = options.use_lld,
719728
.zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir,
720-
.main_pkg_path = options.main_pkg_path,
729+
.main_mod_path = options.main_mod_path orelse options.main_pkg_path,
721730
});
722731
}
723732

@@ -733,6 +742,9 @@ pub const StaticLibraryOptions = struct {
733742
use_llvm: ?bool = null,
734743
use_lld: ?bool = null,
735744
zig_lib_dir: ?LazyPath = null,
745+
main_mod_path: ?LazyPath = null,
746+
747+
/// Deprecated; use `main_mod_path`.
736748
main_pkg_path: ?LazyPath = null,
737749
};
738750

@@ -751,7 +763,7 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile
751763
.use_llvm = options.use_llvm,
752764
.use_lld = options.use_lld,
753765
.zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir,
754-
.main_pkg_path = options.main_pkg_path,
766+
.main_mod_path = options.main_mod_path orelse options.main_pkg_path,
755767
});
756768
}
757769

@@ -769,6 +781,9 @@ pub const TestOptions = struct {
769781
use_llvm: ?bool = null,
770782
use_lld: ?bool = null,
771783
zig_lib_dir: ?LazyPath = null,
784+
main_mod_path: ?LazyPath = null,
785+
786+
/// Deprecated; use `main_mod_path`.
772787
main_pkg_path: ?LazyPath = null,
773788
};
774789

@@ -787,7 +802,7 @@ pub fn addTest(b: *Build, options: TestOptions) *Step.Compile {
787802
.use_llvm = options.use_llvm,
788803
.use_lld = options.use_lld,
789804
.zig_lib_dir = options.zig_lib_dir orelse b.zig_lib_dir,
790-
.main_pkg_path = options.main_pkg_path,
805+
.main_mod_path = options.main_mod_path orelse options.main_pkg_path,
791806
});
792807
}
793808

lib/std/Build/Cache.zig

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ pub const Directory = struct {
99
path: ?[]const u8,
1010
handle: fs.Dir,
1111

12+
pub fn clone(d: Directory, arena: Allocator) Allocator.Error!Directory {
13+
return .{
14+
.path = if (d.path) |p| try arena.dupe(u8, p) else null,
15+
.handle = d.handle,
16+
};
17+
}
18+
19+
pub fn cwd() Directory {
20+
return .{
21+
.path = null,
22+
.handle = fs.cwd(),
23+
};
24+
}
25+
1226
pub fn join(self: Directory, allocator: Allocator, paths: []const []const u8) ![]u8 {
1327
if (self.path) |p| {
1428
// TODO clean way to do this with only 1 allocation
@@ -47,12 +61,16 @@ pub const Directory = struct {
4761
writer: anytype,
4862
) !void {
4963
_ = options;
50-
if (fmt_string.len != 0) fmt.invalidFmtError(fmt, self);
64+
if (fmt_string.len != 0) fmt.invalidFmtError(fmt_string, self);
5165
if (self.path) |p| {
5266
try writer.writeAll(p);
5367
try writer.writeAll(fs.path.sep_str);
5468
}
5569
}
70+
71+
pub fn eql(self: Directory, other: Directory) bool {
72+
return self.handle.fd == other.handle.fd;
73+
}
5674
};
5775

5876
gpa: Allocator,

lib/std/Build/Step/Compile.zig

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ c_std: std.Build.CStd,
6868
/// Set via options; intended to be read-only after that.
6969
zig_lib_dir: ?LazyPath,
7070
/// Set via options; intended to be read-only after that.
71-
main_pkg_path: ?LazyPath,
71+
main_mod_path: ?LazyPath,
7272
exec_cmd_args: ?[]const ?[]const u8,
7373
filter: ?[]const u8,
7474
test_evented_io: bool = false,
@@ -316,6 +316,9 @@ pub const Options = struct {
316316
use_llvm: ?bool = null,
317317
use_lld: ?bool = null,
318318
zig_lib_dir: ?LazyPath = null,
319+
main_mod_path: ?LazyPath = null,
320+
321+
/// deprecated; use `main_mod_path`.
319322
main_pkg_path: ?LazyPath = null,
320323
};
321324

@@ -480,7 +483,7 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
480483
.installed_headers = ArrayList(*Step).init(owner.allocator),
481484
.c_std = std.Build.CStd.C99,
482485
.zig_lib_dir = null,
483-
.main_pkg_path = null,
486+
.main_mod_path = null,
484487
.exec_cmd_args = null,
485488
.filter = options.filter,
486489
.test_runner = options.test_runner,
@@ -515,8 +518,8 @@ pub fn create(owner: *std.Build, options: Options) *Compile {
515518
lp.addStepDependencies(&self.step);
516519
}
517520

518-
if (options.main_pkg_path) |lp| {
519-
self.main_pkg_path = lp.dupe(self.step.owner);
521+
if (options.main_mod_path orelse options.main_pkg_path) |lp| {
522+
self.main_mod_path = lp.dupe(self.step.owner);
520523
lp.addStepDependencies(&self.step);
521524
}
522525

@@ -1998,8 +2001,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
19982001
try zig_args.append(dir.getPath(b));
19992002
}
20002003

2001-
if (self.main_pkg_path) |dir| {
2002-
try zig_args.append("--main-pkg-path");
2004+
if (self.main_mod_path) |dir| {
2005+
try zig_args.append("--main-mod-path");
20032006
try zig_args.append(dir.getPath(b));
20042007
}
20052008

lib/std/array_hash_map.zig

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,14 +1229,41 @@ pub fn ArrayHashMapUnmanaged(
12291229
/// Sorts the entries and then rebuilds the index.
12301230
/// `sort_ctx` must have this method:
12311231
/// `fn lessThan(ctx: @TypeOf(ctx), a_index: usize, b_index: usize) bool`
1232+
/// Uses a stable sorting algorithm.
12321233
pub inline fn sort(self: *Self, sort_ctx: anytype) void {
12331234
if (@sizeOf(ByIndexContext) != 0)
12341235
@compileError("Cannot infer context " ++ @typeName(Context) ++ ", call sortContext instead.");
1235-
return self.sortContext(sort_ctx, undefined);
1236+
return sortContextInternal(self, .stable, sort_ctx, undefined);
12361237
}
12371238

1238-
pub fn sortContext(self: *Self, sort_ctx: anytype, ctx: Context) void {
1239-
self.entries.sort(sort_ctx);
1239+
/// Sorts the entries and then rebuilds the index.
1240+
/// `sort_ctx` must have this method:
1241+
/// `fn lessThan(ctx: @TypeOf(ctx), a_index: usize, b_index: usize) bool`
1242+
/// Uses an unstable sorting algorithm.
1243+
pub inline fn sortUnstable(self: *Self, sort_ctx: anytype) void {
1244+
if (@sizeOf(ByIndexContext) != 0)
1245+
@compileError("Cannot infer context " ++ @typeName(Context) ++ ", call sortUnstableContext instead.");
1246+
return self.sortContextInternal(.unstable, sort_ctx, undefined);
1247+
}
1248+
1249+
pub inline fn sortContext(self: *Self, sort_ctx: anytype, ctx: Context) void {
1250+
return sortContextInternal(self, .stable, sort_ctx, ctx);
1251+
}
1252+
1253+
pub inline fn sortUnstableContext(self: *Self, sort_ctx: anytype, ctx: Context) void {
1254+
return sortContextInternal(self, .unstable, sort_ctx, ctx);
1255+
}
1256+
1257+
fn sortContextInternal(
1258+
self: *Self,
1259+
comptime mode: std.sort.Mode,
1260+
sort_ctx: anytype,
1261+
ctx: Context,
1262+
) void {
1263+
switch (mode) {
1264+
.stable => self.entries.sort(sort_ctx),
1265+
.unstable => self.entries.sortUnstable(sort_ctx),
1266+
}
12401267
const header = self.index_header orelse return;
12411268
header.reset();
12421269
self.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, header);

lib/std/fs/path.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,15 +728,17 @@ pub fn resolvePosix(allocator: Allocator, paths: []const []const u8) Allocator.E
728728
}
729729
}
730730

731-
test "resolve" {
731+
test resolve {
732732
try testResolveWindows(&[_][]const u8{ "a\\b\\c\\", "..\\..\\.." }, ".");
733733
try testResolveWindows(&[_][]const u8{"."}, ".");
734+
try testResolveWindows(&[_][]const u8{""}, ".");
734735

735736
try testResolvePosix(&[_][]const u8{ "a/b/c/", "../../.." }, ".");
736737
try testResolvePosix(&[_][]const u8{"."}, ".");
738+
try testResolvePosix(&[_][]const u8{""}, ".");
737739
}
738740

739-
test "resolveWindows" {
741+
test resolveWindows {
740742
try testResolveWindows(
741743
&[_][]const u8{ "Z:\\", "/usr/local", "lib\\zig\\std\\array_list.zig" },
742744
"Z:\\usr\\local\\lib\\zig\\std\\array_list.zig",
@@ -764,7 +766,7 @@ test "resolveWindows" {
764766
try testResolveWindows(&[_][]const u8{"a/b"}, "a\\b");
765767
}
766768

767-
test "resolvePosix" {
769+
test resolvePosix {
768770
try testResolvePosix(&.{ "/a/b", "c" }, "/a/b/c");
769771
try testResolvePosix(&.{ "/a/b", "c", "//d", "e///" }, "/d/e");
770772
try testResolvePosix(&.{ "/a/b/c", "..", "../" }, "/a");

lib/std/multi_array_list.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ pub fn MultiArrayList(comptime T: type) type {
467467

468468
/// `ctx` has the following method:
469469
/// `fn lessThan(ctx: @TypeOf(ctx), a_index: usize, b_index: usize) bool`
470-
fn sortInternal(self: Self, a: usize, b: usize, ctx: anytype, comptime mode: enum { stable, unstable }) void {
470+
fn sortInternal(self: Self, a: usize, b: usize, ctx: anytype, comptime mode: std.sort.Mode) void {
471471
const sort_context: struct {
472472
sub_ctx: @TypeOf(ctx),
473473
slice: Slice,

lib/std/process.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn getCwdAlloc(allocator: Allocator) ![]u8 {
4646
}
4747
}
4848

49-
test "getCwdAlloc" {
49+
test getCwdAlloc {
5050
if (builtin.os.tag == .wasi) return error.SkipZigTest;
5151

5252
const cwd = try getCwdAlloc(testing.allocator);

lib/std/sort.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const testing = std.testing;
44
const mem = std.mem;
55
const math = std.math;
66

7+
pub const Mode = enum { stable, unstable };
8+
79
pub const block = @import("sort/block.zig").block;
810
pub const pdq = @import("sort/pdq.zig").pdq;
911
pub const pdqContext = @import("sort/pdq.zig").pdqContext;

0 commit comments

Comments
 (0)