Using compiler/codegen options not provided by Zig build system

Is there a way to set codegen options not exposed by the Zig build system? Specifically, I need the -mtp=... option for AArch64 to specify the TPIDR_ELx register used for thread-local accesses. The option seems to be exposed in zig cc, but not in the Zig build system itself, i.e., I cannot attach it to a module/executable.

You can directly pass C/C++ compiler flags via the .flags field in the addCSourceFile method. E.g. smth like:

  lib.addCSourceFiles(.{
      .files = &.{ "src/bla.c", "src/blub.c" },
      .flags = &.{ "-fno-sanitize=undefined", "..." },
  });

…not sure about linker options though.

Oh, I forgot to mention I need those options for Zig codegen, not for C/C++. So addCSourceFiles won’t work for me unfortunately.

Hmm, then I don’t know… AFAIK the cmdline options in zig cc are basically forwarded to the integrated Clang frontend, but don’t do anything for compiling Zig code.

For Zig, this is part of the CPU target (specifically, the CPU feature flags tpidr_el1, tpidr_el2, tpidr_el3).

With zig build-exe, etc the option is -mcpu

With build.zig and when using b.standardTargetOptions, use -Dcpu

If you want to set a specific target within build.zig, you can do something like this:

Use zig targets to see all the possible targets/cpus/features. You can add or remove feature flags by appending +<flag> or -<flag> to the end of the cpu target (and multiple additions and removals can be chained, so e.g. -mcpu=generic+foo-bar)

4 Likes