diff --git a/.gitignore b/.gitignore index 828ba09d..4f576e99 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ node_modules .vscode -course/.vitepress/cache/deps -course/.vitepress/course/.vitepress/dist course/.vitepress/cache/ course/.vitepress/dist/ package-lock.json +zig-cache +*/zig-cache +course/code/zig-out/bin diff --git a/course/code/hello_world_1.zig b/course/code/11/hello_world_1.zig similarity index 100% rename from course/code/hello_world_1.zig rename to course/code/11/hello_world_1.zig diff --git a/course/code/hello_world_2.zig b/course/code/11/hello_world_2.zig similarity index 100% rename from course/code/hello_world_2.zig rename to course/code/11/hello_world_2.zig diff --git a/course/code/hello_world_3.zig b/course/code/11/hello_world_3.zig similarity index 100% rename from course/code/hello_world_3.zig rename to course/code/11/hello_world_3.zig diff --git a/course/code/build.zig b/course/code/build.zig new file mode 100644 index 00000000..b670e7f9 --- /dev/null +++ b/course/code/build.zig @@ -0,0 +1,16 @@ +const builtin = @import("builtin"); +const std = @import("std"); +const Build = std.Build; + +const current_zig = builtin.zig_version; + +const build_11 = @import("build_11.zig").build_11; +const build_12 = @import("build_12.zig").build_12; + +pub fn build(b: *Build) void { + if (current_zig.minor == 11) { + build_11(b); + } else if (current_zig.minor == 12) { + build_12(b); + } +} diff --git a/course/code/build_11.zig b/course/code/build_11.zig new file mode 100644 index 00000000..421382a8 --- /dev/null +++ b/course/code/build_11.zig @@ -0,0 +1,84 @@ +const std = @import("std"); +const Build = std.Build; + +const log = std.log.scoped(.For_11); + +pub fn build_11(b: *Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + var lazy_path = Build.LazyPath{ + .path = "11", + }; + + const path_11 = lazy_path.getPath(b); + var iter_dir = + std.fs.openIterableDirAbsolute(path_11, .{}) catch |err| { + log.err("open 11 path failed, err is {}", .{err}); + std.os.exit(1); + }; + defer iter_dir.close(); + + var itera = iter_dir.iterate(); + + while (itera.next()) |ff| { + if (ff) |entry| { + if (entry.kind == .file) { + const name = entry.name; + const path = std.fmt.allocPrint(b.allocator, "11/{s}", .{name}) catch |err| { + log.err("fmt path for examples failed, err is {}", .{err}); + std.os.exit(1); + }; + + const exe = b.addExecutable(.{ + .name = name, + .root_source_file = .{ .path = path }, + .target = target, + .optimize = optimize, + }); + + b.installArtifact(exe); + + const unit_tests = b.addTest(.{ + .root_source_file = .{ .path = path }, + .target = target, + .optimize = optimize, + }); + + b.getInstallStep().dependOn(&b.addRunArtifact(unit_tests).step); + } else if (entry.kind == .directory) { + const name = entry.name; + const ChildProcess = std.ChildProcess; + const args = [_][]const u8{ "zig", "build" }; + var child = ChildProcess.init(&args, b.allocator); + + const cwd = std.fmt.allocPrint(b.allocator, "{s}/{s}", .{ + path_11, + name, + }) catch |err| { + log.err("fmt path for examples failed, err is {}", .{err}); + std.os.exit(1); + }; + + const dd = std.fs.openDirAbsolute(cwd, .{}) catch unreachable; + const file = dd.openFile("build.zig", .{}) catch { + log.err("not found build.zig in path {s}", .{cwd}); + std.os.exit(1); + }; + + file.close(); + + child.cwd = cwd; + + child.spawn() catch unreachable; + + _ = child.wait() catch unreachable; + } + } else { + break; + } + } else |err| { + log.err("iterate examples_path failed, err is {}", .{err}); + std.os.exit(1); + } +} diff --git a/course/code/build_12.zig b/course/code/build_12.zig new file mode 100644 index 00000000..44e03739 --- /dev/null +++ b/course/code/build_12.zig @@ -0,0 +1,28 @@ +const std = @import("std"); +const Build = std.Build; + +pub fn build_12(b: *Build) void { + _ = b; + // const target = b.standardTargetOptions(.{}); + // const optimize = b.standardOptimizeOption(.{}); + // + // const exe = b.addExecutable(.{ + // .name = "12", + // .root_source_file = .{ .path = "src/main.zig" }, + // .target = target, + // .optimize = optimize, + // }); + // + // b.installArtifact(exe); + // + // const exe_unit_tests = b.addTest(.{ + // .root_source_file = .{ .path = "src/main.zig" }, + // .target = target, + // .optimize = optimize, + // }); + // + // const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + // + // const test_step = b.step("test", "Run unit tests"); + // test_step.dependOn(&run_exe_unit_tests.step); +} diff --git a/course/hello-world.md b/course/hello-world.md index c2b4f1a8..aa8674d0 100644 --- a/course/hello-world.md +++ b/course/hello-world.md @@ -17,7 +17,7 @@ outline: deep -<<<@/code/hello_world_1.zig +<<<@/code/11/hello_world_1.zig _很简单,不是吗?_ @@ -55,7 +55,7 @@ zig 会自动为我们根据后面的参量表推导出对应的类型,当 zig 首先,我要告诉你,zig 并没有一个内置的打印功能,包含“输出”功能的包只有 `log` 包和 `debug` 包,zig 并没有内置类似与 `@print()` 这种函数。再来一个简单的例子告诉你,如何打印东西(**_但是请记住,以下示例代码不应用于生产环境中_**)。 -<<<@/code/hello_world_2.zig +<<<@/code/11/hello_world_2.zig :::info 🅿️ 提示 @@ -71,7 +71,7 @@ zig 会自动为我们根据后面的参量表推导出对应的类型,当 zig 它们都是依靠系统调用来实现输出效果,但是这就面临着性能问题,我们知道系统调用会造成内核上下文切换的开销(系统调用的流程:执行系统调用,此时控制权会切换回内核,由内核执行完成进程需要的系统调用函数后再将控制权返回给进程),所以我们如何解决这个问题呢?可以增加一个缓冲区,等到要打印的内容都到一定程度后再一次性全部 `print`,那么此时的解决方式就如下: -<<<@/code/hello_world_3.zig +<<<@/code/11/hello_world_3.zig 此时我们就分别得到了使用缓冲区的 `stdout` 和 `stderr`, 性能更高了!