mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.51k stars 1.6k forks source link

LTO + static libraries + clang produce unusable static libraries #10300

Open hdante opened 2 years ago

hdante commented 2 years ago

Hello, creating a static library with clang and LTO enabled generates a static library without the target object code (only LLVM IR code). Distributing this static library to the client and trying to link the static library with some program with gcc/ld results in the following error:

/usr/bin/ld: ./librandom.a: error adding symbols: file format not recognized
collect2: error: ld returned 1 exit status
Tachi107 commented 2 years ago

This is normal. In GCC you can pass -ffat-lto-objects to avoid this issue, but I don't know about Clang (source). I think that Meson should warn then the user requests both LTO and static libraries, as they can't easily coexist.

hdante commented 2 years ago

Hello, just giving a warning probably will not be enough, since LTO makes sense to build binaries in the same package. Maybe add support for enabling or disabling LTO on a per target basis ?

Tachi107 commented 2 years ago

I think you can do that with override_options

hdante commented 2 years ago

Ok, I'll give it a try, but what about the binary targets that use the static library ? They could be compiled with LTO enabled and use the library. Would it be necessary to define a second library target for them with LTO enabled ?

Tachi107 commented 2 years ago

I don't know, but if you figure it out please report back here as I'm curious :)

ericonr commented 2 years ago

I've recently been bit by this (or something similar) on GCC as well. I wonder if meson could automatically add that flag when a build with LTO enabled includes static libraries as installable output, or possibly have an option to disable LTO for the static library.