zig/test/standalone/c_compiler/build.zig
Andrew Kelley b92e30ff0b std.Build.ResolvedTarget: rename target field to result
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`.
2024-01-01 17:51:18 -07:00

67 lines
2 KiB
Zig

const std = @import("std");
const builtin = @import("builtin");
pub fn build(b: *std.Build) void {
const test_step = b.step("test", "Test it");
b.default_step = test_step;
if (builtin.os.tag == .windows) {
// https://github.com/ziglang/zig/issues/16965
return;
}
add(b, test_step, "test_c_Debug", "test_cpp_Debug", .Debug);
add(b, test_step, "test_c_ReleaseFast", "test_cpp_ReleaseFast", .ReleaseFast);
add(b, test_step, "test_c_ReleaseSmall", "test_cpp_ReleaseSmall", .ReleaseSmall);
add(b, test_step, "test_c_ReleaseSafe", "test_cpp_ReleaseSafe", .ReleaseSafe);
}
fn add(
b: *std.Build,
test_step: *std.Build.Step,
c_name: []const u8,
cpp_name: []const u8,
optimize: std.builtin.OptimizeMode,
) void {
const target = b.host;
const exe_c = b.addExecutable(.{
.name = c_name,
.optimize = optimize,
.target = target,
});
exe_c.addCSourceFile(.{ .file = .{ .path = "test.c" }, .flags = &[0][]const u8{} });
exe_c.linkLibC();
const exe_cpp = b.addExecutable(.{
.name = cpp_name,
.optimize = optimize,
.target = target,
});
b.default_step.dependOn(&exe_cpp.step);
exe_cpp.addCSourceFile(.{ .file = .{ .path = "test.cpp" }, .flags = &[0][]const u8{} });
exe_cpp.linkLibCpp();
switch (target.result.os.tag) {
.windows => {
// https://github.com/ziglang/zig/issues/8531
exe_cpp.want_lto = false;
},
.macos => {
// https://github.com/ziglang/zig/issues/8680
exe_cpp.want_lto = false;
exe_c.want_lto = false;
},
else => {},
}
const run_c_cmd = b.addRunArtifact(exe_c);
run_c_cmd.expectExitCode(0);
run_c_cmd.skip_foreign_checks = true;
test_step.dependOn(&run_c_cmd.step);
const run_cpp_cmd = b.addRunArtifact(exe_cpp);
run_cpp_cmd.expectExitCode(0);
run_cpp_cmd.skip_foreign_checks = true;
test_step.dependOn(&run_cpp_cmd.step);
}