aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2024-02-19 13:38:42 +0200
committerLasse Collin <lasse.collin@tukaani.org>2024-02-19 16:28:49 +0200
commitb0d1422b6037bfea6f6723683bd82a8e6d77026c (patch)
tree1cc7c171ffbae0258f09627cfe9f4cfe43f77549
parentCMake: Revise the component splitting. (diff)
downloadxz-b0d1422b6037bfea6f6723683bd82a8e6d77026c.tar.xz
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.
-rw-r--r--CMakeLists.txt26
1 files changed, 22 insertions, 4 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ac89b2e6..3a3ec41d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1144,10 +1144,28 @@ if(ALLOW_ARM64_CRC32)
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 should always be defined to 0 or 1. It tells liblzma
+# if __attribute__((__visibility__("default")))
+# and __attribute__((__visibility__("hidden"))) are supported.
+# Those are useful only when the compiler supports -fvisibility=hidden
+# or such option so HAVE_VISIBILITY should be 1 only when both option and
+# the attribute support are present. HAVE_VISIBILITY is ignored on Windows
+# and Cygwin by the liblzma C code; __declspec(dllexport) is used instead.
+#
+# 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_<LANG>_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.) This way we don't
+# set HAVE_VISIBILITY to 1 when visibility isn't actually supported.
+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()