diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -558,6 +558,7 @@ endif() option(LLVM_ENABLE_LOCAL_SUBMODULE_VISIBILITY "Compile with -fmodules-local-submodule-visibility." ON) option(LLVM_ENABLE_LIBCXX "Use libc++ if available." OFF) +option(LLVM_ENABLE_LLVM_LIBC "Set to on to link all LLVM executables against LLVM libc, assuming it is accessible by the host compiler." OFF) option(LLVM_STATIC_LINK_CXX_STDLIB "Statically link the standard library." OFF) option(LLVM_ENABLE_LLD "Use lld as C and C++ linker." OFF) option(LLVM_ENABLE_PEDANTIC "Compile with pedantic enabled." ON) diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -1029,6 +1029,10 @@ target_link_libraries(${name} PRIVATE ${LLVM_PTHREAD_LIB}) endif() + if(HAVE_LLVM_LIBC) + target_link_libraries(${name} PRIVATE llvmlibc) + endif() + llvm_codesign(${name} ENTITLEMENTS ${ARG_ENTITLEMENTS} BUNDLE_PATH ${ARG_BUNDLE_PATH}) endmacro(add_llvm_executable name) diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake --- a/llvm/cmake/modules/HandleLLVMOptions.cmake +++ b/llvm/cmake/modules/HandleLLVMOptions.cmake @@ -1300,3 +1300,10 @@ set(LLVM_THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../third-party CACHE STRING "Directory containing third party software used by LLVM (e.g. googletest)") + +if(LLVM_ENABLE_LLVM_LIBC) + check_library_exists(llvmlibc printf "" HAVE_LLVM_LIBC) + if(NOT HAVE_LLVM_LIBC) + message(WARNING "Unable to link against LLVM libc. LLVM will be built without linking against the LLVM libc overlay.") + endif() +endif() diff --git a/llvm/docs/CMake.rst b/llvm/docs/CMake.rst --- a/llvm/docs/CMake.rst +++ b/llvm/docs/CMake.rst @@ -514,6 +514,11 @@ passed to invocations of both so that the project is built using libc++ instead of stdlibc++. Defaults to OFF. +**LLVM_ENABLE_LLVM_LIBC**: BOOL + If the LLVM libc overlay is installed in a location where the host linker + can access it, all built executables will be linked against the LLVM libc + overlay before linking against the system libc. Defaults to OFF. + **LLVM_ENABLE_LIBPFM**:BOOL Enable building with libpfm to support hardware counter measurements in LLVM tools.