Problem:
The default for libraries built using llvm_add_library is STATIC unless
BUILD_SHARED_LIBS is ON but having BUILD_SHARED_LIBS is an unsupported
configuration for non LLVM developers. See Note for BUILD_SHARED_LIBS:BOOL
in https://llvm.org/docs/CMake.html where it says:
"BUILD_SHARED_LIBS is only recommended for use by LLVM developers. If you
want to build LLVM as a shared library, you should use the
LLVM_BUILD_LLVM_DYLIB option."
As well as building STATIC libraries llvm_add_library can be used to
create SHARED, OBJECT or MODULE libraries. When creating these the first
parameter, ${name} is used as the name of the cmake object and also its
OUTPUT_NAME.
There is one exception to the naming rule, if both STATIC and SHARED are
passed to llvm_add_library both a static and shared libraries are created
and they must have different names. The current code uses ${name} for the
shared library and ${name}_static for the static library.
This causes a problem because by default LLVM sub-projects prefer linking
to LLVM using its static libraries, libLLVM*.a. For example, when clang
does this. But, if one of libclang* libraries is built by passing STATIC
and SHARED to llvm_add_library clang compiles and links just fine, but
when it is run we get an error:
$ ./bin/clang --version : CommandLine Error: Option 'use-dbg-addr' registered more than once! LLVM ERROR: inconsistency in registered CommandLine options
This happens because there are two copies of libLLVM, the static version
linked to the statically linked libclang*.a libraries and the shared
version linked to the shared libclangXxx.so.
Solution:
Use ${name} for the STATIC library and ${name}_shared for the SHARED
library. With this change even if both STATIC and SHARED versions of a
particular library is created ${name} is the static version and there
is no error.