From 0f13fc0e2ceda67c8648b05df18eddfc8bbd6103 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <sezeroz@gmail.com>
Date: Wed, 4 Jun 2025 08:21:50 +0300
Subject: [PATCH] build: revise visibility attributes usage in autotools and cmake

* cmake: set C_VISIBILITY_PRESET to hidden for non-windows only.
  windows handles exports with DLL_EXPORT defined, and old mingw
  toolchains emit a lot of warnings about visibility attributes
  not being supported in this configuration.
* autotools: revise visibility attributes checks by compiling a
  better test program with `-Werror` in CFLAGS, so that warnings
  will correctly indicate unsupported configurations.
---
 CMakeLists.txt            |    9 +++++----
 builds/unix/configure.raw |    9 +++++++--
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b14e813..1ae550a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -450,10 +450,6 @@ add_library(freetype
   ${BASE_SRCS}
 )
 
-set_target_properties(
-  freetype PROPERTIES
-    C_VISIBILITY_PRESET hidden)
-
 target_compile_definitions(
   freetype PRIVATE FT2_BUILD_LIBRARY)
 
@@ -464,6 +460,11 @@ if (WIN32)
     target_compile_definitions(
       freetype PRIVATE DLL_EXPORT)
   endif ()
+else ()
+  # windows handles exports with DLL_EXPORT defined above.
+  set_target_properties(
+    freetype PROPERTIES
+      C_VISIBILITY_PRESET hidden)
 endif ()
 
 if (BUILD_SHARED_LIBS)
diff --git a/builds/unix/configure.raw b/builds/unix/configure.raw
index f18c8a0..03d8206 100644
--- a/builds/unix/configure.raw
+++ b/builds/unix/configure.raw
@@ -239,9 +239,14 @@ AC_SUBST([XX_ANSIFLAGS])
 found_visibility_flag=no
 AC_MSG_CHECKING([for -fvisibility=hidden compiler flag])
 orig_CFLAGS="${CFLAGS}"
-CFLAGS="${CFLAGS} -fvisibility=hidden"
-AC_LINK_IFELSE([AC_LANG_PROGRAM([], [])],
+CFLAGS="${CFLAGS} -fvisibility=hidden -Werror"
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
+__attribute__((visibility("default"))) int foo(void);
+__attribute__((visibility("hidden")))  int bar(void);
+int foo(void) { return 0; }
+int bar(void) { return 1; }], [])],
                [found_visibility_flag=yes
+                CFLAGS="${orig_CFLAGS} -fvisibility=hidden"
                 AC_MSG_RESULT(yes)],
                [CFLAGS="${orig_CFLAGS}"
                 AC_MSG_RESULT(no)])
-- 
1.7.1

