Skip to content

Commit efa25e7

Browse files
authored
Merge pull request #14498 from ziglang/zig-build-api
Several enhancements to the build system. Many breaking changes to the API. * combine `std.build` and `std.build.Builder` into `std.Build` * eliminate `setTarget` and `setBuildMode`; use an options struct for `b.addExecutable` and friends * implement passing options to dependency packages. closes #14285 * rename `LibExeObjStep` to `CompileStep` * move src.type.CType to std lib, use it from std.Build, this helps with populating config.h files.
2 parents 6f13a72 + 8d37c6f commit efa25e7

File tree

105 files changed

+4009
-3739
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+4009
-3739
lines changed

build.zig

Lines changed: 55 additions & 204 deletions
Large diffs are not rendered by default.

doc/langref.html.in

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9528,11 +9528,15 @@ fn foo(comptime T: type, ptr: *T) T {
95289528
To add standard build options to a <code class="file">build.zig</code> file:
95299529
</p>
95309530
{#code_begin|syntax|build#}
9531-
const Builder = @import("std").build.Builder;
9531+
const std = @import("std");
95329532

9533-
pub fn build(b: *Builder) void {
9534-
const exe = b.addExecutable("example", "example.zig");
9535-
exe.setBuildMode(b.standardReleaseOptions());
9533+
pub fn build(b: *std.Build) void {
9534+
const optimize = b.standardOptimizeOption(.{});
9535+
const exe = b.addExecutable(.{
9536+
.name = "example",
9537+
.root_source_file = .{ .path = "example.zig" },
9538+
.optimize = optimize,
9539+
});
95369540
b.default_step.dependOn(&exe.step);
95379541
}
95389542
{#code_end#}
@@ -10547,22 +10551,26 @@ const separator = if (builtin.os.tag == .windows) '\\' else '/';
1054710551
<p>This <code class="file">build.zig</code> file is automatically generated
1054810552
by <kbd>zig init-exe</kbd>.</p>
1054910553
{#code_begin|syntax|build_executable#}
10550-
const Builder = @import("std").build.Builder;
10554+
const std = @import("std");
1055110555

10552-
pub fn build(b: *Builder) void {
10556+
pub fn build(b: *std.Build) void {
1055310557
// Standard target options allows the person running `zig build` to choose
1055410558
// what target to build for. Here we do not override the defaults, which
1055510559
// means any target is allowed, and the default is native. Other options
1055610560
// for restricting supported target set are available.
1055710561
const target = b.standardTargetOptions(.{});
1055810562

10559-
// Standard release options allow the person running `zig build` to select
10560-
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
10561-
const mode = b.standardReleaseOptions();
10563+
// Standard optimization options allow the person running `zig build` to select
10564+
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
10565+
// set a preferred release mode, allowing the user to decide how to optimize.
10566+
const optimize = b.standardOptimizeOption(.{});
1056210567

10563-
const exe = b.addExecutable("example", "src/main.zig");
10564-
exe.setTarget(target);
10565-
exe.setBuildMode(mode);
10568+
const exe = b.addExecutable(.{
10569+
.name = "example",
10570+
.root_source_file = .{ .path = "src/main.zig" },
10571+
.target = target,
10572+
.optimize = optimize,
10573+
});
1056610574
exe.install();
1056710575

1056810576
const run_cmd = exe.run();
@@ -10581,16 +10589,21 @@ pub fn build(b: *Builder) void {
1058110589
<p>This <code class="file">build.zig</code> file is automatically generated
1058210590
by <kbd>zig init-lib</kbd>.</p>
1058310591
{#code_begin|syntax|build_library#}
10584-
const Builder = @import("std").build.Builder;
10592+
const std = @import("std");
1058510593

10586-
pub fn build(b: *Builder) void {
10587-
const mode = b.standardReleaseOptions();
10588-
const lib = b.addStaticLibrary("example", "src/main.zig");
10589-
lib.setBuildMode(mode);
10594+
pub fn build(b: *std.Build) void {
10595+
const optimize = b.standardOptimizeOption(.{});
10596+
const lib = b.addStaticLibrary(.{
10597+
.name = "example",
10598+
.root_source_file = .{ .path = "src/main.zig" },
10599+
.optimize = optimize,
10600+
});
1059010601
lib.install();
1059110602

10592-
var main_tests = b.addTest("src/main.zig");
10593-
main_tests.setBuildMode(mode);
10603+
const main_tests = b.addTest(.{
10604+
.root_source_file = .{ .path = "src/main.zig" },
10605+
.optimize = optimize,
10606+
});
1059410607

1059510608
const test_step = b.step("test", "Run library tests");
1059610609
test_step.dependOn(&main_tests.step);
@@ -10949,12 +10962,17 @@ int main(int argc, char **argv) {
1094910962
}
1095010963
{#end_syntax_block#}
1095110964
{#code_begin|syntax|build_c#}
10952-
const Builder = @import("std").build.Builder;
10953-
10954-
pub fn build(b: *Builder) void {
10955-
const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0));
10965+
const std = @import("std");
1095610966

10957-
const exe = b.addExecutable("test", null);
10967+
pub fn build(b: *std.Build) void {
10968+
const lib = b.addSharedLibrary(.{
10969+
.name = "mathtest",
10970+
.root_source_file = .{ .path = "mathtest.zig" },
10971+
.version = .{ .major = 1, .minor = 0, .patch = 0 },
10972+
});
10973+
const exe = b.addExecutable(.{
10974+
.name = "test",
10975+
});
1095810976
exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"});
1095910977
exe.linkLibrary(lib);
1096010978
exe.linkSystemLibrary("c");
@@ -11011,12 +11029,17 @@ int main(int argc, char **argv) {
1101111029
}
1101211030
{#end_syntax_block#}
1101311031
{#code_begin|syntax|build_object#}
11014-
const Builder = @import("std").build.Builder;
11032+
const std = @import("std");
1101511033

11016-
pub fn build(b: *Builder) void {
11017-
const obj = b.addObject("base64", "base64.zig");
11034+
pub fn build(b: *std.Build) void {
11035+
const obj = b.addObject(.{
11036+
.name = "base64",
11037+
.root_source_file = .{ .path = "base64.zig" },
11038+
});
1101811039

11019-
const exe = b.addExecutable("test", null);
11040+
const exe = b.addExecutable(.{
11041+
.name = "test",
11042+
});
1102011043
exe.addCSourceFile("test.c", &[_][]const u8{"-std=c99"});
1102111044
exe.addObject(obj);
1102211045
exe.linkSystemLibrary("c");

lib/build_runner.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const std = @import("std");
33
const builtin = @import("builtin");
44
const io = std.io;
55
const fmt = std.fmt;
6-
const Builder = std.build.Builder;
76
const mem = std.mem;
87
const process = std.process;
98
const ArrayList = std.ArrayList;
@@ -42,12 +41,15 @@ pub fn main() !void {
4241
return error.InvalidArgs;
4342
};
4443

45-
const builder = try Builder.create(
44+
const host = try std.zig.system.NativeTargetInfo.detect(.{});
45+
46+
const builder = try std.Build.create(
4647
allocator,
4748
zig_exe,
4849
build_root,
4950
cache_root,
5051
global_cache_root,
52+
host,
5153
);
5254
defer builder.destroy();
5355

@@ -58,7 +60,7 @@ pub fn main() !void {
5860
const stdout_stream = io.getStdOut().writer();
5961

6062
var install_prefix: ?[]const u8 = null;
61-
var dir_list = Builder.DirList{};
63+
var dir_list = std.Build.DirList{};
6264

6365
// before arg parsing, check for the NO_COLOR environment variable
6466
// if it exists, default the color setting to .off
@@ -230,7 +232,7 @@ pub fn main() !void {
230232
};
231233
}
232234

233-
fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void {
235+
fn usage(builder: *std.Build, already_ran_build: bool, out_stream: anytype) !void {
234236
// run the build script to collect the options
235237
if (!already_ran_build) {
236238
builder.resolveInstallPrefix(null, .{});
@@ -330,7 +332,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void
330332
);
331333
}
332334

333-
fn usageAndErr(builder: *Builder, already_ran_build: bool, out_stream: anytype) void {
335+
fn usageAndErr(builder: *std.Build, already_ran_build: bool, out_stream: anytype) void {
334336
usage(builder, already_ran_build, out_stream) catch {};
335337
process.exit(1);
336338
}

lib/init-exe/build.zig

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,67 @@
11
const std = @import("std");
22

3-
pub fn build(b: *std.build.Builder) void {
3+
// Although this function looks imperative, note that its job is to
4+
// declaratively construct a build graph that will be executed by an external
5+
// runner.
6+
pub fn build(b: *std.Build) void {
47
// Standard target options allows the person running `zig build` to choose
58
// what target to build for. Here we do not override the defaults, which
69
// means any target is allowed, and the default is native. Other options
710
// for restricting supported target set are available.
811
const target = b.standardTargetOptions(.{});
912

10-
// Standard release options allow the person running `zig build` to select
11-
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
12-
const mode = b.standardReleaseOptions();
13+
// Standard optimization options allow the person running `zig build` to select
14+
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
15+
// set a preferred release mode, allowing the user to decide how to optimize.
16+
const optimize = b.standardOptimizeOption(.{});
1317

14-
const exe = b.addExecutable("$", "src/main.zig");
15-
exe.setTarget(target);
16-
exe.setBuildMode(mode);
18+
const exe = b.addExecutable(.{
19+
.name = "$",
20+
// In this case the main source file is merely a path, however, in more
21+
// complicated build scripts, this could be a generated file.
22+
.root_source_file = .{ .path = "src/main.zig" },
23+
.target = target,
24+
.optimize = optimize,
25+
});
26+
27+
// This declares intent for the executable to be installed into the
28+
// standard location when the user invokes the "install" step (the default
29+
// step when running `zig build`).
1730
exe.install();
1831

32+
// This *creates* a RunStep in the build graph, to be executed when another
33+
// step is evaluated that depends on it. The next line below will establish
34+
// such a dependency.
1935
const run_cmd = exe.run();
36+
37+
// By making the run step depend on the install step, it will be run from the
38+
// installation directory rather than directly from within the cache directory.
39+
// This is not necessary, however, if the application depends on other installed
40+
// files, this ensures they will be present and in the expected location.
2041
run_cmd.step.dependOn(b.getInstallStep());
42+
43+
// This allows the user to pass arguments to the application in the build
44+
// command itself, like this: `zig build run -- arg1 arg2 etc`
2145
if (b.args) |args| {
2246
run_cmd.addArgs(args);
2347
}
2448

49+
// This creates a build step. It will be visible in the `zig build --help` menu,
50+
// and can be selected like this: `zig build run`
51+
// This will evaluate the `run` step rather than the default, which is "install".
2552
const run_step = b.step("run", "Run the app");
2653
run_step.dependOn(&run_cmd.step);
2754

28-
const exe_tests = b.addTest("src/main.zig");
29-
exe_tests.setTarget(target);
30-
exe_tests.setBuildMode(mode);
55+
// Creates a step for unit testing.
56+
const exe_tests = b.addTest(.{
57+
.root_source_file = .{ .path = "src/main.zig" },
58+
.target = target,
59+
.optimize = optimize,
60+
});
3161

62+
// Similar to creating the run step earlier, this exposes a `test` step to
63+
// the `zig build --help` menu, providing a way for the user to request
64+
// running the unit tests.
3265
const test_step = b.step("test", "Run unit tests");
3366
test_step.dependOn(&exe_tests.step);
3467
}

lib/init-lib/build.zig

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

3-
pub fn build(b: *std.build.Builder) void {
4-
// Standard release options allow the person running `zig build` to select
5-
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
6-
const mode = b.standardReleaseOptions();
3+
// Although this function looks imperative, note that its job is to
4+
// declaratively construct a build graph that will be executed by an external
5+
// runner.
6+
pub fn build(b: *std.Build) void {
7+
// Standard target options allows the person running `zig build` to choose
8+
// what target to build for. Here we do not override the defaults, which
9+
// means any target is allowed, and the default is native. Other options
10+
// for restricting supported target set are available.
11+
const target = b.standardTargetOptions(.{});
712

8-
const lib = b.addStaticLibrary("$", "src/main.zig");
9-
lib.setBuildMode(mode);
13+
// Standard optimization options allow the person running `zig build` to select
14+
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
15+
// set a preferred release mode, allowing the user to decide how to optimize.
16+
const optimize = b.standardOptimizeOption(.{});
17+
18+
const lib = b.addStaticLibrary(.{
19+
.name = "$",
20+
// In this case the main source file is merely a path, however, in more
21+
// complicated build scripts, this could be a generated file.
22+
.root_source_file = .{ .path = "src/main.zig" },
23+
.target = target,
24+
.optimize = optimize,
25+
});
26+
27+
// This declares intent for the library to be installed into the standard
28+
// location when the user invokes the "install" step (the default step when
29+
// running `zig build`).
1030
lib.install();
1131

12-
const main_tests = b.addTest("src/main.zig");
13-
main_tests.setBuildMode(mode);
32+
// Creates a step for unit testing.
33+
const main_tests = b.addTest(.{
34+
.root_source_file = .{ .path = "src/main.zig" },
35+
.target = target,
36+
.optimize = optimize,
37+
});
1438

39+
// This creates a build step. It will be visible in the `zig build --help` menu,
40+
// and can be selected like this: `zig build test`
41+
// This will evaluate the `test` step rather than the default, which is "install".
1542
const test_step = b.step("test", "Run library tests");
1643
test_step.dependOn(&main_tests.step);
1744
}

0 commit comments

Comments
 (0)