From 803f19f004eb6a5b525c48fff6f46a493d25775c Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Tue, 18 Apr 2023 08:20:05 +0000 Subject: [PATCH] cmake: dedupe setting `-DHAVE_CONFIG_H` Move `libssh2_config.h` generation and setting `-DHAVE_CONFIG_H` to the root `CMakeFile.txt`. Also move symbol hiding setup there. It needs to be done before generating the config file for `LIBSSH2_API` value to be set in it. After this change the `HIDE_SYMBOLS` setting is accepted without an annoying CMake warning when not actually building a shared libssh2 lib. Closes #981 --- CMakeLists.txt | 25 +++++++++++++++++++++++++ example/CMakeLists.txt | 2 -- src/CMakeLists.txt | 29 ++--------------------------- tests/CMakeLists.txt | 2 -- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a28c9be..47870cd5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,6 +117,24 @@ endif() set(LIB_STATIC "libssh2_static") set(LIB_SHARED "libssh2_shared") # Must match libssh2_shared_EXPORTS macro in include/libssh2.h +# Symbol hiding + +option(HIDE_SYMBOLS "Set to ON to hide all libssh2 symbols that aren't officially external" ON) +mark_as_advanced(HIDE_SYMBOLS) +if(HIDE_SYMBOLS) + set(LIB_SHARED_DEFINITIONS LIBSSH2_EXPORTS) + if(WIN32) + elseif((CMAKE_C_COMPILER_ID MATCHES "Clang") OR + (CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.0) OR + (CMAKE_C_COMPILER_ID MATCHES "Intel" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 9.1)) + set(LIB_SHARED_C_FLAGS -fvisibility=hidden) + set(LIBSSH2_API "__attribute__ ((__visibility__ (\"default\")))") + elseif(CMAKE_C_COMPILER_ID MATCHES "SunPro" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.0) + set(LIB_SHARED_C_FLAGS -xldscope=hidden) + set(LIBSSH2_API "__global") + endif() +endif() + # Auto-detection ## Platform checks @@ -195,6 +213,13 @@ if(NOT WIN32) cmake_pop_check_state() endif() +# Config file + +add_definitions(-DHAVE_CONFIG_H) + +configure_file(src/libssh2_config_cmake.h.in + ${CMAKE_CURRENT_BINARY_DIR}/src/libssh2_config.h) + ## Cryptography backend choice set(CRYPTO_BACKEND diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 26f83429..e580cf61 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -37,8 +37,6 @@ include(CopyRuntimeDependencies) list(APPEND LIBRARIES ${SOCKET_LIBRARIES}) -add_definitions(-DHAVE_CONFIG_H) - transform_makefile_inc("Makefile.am" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.am.cmake") # Get 'noinst_PROGRAMS' variable include(${CMAKE_CURRENT_BINARY_DIR}/Makefile.am.cmake) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cd284e5a..4f0ad35f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -126,7 +126,8 @@ if(BUILD_SHARED_LIBS) if(WIN32) set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES libssh2.rc) endif() - target_compile_definitions(${LIB_SHARED} PRIVATE ${PRIVATE_COMPILE_DEFINITIONS} ${libssh2_DEFINITIONS}) + target_compile_definitions(${LIB_SHARED} PRIVATE ${PRIVATE_COMPILE_DEFINITIONS} ${libssh2_DEFINITIONS} ${LIB_SHARED_DEFINITIONS}) + target_compile_options(${LIB_SHARED} PRIVATE ${LIB_SHARED_C_FLAGS}) target_link_libraries(${LIB_SHARED} PRIVATE ${LIBRARIES}) set_target_properties(${LIB_SHARED} PROPERTIES PREFIX "" IMPORT_PREFIX "" OUTPUT_NAME "libssh2") if(WIN32 AND BUILD_STATIC_LIBS AND NOT STATIC_LIB_SUFFIX AND @@ -142,34 +143,8 @@ if(BUILD_SHARED_LIBS) PUBLIC $ $/${CMAKE_INSTALL_INCLUDEDIR}>) - - # Symbol hiding - - option(HIDE_SYMBOLS "Set to ON to hide all libssh2 symbols that aren't officially external." ON) - mark_as_advanced(HIDE_SYMBOLS) - - if(HIDE_SYMBOLS) - target_compile_definitions(${LIB_SHARED} PRIVATE LIBSSH2_EXPORTS) - if(WIN32) - elseif((CMAKE_C_COMPILER_ID MATCHES "Clang") OR - (CMAKE_COMPILER_IS_GNUCC AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 4.0) OR - (CMAKE_C_COMPILER_ID MATCHES "Intel" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 9.1)) - target_compile_options(${LIB_SHARED} PRIVATE -fvisibility=hidden) - set(LIBSSH2_API "__attribute__ ((__visibility__ (\"default\")))") - elseif(CMAKE_C_COMPILER_ID MATCHES "SunPro" AND NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.0) - target_compile_options(${LIB_SHARED} PRIVATE -xldscope=hidden) - set(LIBSSH2_API "__global") - endif() - endif() endif() -# Config file - -add_definitions(-DHAVE_CONFIG_H) - -configure_file(libssh2_config_cmake.h.in - ${CMAKE_CURRENT_BINARY_DIR}/libssh2_config.h) - ## Installation install(FILES diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 61dc2e99..2d9e204c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -37,8 +37,6 @@ include(CopyRuntimeDependencies) list(APPEND LIBRARIES ${SOCKET_LIBRARIES}) -add_definitions(-DHAVE_CONFIG_H) - if(CMAKE_COMPILER_IS_GNUCC) find_program(GCOV_PATH gcov) if(GCOV_PATH)