rust-cross / cargo-zigbuild

Compile Cargo project with zig as linker
MIT License
1.45k stars 52 forks source link

Getting compiler errors about a missing __COLD attribute? from zig headers #268

Open weiznich opened 4 weeks ago

weiznich commented 4 weeks ago

I have a project that depends on netcdf-src 0.4.0. Trying to build that in the official rust-lang docker container with cargo-zigbuild 0.19.1 and zig 0.13.0 installed fails with the following error messages:

/opt/zig/lib/libc/include/generic-glibc/stdio.h:878:38: error: expected function body after function declarator
    878 | extern void perror (const char *__s) __COLD;
        |                                      ^
  In file included from /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/netcdf-src-0.4.0/source/libdispatch/dcopy.c:11:
  In file included from /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/netcdf-src-0.4.0/source/include/ncdispatch.h:17:
  /opt/zig/lib/libc/include/generic-glibc/stdio.h:878:38: error: expected function body after function declarator
    878 | extern void perror (const char *__s) __COLD;
        |                                      ^
  1 error generated.
  gmake[2]: *** [libsrc/CMakeFiles/netcdf3.dir/build.make:88: libsrc/CMakeFiles/netcdf3.dir/v1hpg.c.o] Error 1
  gmake[2]: *** Waiting for unfinished jobs....
  1 error generated.
// repeated many times

I can workaround that error by just adding CFLAGS="-D__COLD=\"\"" to the compilation environment. I report this here as this either seems to be an issue in cargo-zigbuild (not setting that option) or in zig itself (__COLD) shouldn't be there.

Interestingly it works outside of the docker container with the same rust, zig, zig-build and netcdf-src version…

messense commented 4 weeks ago

https://github.com/bminor/glibc/commit/0ab341b247fc1c2cd101a87c78f38d2da00c335c

It should have been defined by glibc itself (in glibc 2.38+), can you try to inspect the code of the same file in /opt/zig/lib/libc/include/?

weiznich commented 4 weeks ago

Thanks for the response.

I checked it in the rust docker container and it seem like:

Also ldd reports that the system uses glibc 2.36

It seems like that for some reason the system header from /usr is used and not the bundled header from zig

For reference that's with the rust:1.80.1 docker image (+ a view custom layers to install zig + cargo-zigbuild)

messense commented 4 weeks ago

Maybe it's caused by cmake? There is support for cmake in cargo-zigbuild but it's not really tested thoroughly.

https://github.com/rust-cross/cargo-zigbuild/blob/d420792226cef24e8a718dbef87fec14df77f3d2/src/zig.rs#L475-L486

weiznich commented 4 weeks ago

That might be the case, I'm not sure about that.

As a general note: The project I'm building depends on quite a few complex cmake projects (netcdf, hdf5, gdal, proj, just to name some). It works fine as soon as I specify that additional define manually. I believe that this means the cmake integration should be mostly fine.