#Copy compile options around

1 messages · Page 1 of 1 (latest)

granite pivot
#

Is there a proper Zig Way for passing compile/build configuration stuff into other files in a project?

I could just set a pub var in a given file but something about that feels off...like if it's a comptime-known value then it should be const, which of course you can't just set outside its actual declaration.

long mason
#

not sure I understand

#

if you just want to share a bunch of values from a file, just make a file with a bunch of pub consts

#

doesn't need to be var

granite pivot
#

Well for instance, how would I go about querying the build target in a source file?
builtin.os.tag is just the native OS, right?

long mason
#

no, that's the compilation target

#

though should be noted that in a build.zig, it is always the native target, because that is just compiled into an executable that is executed on your machine

granite pivot
#

Or, say I want to get a compile option passed via -D, how should I get that value from build.option()

long mason
#

that's, just how you get it?

#

you say b.option(T, "foo", "bar") orelse ..., store that, and then you make that available through whatever means you want

#

the most common way is to use b.addOptions()

#

that returns a build step which will construct a file of values

granite pivot
#

Okay, I'm probably just overcomplicating it/being picky about how to doit

long mason
#

e.g.

const build_options = b.addOptions();
exe.addOptions("build-options", build_options);
build_options.addOption(bool, "foo", true); // adds `pub const foo = true;`
#

this then allows you to do @import("build-options").foo in your exe

#

the value passed as the third parameter can be whatever you want, including the result of a call to b.option(...)

granite pivot
#

Oh okay, great, exactly what I was looking for. I'd just been doing build.option and storing the return in a local variable, and getting annoyed that I had to copy it to a var elsewhere

long mason
#

well, that wouldn't have worked in the first place

#

the build runner runs before the source files are ever compiled

#

so the source files would only see the initial value

#

can't just magically create inter-file global state