From what I gather when Meson performs linking of ELF binaries it trims unneeded shared libraries by default by passing --as-needed
flag to the linker. This answer says that the flag makes linker to go over the libraries list, check if the binary being linked really uses it and if not, remove it from the list.
Now, CMake has the CMAKE_LINK_WHAT_YOU_USE variable which seemingly does the same as b_asneeded
in Meson, although unlike Meson it is OFF by default in CMake. However, digging into CMake internals it turns out that CMAKE_LINK_WHAT_YOU_USE actually passes --no-as-needed
which is a default behavior anyways:
% grep -r LINK_WHAT_YOU_USE_FLAG /usr/local/share/cmake/
...
Modules/CMakeCXXInformation.cmake: if(NOT DEFINED CMAKE_CXX_LINK_WHAT_YOU_USE_FLAG)
Modules/CMakeCXXInformation.cmake: set(CMAKE_CXX_LINK_WHAT_YOU_USE_FLAG "LINKER:--no-as-needed")
This confuses me. How would I end up with unused libraries removed when using CMAKE_LINK_WHAT_YOU_USE
?
There is no direct analog to Meson's b_asneeded
in CMake at the moment of writing.