Skip to content

Commit 4235d6d

Browse files
committed
fix: #217
1 parent 48bc48f commit 4235d6d

File tree

11 files changed

+218
-0
lines changed

11 files changed

+218
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const std = @import("std");
2+
const ChildProcess = std.process.Child;
3+
4+
const args = [_][]const u8{ "zig", "build" };
5+
6+
pub fn build(b: *std.Build) !void {
7+
const full_path = try std.process.getCwdAlloc(b.allocator);
8+
9+
var dir = std.fs.openDirAbsolute(full_path, .{ .iterate = true }) catch |err| {
10+
std.log.err("open path failed {s}, err is {}", .{ full_path, err });
11+
std.process.exit(1);
12+
};
13+
defer dir.close();
14+
15+
var iterate = dir.iterate();
16+
17+
while (iterate.next()) |val| {
18+
if (val) |entry| {
19+
// get the entry name, entry can be file or directory
20+
const name = entry.name;
21+
if (entry.kind == .directory) {
22+
if (eqlu8(name, ".zig-cache") or eqlu8(name, "zig-out") or eqlu8(name, "zig-cache"))
23+
continue;
24+
25+
// build child process
26+
var child = ChildProcess.init(&args, b.allocator);
27+
28+
// build cwd
29+
const cwd = std.fs.path.join(b.allocator, &[_][]const u8{
30+
full_path,
31+
name,
32+
}) catch |err| {
33+
std.log.err("fmt path failed, err is {}", .{err});
34+
std.process.exit(1);
35+
};
36+
37+
// open entry dir
38+
const entry_dir = std.fs.openDirAbsolute(cwd, .{}) catch unreachable;
39+
entry_dir.access("build.zig", .{}) catch {
40+
std.log.err("not found build.zig in path {s}", .{cwd});
41+
std.process.exit(1);
42+
};
43+
44+
// set child cwd
45+
// this api maybe changed in the future
46+
child.cwd = cwd;
47+
48+
// spawn and wait child process
49+
_ = child.spawnAndWait() catch unreachable;
50+
}
51+
} else {
52+
// Stop endless loop
53+
break;
54+
}
55+
} else |err| {
56+
std.log.err("iterate examples_path failed, err is {}", .{err});
57+
std.process.exit(1);
58+
}
59+
}
60+
61+
fn eqlu8(a: []const u8, b: []const u8) bool {
62+
return std.mem.eql(u8, a, b);
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
@import("pkg2").helperFunction(b);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.{
2+
.name = "pkg1",
3+
.version = "0.0.0",
4+
.dependencies = .{
5+
.pkg2 = .{
6+
// path 为本地包的路径
7+
.path = "../pkg2",
8+
},
9+
},
10+
.paths = .{
11+
"build.zig",
12+
"build.zig.zon",
13+
},
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
_ = b;
5+
}
6+
7+
pub fn helperFunction(artifact: *std.Build) void {
8+
_ = artifact;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.{
2+
.name = "pkg2",
3+
.version = "0.0.0",
4+
.dependencies = .{},
5+
.paths = .{
6+
"build.zig",
7+
"build.zig.zon",
8+
},
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const std = @import("std");
2+
const ChildProcess = std.process.Child;
3+
4+
const args = [_][]const u8{ "zig", "build" };
5+
6+
pub fn build(b: *std.Build) !void {
7+
const full_path = try std.process.getCwdAlloc(b.allocator);
8+
9+
var dir = std.fs.openDirAbsolute(full_path, .{ .iterate = true }) catch |err| {
10+
std.log.err("open path failed {s}, err is {}", .{ full_path, err });
11+
std.process.exit(1);
12+
};
13+
defer dir.close();
14+
15+
var iterate = dir.iterate();
16+
17+
while (iterate.next()) |val| {
18+
if (val) |entry| {
19+
// get the entry name, entry can be file or directory
20+
const name = entry.name;
21+
if (entry.kind == .directory) {
22+
if (eqlu8(name, ".zig-cache") or eqlu8(name, "zig-out") or eqlu8(name, "zig-cache"))
23+
continue;
24+
25+
// build child process
26+
var child = ChildProcess.init(&args, b.allocator);
27+
28+
// build cwd
29+
const cwd = std.fs.path.join(b.allocator, &[_][]const u8{
30+
full_path,
31+
name,
32+
}) catch |err| {
33+
std.log.err("fmt path failed, err is {}", .{err});
34+
std.process.exit(1);
35+
};
36+
37+
// open entry dir
38+
const entry_dir = std.fs.openDirAbsolute(cwd, .{}) catch unreachable;
39+
entry_dir.access("build.zig", .{}) catch {
40+
std.log.err("not found build.zig in path {s}", .{cwd});
41+
std.process.exit(1);
42+
};
43+
44+
// set child cwd
45+
// this api maybe changed in the future
46+
child.cwd = cwd;
47+
48+
// spawn and wait child process
49+
_ = child.spawnAndWait() catch unreachable;
50+
}
51+
} else {
52+
// Stop endless loop
53+
break;
54+
}
55+
} else |err| {
56+
std.log.err("iterate examples_path failed, err is {}", .{err});
57+
std.process.exit(1);
58+
}
59+
}
60+
61+
fn eqlu8(a: []const u8, b: []const u8) bool {
62+
return std.mem.eql(u8, a, b);
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
@import("pkg2").helperFunction(b);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.{
2+
.name = "pkg1",
3+
.version = "0.0.0",
4+
.dependencies = .{
5+
.pkg2 = .{
6+
// path 为本地包的路径
7+
.path = "../pkg2",
8+
},
9+
},
10+
.paths = .{
11+
"build.zig",
12+
"build.zig.zon",
13+
},
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
_ = b;
5+
}
6+
7+
pub fn helperFunction(artifact: *std.Build) void {
8+
_ = artifact;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.{
2+
.name = "pkg2",
3+
.version = "0.0.0",
4+
.dependencies = .{},
5+
.paths = .{
6+
"build.zig",
7+
"build.zig.zon",
8+
},
9+
}

course/engineering/build-system.md

+18
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,24 @@ zig 本身提供了一个实验性的文档生成器,它支持搜索查询,
180180

181181
## 高级功能
182182

183+
### 引用依赖中的 `build.zig`
184+
185+
该特性自 `0.11` 引入,允许包引用依赖的 `build.zig` 中提供的一些函数,示例如下:
186+
187+
`pkg1` 的源代码部分:
188+
189+
::: code-group
190+
<<<@/code/release/import_dependency_build/pkg1/build.zig
191+
<<<@/code/release/import_dependency_build/pkg1/build.zig.zon{zig}
192+
:::
193+
194+
`pkg2` 的源代码部分:
195+
196+
::: code-group
197+
<<<@/code/release/import_dependency_build/pkg2/build.zig
198+
<<<@/code/release/import_dependency_build/pkg2/build.zig.zon{zig}
199+
:::
200+
183201
### 交叉编译
184202

185203
得益于 LLVM 的存在,zig 支持交叉编译到任何 LLVM 的目标代码,zig 可以很方便的处理交叉编译,只需要指定好恰当的 target 即可。

0 commit comments

Comments
 (0)