diff --git a/CMakeLists.txt b/CMakeLists.txt index 11bff6cd..6c453893 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,13 +114,20 @@ endif() option(BUILD_EXAMPLES "Build libssh2 examples" 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) endif() set(LIB_STATIC "libssh2_static") 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 option(HIDE_SYMBOLS "Set to ON to hide all libssh2 symbols that are not officially external" ON) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index e580cf61..7b820027 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -47,7 +47,7 @@ foreach(example ${EXAMPLES}) list(APPEND EXAMPLE_TARGETS ${example}) # to find generated header 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() add_target_to_copy_dependencies( diff --git a/example/ssh2.c b/example/ssh2.c index 2fcee0be..a3b9ecec 100644 --- a/example/ssh2.c +++ b/example/ssh2.c @@ -32,10 +32,6 @@ #include #include -#if defined(_MSC_VER) && _MSC_VER < 1900 -#define snprintf _snprintf -#endif - static const char *pubkey = ".ssh/id_rsa.pub"; static const char *privkey = ".ssh/id_rsa"; static const char *username = "username"; diff --git a/example/subsystem_netconf.c b/example/subsystem_netconf.c index fa96b13b..b40aa8e0 100644 --- a/example/subsystem_netconf.c +++ b/example/subsystem_netconf.c @@ -21,10 +21,6 @@ #define INADDR_NONE (in_addr_t)~0 #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 *privkey = "/home/username/.ssh/id_rsa"; static const char *username = "username"; diff --git a/src/libssh2_setup.h b/src/libssh2_setup.h index b3b4ab50..cb3cf0cd 100644 --- a/src/libssh2_setup.h +++ b/src/libssh2_setup.h @@ -77,6 +77,13 @@ # endif # ifndef _WINSOCK_DEPRECATED_NO_WARNINGS # 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 # if _MSC_VER < 1500 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 793f0800..08654597 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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_compile_definitions(runner PRIVATE FIXTURE_WORKDIR="${CMAKE_CURRENT_SOURCE_DIR}") -# test building against shared libssh2 lib -if(BUILD_SHARED_LIBS) - foreach(test test_ssh2) - add_executable(${test}_shared ${test}.c) - target_include_directories(${test}_shared PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../src" ../src) - target_link_libraries(${test}_shared ${LIB_SHARED} ${LIBRARIES}) - endforeach() -endif() +# These programs use internal libssh2 functions so they need to be statically +# linked against libssh2 +set(TESTS_WITH_LIB_STATIC + test_auth_keyboard_info_request + test_hostkey + test_simple +) foreach(test ${DOCKER_TESTS} ${STANDALONE_TESTS} ${SSHD_TESTS}) # 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) target_compile_options(${test} BEFORE PRIVATE ${GCOV_OPTIONS}) 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}) + else() + target_link_libraries(${test} runner ${LIB_SELECTED} ${LIBRARIES}) endif() list(APPEND TEST_TARGETS ${test})