ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
33.51k stars 2.45k forks source link

`zig cc -dM -E - </dev/null` broken for `x86-*` targets (regression in v0.11.x) #16630

Open PiotrSikora opened 1 year ago

PiotrSikora commented 1 year ago

Zig Version

0.11.0-dev.4320+6f0a613b6

Steps to Reproduce and Observed Behavior

Single target (to be sure that I didn't mess the loop):

$ zig cc -target x86-linux-gnu -dM -E - </dev/null
error: unknown target triple 'x86-unknown-linux-gnu', please use -triple or -arch

All targets:

for target in `zig targets | jq .libc[] | cut -d\" -f2`; do
    echo -n "$target => "; echo "`zig cc -target $target -dM -E - </dev/null >/dev/null && echo OK`";
done
aarch64_be-linux-gnu => OK
aarch64_be-linux-musl => OK
aarch64_be-windows-gnu => OK
aarch64-linux-gnu => OK
aarch64-linux-musl => OK
aarch64-windows-gnu => OK
aarch64-macos-none => OK
aarch64-macos-none => OK
aarch64-macos-none => OK
armeb-linux-gnueabi => OK
armeb-linux-gnueabihf => OK
armeb-linux-musleabi => OK
armeb-linux-musleabihf => OK
armeb-windows-gnu => OK
arm-linux-gnueabi => OK
arm-linux-gnueabihf => OK
arm-linux-musleabi => OK
arm-linux-musleabihf => OK
thumb-linux-gnueabi => OK
thumb-linux-gnueabihf => OK
thumb-linux-musleabi => OK
thumb-linux-musleabihf => OK
arm-windows-gnu => OK
csky-linux-gnueabi => OK
csky-linux-gnueabihf => OK
x86-linux-gnu => error: unknown target triple 'x86-unknown-linux-gnu', please use -triple or -arch

x86-linux-musl => error: unknown target triple 'x86-unknown-linux-musl', please use -triple or -arch

x86-windows-gnu => error: unknown target triple 'x86-unknown-windows-gnu', please use -triple or -arch

m68k-linux-gnu => OK
m68k-linux-musl => OK
mips64el-linux-gnuabi64 => OK
mips64el-linux-gnuabin32 => OK
mips64el-linux-musl => OK
mips64-linux-gnuabi64 => OK
mips64-linux-gnuabin32 => OK
mips64-linux-musl => OK
mipsel-linux-gnueabi => OK
mipsel-linux-gnueabihf => OK
mipsel-linux-musl => OK
mips-linux-gnueabi => OK
mips-linux-gnueabihf => OK
mips-linux-musl => OK
powerpc64le-linux-gnu => OK
powerpc64le-linux-musl => OK
powerpc64-linux-gnu => OK
powerpc64-linux-musl => OK
powerpc-linux-gnueabi => OK
powerpc-linux-gnueabihf => OK
powerpc-linux-musl => OK
riscv64-linux-gnu => OK
riscv64-linux-musl => OK
s390x-linux-gnu => OK
s390x-linux-musl => OK
sparc-linux-gnu => OK
sparc64-linux-gnu => OK
wasm32-freestanding-musl => OK
wasm32-wasi-musl => OK
x86_64-linux-gnu => OK
x86_64-linux-gnux32 => OK
x86_64-linux-musl => OK
x86_64-windows-gnu => OK
x86_64-macos-none => OK
x86_64-macos-none => OK
x86_64-macos-none => OK

Expected Behavior

All targets should be supported when using zig cc -dM -E - </dev/null.

Note that zig cc works for those targets when using zig cc -dM -E <file.c>, and so does the compilation, so it's not clear to me why this is happening when reading from stdin, but it works for all the other targets, and it worked for i386-* targets in zig v0.10.1, so this is clearly a regression from the rename to x86-* in the v0.11.x release cycle:

for target in `zig targets | jq .libc[] | grep i386 | cut -d\" -f2`; do
    echo -n "$target => "; echo "`zig cc -target $target -dM -E - </dev/null >/dev/null && echo OK`";
done
i386-linux-gnu => OK
i386-linux-musl => OK
i386-windows-gnu => OK
andrewrk commented 1 year ago

Problem is here:

https://github.com/ziglang/zig/blob/e84cda0ebf8886346d42db78e8f3eb8d0bf515bd/src/main.zig#L2284-L2287

Args are passed to clang directly, but -target is not translated from zig style to clang style.

That logic cannot be naively deleted however, because the rest of the compiler is not designed to read from stdin, and will fail like this:

$ stage4/bin/zig cc -target x86-linux-gnu -dM -E - </dev/null
LLD Link... ld.lld: error: cannot open -: No such file or directory

The proper fix is to indeed delete this logic, but then edit Compilation.zig to handle this case specially and forward - and stdin to clang, but with the zig target and CPU feature flags. This will have the effect of fixing some other subtle bugs - anywhere zig has different CPU target defaults than clang.