feat: use CMake to control symbol visibility (#574)

In contrast to #572 this solution does not need any extra headers. But it is also limited to GCC-like compilers.

Fix By: Gregor Jasny (@gjasny)
pull/582/head
Gregor Jasny 1 year ago committed by GitHub
parent c3e050ac72
commit dcf03e6cdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      CMakeLists.txt
  2. 7
      src/lib/CMakeLists.txt
  3. 4
      src/lib/ares_config.h.cmake

@ -42,6 +42,7 @@ OPTION (CARES_STATIC_PIC "Build the static library as PIC (position independent)
OPTION (CARES_BUILD_TESTS "Build and run tests" OFF) OPTION (CARES_BUILD_TESTS "Build and run tests" OFF)
OPTION (CARES_BUILD_CONTAINER_TESTS "Build and run container tests (implies CARES_BUILD_TESTS, Linux only)" OFF) OPTION (CARES_BUILD_CONTAINER_TESTS "Build and run container tests (implies CARES_BUILD_TESTS, Linux only)" OFF)
OPTION (CARES_BUILD_TOOLS "Build tools" ON) OPTION (CARES_BUILD_TOOLS "Build tools" ON)
OPTION (CARES_SYMBOL_HIDING "Hide private symbols in shared libraries" OFF)
SET (CARES_RANDOM_FILE "/dev/urandom" CACHE STRING "Suitable File / Device Path for entropy, such as /dev/urandom") SET (CARES_RANDOM_FILE "/dev/urandom" CACHE STRING "Suitable File / Device Path for entropy, such as /dev/urandom")
@ -74,6 +75,21 @@ IF (MSVC)
ENDIF () ENDIF ()
ENDIF () ENDIF ()
IF (CARES_SYMBOL_HIDING)
IF (CMAKE_VERSION VERSION_LESS 3.12)
MESSAGE (FATAL_ERROR "Hiding symbols requires CMake 3.12")
ENDIF ()
CMAKE_POLICY (SET CMP0063 NEW)
SET (CARES_SYMBOL_SCOPE_EXTERN [=[__attribute__ ((visibility("default")))]=])
CHECK_C_SOURCE_COMPILES ("
${CARES_SYMBOL_SCOPE_EXTERN} int somefunc() { return 0; }
int main() { return somefunc(); }
" HAVE_VISIBILITY_ATTRIBUTE)
IF (NOT HAVE_VISIBILITY_ATTRIBUTE)
MESSAGE (FATAL_ERROR "C compiler does not accept visibility attribute")
ENDIF ()
ENDIF ()
# Keep build organized. # Keep build organized.
SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}") SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}") SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")

@ -37,6 +37,13 @@ IF (CARES_SHARED)
C_STANDARD 90 C_STANDARD 90
) )
IF (CARES_SYMBOL_HIDING)
SET_TARGET_PROPERTIES (${PROJECT_NAME} PROPERTIES
C_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN YES
)
ENDIF ()
TARGET_INCLUDE_DIRECTORIES (${PROJECT_NAME} TARGET_INCLUDE_DIRECTORIES (${PROJECT_NAME}
PUBLIC "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>" PUBLIC "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>"
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>" "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>"

@ -17,10 +17,10 @@
#undef CARES_EXPOSE_STATICS #undef CARES_EXPOSE_STATICS
/* Defined for build with symbol hiding. */ /* Defined for build with symbol hiding. */
#undef CARES_SYMBOL_HIDING #cmakedefine CARES_SYMBOL_HIDING
/* Definition to make a library symbol externally visible. */ /* Definition to make a library symbol externally visible. */
#undef CARES_SYMBOL_SCOPE_EXTERN #define CARES_SYMBOL_SCOPE_EXTERN @CARES_SYMBOL_SCOPE_EXTERN@
/* Use resolver library to configure cares */ /* Use resolver library to configure cares */
#cmakedefine CARES_USE_LIBRESOLV #cmakedefine CARES_USE_LIBRESOLV

Loading…
Cancel
Save