1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-07-28 01:41:49 +03:00

cmake: use shared libs again in example and tests

Re-sync with autotools and v1.10.0 behavior.

This improves build times. It also allows to stop building our special
shared test target to test shared builds.

Follow-up to 4e2580628d

Cherry-picked from #1017
Closes #1022
This commit is contained in:
Viktor Szakats
2023-05-02 22:21:01 +00:00
parent bc120a343b
commit 612ca85aaa
6 changed files with 26 additions and 19 deletions

View File

@ -114,13 +114,20 @@ endif()
option(BUILD_EXAMPLES "Build libssh2 examples" ON) option(BUILD_EXAMPLES "Build libssh2 examples" ON)
option(BUILD_TESTING "Build libssh2 test suite" ON) option(BUILD_TESTING "Build libssh2 test suite" ON)
if(NOT BUILD_STATIC_LIBS AND (NOT BUILD_SHARED_LIBS OR BUILD_EXAMPLES OR BUILD_TESTING)) if(NOT BUILD_STATIC_LIBS AND (NOT BUILD_SHARED_LIBS OR BUILD_TESTING))
set(BUILD_STATIC_LIBS ON) set(BUILD_STATIC_LIBS ON)
endif() endif()
set(LIB_STATIC "libssh2_static") set(LIB_STATIC "libssh2_static")
set(LIB_SHARED "libssh2_shared") set(LIB_SHARED "libssh2_shared")
# lib flavour selected for example and test programs.
if(BUILD_SHARED_LIBS)
set(LIB_SELECTED ${LIB_SHARED})
else()
set(LIB_SELECTED ${LIB_STATIC})
endif()
# Symbol hiding # Symbol hiding
option(HIDE_SYMBOLS "Set to ON to hide all libssh2 symbols that are not officially external" ON) option(HIDE_SYMBOLS "Set to ON to hide all libssh2 symbols that are not officially external" ON)

View File

@ -47,7 +47,7 @@ foreach(example ${EXAMPLES})
list(APPEND EXAMPLE_TARGETS ${example}) list(APPEND EXAMPLE_TARGETS ${example})
# to find generated header # to find generated header
target_include_directories(${example} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../src ../src) target_include_directories(${example} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../src ../src)
target_link_libraries(${example} ${LIB_STATIC} ${LIBRARIES}) target_link_libraries(${example} ${LIB_SELECTED} ${LIBRARIES})
endforeach() endforeach()
add_target_to_copy_dependencies( add_target_to_copy_dependencies(

View File

@ -32,10 +32,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf _snprintf
#endif
static const char *pubkey = ".ssh/id_rsa.pub"; static const char *pubkey = ".ssh/id_rsa.pub";
static const char *privkey = ".ssh/id_rsa"; static const char *privkey = ".ssh/id_rsa";
static const char *username = "username"; static const char *username = "username";

View File

@ -21,10 +21,6 @@
#define INADDR_NONE (in_addr_t)~0 #define INADDR_NONE (in_addr_t)~0
#endif #endif
#if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf _snprintf
#endif
static const char *pubkey = "/home/username/.ssh/id_rsa.pub"; static const char *pubkey = "/home/username/.ssh/id_rsa.pub";
static const char *privkey = "/home/username/.ssh/id_rsa"; static const char *privkey = "/home/username/.ssh/id_rsa";
static const char *username = "username"; static const char *username = "username";

View File

@ -77,6 +77,13 @@
# endif # endif
# ifndef _WINSOCK_DEPRECATED_NO_WARNINGS # ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
# define _WINSOCK_DEPRECATED_NO_WARNINGS /* for inet_addr() */ # define _WINSOCK_DEPRECATED_NO_WARNINGS /* for inet_addr() */
# endif
/* we cannot access our internal snprintf() implementation in examples and
tests when linking to a shared libssh2. */
# if _MSC_VER < 1900
# undef HAVE_SNPRINTF
# define HAVE_SNPRINTF
# define snprintf _snprintf
# endif # endif
# endif # endif
# if _MSC_VER < 1500 # if _MSC_VER < 1500

View File

@ -65,14 +65,13 @@ target_compile_definitions(runner PRIVATE "${CRYPTO_BACKEND_DEFINE}")
target_include_directories(runner PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../src" ../src ../include "${CRYPTO_BACKEND_INCLUDE_DIR}") target_include_directories(runner PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../src" ../src ../include "${CRYPTO_BACKEND_INCLUDE_DIR}")
target_compile_definitions(runner PRIVATE FIXTURE_WORKDIR="${CMAKE_CURRENT_SOURCE_DIR}") target_compile_definitions(runner PRIVATE FIXTURE_WORKDIR="${CMAKE_CURRENT_SOURCE_DIR}")
# test building against shared libssh2 lib # These programs use internal libssh2 functions so they need to be statically
if(BUILD_SHARED_LIBS) # linked against libssh2
foreach(test test_ssh2) set(TESTS_WITH_LIB_STATIC
add_executable(${test}_shared ${test}.c) test_auth_keyboard_info_request
target_include_directories(${test}_shared PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../src" ../src) test_hostkey
target_link_libraries(${test}_shared ${LIB_SHARED} ${LIBRARIES}) test_simple
endforeach() )
endif()
foreach(test ${DOCKER_TESTS} ${STANDALONE_TESTS} ${SSHD_TESTS}) foreach(test ${DOCKER_TESTS} ${STANDALONE_TESTS} ${SSHD_TESTS})
# We support the same target as both Docker and SSHD test. Build those just once. # We support the same target as both Docker and SSHD test. Build those just once.
@ -85,8 +84,10 @@ foreach(test ${DOCKER_TESTS} ${STANDALONE_TESTS} ${SSHD_TESTS})
if(GCOV_PATH AND test STREQUAL test_auth_keyboard_info_request) if(GCOV_PATH AND test STREQUAL test_auth_keyboard_info_request)
target_compile_options(${test} BEFORE PRIVATE ${GCOV_OPTIONS}) target_compile_options(${test} BEFORE PRIVATE ${GCOV_OPTIONS})
target_link_libraries(${test} runner ${LIB_STATIC} ${LIBRARIES} gcov) target_link_libraries(${test} runner ${LIB_STATIC} ${LIBRARIES} gcov)
else() elseif(";${TESTS_WITH_LIB_STATIC};" MATCHES ";${test};")
target_link_libraries(${test} runner ${LIB_STATIC} ${LIBRARIES}) target_link_libraries(${test} runner ${LIB_STATIC} ${LIBRARIES})
else()
target_link_libraries(${test} runner ${LIB_SELECTED} ${LIBRARIES})
endif() endif()
list(APPEND TEST_TARGETS ${test}) list(APPEND TEST_TARGETS ${test})