rust-lang / cc-rs

Rust library for build scripts to compile C/C++ code into a Rust library
https://docs.rs/cc
Apache License 2.0
1.77k stars 427 forks source link

`--gdwarf=4` is not compatible with some newer mainstream gcc/as toolchains #1075

Closed mqudsi closed 1 month ago

mqudsi commented 1 month ago

I ran into a situation where I'm unable to compile C code via cc if $CC is gcc-11 or gcc-13, though it works if I use env CC=gcc-9 and, of course, with CC set to any variant of clang. All three gcc toolchains were installed via apt under Ubuntu 23.10.

The actual command executed by the compiler:

[fish 0.1.0] error occurred: Command "sccache" "gcc-13" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-4" "-fno-omit-frame-pointer" "-m64" "-I" "/mnt/d/rand/fish/target/debug/build/fish-1dce6c150ef9f00d/out" "-o" "/mnt/d/rand/fish/target/debug/build/fish-1dce6c150ef9f00d/out/2e40c9e35e9506f4-libc.o" "-c" "src/libc.c" with args gcc-13 did not execute successfully (status code exit status: 1).
[fish 0.1.0]
[fish 0.1.0]
The following warnings were emitted during compilation:

warning: fish@0.1.0: as: unrecognized option '--gdwarf-4'

The interesting thing is that gcc-9 -dumpspecs | grep gdwarf prints only gdwarf2 support, but both gcc-11 and gcc-13 show support for up to gdwarf5:

> gcc-9 -dumpspecs | grep dwarf
%{%:debug-level-gt(0):%{gstabs*:--gstabs}%{!gstabs*:%{g*:--gdwarf2}}} %{fdebug-prefix-map=*:--debug-prefix-map %*}
%{gsplit-dwarf:
> gcc-11 -dumpspecs | grep dwarf
%{%:debug-level-gt(0):%{!gstabs*:%{g*:%{%:dwarf-version-gt(4):--gdwarf-5 ;%:dwarf-version-gt(3):--gdwarf-4 ;%:dwarf-version-gt(2):--gdwarf-3 ;:--gdwarf2 }}}}
%{gsplit-dwarf:

so if anything, I would have expected gcc-9 to be the one that doesn't support it, but the problem is with as:

> as --gdwarf-4
as: unrecognized option '--gdwarf-4'

-gdwarf-4 was introduced in 0fff25ae9aebfc0635e8b2ac719fda4047b4e252 (cc 1.0.77+), with the rationale that this would cause the compiler to match the compiler format rust is using, but I think this doesn't account for the fact that rust could be using an entirely different assembler/toolchain than what C code executed by build.rs uses.

mqudsi commented 1 month ago

It's an issue with newer versions of gcc not playing nicely with older versions of as.