Index: lld/CMakeLists.txt =================================================================== --- lld/CMakeLists.txt +++ lld/CMakeLists.txt @@ -192,6 +192,14 @@ option(LLD_BUILD_TOOLS "Build the lld tools. If OFF, just generate build targets." ON) +set(LLD_LINK_LLD_DYLIB ${LLVM_LINK_LLVM_DYLIB} CACHE BOOL + "Link tools against libLLD.so") + +if (NOT LLVM_LINK_LLVM_DYLIB AND LLD_LINK_LLD_DYLIB) + message(FATAL_ERROR "Cannot set LLD_LINK_LLD_DYLIB=ON when " + "LLVM_LINK_LLVM_DYLIB=OFF") +endif() + if (MSVC) add_definitions(-wd4530) # Suppress 'warning C4530: C++ exception handler used, but unwind semantics are not enabled.' add_definitions(-wd4062) # Suppress 'warning C4062: enumerator X in switch of enum Y is not handled' from system header. @@ -213,7 +221,6 @@ add_subdirectory(Common) add_subdirectory(lib) -add_subdirectory(tools/lld) if (LLVM_INCLUDE_TESTS) add_subdirectory(test) @@ -227,4 +234,7 @@ add_subdirectory(MinGW) add_subdirectory(wasm) +add_subdirectory(tools/lld) +add_subdirectory(tools/lld-shlib) + add_subdirectory(cmake/modules) Index: lld/cmake/modules/AddLLD.cmake =================================================================== --- lld/cmake/modules/AddLLD.cmake +++ lld/cmake/modules/AddLLD.cmake @@ -1,16 +1,19 @@ macro(add_lld_library name) cmake_parse_arguments(ARG - "SHARED" + "SHARED;INSTALL_WITH_TOOLCHAIN" "" "" ${ARGN}) if(ARG_SHARED) - set(ARG_ENABLE_SHARED SHARED) + set(LIBTYPE SHARED) + else() + set(LIBTYPE STATIC OBJECT) + set_property(GLOBAL APPEND PROPERTY LLD_STATIC_LIBS ${name}) endif() - llvm_add_library(${name} ${ARG_ENABLE_SHARED} ${ARG_UNPARSED_ARGUMENTS}) + llvm_add_library(${name} ${LIBTYPE} ${ARG_UNPARSED_ARGUMENTS}) set_target_properties(${name} PROPERTIES FOLDER "lld libraries") - if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY) + if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY OR ARG_INSTALL_WITH_TOOLCHAIN) if(${name} IN_LIST LLVM_DISTRIBUTION_COMPONENTS OR NOT LLVM_DISTRIBUTION_COMPONENTS) set(export_to_lldtargets EXPORT LLDTargets) Index: lld/tools/lld-shlib/CMakeLists.txt =================================================================== --- /dev/null +++ lld/tools/lld-shlib/CMakeLists.txt @@ -0,0 +1,50 @@ +# Building libLLD.so fails if LLVM_ENABLE_PIC=Off +if (NOT LLVM_ENABLE_PIC) + return() +endif() + +get_property(lld_libs GLOBAL PROPERTY LLD_STATIC_LIBS) + +foreach (lib ${lld_libs}) + if(XCODE) + # Xcode doesn't support object libraries, so we have to trick it into + # linking the static libraries instead. + list(APPEND _DEPS "-force_load" ${lib}) + else() + list(APPEND _OBJECTS $) + endif() + if (BUILD_SHARED_LIBS) + # If we are building static libraries, then we don't need to add the static + # libraries as a depedency, because we are already linking against the + # individual object files. + list(APPEND _DEPS $) + endif() + + # lld libraries are redundant since we are linking all the individual + # object files into libclang-cpp.so, so filter them out from _DEPS. + # This avoids problems with LLVM global data when building with + # BUILD_SHARED_LIBS=ON + # FIXME: We could use list(FILTER) with cmake >= 3.6 + # FIXME: With cmake >= 3.15 we could use the generator expression + # $ + get_target_property(interface ${lib} LINK_LIBRARIES) + if (interface) + foreach(lib ${interface}) + if (NOT ${lib} MATCHES "^LLD") + list(APPEND _DEPS ${lib}) + endif() + endforeach() + endif() +endforeach () + +if (LLD_LINK_LLD_DYLIB) + set(INSTALL_WITH_TOOLCHAIN INSTALL_WITH_TOOLCHAIN) +endif() + +add_lld_library(LLD + SHARED + ${INSTALL_WITH_TOOLCHAIN} + lld-shlib.cpp + ${_OBJECTS} + LINK_LIBS + ${_DEPS}) Index: lld/tools/lld-shlib/lld-shlib.cpp =================================================================== --- /dev/null +++ lld/tools/lld-shlib/lld-shlib.cpp @@ -0,0 +1 @@ +// Intentionally empty source file to make CMake happy Index: lld/tools/lld/CMakeLists.txt =================================================================== --- lld/tools/lld/CMakeLists.txt +++ lld/tools/lld/CMakeLists.txt @@ -9,16 +9,22 @@ ) export_executable_symbols_for_plugins(lld) -target_link_libraries(lld - PRIVATE - lldCommon - lldCOFF - lldDriver - lldELF - lldMachO2 - lldMinGW - lldWasm - ) +if (LLD_LINK_LLD_DYLIB) + target_link_libraries(lld + PRIVATE + LLD) +else() + target_link_libraries(lld + PRIVATE + lldCommon + lldCOFF + lldDriver + lldELF + lldMachO2 + lldMinGW + lldWasm + ) +endif() install(TARGETS lld RUNTIME DESTINATION bin)