From a9a5174852581b6e9729627b811072cb2fd47cf2 Mon Sep 17 00:00:00 2001 From: Lasse Collin Date: Sun, 15 Oct 2023 02:20:47 +0300 Subject: [PATCH] CMake: Don't assume that -fvisibility=hidden is supported outside Windows. The original code was good enough for supporting GNU/Linux and a few others but it wasn't very portable. CMake doesn't support Solaris Studio's -xldscope=hidden. If it ever does, things should still work with this commit as Solaris Studio supports not only its own __global but also the GNU C __attribute__((visibility("default"))). Support for the attribute was added in 2007 to Sun Studio 12 compiler version 5.9. --- CMakeLists.txt | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d3dabec..8f6135bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -856,10 +856,24 @@ calculation if supported by the system" ON) endif() endif() -# Support -fvisiblity=hidden when building shared liblzma. -# These lines do nothing on Windows (even under Cygwin). -# HAVE_VISIBILITY should always be defined to 0 or 1. -if(BUILD_SHARED_LIBS) +# Symbol visibility support: +# +# The C_VISIBILITY_PRESET property takes care of adding the compiler +# option -fvisibility=hidden (or equivalent) if and only if it is supported. +# +# HAVE_VISIBILITY indicates if __attribute__((__visibility__("default"))) +# is supported. HAVE_VISIBILITY is ignored on Windows and Cygwin in +# the C code so we don't need to handle that here. HAVE_VISIBILITY +# should always be defined to 0 or 1. +# +# CMake's GenerateExportHeader module is too fancy since liblzma already +# has the necessary macros. Instead, check CMake's internal variable +# CMAKE_C_COMPILE_OPTIONS_VISIBILITY (it's the C-specific variant of +# CMAKE__COMPILE_OPTIONS_VISIBILITY) which contains the compiler +# command line option for visibility support. It's empty or unset when +# visibility isn't supported. It was added to CMake 2.8.12 in the commit +# 0e9f4bc00c6b26f254e74063e4026ac33b786513 in 2013. +if(BUILD_SHARED_LIBS AND CMAKE_C_COMPILE_OPTIONS_VISIBILITY) set_target_properties(liblzma PROPERTIES C_VISIBILITY_PRESET hidden) target_compile_definitions(liblzma PRIVATE HAVE_VISIBILITY=1) else()