Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 23 additions & 27 deletions build/0.16.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const std = @import("std");
const Build = std.Build;
const ChildProcess = std.process.Child;

const log = std.log.scoped(.For_0_16_0);
const version = "16";

Expand All @@ -13,25 +11,31 @@ pub fn build(b: *Build) void {
// get target and optimize
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const io = b.graph.io;

var lazy_path = b.path(relative_path);

const full_path = lazy_path.getPath(b);

// open dir
var dir = std.fs.openDirAbsolute(full_path, .{ .iterate = true }) catch |err| {
log.err("open 15 path failed, err is {}", .{err});
var dir = std.Io.Dir.openDirAbsolute(io, full_path, .{ .iterate = true }) catch |err| {
log.err("open 16 path failed, err is {}", .{err});
std.process.exit(1);
};
defer dir.close();
defer dir.close(io);

// make a iterate for release ath
var iterate = dir.iterate();

while (iterate.next()) |val| {
if (val) |entry| {
while (iterate.next(io) catch |err| {
log.err("iterate examples_path failed, err is {}", .{err});
std.process.exit(1);
}) |entry| {
// get the entry name, entry can be file or directory
const output_name = std.mem.trimRight(u8, entry.name, ".zig");
const output_name = if (std.mem.endsWith(u8, entry.name, ".zig"))
entry.name[0 .. entry.name.len - ".zig".len]
else
entry.name;
if (entry.kind == .file) {
// connect path
const path = std.fs.path.join(b.allocator, &[_][]const u8{ relative_path, entry.name }) catch |err| {
Expand All @@ -48,11 +52,11 @@ pub fn build(b: *Build) void {
.optimize = optimize,
}),
});
exe.linkLibC();
exe.root_module.linkSystemLibrary("c", .{});

if (exe.root_module.resolved_target.?.result.os.tag == .windows and std.mem.eql(u8, "echo_tcp_server.zig", entry.name)) {
std.log.info("link ws2_32 for {s}", .{entry.name});
exe.linkSystemLibrary("ws2_32");
exe.root_module.linkSystemLibrary("ws2_32", .{});
}
// add to default install
b.installArtifact(exe);
Expand All @@ -75,8 +79,6 @@ pub fn build(b: *Build) void {
} else if (entry.kind == .directory) {

// build child process
var child = ChildProcess.init(&args, b.allocator);

// build cwd
const cwd = std.fs.path.join(b.allocator, &[_][]const u8{
full_path,
Expand All @@ -87,25 +89,19 @@ pub fn build(b: *Build) void {
};

// open entry dir
const entry_dir = std.fs.openDirAbsolute(cwd, .{}) catch unreachable;
entry_dir.access("build.zig", .{}) catch {
const entry_dir = std.Io.Dir.openDirAbsolute(io, cwd, .{}) catch unreachable;
defer entry_dir.close(io);

entry_dir.access(io, "build.zig", .{}) catch {
log.err("not found build.zig in path {s}", .{cwd});
std.process.exit(1);
};

// set child cwd
// this api maybe changed in the future
child.cwd = cwd;

// spawn and wait child process
_ = child.spawnAndWait() catch unreachable;
var child = std.process.spawn(io, .{
.argv = &args,
.cwd = .{ .path = cwd },
}) catch unreachable;
_ = child.wait(io) catch unreachable;
}
} else {
// Stop endless loop
break;
}
} else |err| {
log.err("iterate examples_path failed, err is {}", .{err});
std.process.exit(1);
}
}
8 changes: 8 additions & 0 deletions course/.vitepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ export default [
text: "版本说明",
collapsed: true,
items: [
{
text: "0.16.0 升级指南",
link: "/update/upgrade-0.16.0",
},
{
text: "0.16.0 版本说明",
link: "/update/0.16.0-description",
},
{
text: "0.15.1 升级指南",
link: "/update/upgrade-0.15.1",
Expand Down
2 changes: 1 addition & 1 deletion course/.vitepress/theme/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const version: string = "0.15.1";
const version: string = "0.16.0";

export { version };
2 changes: 1 addition & 1 deletion course/advanced/atomic.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,5 @@ outline: deep
- **Mutex**:适合等待时间不确定或较长的场景,会让出 CPU 给其他线程

::: warning ⚠️ 警告
不当使用自旋等待会导致 CPU 资源浪费。如果等待时间较长或不确定,应使用 `std.Thread.Mutex` 等同步原语
不当使用自旋等待会导致 CPU 资源浪费。如果等待时间较长或不确定,应使用 `std.Io.Mutex` 这类会让出 CPU 的同步原语
:::
2 changes: 1 addition & 1 deletion course/advanced/interact-with-c.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ C 语言共享类型通常是通过引入头文件实现,这点在 zig 中可

::: info 🅿️ 提示

注意:为了构建这个,我们需要引入 `libc`,可以在 `build.zig` 中添加 `exe.linkLibC` 函数,`exe` 是默认的构建变量
注意:为了构建这个,我们需要引入 `libc`。在 Zig 0.16 的构建脚本中,可以让对应模块链接 C 标准库,例如 `exe.root_module.linkSystemLibrary("c", .{})`

或者我们可以手动执行构建:`zig build-exe source.zig -lc`

Expand Down
6 changes: 3 additions & 3 deletions course/advanced/memory_manage.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ outline: deep

这是一个用于调试的分配器,现阶段适用于在调试模式下使用该分配器,它的性能并不高!

这个分配器的目的不是为了性能,而是为了安全,它支持线程安全,安全检查,检查是否存在泄露等特性,这些特性均可手动配置是否开启
这个分配器的目的不是为了性能,而是为了安全。默认配置下它支持线程安全、安全检查、泄漏检测等能力,并且这些特性都可以按需配置

<<<@/code/release/memory_manager.zig#DebugAllocator

Expand All @@ -66,7 +66,7 @@ outline: deep

## `FixedBufferAllocator`

这个分配器是固定大小的内存缓冲区,无法扩容,常常在你需要缓冲某些东西时使用注意默认情况下它不是线程安全的,但是存在着变体 [`ThreadSafeAllocator`](https://ziglang.org/documentation/master/std/#std.heap.ThreadSafeAllocator),使用 `ThreadSafeAllocator` 包裹一下它即可
这个分配器是固定大小的内存缓冲区,无法扩容,常常在你需要缓冲某些东西时使用注意默认情况下它不是线程安全的;而在 Zig 0.16 中,`ThreadSafeAllocator` 已被移除。如果你需要跨线程共享同一个 `FixedBufferAllocator`,应当像示例那样在外层自行加锁,或者直接改用更适合并发场景的 `SmpAllocator`

::: code-group

Expand All @@ -84,7 +84,7 @@ outline: deep

## `c_allocator`

这是纯粹的 C 的 `malloc`,它会直接尝试调用 C 库的内存分配,使用它需要在 `build.zig` 中添加上 `linkLibC` 功能
这是纯粹的 C 的 `malloc`,它会直接尝试调用 C 库的内存分配。使用它时,需要在 `build.zig` 中让对应模块链接 libc,例如 `exe.root_module.linkSystemLibrary("c", .{})`

<<<@/code/release/memory_manager.zig#c_allocator

Expand Down
4 changes: 2 additions & 2 deletions course/advanced/result-location.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ outline: deep

<<<@/code/release/result-location.zig#stdlib_arraylist

### GeneralPurposeAllocator
### DebugAllocator

<<<@/code/release/result-location.zig#stdlib_gpa
<<<@/code/release/result-location.zig#stdlib_debug_allocator

## 字段和声明不可重名

Expand Down
2 changes: 1 addition & 1 deletion course/advanced/type_cast.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ undefined 是一个神奇的值,它可以赋值给所有类型,代表这个
- [`@floatFromInt`](https://ziglang.org/documentation/master/#floatFromInt) 将整数显式强制转换为浮点数
- [`@intCast`](https://ziglang.org/documentation/master/#intCast) 在不同的整数类型中显式强制转换
- [`@intFromBool`](https://ziglang.org/documentation/master/#intFromBool) 将 `true` 转换为 `1`,`false` 转换为 `0`
- [`@intFromEnum`](https://ziglang.org/documentation/master/#intFromEnum) 根据整数值获取对应的联合标记或者枚举值
- [`@intFromEnum`](https://ziglang.org/documentation/master/#intFromEnum) 获取枚举值或联合标记对应的整数值
- [`@intFromError`](https://ziglang.org/documentation/master/#intFromError) 获取对应错误的整数值
- [`@intFromFloat`](https://ziglang.org/documentation/master/#intFromFloat) 获取浮点数的整数部分
- [`@intFromPtr`](https://ziglang.org/documentation/master/#intFromPtr) 获取指针指向的地址(整数 `usize`),这在嵌入式开发和内核开发时很常用
Expand Down
2 changes: 1 addition & 1 deletion course/basic/advanced_type/pointer.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Zig 支持指针的加减运算,但建议在进行运算前,将指针转换

<<<@/code/release/pointer.zig#st_pointer

以上代码编译需要额外链接 libc,你只需要在你的 `build.zig` 中添加 `exe.linkLibC();` 即可
以上代码编译需要额外链接 `libc`。在 Zig 0.16 的构建脚本中,可以让对应模块链接 C 标准库,例如 `exe.root_module.linkSystemLibrary("c", .{})`

:::

Expand Down
7 changes: 4 additions & 3 deletions course/code/16/build_system/basic/src/main.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const std = @import("std");

pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
pub fn main(init: std.process.Init) !void {
const io = init.io;
// `std.debug.print` 会输出到标准错误。
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});

// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
var stdout_writer = std.Io.File.stdout().writer(io, &stdout_buffer);
const stdout = &stdout_writer.interface;

try stdout.print("Run `zig build test` to run the tests.\n", .{});
Expand Down
81 changes: 37 additions & 44 deletions course/code/16/build_system/build.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const std = @import("std");
const ChildProcess = std.process.Child;

const args = [_][]const u8{ "zig", "build" };

pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{});
const io = b.graph.io;
// #region crossTarget
// 构建一个target
const target_query = std.Target.Query{
Expand Down Expand Up @@ -36,57 +35,51 @@ pub fn build(b: *std.Build) !void {

b.installArtifact(exe);

const full_path = try std.process.getCwdAlloc(b.allocator);
const full_path = try std.process.currentPathAlloc(io, b.allocator);
defer b.allocator.free(full_path);

var dir = std.fs.openDirAbsolute(full_path, .{ .iterate = true }) catch |err| {
var dir = std.Io.Dir.openDirAbsolute(io, full_path, .{ .iterate = true }) catch |err| {
std.log.err("open path failed {s}, err is {}", .{ full_path, err });
std.process.exit(1);
};
defer dir.close();
defer dir.close(io);

var iterate = dir.iterate();

while (iterate.next()) |val| {
if (val) |entry| {
// get the entry name, entry can be file or directory
const name = entry.name;
if (entry.kind == .directory) {
if (eqlu8(name, ".zig-cache") or eqlu8(name, "zig-out") or eqlu8(name, "zig-cache"))
continue;

// build child process
var child = ChildProcess.init(&args, b.allocator);

// build cwd
const cwd = std.fs.path.join(b.allocator, &[_][]const u8{
full_path,
name,
}) catch |err| {
std.log.err("fmt path failed, err is {}", .{err});
std.process.exit(1);
};

// open entry dir
const entry_dir = std.fs.openDirAbsolute(cwd, .{}) catch unreachable;
entry_dir.access("build.zig", .{}) catch {
std.log.err("not found build.zig in path {s}", .{cwd});
std.process.exit(1);
};

// set child cwd
// this api maybe changed in the future
child.cwd = cwd;

// spawn and wait child process
_ = child.spawnAndWait() catch unreachable;
}
} else {
// Stop endless loop
break;
}
} else |err| {
while (iterate.next(io) catch |err| {
std.log.err("iterate examples_path failed, err is {}", .{err});
std.process.exit(1);
}) |entry| {
// get the entry name, entry can be file or directory
const name = entry.name;
if (entry.kind == .directory) {
if (eqlu8(name, ".zig-cache") or eqlu8(name, "zig-out") or eqlu8(name, "zig-cache"))
continue;

// build cwd
const cwd = std.fs.path.join(b.allocator, &[_][]const u8{
full_path,
name,
}) catch |err| {
std.log.err("fmt path failed, err is {}", .{err});
std.process.exit(1);
};

// open entry dir
const entry_dir = std.Io.Dir.openDirAbsolute(io, cwd, .{}) catch unreachable;
defer entry_dir.close(io);

entry_dir.access(io, "build.zig", .{}) catch {
std.log.err("not found build.zig in path {s}", .{cwd});
std.process.exit(1);
};

var child = std.process.spawn(io, .{
.argv = &args,
.cwd = .{ .path = cwd },
}) catch unreachable;
_ = child.wait(io) catch unreachable;
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions course/code/16/build_system/cli/src/main.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const std = @import("std");

pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
pub fn main(init: std.process.Init) !void {
const io = init.io;
// `std.debug.print` 会输出到标准错误。
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});

// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
var stdout_writer = std.Io.File.stdout().writer(io, &stdout_buffer);
const stdout = &stdout_writer.interface;

try stdout.print("Run `zig build test` to run the tests.\n", .{});
Expand Down
2 changes: 1 addition & 1 deletion course/code/16/build_system/lib/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn build(b: *std.Build) void {
}),
});

exe.linkLibrary(lib);
exe.root_module.linkLibrary(lib);

b.installArtifact(exe);
}
7 changes: 4 additions & 3 deletions course/code/16/build_system/lib/src/main.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const std = @import("std");

pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
pub fn main(init: std.process.Init) !void {
const io = init.io;
// `std.debug.print` 会输出到标准错误。
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});

// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
var stdout_writer = std.Io.File.stdout().writer(io, &stdout_buffer);
const stdout = &stdout_writer.interface;

try stdout.print("Run `zig build test` to run the tests.\n", .{});
Expand Down
7 changes: 4 additions & 3 deletions course/code/16/build_system/step/src/main.zig
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const std = @import("std");

pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
pub fn main(init: std.process.Init) !void {
const io = init.io;
// `std.debug.print` 会输出到标准错误。
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});

// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
var stdout_writer = std.Io.File.stdout().writer(io, &stdout_buffer);
const stdout = &stdout_writer.interface;

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