Zig `-fincremental` requires C `-fPIE`? (and what to do about it)

Interesting detail just popped up in the sokol-zig issue tracker: it looks like that Zig’s incremental compilation requires any C dependencies to be built as position-independent (-fPIE), otherwise a compiler crash happens with thread 22765 panic: REX_GOTPCRELX (in the non-LLVM x86 backend).

Compiling C deps with -fPIE seems to workaround that crash (haven’t tested that myself though), see the comments towards the end:

…so regardless whether that’s a compiler bug or expected behaviour, that got me thinking how the build system of C dependencies should react to similar requirements from the Zig build (e.g. basically “if Zig build option X is enabled, C build option Y also needs to be enabled”).

Is the Zig build system implementation expected to take care of that (e.g. the build system would automatically inject an -fPIE into all C dependencies when the Zig code is built with -fincremental), or if the C dependency build system is expected to handle such cases, what’s the recommend way?

Adding a build option (e.g. -D...) isn’t really an option (heh), because that would need to be passed down the entire build tree explicitly, even into Zig dependencies which might theoretically depend on C dependencies.

Or maybe just build all C code as position-independent?

(also: related older ticket: https://codeberg.org/ziglang/zig/issues/30780)

2 Likes