はじめに
zig v0.16まではstd.Build.Step.idに.customを指定することで、自作のビルドタスクを走らせることができた。
この機能は、zig v0.17で廃止され、以下の組み込みのサブタスクのみとなった。
pub const Step = extern struct {
...
pub const Tag = enum(u5) {
check_file,
compile,
config_header,
fail,
find_program,
fmt,
install_artifact,
install_dir,
install_file,
obj_copy,
options,
run,
top_level,
translate_c,
update_source_files,
write_file,
};
...
};
型で完全に固定されているため、拡張する余地がなくなった。
これはおそらく以下に起因している。
Separate the Maker Process from the Configurer Process
zig build now runs projects' build.zig code in a separate executable than the one that performs Package Management and executes the build graph, making zig build faster for several reasons (#35428):
The maker executable remains unmodified when build.zig script is edited, and therefore only needs to be built exactly once ("first time setup") after installing Zig.
The maker executable is built with optimizations enabled, which is starting to become more valuable now that we have introduced --watch and --fuzz.
build.zig logic can be skipped sometimes depending on what CLI flags are used with zig build.Furthermore, configuration is now serialized into a compact binary format that can be consumed by third party tooling and is part of the new Build Server Protocol. The prior way of satisfying this use case by forking the build runner is no longer supported.
To render configuration as .zon to stdout, pass --print-configuration.
Separate the Maker Process from the Configurer Process
ビルド速度のために、やつは犠牲になったのじゃ。
対処方法
使えないと言われると、困っちゃうわけだが、ちゃんとプランBは用意されている。
上で引用した内容にも書かれているが、Maker processを用意すればいい。
具体的には
- 自身のパッケージの
build.zigで、タスクを実行するためのexecutableを構成する
const exe = b.addExecutable(.{
.name = "my-step-cli",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = ...,
.optimize = ...,
}),
});
b.installArtifact(exe);
- パッケージを利用する側の
build.zigで、構成したタスク実行主体をビルド構成に組み込む
const dep = b.dependency("my-step", .{});
const dep_cli = dep.artifact("my-step-cli");
const dep_cli_runner = b.addRunArtifact(dep_cli);
dep_cli_runner.addPassthruArgs();
// 必要ならここで追加の引数の構成を行う
// ex: dep_cli_runner.addArg(...);
b.step("mystep", "....").dependsOn(&dep_cli_runner.step);
- 以下のように実行する
zig build mystep
記述した内容をヘルパー関数としてまとめパッケージから提供することで、利用する側の負担を減らすことができる(かもしれない)。
おわりに
カスタムタスクが使えなくなって途方に暮れていたところ、translate-cも外部パッケージ化されたことを思い出し、どう実現してるのかを理解することでことなきを得た。
このへんとか。
従来の方法は、std.Build.Step.make()関数から、@fieldParentPtr経由で保持する型を取り出すという、ともすればと危険とも取れる方法に依存していたため、再現可能がわかってからはこれで良かったのかと思ってる。
めでたしめでたし。
おまけ
この記事は、自作のzig.build.zonのバージョン記述を更新・確認するパッケージであるzig-set-versionを修正した成果である。
zig-set-versionでは以下のようにヘルパー関数を用意している。
pub fn addStep(build: *std.Build, options: StepOptions) void {
const dep_set_version = build.dependency(options.dep_name, .{});
const cli = dep_set_version.artifact("set-version-cli");
const cli_runner = build.addRunArtifact(cli);
cli_runner.addArg(build.pathJoin(&.{build.root.root_dir.path.?, "build.zig.zon"}));
cli_runner.addPassthruArgs();
build.step("version", "Manage project version").dependOn(&cli_runner.step);
}
機会があればお手に取ってみてくだされ。