I ran into the need for both while trying to compile zig (https://ziglang.org)
on my Arch Linux system and it fails. The problem is that the Arch Linux
clang package only provides shared libraries using the following cmake
command:
cmake .. -G Ninja \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_PREFIX=/usr \ -DPYTHON_EXECUTABLE=/usr/bin/python \ -DBUILD_SHARED_LIBS=ON \ -DLLVM_LINK_LLVM_DYLIB=ON \ -DLLVM_ENABLE_RTTI=ON \ -DLLVM_BUILD_TESTS=ON \ -DLLVM_INCLUDE_DOCS=ON \ -DLLVM_BUILD_DOCS=ON \ -DLLVM_ENABLE_SPHINX=ON \ -DSPHINX_WARNINGS_AS_ERRORS=OFF \ -DLLVM_EXTERNAL_LIT=/usr/bin/lit \ -DLLVM_MAIN_SRC_DIR="$srcdir/llvm-$pkgver.src"
In particular, it uses "BUILD_SHARED_LIBS=ON" to get the shared
libraries. This works but there are no static libraries and further
a note in the documentation says,
https://llvm.org/docs/CMake.html#llvm-specific-variables :
"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."
So it seemed to me it a solution would be to provide an easy way to build
clang or other llvm subprojects with both static and shared libraries.
As it turns out on small number modifications were needed:
clang/CMAkeLists.txt: - Add options CLANG_ENABLE_STATIC_LIBRARIES and CLANG_ENABLE_SHARED_LIBRARIES These are defaulted to ON so libclang* libraries are built as static and shared. As these are options they can be overridden on the cmake command line. clang/cmake/modules/AddClang.cmake add_clang_library: - Add support for STATIC, SHARED or both - Add support for OUTPUT_NAME which defaults to the ${name} passed to add_clang_library llvm/cmake/modules/AddLLVM.cmake llvm_add_library: - Changed so when both SHARED and STATIC are passed SHARED is built first as cmake object ${name}_shared. Without this change llvm_add_library causes the shared libraries to be linked to clang rather than the static library and clang fails when run with: $ ./bin/clang-9 --version : CommandLine Error: Option 'use-dbg-addr' registered more than once! LLVM ERROR: inconsistency in registered CommandLine options
These shouldn't both default to On, that is a change in behavior that would be a build-time regression for anyone not interested in building shared libraries. STATIC should default On, and SHARED default Off.
Also you need to check that one of the two options is enabled. If both are Off confusing things will happen.