mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 05:44:20 +00:00
This change is seemingly insignificant but I actually agonized over this
for three days. Some other things I considered:
* (status quo in master branch) make Compile step creation functions
accept a Target.Query and delete the ResolvedTarget struct.
- downside: redundantly resolve target queries many times
* same as before but additionally add a hash map to cache target query
resolutions.
- downside: now there is a hash map that doesn't actually need to
exist, just to make the API more ergonomic.
* add is_native_os and is_native_abi fields to std.Target and use it
directly as the result of resolving a target query.
- downside: they really don't belong there. They would be available
as comptime booleans via `@import("builtin")` but they should not
be exposed that way.
With this change the downsides are:
* the option name of addExecutable and friends is `target` instead of
`resolved_target` matching the type name.
- upside: this does not break compatibility with existing build
scripts
* you likely end up seeing `target.result.cpu.arch` rather than
`target.cpu.arch`.
- upside: this is an improvement over `target.target.cpu.arch` which
it was before this commit.
- downside: `b.host.target` is now `b.host.result`.
38 lines
1.3 KiB
Zig
38 lines
1.3 KiB
Zig
const std = @import("std");
|
|
|
|
pub const requires_macos_sdk = true;
|
|
pub const requires_symlinks = true;
|
|
|
|
pub fn build(b: *std.Build) void {
|
|
const test_step = b.step("test", "Test it");
|
|
b.default_step = test_step;
|
|
|
|
add(b, test_step, .Debug);
|
|
add(b, test_step, .ReleaseFast);
|
|
add(b, test_step, .ReleaseSmall);
|
|
add(b, test_step, .ReleaseSafe);
|
|
}
|
|
|
|
fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
|
|
const target = b.resolveTargetQuery(.{ .os_tag = .macos });
|
|
const sdk = std.zig.system.darwin.getSdk(b.allocator, target.result) orelse
|
|
@panic("macOS SDK is required to run the test");
|
|
|
|
const exe = b.addExecutable(.{
|
|
.name = "test",
|
|
.optimize = optimize,
|
|
.target = b.host,
|
|
});
|
|
exe.addSystemIncludePath(.{ .path = b.pathJoin(&.{ sdk, "/usr/include" }) });
|
|
exe.addIncludePath(.{ .path = b.pathJoin(&.{ sdk, "/usr/include/c++/v1" }) });
|
|
exe.addCSourceFile(.{ .file = .{ .path = "test.cpp" }, .flags = &.{
|
|
"-nostdlib++",
|
|
"-nostdinc++",
|
|
} });
|
|
exe.addObjectFile(.{ .path = b.pathJoin(&.{ sdk, "/usr/lib/libc++.tbd" }) });
|
|
|
|
const run_cmd = b.addRunArtifact(exe);
|
|
run_cmd.expectStdErrEqual("x: 5\n");
|
|
|
|
test_step.dependOn(&run_cmd.step);
|
|
}
|