#Check operating system at compile time for builds?

1 messages · Page 1 of 1 (latest)

cyan rose
#

In other languages, there are ways to add compile-time conditionals. I've used this a lot for build system. Is there something similar for Zig?

Example with Nim:

when defined(linux):
  # Do some Linux specific thing
else:
  # Handle other systems

In the case above, the code inside the when/else block would be eliminated at compile time based on the condition

glacial fox
#

branches not taken at comptime won't be semantically analyzed

cyan rose
#

That's great, so I'd just need to get the system type at comptime

nocturne flame
#
switch (@import("builtin").os.tag) {
    .linux => {},
    else => {},
}
#

You can ofc also use if/else if you prefer :)

glacial fox
#
if (builtin.os.tag == .linux) {
  // do the linux thing
} else {
  // do the other thing
}```