I am building a library that utilizes a lot of floating-point arithmetic in performance-critical code, and was attempting to expose an enumeration value as a build-option to use @setFloatMode
with, which I would use as the value of to set where needed.
Minimal example, nothing surprising:
build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
// ---------------------------------------
const options = b.addOptions();
options.addOption(std.builtin.FloatMode, "float_mode", std.builtin.FloatMode.optimized);
exe_mod.addOptions("config", options);
// ---------------------------------------
const exe = b.addExecutable(.{
.name = "app",
.root_module = exe_mod,
});
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
main.zig
const std = @import("std");
const config = @import("config");
pub fn main() !void {
@setFloatMode(config.float_mode);
}
To my surprise:
β― zig build run
run
ββ run temp
ββ install
ββ install temp
ββ zig build-exe temp Debug native 1 errors
src/main.zig:5:25: error: expected type 'builtin.FloatMode', found 'options.builtin.FloatMode'
@setFloatMode(config.float_mode);
~~~~~~^~~~~~~~~~~
.zig-cache/c/00ba311644cb64fb948884833c869798/options.zig:1:34: note: enum declared here
pub const @"builtin.FloatMode" = enum (u1) {
^~~~
/home/eric/.local/share/zvm/0.14.0/lib/std/builtin.zig:806:23: note: enum declared here
pub const FloatMode = enum {
^~~~
error: the following command failed with 1 compilation errors:
/home/eric/.local/share/zvm/0.14.0/zig build-exe -ODebug --dep config -Mroot=/home/eric/downloads/temp/src/main.zig -Mconfig=/home/eric/downloads/temp/.zig-cache/c/00ba311644cb64fb948884833c869798/options.zig --ca
che-dir /home/eric/downloads/temp/.zig-cache --global-cache-dir /home/eric/.cache/zig --name temp --zig-lib-dir /home/eric/.local/share/zvm/0.14.0/lib/ --listen=-
Build Summary: 1/6 steps succeeded; 1 failed
run transitive failure
ββ run temp transitive failure
ββ zig build-exe temp Debug native 1 errors
ββ install transitive failure
ββ install temp transitive failure
ββ zig build-exe temp Debug native (+1 more reused dependencies)
error: the following build command failed with exit code 1:
/home/eric/downloads/temp/.zig-cache/o/03d6290b99f12aa1397fa251451bf735/build /home/eric/.local/share/zvm/0.14.0/zig /home/eric/.local/share/zvm/0.14.0/lib /home/eric/downloads/temp /home/eric/downloads/temp/.zig-
cache /home/eric/.cache/zig --seed 0x1856d50d -Zd8bac23338536ccf run
Pertinent line:
error: expected type 'builtin.FloatMode', found 'options.builtin.FloatMode'
What? Why?
I have come up with an acceptable workaround using a bool
, though it is less than ideal for my purposes. i was hoping someone would be willing to explain why this causes an error, or point out the obivous that I am missing.