Skip to content

Commit 142471f

Browse files
committed
zig build system: change target, compilation, and module APIs
Introduce the concept of "target query" and "resolved target". A target query is what the user specifies, with some things left to default. A resolved target has the default things discovered and populated. In the future, std.zig.CrossTarget will be rename to std.Target.Query. Introduces `std.Build.resolveTargetQuery` to get from one to the other. The concept of `main_mod_path` is gone, no longer supported. You have to put the root source file at the module root now. * remove deprecated API * update build.zig for the breaking API changes in this branch * move std.Build.Step.Compile.BuildId to std.zig.BuildId * add more options to std.Build.ExecutableOptions, std.Build.ObjectOptions, std.Build.SharedLibraryOptions, std.Build.StaticLibraryOptions, and std.Build.TestOptions. * remove `std.Build.constructCMacro`. There is no use for this API. * deprecate `std.Build.Step.Compile.defineCMacro`. Instead, `std.Build.Module.addCMacro` is provided. - remove `std.Build.Step.Compile.defineCMacroRaw`. * deprecate `std.Build.Step.Compile.linkFrameworkNeeded` - use `std.Build.Module.linkFramework` * deprecate `std.Build.Step.Compile.linkFrameworkWeak` - use `std.Build.Module.linkFramework` * move more logic into `std.Build.Module` * allow `target` and `optimize` to be `null` when creating a Module. Along with other fields, those unspecified options will be inherited from parent `Module` when inserted into an import table. * the `target` field of `addExecutable` is now required. pass `b.host` to get the host target.
1 parent 579f572 commit 142471f

File tree

122 files changed

+1812
-1338
lines changed

Some content is hidden

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

122 files changed

+1812
-1338
lines changed

build.zig

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ pub fn build(b: *std.Build) !void {
3939
const docgen_exe = b.addExecutable(.{
4040
.name = "docgen",
4141
.root_source_file = .{ .path = "tools/docgen.zig" },
42-
.target = .{},
42+
.target = b.host,
4343
.optimize = .Debug,
44+
.single_threaded = single_threaded,
4445
});
45-
docgen_exe.single_threaded = single_threaded;
4646

4747
const docgen_cmd = b.addRunArtifact(docgen_exe);
4848
docgen_cmd.addArgs(&.{ "--zig", b.zig_exe });
@@ -89,11 +89,11 @@ pub fn build(b: *std.Build) !void {
8989
const check_case_exe = b.addExecutable(.{
9090
.name = "check-case",
9191
.root_source_file = .{ .path = "test/src/Cases.zig" },
92+
.target = b.host,
9293
.optimize = optimize,
93-
.main_mod_path = .{ .path = "." },
94+
.single_threaded = single_threaded,
9495
});
9596
check_case_exe.stack_size = stack_size;
96-
check_case_exe.single_threaded = single_threaded;
9797

9898
const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;
9999
const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false;
@@ -194,14 +194,18 @@ pub fn build(b: *std.Build) !void {
194194
break :blk 4;
195195
};
196196

197-
const exe = addCompilerStep(b, optimize, target);
198-
exe.strip = strip;
197+
const exe = addCompilerStep(b, .{
198+
.optimize = optimize,
199+
.target = target,
200+
.strip = strip,
201+
.sanitize_thread = sanitize_thread,
202+
.single_threaded = single_threaded,
203+
});
199204
exe.pie = pie;
200-
exe.sanitize_thread = sanitize_thread;
201205
exe.entitlements = entitlements;
202206

203207
exe.build_id = b.option(
204-
std.Build.Step.Compile.BuildId,
208+
std.zig.BuildId,
205209
"build-id",
206210
"Request creation of '.note.gnu.build-id' section",
207211
);
@@ -217,9 +221,7 @@ pub fn build(b: *std.Build) !void {
217221

218222
test_step.dependOn(&exe.step);
219223

220-
exe.single_threaded = single_threaded;
221-
222-
if (target.isWindows() and target.getAbi() == .gnu) {
224+
if (target.target.os.tag == .windows and target.target.abi == .gnu) {
223225
// LTO is currently broken on mingw, this can be removed when it's fixed.
224226
exe.want_lto = false;
225227
check_case_exe.want_lto = false;
@@ -230,7 +232,7 @@ pub fn build(b: *std.Build) !void {
230232
exe.use_lld = use_llvm;
231233

232234
const exe_options = b.addOptions();
233-
exe.addOptions("build_options", exe_options);
235+
exe.root_module.addOptions("build_options", exe_options);
234236

235237
exe_options.addOption(u32, "mem_leak_frames", mem_leak_frames);
236238
exe_options.addOption(bool, "skip_non_native", skip_non_native);
@@ -345,7 +347,7 @@ pub fn build(b: *std.Build) !void {
345347
try addStaticLlvmOptionsToExe(exe);
346348
try addStaticLlvmOptionsToExe(check_case_exe);
347349
}
348-
if (target.isWindows()) {
350+
if (target.target.os.tag == .windows) {
349351
inline for (.{ exe, check_case_exe }) |artifact| {
350352
artifact.linkSystemLibrary("version");
351353
artifact.linkSystemLibrary("uuid");
@@ -369,19 +371,19 @@ pub fn build(b: *std.Build) !void {
369371
);
370372

371373
// On mingw, we need to opt into windows 7+ to get some features required by tracy.
372-
const tracy_c_flags: []const []const u8 = if (target.isWindows() and target.getAbi() == .gnu)
374+
const tracy_c_flags: []const []const u8 = if (target.target.os.tag == .windows and target.target.abi == .gnu)
373375
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined", "-D_WIN32_WINNT=0x601" }
374376
else
375377
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" };
376378

377379
exe.addIncludePath(.{ .cwd_relative = tracy_path });
378380
exe.addCSourceFile(.{ .file = .{ .cwd_relative = client_cpp }, .flags = tracy_c_flags });
379381
if (!enable_llvm) {
380-
exe.linkSystemLibraryName("c++");
382+
exe.root_module.linkSystemLibrary("c++", .{ .use_pkg_config = .no });
381383
}
382384
exe.linkLibC();
383385

384-
if (target.isWindows()) {
386+
if (target.target.os.tag == .windows) {
385387
exe.linkSystemLibrary("dbghelp");
386388
exe.linkSystemLibrary("ws2_32");
387389
}
@@ -390,7 +392,7 @@ pub fn build(b: *std.Build) !void {
390392
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
391393

392394
const test_cases_options = b.addOptions();
393-
check_case_exe.addOptions("build_options", test_cases_options);
395+
check_case_exe.root_module.addOptions("build_options", test_cases_options);
394396

395397
test_cases_options.addOption(bool, "enable_tracy", false);
396398
test_cases_options.addOption(bool, "enable_logging", enable_logging);
@@ -540,16 +542,19 @@ pub fn build(b: *std.Build) !void {
540542
fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
541543
const semver = try std.SemanticVersion.parse(version);
542544

543-
var target: std.zig.CrossTarget = .{
545+
var target_query: std.zig.CrossTarget = .{
544546
.cpu_arch = .wasm32,
545547
.os_tag = .wasi,
546548
};
547-
target.cpu_features_add.addFeature(@intFromEnum(std.Target.wasm.Feature.bulk_memory));
549+
target_query.cpu_features_add.addFeature(@intFromEnum(std.Target.wasm.Feature.bulk_memory));
548550

549-
const exe = addCompilerStep(b, .ReleaseSmall, target);
551+
const exe = addCompilerStep(b, .{
552+
.optimize = .ReleaseSmall,
553+
.target = b.resolveTargetQuery(target_query),
554+
});
550555

551556
const exe_options = b.addOptions();
552-
exe.addOptions("build_options", exe_options);
557+
exe.root_module.addOptions("build_options", exe_options);
553558

554559
exe_options.addOption(u32, "mem_leak_frames", 0);
555560
exe_options.addOption(bool, "have_llvm", false);
@@ -584,33 +589,40 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
584589
update_zig1_step.dependOn(&copy_zig_h.step);
585590
}
586591

587-
fn addCompilerStep(
588-
b: *std.Build,
592+
const AddCompilerStepOptions = struct {
589593
optimize: std.builtin.OptimizeMode,
590-
target: std.zig.CrossTarget,
591-
) *std.Build.Step.Compile {
594+
target: std.Build.ResolvedTarget,
595+
strip: ?bool = null,
596+
sanitize_thread: ?bool = null,
597+
single_threaded: ?bool = null,
598+
};
599+
600+
fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.Step.Compile {
592601
const exe = b.addExecutable(.{
593602
.name = "zig",
594603
.root_source_file = .{ .path = "src/main.zig" },
595-
.target = target,
596-
.optimize = optimize,
604+
.target = options.target,
605+
.optimize = options.optimize,
597606
.max_rss = 7_000_000_000,
607+
.strip = options.strip,
608+
.sanitize_thread = options.sanitize_thread,
609+
.single_threaded = options.single_threaded,
598610
});
599611
exe.stack_size = stack_size;
600612

601613
const aro_options = b.addOptions();
602614
aro_options.addOption([]const u8, "version_str", "aro-zig");
603615
const aro_options_module = aro_options.createModule();
604616
const aro_backend = b.createModule(.{
605-
.source_file = .{ .path = "deps/aro/backend.zig" },
606-
.dependencies = &.{.{
617+
.root_source_file = .{ .path = "deps/aro/backend.zig" },
618+
.imports = &.{.{
607619
.name = "build_options",
608620
.module = aro_options_module,
609621
}},
610622
});
611623
const aro_module = b.createModule(.{
612-
.source_file = .{ .path = "deps/aro/aro.zig" },
613-
.dependencies = &.{
624+
.root_source_file = .{ .path = "deps/aro/aro.zig" },
625+
.imports = &.{
614626
.{
615627
.name = "build_options",
616628
.module = aro_options_module,
@@ -625,7 +637,7 @@ fn addCompilerStep(
625637
},
626638
});
627639

628-
exe.addModule("aro", aro_module);
640+
exe.root_module.addImport("aro", aro_module);
629641
return exe;
630642
}
631643

@@ -649,7 +661,7 @@ fn addCmakeCfgOptionsToExe(
649661
exe: *std.Build.Step.Compile,
650662
use_zig_libcxx: bool,
651663
) !void {
652-
if (exe.target.isDarwin()) {
664+
if (exe.rootModuleTarget().isDarwin()) {
653665
// useful for package maintainers
654666
exe.headerpad_max_install_names = true;
655667
}
@@ -677,8 +689,8 @@ fn addCmakeCfgOptionsToExe(
677689
// against system-provided LLVM, Clang, LLD.
678690
const need_cpp_includes = true;
679691
const static = cfg.llvm_linkage == .static;
680-
const lib_suffix = if (static) exe.target.staticLibSuffix()[1..] else exe.target.dynamicLibSuffix()[1..];
681-
switch (exe.target.getOsTag()) {
692+
const lib_suffix = if (static) exe.rootModuleTarget().staticLibSuffix()[1..] else exe.rootModuleTarget().dynamicLibSuffix()[1..];
693+
switch (exe.rootModuleTarget().os.tag) {
682694
.linux => {
683695
// First we try to link against the detected libcxx name. If that doesn't work, we fall
684696
// back to -lc++ and cross our fingers.
@@ -694,7 +706,7 @@ fn addCmakeCfgOptionsToExe(
694706
exe.linkLibCpp();
695707
},
696708
.windows => {
697-
if (exe.target.getAbi() != .msvc) exe.linkLibCpp();
709+
if (exe.rootModuleTarget().abi != .msvc) exe.linkLibCpp();
698710
},
699711
.freebsd => {
700712
if (static) {
@@ -756,12 +768,12 @@ fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void {
756768
exe.linkSystemLibrary("z");
757769
exe.linkSystemLibrary("zstd");
758770

759-
if (exe.target.getOs().tag != .windows or exe.target.getAbi() != .msvc) {
771+
if (exe.rootModuleTarget().os.tag != .windows or exe.rootModuleTarget().abi != .msvc) {
760772
// This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.
761773
exe.linkSystemLibrary("c++");
762774
}
763775

764-
if (exe.target.getOs().tag == .windows) {
776+
if (exe.rootModuleTarget().os.tag == .windows) {
765777
exe.linkSystemLibrary("version");
766778
exe.linkSystemLibrary("uuid");
767779
exe.linkSystemLibrary("ole32");
@@ -810,7 +822,9 @@ fn addCMakeLibraryList(exe: *std.Build.Step.Compile, list: []const u8) void {
810822
while (it.next()) |lib| {
811823
if (mem.startsWith(u8, lib, "-l")) {
812824
exe.linkSystemLibrary(lib["-l".len..]);
813-
} else if (exe.target.isWindows() and mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib)) {
825+
} else if (exe.rootModuleTarget().os.tag == .windows and
826+
mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib))
827+
{
814828
exe.linkSystemLibrary(lib[0 .. lib.len - ".lib".len]);
815829
} else {
816830
exe.addObjectFile(.{ .cwd_relative = lib });

deps/aro/build/GenerateDef.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub const Options = struct {
2121
pub const Kind = enum { dafsa, named };
2222
};
2323

24-
pub fn create(owner: *std.Build, options: Options) std.Build.ModuleDependency {
24+
pub fn create(owner: *std.Build, options: Options) std.Build.Module.Import {
2525
const self = owner.allocator.create(GenerateDef) catch @panic("OOM");
2626
const path = owner.pathJoin(&.{ options.src_prefix, options.name });
2727

@@ -39,7 +39,7 @@ pub fn create(owner: *std.Build, options: Options) std.Build.ModuleDependency {
3939
.generated_file = .{ .step = &self.step },
4040
};
4141
const module = self.step.owner.createModule(.{
42-
.source_file = .{ .generated = &self.generated_file },
42+
.root_source_file = .{ .generated = &self.generated_file },
4343
});
4444
return .{
4545
.module = module,

lib/build_runner.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ pub fn main() !void {
4646
return error.InvalidArgs;
4747
};
4848

49-
const host = try std.zig.system.NativeTargetInfo.detect(.{});
49+
const detected = try std.zig.system.NativeTargetInfo.detect(.{});
50+
const host: std.Build.ResolvedTarget = .{
51+
.query = .{},
52+
.target = detected.target,
53+
.dynamic_linker = detected.dynamic_linker,
54+
};
5055

5156
const build_root_directory: std.Build.Cache.Directory = .{
5257
.path = build_root,

0 commit comments

Comments
 (0)