[llvm] Use hidden visibility when building for MinGW with Clang
Since c5b3de6745
(git main,
August 11th), Clang does generate working hidden visibility
on MinGW targets. Using that reduces the number of exports from
a dylib build of LLVM significantly, which is vital for fitting
within the limit of 64k exported symbols from a DLL.
It's essential that if we set CMAKE_CXX_VISIBILITY_PRESET=hidden
(which passes -fvisibility=hidden on the command line), we also
must define LLVM_EXTERNAL_VISIBILITY consistently to override
it. (If there are mismatches, e.g. setting hidden visibility generally
but never overriding it back to default for the symbols that do need
to be exported, we'd get broken builds in such configurations.)
We don't want to be using __attribute__((visibility("hidden"))) on
MinGW with GCC, because GCC produces a warning about it. (GCC hasn't
warned about the command line options that set hidden visibility
though.) Clang has historically not warned about either of them, so
it is harmless to use the hidden visibility when building with older
Clang (so we don't need to detect the exact version of Clang/LLVM where
it has an effect).
This reduces the number of exported symbols for a dylib build of LLVM;
previously libLLVM exported around 64650 symbols (when the maximum is
65536) when the ARM, AArch64 and X86 targets were enabled. If enabling
more targets (or if building with e.g. assertions enabled), it would
exceed the limit. Now with visibility flags in use, the same build
with ARM, AArch64 and X86 ends up at around 35k exported symbols.
Differential Revision: https://reviews.llvm.org/D131661
This commit is contained in:
parent
a5881e8a81
commit
2c2fb0c737
|
@ -351,8 +351,11 @@ if( LLVM_ENABLE_PIC )
|
|||
endif()
|
||||
endif()
|
||||
|
||||
if(NOT WIN32 AND NOT CYGWIN AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX"))
|
||||
# MinGW warns if -fvisibility-inlines-hidden is used.
|
||||
if((NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX")) AND
|
||||
(NOT (WIN32 OR CYGWIN) OR (MINGW AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")))
|
||||
# GCC for MinGW does nothing about -fvisibility-inlines-hidden, but warns
|
||||
# about use of the attributes. As long as we don't use the attributes (to
|
||||
# override the default) we shouldn't set the command line options either.
|
||||
# GCC on AIX warns if -fvisibility-inlines-hidden is used and Clang on AIX doesn't currently support visibility.
|
||||
check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG)
|
||||
append_if(SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG "-fvisibility-inlines-hidden" CMAKE_CXX_FLAGS)
|
||||
|
|
|
@ -113,8 +113,9 @@
|
|||
/// LLVM_EXTERNAL_VISIBILITY - classes, functions, and variables marked with
|
||||
/// this attribute will be made public and visible outside of any shared library
|
||||
/// they are linked in to.
|
||||
#if __has_attribute(visibility) && !defined(__MINGW32__) && \
|
||||
!defined(__CYGWIN__) && !defined(_WIN32)
|
||||
#if __has_attribute(visibility) && \
|
||||
(!(defined(_WIN32) || defined(__CYGWIN__)) || \
|
||||
(defined(__MINGW32__) && defined(__clang__)))
|
||||
#define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
|
||||
#if defined(LLVM_BUILD_LLVM_DYLIB) || defined(LLVM_BUILD_SHARED_LIBS)
|
||||
#define LLVM_EXTERNAL_VISIBILITY __attribute__((visibility("default")))
|
||||
|
|
|
@ -22,7 +22,7 @@ add_llvm_component_library(LLVMTarget
|
|||
# When building shared objects for each target there are some internal APIs
|
||||
# that are used across shared objects which we can't hide.
|
||||
if (NOT BUILD_SHARED_LIBS AND NOT APPLE AND
|
||||
NOT MINGW AND
|
||||
(NOT (WIN32 OR CYGWIN) OR (MINGW AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")) AND
|
||||
NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX") AND
|
||||
NOT DEFINED CMAKE_CXX_VISIBILITY_PRESET)
|
||||
# Set default visibility to hidden, so we don't export all the Target classes
|
||||
|
|
Loading…
Reference in New Issue