Conditional compilation with build time enums?

Is there a way to have build time user defined enum values? I want to be able to do conditional compilation similar to what builtin gives us.

For example, with builtin we can do conditional compilation using cpu.arch and os.tag enums like this:

if (builtin.cpu.arch == .aarch64) {
  // do this
}
if (builtin.os.tag == .windows) {
  // do that
}

and I want something similar:

$ zig build -Dtask=both
if (conditions.task == .first or conditions.task == .both) {
  task1()
}
if (conditions.task == .second or conditions.task == .both) {
  task2()
}

You can find it here: Zig Build System ⚡ Zig Programming Language

1 Like

@hizani Just be aware when you look at that:

  • std.Build.option() (i.e. b.option() in this example) creates a command-line switch / parameter for zig build.
  • std.Build.addOptions() (i.e. b.addOptions) creates a container for key-value pairs that can be converted to Zig code.
  • std.Build.Step.Options.addOption() (i.e. options.addOption() adds key-value pairs to that container.
  • std.Build.Module.addOptions() (i.e. exe.root_module.addOptions()) adds the key-value container, as a Zig code, to the project so it can be @imported.

IMHO somebody needs to buy a thesaurus and find some alternative nouns to “option” for that API. I found it all highly confusing the first time I read it.

1 Like