From 996e62eef750942e174c4b80892b28e198e3a8d0 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Thu, 30 May 2019 01:34:41 +0000 Subject: [runtimes] Support ELF dependent libraries feature As of r360984, LLD supports dependent libraries feature for ELF. libunwind, libc++abi and libc++ have library dependencies: libdl librt and libpthread, which means that when libunwind and libc++ are being statically linked (using -static-libstdc++ flag), user has to manually specify -ldl -lpthread which is onerous. This change includes the lib pragma to specify the library dependencies directly in the source that uses those libraries. This doesn't make any difference when using linkers that don't support dependent libraries. However, when using LLD that has dependent libraries feature, users no longer have to manually specifying library dependencies when using static linking, linker will pick the library automatically. Differential Revision: https://reviews.llvm.org/D62090 llvm-svn: 362048 --- libcxx/src/algorithm.cpp | 5 +++++ libcxx/src/chrono.cpp | 4 ++++ libcxx/src/condition_variable.cpp | 4 ++++ libcxx/src/debug.cpp | 5 +++++ libcxx/src/experimental/memory_resource.cpp | 3 +++ libcxx/src/filesystem/operations.cpp | 4 ++++ libcxx/src/memory.cpp | 3 +++ libcxx/src/mutex.cpp | 6 ++++++ libcxx/src/shared_mutex.cpp | 3 +++ libcxx/src/thread.cpp | 4 ++++ libcxxabi/src/cxa_guard_impl.h | 5 +++++ libcxxabi/src/cxa_thread_atexit.cpp | 6 ++++++ libcxxabi/src/fallback_malloc.cpp | 5 +++++ libunwind/src/AddressSpace.hpp | 3 +++ libunwind/src/RWMutex.hpp | 3 +++ 15 files changed, 63 insertions(+) diff --git a/libcxx/src/algorithm.cpp b/libcxx/src/algorithm.cpp index 28e452f52df..5ce2a23b464 100644 --- a/libcxx/src/algorithm.cpp +++ b/libcxx/src/algorithm.cpp @@ -8,7 +8,12 @@ #include "algorithm" #include "random" +#ifndef _LIBCPP_HAS_NO_THREADS #include "mutex" +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "pthread") +#endif +#endif _LIBCPP_BEGIN_NAMESPACE_STD diff --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp index c1eb67b61d7..a2f88c94c07 100644 --- a/libcxx/src/chrono.cpp +++ b/libcxx/src/chrono.cpp @@ -37,6 +37,10 @@ #endif #endif +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "rt") +#endif + _LIBCPP_BEGIN_NAMESPACE_STD namespace chrono diff --git a/libcxx/src/condition_variable.cpp b/libcxx/src/condition_variable.cpp index 4022ff2e9ad..69264c680d9 100644 --- a/libcxx/src/condition_variable.cpp +++ b/libcxx/src/condition_variable.cpp @@ -15,6 +15,10 @@ #include "system_error" #include "__undef_macros" +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "pthread") +#endif + _LIBCPP_BEGIN_NAMESPACE_STD condition_variable::~condition_variable() diff --git a/libcxx/src/debug.cpp b/libcxx/src/debug.cpp index 7fdf90c37d9..95024131011 100644 --- a/libcxx/src/debug.cpp +++ b/libcxx/src/debug.cpp @@ -13,7 +13,12 @@ #include "string" #include "cstdio" #include "__hash_table" +#ifndef _LIBCPP_HAS_NO_THREADS #include "mutex" +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "pthread") +#endif +#endif _LIBCPP_BEGIN_NAMESPACE_STD diff --git a/libcxx/src/experimental/memory_resource.cpp b/libcxx/src/experimental/memory_resource.cpp index 22bc12c1005..84c95080496 100644 --- a/libcxx/src/experimental/memory_resource.cpp +++ b/libcxx/src/experimental/memory_resource.cpp @@ -12,6 +12,9 @@ #include "atomic" #elif !defined(_LIBCPP_HAS_NO_THREADS) #include "mutex" +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "pthread") +#endif #endif _LIBCPP_BEGIN_NAMESPACE_LFTS_PMR diff --git a/libcxx/src/filesystem/operations.cpp b/libcxx/src/filesystem/operations.cpp index 5ba979ca905..319d9f65d73 100644 --- a/libcxx/src/filesystem/operations.cpp +++ b/libcxx/src/filesystem/operations.cpp @@ -44,6 +44,10 @@ #include // for gettimeofday and timeval #endif // !defined(CLOCK_REALTIME) +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "rt") +#endif + #if defined(_LIBCPP_COMPILER_GCC) #if _GNUC_VER < 500 #pragma GCC diagnostic ignored "-Wmissing-field-initializers" diff --git a/libcxx/src/memory.cpp b/libcxx/src/memory.cpp index 8b05c3f1649..6df7226b357 100644 --- a/libcxx/src/memory.cpp +++ b/libcxx/src/memory.cpp @@ -10,6 +10,9 @@ #ifndef _LIBCPP_HAS_NO_THREADS #include "mutex" #include "thread" +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "pthread") +#endif #endif #include "include/atomic_support.h" diff --git a/libcxx/src/mutex.cpp b/libcxx/src/mutex.cpp index cecb89b3399..d100f2df233 100644 --- a/libcxx/src/mutex.cpp +++ b/libcxx/src/mutex.cpp @@ -12,6 +12,12 @@ #include "include/atomic_support.h" #include "__undef_macros" +#ifndef _LIBCPP_HAS_NO_THREADS +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "pthread") +#endif +#endif + _LIBCPP_BEGIN_NAMESPACE_STD #ifndef _LIBCPP_HAS_NO_THREADS diff --git a/libcxx/src/shared_mutex.cpp b/libcxx/src/shared_mutex.cpp index e918e1bdf60..3f1aecfdfe1 100644 --- a/libcxx/src/shared_mutex.cpp +++ b/libcxx/src/shared_mutex.cpp @@ -10,6 +10,9 @@ #ifndef _LIBCPP_HAS_NO_THREADS #include "shared_mutex" +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "pthread") +#endif _LIBCPP_BEGIN_NAMESPACE_STD diff --git a/libcxx/src/thread.cpp b/libcxx/src/thread.cpp index 29b06fdf660..92690f66798 100644 --- a/libcxx/src/thread.cpp +++ b/libcxx/src/thread.cpp @@ -35,6 +35,10 @@ #include #endif +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "pthread") +#endif + _LIBCPP_BEGIN_NAMESPACE_STD thread::~thread() diff --git a/libcxxabi/src/cxa_guard_impl.h b/libcxxabi/src/cxa_guard_impl.h index 412099e6f4e..bd6b15fce6a 100644 --- a/libcxxabi/src/cxa_guard_impl.h +++ b/libcxxabi/src/cxa_guard_impl.h @@ -49,6 +49,11 @@ #include #include <__threading_support> +#ifndef _LIBCXXABI_HAS_NO_THREADS +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "pthread") +#endif +#endif // To make testing possible, this header is included from both cxa_guard.cpp // and a number of tests. diff --git a/libcxxabi/src/cxa_thread_atexit.cpp b/libcxxabi/src/cxa_thread_atexit.cpp index da1df861708..38787f18fe3 100644 --- a/libcxxabi/src/cxa_thread_atexit.cpp +++ b/libcxxabi/src/cxa_thread_atexit.cpp @@ -9,6 +9,12 @@ #include "abort_message.h" #include "cxxabi.h" #include <__threading_support> +#ifndef _LIBCXXABI_HAS_NO_THREADS +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "pthread") +#endif +#endif + #include namespace __cxxabiv1 { diff --git a/libcxxabi/src/fallback_malloc.cpp b/libcxxabi/src/fallback_malloc.cpp index 8ec1eeefd84..bae0fa4ac23 100644 --- a/libcxxabi/src/fallback_malloc.cpp +++ b/libcxxabi/src/fallback_malloc.cpp @@ -12,6 +12,11 @@ #include "fallback_malloc.h" #include <__threading_support> +#ifndef _LIBCXXABI_HAS_NO_THREADS +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "pthread") +#endif +#endif #include // for malloc, calloc, free #include // for memset diff --git a/libunwind/src/AddressSpace.hpp b/libunwind/src/AddressSpace.hpp index fb370ad1e79..66439530309 100644 --- a/libunwind/src/AddressSpace.hpp +++ b/libunwind/src/AddressSpace.hpp @@ -27,6 +27,9 @@ #if _LIBUNWIND_USE_DLADDR #include +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "dl") +#endif #endif #ifdef __APPLE__ diff --git a/libunwind/src/RWMutex.hpp b/libunwind/src/RWMutex.hpp index 7a08bb2af32..4f234a77edf 100644 --- a/libunwind/src/RWMutex.hpp +++ b/libunwind/src/RWMutex.hpp @@ -17,6 +17,9 @@ #include #elif !defined(_LIBUNWIND_HAS_NO_THREADS) #include +#if defined(__unix__) && defined(__ELF__) && defined(__clang__) +#pragma comment(lib, "pthread") +#endif #endif namespace libunwind { -- cgit v1.2.1