Skip to content

Commit 21ce1b5

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 a9d0b66 commit 21ce1b5

File tree

122 files changed

+1797
-1329
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

+1797
-1329
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);
@@ -338,7 +340,7 @@ pub fn build(b: *std.Build) !void {
338340
try addStaticLlvmOptionsToExe(exe);
339341
try addStaticLlvmOptionsToExe(check_case_exe);
340342
}
341-
if (target.isWindows()) {
343+
if (target.target.os.tag == .windows) {
342344
inline for (.{ exe, check_case_exe }) |artifact| {
343345
artifact.linkSystemLibrary("version");
344346
artifact.linkSystemLibrary("uuid");
@@ -362,19 +364,19 @@ pub fn build(b: *std.Build) !void {
362364
);
363365

364366
// On mingw, we need to opt into windows 7+ to get some features required by tracy.
365-
const tracy_c_flags: []const []const u8 = if (target.isWindows() and target.getAbi() == .gnu)
367+
const tracy_c_flags: []const []const u8 = if (target.target.os.tag == .windows and target.target.abi == .gnu)
366368
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined", "-D_WIN32_WINNT=0x601" }
367369
else
368370
&[_][]const u8{ "-DTRACY_ENABLE=1", "-fno-sanitize=undefined" };
369371

370372
exe.addIncludePath(.{ .cwd_relative = tracy_path });
371373
exe.addCSourceFile(.{ .file = .{ .cwd_relative = client_cpp }, .flags = tracy_c_flags });
372374
if (!enable_llvm) {
373-
exe.linkSystemLibraryName("c++");
375+
exe.root_module.linkSystemLibrary("c++", .{ .use_pkg_config = .no });
374376
}
375377
exe.linkLibC();
376378

377-
if (target.isWindows()) {
379+
if (target.target.os.tag == .windows) {
378380
exe.linkSystemLibrary("dbghelp");
379381
exe.linkSystemLibrary("ws2_32");
380382
}
@@ -383,7 +385,7 @@ pub fn build(b: *std.Build) !void {
383385
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
384386

385387
const test_cases_options = b.addOptions();
386-
check_case_exe.addOptions("build_options", test_cases_options);
388+
check_case_exe.root_module.addOptions("build_options", test_cases_options);
387389

388390
test_cases_options.addOption(bool, "enable_tracy", false);
389391
test_cases_options.addOption(bool, "enable_logging", enable_logging);
@@ -533,16 +535,19 @@ pub fn build(b: *std.Build) !void {
533535
fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
534536
const semver = try std.SemanticVersion.parse(version);
535537

536-
var target: std.zig.CrossTarget = .{
538+
var target_query: std.zig.CrossTarget = .{
537539
.cpu_arch = .wasm32,
538540
.os_tag = .wasi,
539541
};
540-
target.cpu_features_add.addFeature(@intFromEnum(std.Target.wasm.Feature.bulk_memory));
542+
target_query.cpu_features_add.addFeature(@intFromEnum(std.Target.wasm.Feature.bulk_memory));
541543

542-
const exe = addCompilerStep(b, .ReleaseSmall, target);
544+
const exe = addCompilerStep(b, .{
545+
.optimize = .ReleaseSmall,
546+
.target = b.resolveTargetQuery(target_query),
547+
});
543548

544549
const exe_options = b.addOptions();
545-
exe.addOptions("build_options", exe_options);
550+
exe.root_module.addOptions("build_options", exe_options);
546551

547552
exe_options.addOption(u32, "mem_leak_frames", 0);
548553
exe_options.addOption(bool, "have_llvm", false);
@@ -577,33 +582,40 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
577582
update_zig1_step.dependOn(&copy_zig_h.step);
578583
}
579584

580-
fn addCompilerStep(
581-
b: *std.Build,
585+
const AddCompilerStepOptions = struct {
582586
optimize: std.builtin.OptimizeMode,
583-
target: std.zig.CrossTarget,
584-
) *std.Build.Step.Compile {
587+
target: std.Build.ResolvedTarget,
588+
strip: ?bool = null,
589+
sanitize_thread: ?bool = null,
590+
single_threaded: ?bool = null,
591+
};
592+
593+
fn addCompilerStep(b: *std.Build, options: AddCompilerStepOptions) *std.Build.Step.Compile {
585594
const exe = b.addExecutable(.{
586595
.name = "zig",
587596
.root_source_file = .{ .path = "src/main.zig" },
588-
.target = target,
589-
.optimize = optimize,
597+
.target = options.target,
598+
.optimize = options.optimize,
590599
.max_rss = 7_000_000_000,
600+
.strip = options.strip,
601+
.sanitize_thread = options.sanitize_thread,
602+
.single_threaded = options.single_threaded,
591603
});
592604
exe.stack_size = stack_size;
593605

594606
const aro_options = b.addOptions();
595607
aro_options.addOption([]const u8, "version_str", "aro-zig");
596608
const aro_options_module = aro_options.createModule();
597609
const aro_backend = b.createModule(.{
598-
.source_file = .{ .path = "deps/aro/backend.zig" },
599-
.dependencies = &.{.{
610+
.root_source_file = .{ .path = "deps/aro/backend.zig" },
611+
.imports = &.{.{
600612
.name = "build_options",
601613
.module = aro_options_module,
602614
}},
603615
});
604616
const aro_module = b.createModule(.{
605-
.source_file = .{ .path = "deps/aro/aro.zig" },
606-
.dependencies = &.{
617+
.root_source_file = .{ .path = "deps/aro/aro.zig" },
618+
.imports = &.{
607619
.{
608620
.name = "build_options",
609621
.module = aro_options_module,
@@ -618,7 +630,7 @@ fn addCompilerStep(
618630
},
619631
});
620632

621-
exe.addModule("aro", aro_module);
633+
exe.root_module.addImport("aro", aro_module);
622634
return exe;
623635
}
624636

@@ -642,7 +654,7 @@ fn addCmakeCfgOptionsToExe(
642654
exe: *std.Build.Step.Compile,
643655
use_zig_libcxx: bool,
644656
) !void {
645-
if (exe.target.isDarwin()) {
657+
if (exe.rootModuleTarget().isDarwin()) {
646658
// useful for package maintainers
647659
exe.headerpad_max_install_names = true;
648660
}
@@ -670,8 +682,8 @@ fn addCmakeCfgOptionsToExe(
670682
// against system-provided LLVM, Clang, LLD.
671683
const need_cpp_includes = true;
672684
const static = cfg.llvm_linkage == .static;
673-
const lib_suffix = if (static) exe.target.staticLibSuffix()[1..] else exe.target.dynamicLibSuffix()[1..];
674-
switch (exe.target.getOsTag()) {
685+
const lib_suffix = if (static) exe.rootModuleTarget().staticLibSuffix()[1..] else exe.rootModuleTarget().dynamicLibSuffix()[1..];
686+
switch (exe.rootModuleTarget().os.tag) {
675687
.linux => {
676688
// First we try to link against the detected libcxx name. If that doesn't work, we fall
677689
// back to -lc++ and cross our fingers.
@@ -687,7 +699,7 @@ fn addCmakeCfgOptionsToExe(
687699
exe.linkLibCpp();
688700
},
689701
.windows => {
690-
if (exe.target.getAbi() != .msvc) exe.linkLibCpp();
702+
if (exe.rootModuleTarget().abi != .msvc) exe.linkLibCpp();
691703
},
692704
.freebsd => {
693705
if (static) {
@@ -749,12 +761,12 @@ fn addStaticLlvmOptionsToExe(exe: *std.Build.Step.Compile) !void {
749761
exe.linkSystemLibrary("z");
750762
exe.linkSystemLibrary("zstd");
751763

752-
if (exe.target.getOs().tag != .windows or exe.target.getAbi() != .msvc) {
764+
if (exe.rootModuleTarget().os.tag != .windows or exe.rootModuleTarget().abi != .msvc) {
753765
// This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.
754766
exe.linkSystemLibrary("c++");
755767
}
756768

757-
if (exe.target.getOs().tag == .windows) {
769+
if (exe.rootModuleTarget().os.tag == .windows) {
758770
exe.linkSystemLibrary("version");
759771
exe.linkSystemLibrary("uuid");
760772
exe.linkSystemLibrary("ole32");
@@ -803,7 +815,9 @@ fn addCMakeLibraryList(exe: *std.Build.Step.Compile, list: []const u8) void {
803815
while (it.next()) |lib| {
804816
if (mem.startsWith(u8, lib, "-l")) {
805817
exe.linkSystemLibrary(lib["-l".len..]);
806-
} else if (exe.target.isWindows() and mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib)) {
818+
} else if (exe.rootModuleTarget().os.tag == .windows and
819+
mem.endsWith(u8, lib, ".lib") and !fs.path.isAbsolute(lib))
820+
{
807821
exe.linkSystemLibrary(lib[0 .. lib.len - ".lib".len]);
808822
} else {
809823
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)