authorgravatar for goon.pri.low@gmail.comKendall Condon <goon.pri.low@gmail.com> 2026-06-29 21:12:51-04:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-10-09 10:14:16+02:00
logaa8c774e0f15617137f56eb15c667e259b3be75a
tree8e8148a43d71ff452a2fba5404ffa8b29802fb60
parent3944c72a148096e3933745a9fda0397c0dd67cbf

allow standardOptimizeOption to return null

In order to allow the compiler to default to ReleaseSafe when compiling for fuzzing, Build.standardOptimizeOption has been changed to return an optional value.

5 files changed, 17 insertions(+), 14 deletions(-)

build.zig+1-1
......@@ -883,7 +883,7 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
883883}
884884
885885const AddCompilerModOptions = struct {
886 optimize: std.lang.Optimize,
886 optimize: ?std.lang.Optimize,
887887 target: std.Build.ResolvedTarget,
888888 strip: ?bool = null,
889889 valgrind: ?bool = null,
lib/std/Build.zig+3-3
......@@ -1190,14 +1190,14 @@ pub const StandardOptimizeOptionOptions = struct {
11901190 preferred_optimize_mode: ?std.builtin.Optimize = null,
11911191};
11921192
1193pub fn standardOptimizeOption(b: *Build, options: StandardOptimizeOptionOptions) std.builtin.Optimize {
1193pub fn standardOptimizeOption(b: *Build, options: StandardOptimizeOptionOptions) ?std.builtin.Optimize {
11941194 const graph = b.graph;
11951195
11961196 if (options.preferred_optimize_mode) |mode| {
11971197 if (b.option(bool, "release", "optimize for end users") orelse (graph.release_mode != .off)) {
11981198 return mode;
11991199 } else {
1200 return .debug;
1200 return null;
12011201 }
12021202 }
12031203
......@@ -1210,7 +1210,7 @@ pub fn standardOptimizeOption(b: *Build, options: StandardOptimizeOptionOptions)
12101210 }
12111211
12121212 return switch (graph.release_mode) {
1213 .off => .debug,
1213 .off => null,
12141214 .any => {
12151215 std.debug.print("the project does not declare a preferred optimization mode. choose: --release=fast, --release=safe, or --release=small\n", .{});
12161216 process.exit(1);
test/standalone/dependency_options/build.zig+3-3
......@@ -10,14 +10,14 @@ pub fn build(b: *std.Build) !void {
1010
1111 const none_specified_mod = none_specified.module("dummy");
1212 if (!none_specified_mod.resolved_target.?.query.eql(b.graph.host.query)) return error.TestFailed;
13 const expected_optimize: std.builtin.Optimize = switch (b.graph.release_mode) {
14 .off => .debug,
13 const expected_optimize: ?std.builtin.OptimizeMode = switch (b.graph.release_mode) {
14 .off => null,
1515 .any => unreachable,
1616 .fast => .fast,
1717 .safe => .safe,
1818 .small => .small,
1919 };
20 if (none_specified_mod.optimize.? != expected_optimize) return error.TestFailed;
20 if (none_specified_mod.optimize != expected_optimize) return error.TestFailed;
2121
2222 // Passing null is the same as not specifying the option,
2323 // so this should resolve to the same cached dependency instance.
test/standalone/posix/build.zig+1-1
......@@ -55,7 +55,7 @@ pub fn build(b: *std.Build) void {
5555 }
5656}
5757
58fn run_exe(b: *std.Build, optimize: std.builtin.Optimize, case: *const Case, target: std.Build.ResolvedTarget, link_libc: bool) *std.Build.Step.Run {
58fn run_exe(b: *std.Build, optimize: ?std.builtin.Optimize, case: *const Case, target: std.Build.ResolvedTarget, link_libc: bool) *std.Build.Step.Run {
5959 const exe_name = b.fmt("test-posix-{s}{s}{s}", .{
6060 std.fs.path.stem(case.src_path),
6161 if (link_libc) "-libc" else "",
test/tests.zig+9-6
......@@ -20,7 +20,7 @@ pub const LinkContext = @import("src/Link.zig");
2020const ModuleTestTarget = struct {
2121 linkage: ?std.builtin.LinkMode = null,
2222 target: std.Target.Query = .{},
23 optimize_mode: std.builtin.Optimize = .debug,
23 optimize_mode: ?std.builtin.Optimize = .debug,
2424 link_libc: ?bool = null,
2525 single_threaded: ?bool = null,
2626 use_llvm: ?bool = null,
......@@ -2795,7 +2795,7 @@ pub const ModuleTestOptions = struct {
27952795
27962796 pub const TestOnly = union(enum) {
27972797 default: void,
2798 fuzz: OptimizeMode,
2798 fuzz: ?OptimizeMode,
27992799 };
28002800};
28012801
......@@ -2896,6 +2896,9 @@ fn addOneModuleTest(
28962896 options: ModuleTestOptions,
28972897) void {
28982898 const target = &resolved_target.result;
2899 const optimize_suffix = if (test_target.optimize_mode) |mode| switch (mode) {
2900 inline else => |t| "-" ++ @tagName(t),
2901 } else "";
28992902 const libc_suffix = if (test_target.link_libc == true) "-libc" else "";
29002903 const model_txt = target.cpu.model.name;
29012904
......@@ -2948,11 +2951,11 @@ fn addOneModuleTest(
29482951
29492952 for (options.include_paths) |include_path| these_tests.root_module.addIncludePath(b.path(include_path));
29502953
2951 const qualified_name = b.fmt("{s}-{s}-{s}-{t}{s}{s}{s}{s}{s}{s}", .{
2954 const qualified_name = b.fmt("{s}-{s}-{s}{s}{s}{s}{s}{s}{s}{s}", .{
29522955 options.name,
29532956 triple_txt,
29542957 model_txt,
2955 test_target.optimize_mode,
2958 optimize_suffix,
29562959 libc_suffix,
29572960 single_threaded_suffix,
29582961 backend_suffix,
......@@ -3087,11 +3090,11 @@ fn addOneModuleTest(
30873090 }
30883091}
30893092
3090pub fn wouldUseLlvm(use_llvm: ?bool, query: std.Target.Query, optimize_mode: OptimizeMode) bool {
3093pub fn wouldUseLlvm(use_llvm: ?bool, query: std.Target.Query, optimize_mode: ?OptimizeMode) bool {
30913094 if (comptime builtin.cpu.arch.endian() == .big) return true; // https://github.com/ziglang/zig/issues/25961
30923095 if (use_llvm) |x| return x;
30933096 if (query.ofmt == .c) return false;
3094 switch (optimize_mode) {
3097 switch (optimize_mode orelse .debug) {
30953098 .debug => {},
30963099 else => return true,
30973100 }