This is an archive of the discontinued LLVM Phabricator instance.

[flang] Fix DYLIB builds
ClosedPublic

Authored by awarzynski on Mar 11 2022, 6:19 AM.

Details

Summary

https://reviews.llvm.org/D120568 broke builds that set
both LLVM_BUILD_LLVM_DYLIB and LLVM_LINK_LLVM_DYLIB. This patch
fixes that.

The build failure was caused by the fact that some LLVM libraries (which
are also LLVM components) were listed directly as link-time dependencies
instead of using LINK_COMPONENTS in CMake files. This lead to a linker
invocation like this (simplified version to demonstrate the problem):

ld lib/libLLVM.so lib/libLLVMAnalysis.a lib/libLLVMTarget.a

That's problematic and unnecessary because libLLVM.so incorporates
libLLVMAnalysis and libLLVMTarget. A correct invocation would look
like this (LLVM_LINK_LLVM_DYLIB _is not_ set):

ld  lib/libLLVMAnalysis.a lib/libLLVMTarget.a

or this (LLVM_LINK_LLVM_DYLIB _is_ set):

ld lib/libLLVM.so

Diff Detail

Event Timeline

awarzynski created this revision.Mar 11 2022, 6:19 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptMar 11 2022, 6:19 AM
Herald added a subscriber: mgorny. · View Herald Transcript
awarzynski requested review of this revision.Mar 11 2022, 6:19 AM

So LINK_LIBS is what needs to be named in the linker command and LINK_COMPONENTS is what must be somewhere in the libraries in the link command, but could be part of another one?

Hence why we got an option registered twice because the registration ran once in some library and again in another than also contained the option registration.

Yes and yes :)

Items under LINK_LIBS are libraries, which are simply added to the linker invocation. The semantics are identical to target_link_libraries().

Items under LINK_COMPONENTS are components rather than libraries. A component will be translated into one or more libraries and this translation depends on whether various other flags, e.g. LLVM_LINK_LLVM_DYLIB, are used. IIUC, when this flag is set then all components are translated into libLLVM (see here). You can also experiment with llvm-config (e.g. bin/llvm-config --libs Target) and see what you get depending on the build flags used.

Basically, one should not mix LINK_LIBS and LINK_COMPONENTS because that may lead to the same library being linked twice (and then various CL options being registered twice).

This revision is now accepted and ready to land.Mar 11 2022, 7:45 AM
This revision was automatically updated to reflect the committed changes.