Skip to content

Commit ef8fc4d

Browse files
committed
fix(course/15): 适配 Zig 0.15 构建系统与 IO 变更
- 全面替换 std.io.getStdOut/getStdErr.writer 为 std.fs.File.stdout/stderr().writer 并增加缓冲区 - 构建脚本统一使用 .root_module = b.createModule 语法,修正 addModule 用法 - 更新 hello_world、build_system、package_management_importer 等示例以适配新 IO API - 修正 tinytetris、options 等 build.zig 文件的模块创建方式 - 升级 zig-msgpack 依赖至 0.0.8,更新 hash 与 url - 增加 Windows 下 echo_tcp_server.zig 自动链接 ws2_32 库 - 移除冗余注释,优化代码风格与一致性
1 parent 52eefaa commit ef8fc4d

File tree

15 files changed

+79
-96
lines changed

15 files changed

+79
-96
lines changed

build/0.15.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ pub fn build(b: *Build) void {
5252
});
5353
exe.linkLibC();
5454

55+
if (exe.root_module.resolved_target.?.result.os.tag == .windows and std.mem.eql(u8, "echo_tcp_server.zig", entry.name)) {
56+
std.log.info("link ws2_32 for {s}", .{entry.name});
57+
exe.linkSystemLibrary("ws2_32");
58+
}
5559
// add to default install
5660
b.installArtifact(exe);
5761

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
const std = @import("std");
22

33
pub fn build(b: *std.Build) void {
4-
// 标准构建目标
54
const target = b.standardTargetOptions(.{});
6-
7-
// 标准构建模式
85
const optimize = b.standardOptimizeOption(.{});
96

10-
// 添加一个二进制可执行程序构建
117
const exe = b.addExecutable(.{
128
.name = "zig",
13-
.root_module = b.addModule("zig", .{
9+
.root_module = b.createModule(.{
1410
.root_source_file = b.path("src/main.zig"),
1511
.target = target,
1612
.optimize = optimize,
1713
}),
1814
});
1915

20-
// 添加到顶级 install step 中作为依赖
2116
b.installArtifact(exe);
2217
}

course/code/15/build_system/basic/src/main.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ pub fn main() !void {
77
// stdout is for the actual output of your application, for example if you
88
// are implementing gzip, then only the compressed bytes should be sent to
99
// stdout, not any debugging messages.
10-
const stdout_file = std.io.getStdOut().writer();
11-
var bw = std.io.bufferedWriter(stdout_file);
12-
const stdout = bw.writer();
10+
var stdout_buffer: [1024]u8 = undefined;
11+
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
12+
const stdout = &stdout_writer.interface;
13+
1314
try stdout.print("Run `zig build test` to run the tests.\n", .{});
1415

15-
try bw.flush(); // don't forget to flush!
16+
try stdout.flush();
1617
}
1718

1819
test "simple test" {
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
const std = @import("std");
22

33
pub fn build(b: *std.Build) void {
4-
// 标准构建目标
54
const target = b.standardTargetOptions(.{});
6-
7-
// 标准构建模式
85
const optimize = b.standardOptimizeOption(.{});
9-
10-
// 使用 option 来获取命令参数决定是否剥离调试信息
116
const is_strip =
127
b.option(bool, "is_strip", "whether strip executable") orelse
138
false;
149

15-
// 添加一个二进制可执行程序构建
1610
const exe = b.addExecutable(.{
1711
.name = "zig",
18-
.root_module = b.addModule("zig", .{
12+
.root_module = b.createModule(.{
1913
.root_source_file = b.path("src/main.zig"),
2014
.target = target,
2115
.optimize = optimize,
22-
// 设置 exe 的 strip
2316
.strip = is_strip,
2417
}),
2518
});
2619

27-
// 添加到顶级 install step 中作为依赖
2820
b.installArtifact(exe);
2921
}

course/code/15/build_system/cli/src/main.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ pub fn main() !void {
77
// stdout is for the actual output of your application, for example if you
88
// are implementing gzip, then only the compressed bytes should be sent to
99
// stdout, not any debugging messages.
10-
const stdout_file = std.io.getStdOut().writer();
11-
var bw = std.io.bufferedWriter(stdout_file);
12-
const stdout = bw.writer();
10+
var stdout_buffer: [1024]u8 = undefined;
11+
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
12+
const stdout = &stdout_writer.interface;
1313

1414
try stdout.print("Run `zig build test` to run the tests.\n", .{});
1515

16-
try bw.flush(); // don't forget to flush!
16+
try stdout.flush(); // don't forget to flush!
1717
}
1818

1919
test "simple test" {

course/code/15/build_system/lib/build.zig

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,24 @@ pub fn build(b: *std.Build) void {
88
const optimize = b.standardOptimizeOption(.{});
99

1010
// 尝试添加一个静态库
11-
const lib = b.addStaticLibrary(.{
12-
// 库的名字
13-
.name = "example",
14-
// 源文件地址
15-
.root_module = b.addModule("example", .{
16-
.root_source_file = b.path("src/root.zig"),
17-
// 构建目标
18-
.target = target,
19-
// 构建模式
20-
.optimize = optimize,
21-
}),
22-
});
11+
const lib = b.addLibrary(.{ .name = "example", .root_module = b.createModule(.{
12+
.root_source_file = b.path("src/root.zig"),
13+
.target = target,
14+
.optimize = optimize,
15+
}) });
2316

24-
// 这代替原本的 lib.install,在构建时自动构建 lib
25-
// 但其实这是不必要的,因为如果有可执行二进制程序构建使用了 lib,那么它会自动被构建
2617
b.installArtifact(lib);
2718

28-
// 添加一个二进制可执行程序构建
2919
const exe = b.addExecutable(.{
3020
.name = "zig",
31-
.root_module = b.addModule("zig", .{
21+
.root_module = b.createModule(.{
3222
.root_source_file = b.path("src/main.zig"),
3323
.target = target,
3424
.optimize = optimize,
3525
}),
3626
});
37-
// 链接 lib
27+
3828
exe.linkLibrary(lib);
3929

40-
// 添加到顶级 install step 中作为依赖,构建 exe
4130
b.installArtifact(exe);
4231
}

course/code/15/build_system/lib/src/main.zig

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,11 @@ pub fn main() !void {
77
// stdout is for the actual output of your application, for example if you
88
// are implementing gzip, then only the compressed bytes should be sent to
99
// stdout, not any debugging messages.
10-
const stdout_file = std.io.getStdOut().writer();
11-
var bw = std.io.bufferedWriter(stdout_file);
12-
const stdout = bw.writer();
10+
var stdout_buffer: [1024]u8 = undefined;
11+
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
12+
const stdout = &stdout_writer.interface;
1313

1414
try stdout.print("Run `zig build test` to run the tests.\n", .{});
1515

16-
try bw.flush(); // don't forget to flush!
17-
}
18-
19-
test "simple test" {
20-
var list = std.ArrayList(i32).init(std.testing.allocator);
21-
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
22-
try list.append(42);
23-
try std.testing.expectEqual(@as(i32, 42), list.pop());
16+
try stdout.flush();
2417
}

course/code/15/build_system/options/build.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ pub fn build(b: *std.Build) void {
1010
// 添加一个二进制可执行程序构建
1111
const exe = b.addExecutable(.{
1212
.name = "zig",
13-
.root_source_file = b.path("src/main.zig"),
14-
.target = target,
15-
.optimize = optimize,
13+
.root_module = b.createModule(.{
14+
.root_source_file = b.path("src/main.zig"),
15+
.target = target,
16+
.optimize = optimize,
17+
}),
1618
});
1719

1820
// 通过标准库获取时间戳

course/code/15/build_system/step/src/main.zig

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,11 @@ pub fn main() !void {
77
// stdout is for the actual output of your application, for example if you
88
// are implementing gzip, then only the compressed bytes should be sent to
99
// stdout, not any debugging messages.
10-
const stdout_file = std.io.getStdOut().writer();
11-
var bw = std.io.bufferedWriter(stdout_file);
12-
const stdout = bw.writer();
10+
var stdout_buffer: [1024]u8 = undefined;
11+
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
12+
const stdout = &stdout_writer.interface;
1313

1414
try stdout.print("Run `zig build test` to run the tests.\n", .{});
1515

16-
try bw.flush(); // don't forget to flush!
17-
}
18-
19-
test "simple test" {
20-
var list = std.ArrayList(i32).init(std.testing.allocator);
21-
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
22-
try list.append(42);
23-
try std.testing.expectEqual(@as(i32, 42), list.pop());
16+
try stdout.flush();
2417
}

course/code/15/build_system/test/src/main.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ pub fn main() !void {
77
// stdout is for the actual output of your application, for example if you
88
// are implementing gzip, then only the compressed bytes should be sent to
99
// stdout, not any debugging messages.
10-
const stdout_file = std.io.getStdOut().writer();
11-
var bw = std.io.bufferedWriter(stdout_file);
12-
const stdout = bw.writer();
10+
var stdout_buffer: [1024]u8 = undefined;
11+
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
12+
const stdout = &stdout_writer.interface;
1313

1414
try stdout.print("Run `zig build test` to run the tests.\n", .{});
1515

16-
try bw.flush(); // don't forget to flush!
16+
try stdout.flush();
1717
}
1818

1919
test "simple test" {

0 commit comments

Comments
 (0)