xref: /curl/CMake/CurlSymbolHiding.cmake (revision b3e1fe6d)
1#***************************************************************************
2#                                  _   _ ____  _
3#  Project                     ___| | | |  _ \| |
4#                             / __| | | | |_) | |
5#                            | (__| |_| |  _ <| |___
6#                             \___|\___/|_| \_\_____|
7#
8# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9#
10# This software is licensed as described in the file COPYING, which
11# you should have received as part of this distribution. The terms
12# are also available at https://curl.se/docs/copyright.html.
13#
14# You may opt to use, copy, modify, merge, publish, distribute and/or sell
15# copies of the Software, and permit persons to whom the Software is
16# furnished to do so, under the terms of the COPYING file.
17#
18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19# KIND, either express or implied.
20#
21# SPDX-License-Identifier: curl
22#
23###########################################################################
24include(CheckCSourceCompiles)
25
26option(CURL_HIDDEN_SYMBOLS "Hide libcurl internal symbols (=hide all symbols that are not officially external)" ON)
27mark_as_advanced(CURL_HIDDEN_SYMBOLS)
28
29if(WIN32 AND (ENABLE_DEBUG OR ENABLE_CURLDEBUG))
30  # We need to export internal debug functions,
31  # e.g. curl_easy_perform_ev() or curl_dbg_*(),
32  # so disable symbol hiding for debug builds and for memory tracking.
33  set(CURL_HIDDEN_SYMBOLS OFF)
34endif()
35
36if(CURL_HIDDEN_SYMBOLS)
37  set(_supports_symbol_hiding FALSE)
38
39  if(CMAKE_C_COMPILER_ID MATCHES "Clang" AND NOT MSVC)
40    set(_supports_symbol_hiding TRUE)
41    set(_symbol_extern "__attribute__ ((__visibility__ (\"default\")))")
42    set(_cflag_symbols_hide "-fvisibility=hidden")
43  elseif(CMAKE_COMPILER_IS_GNUCC)
44    if(NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 3.4)
45      # Note: This is considered buggy prior to 4.0 but the autotools do not care, so let us ignore that fact
46      set(_supports_symbol_hiding TRUE)
47      set(_symbol_extern "__attribute__ ((__visibility__ (\"default\")))")
48      set(_cflag_symbols_hide "-fvisibility=hidden")
49    endif()
50  elseif(CMAKE_C_COMPILER_ID MATCHES "SunPro" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.0)
51    set(_supports_symbol_hiding TRUE)
52    set(_symbol_extern "__global")
53    set(_cflag_symbols_hide "-xldscope=hidden")
54  elseif(CMAKE_C_COMPILER_ID MATCHES "Intel" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 9.0)
55    # Note: This should probably just check for version 9.1.045 but I am not 100% sure
56    #       so let us do it the same way autotools do.
57    set(_supports_symbol_hiding TRUE)
58    set(_symbol_extern "__attribute__ ((__visibility__ (\"default\")))")
59    set(_cflag_symbols_hide "-fvisibility=hidden")
60    check_c_source_compiles("#include <stdio.h>
61      int main(void) { printf(\"icc fvisibility bug test\"); return 0; }" _no_bug)
62    if(NOT _no_bug)
63      set(_supports_symbol_hiding FALSE)
64      set(_symbol_extern "")
65      set(_cflag_symbols_hide "")
66    endif()
67  elseif(MSVC)
68    set(_supports_symbol_hiding TRUE)
69  endif()
70
71  set(CURL_HIDES_PRIVATE_SYMBOLS ${_supports_symbol_hiding})
72else()
73  if(MSVC)
74    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
75  endif()
76  set(CURL_HIDES_PRIVATE_SYMBOLS FALSE)
77endif()
78
79set(CURL_CFLAG_SYMBOLS_HIDE ${_cflag_symbols_hide})
80set(CURL_EXTERN_SYMBOL ${_symbol_extern})
81