1
0
mirror of https://github.com/libssh2/libssh2.git synced 2025-08-05 20:55:47 +03:00

cmake: tidy up syntax, minor improvements

- make internal variables underscore-lowercase.
- unfold lines.
- fold lines setting header directories.
- fix indent.
- drop interim variable `EXAMPLES`.
- initialize some variables before populating them.
- clear a variable after use.
- add `libssh2_dumpvars()` function for debugging.
- allow to override default `CMAKE_UNITY_BUILD_BATCH_SIZE`.
- bump up default `CMAKE_UNITY_BUILD_BATCH_SIZE` to 0 (was 32).
- tidy up option descriptions.

Closes #1446
This commit is contained in:
Viktor Szakats
2024-08-19 20:33:58 +02:00
parent 570de0f23f
commit 9d9ee7807d
6 changed files with 142 additions and 127 deletions

View File

@@ -52,31 +52,33 @@ include(CheckNonblockingSocketSupport)
project(libssh2 C) project(libssh2 C)
set(CMAKE_UNITY_BUILD_BATCH_SIZE 32) function(libssh2_dumpvars) # Dump all defined variables with their values
message("::group::CMake Variable Dump")
get_cmake_property(_vars VARIABLES)
foreach(_var ${_vars})
message("${_var} = ${${_var}}")
endforeach()
message("::endgroup::")
endfunction()
option(BUILD_STATIC_LIBS "Build Static Libraries" ON) if(NOT DEFINED CMAKE_UNITY_BUILD_BATCH_SIZE)
add_feature_info("Static library" BUILD_STATIC_LIBS set(CMAKE_UNITY_BUILD_BATCH_SIZE 0)
"creating libssh2 static library") endif()
option(BUILD_SHARED_LIBS "Build Shared Libraries" ON) option(BUILD_STATIC_LIBS "Build static libraries" ON)
add_feature_info("Shared library" BUILD_SHARED_LIBS add_feature_info("Static library" BUILD_STATIC_LIBS "creating libssh2 static library")
"creating libssh2 shared library (.so/.dll)")
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
add_feature_info("Shared library" BUILD_SHARED_LIBS "creating libssh2 shared library (.so/.dll)")
# Parse version # Parse version
file(READ "${PROJECT_SOURCE_DIR}/include/libssh2.h" _HEADER_CONTENTS) file(READ "${PROJECT_SOURCE_DIR}/include/libssh2.h" _header_contents)
string( string(REGEX REPLACE ".*#define LIBSSH2_VERSION[ \t]+\"([^\"]+)\".*" "\\1" LIBSSH2_VERSION "${_header_contents}")
REGEX REPLACE ".*#define LIBSSH2_VERSION[ \t]+\"([^\"]+)\".*" "\\1" string(REGEX REPLACE ".*#define LIBSSH2_VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" LIBSSH2_VERSION_MAJOR "${_header_contents}")
LIBSSH2_VERSION "${_HEADER_CONTENTS}") string(REGEX REPLACE ".*#define LIBSSH2_VERSION_MINOR[ \t]+([0-9]+).*" "\\1" LIBSSH2_VERSION_MINOR "${_header_contents}")
string( string(REGEX REPLACE ".*#define LIBSSH2_VERSION_PATCH[ \t]+([0-9]+).*" "\\1" LIBSSH2_VERSION_PATCH "${_header_contents}")
REGEX REPLACE ".*#define LIBSSH2_VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" unset(_header_contents)
LIBSSH2_VERSION_MAJOR "${_HEADER_CONTENTS}")
string(
REGEX REPLACE ".*#define LIBSSH2_VERSION_MINOR[ \t]+([0-9]+).*" "\\1"
LIBSSH2_VERSION_MINOR "${_HEADER_CONTENTS}")
string(
REGEX REPLACE ".*#define LIBSSH2_VERSION_PATCH[ \t]+([0-9]+).*" "\\1"
LIBSSH2_VERSION_PATCH "${_HEADER_CONTENTS}")
if(NOT LIBSSH2_VERSION OR if(NOT LIBSSH2_VERSION OR
NOT LIBSSH2_VERSION_MAJOR MATCHES "^[0-9]+$" OR NOT LIBSSH2_VERSION_MAJOR MATCHES "^[0-9]+$" OR
@@ -128,7 +130,7 @@ 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 "Hide all libssh2 symbols that are not officially external" ON)
mark_as_advanced(HIDE_SYMBOLS) mark_as_advanced(HIDE_SYMBOLS)
if(HIDE_SYMBOLS) if(HIDE_SYMBOLS)
set(LIB_SHARED_DEFINITIONS "LIBSSH2_EXPORTS") set(LIB_SHARED_DEFINITIONS "LIBSSH2_EXPORTS")
@@ -152,7 +154,7 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
else() else()
set(DEBUG_LOGGING_DEFAULT OFF) set(DEBUG_LOGGING_DEFAULT OFF)
endif() endif()
option(ENABLE_DEBUG_LOGGING "log execution with debug trace" ${DEBUG_LOGGING_DEFAULT}) option(ENABLE_DEBUG_LOGGING "Log execution with debug trace" ${DEBUG_LOGGING_DEFAULT})
add_feature_info(Logging ENABLE_DEBUG_LOGGING "Logging of execution with debug trace") add_feature_info(Logging ENABLE_DEBUG_LOGGING "Logging of execution with debug trace")
if(ENABLE_DEBUG_LOGGING) if(ENABLE_DEBUG_LOGGING)
# Must be visible to the library and tests using internals # Must be visible to the library and tests using internals
@@ -282,20 +284,19 @@ configure_file("src/libssh2_config_cmake.h.in"
## Cryptography backend choice ## Cryptography backend choice
set(CRYPTO_BACKEND "" CACHE STRING set(CRYPTO_BACKEND "" CACHE
"The backend to use for cryptography: OpenSSL, wolfSSL, Libgcrypt, STRING "The backend to use for cryptography: OpenSSL, wolfSSL, Libgcrypt, WinCNG, mbedTLS, or empty to try any available")
WinCNG, mbedTLS, or empty to try any available")
# If the crypto backend was given, rather than searching for the first # If the crypto backend was given, rather than searching for the first
# we are able to find, the find_package commands must abort configuration # we are able to find, the find_package commands must abort configuration
# and report to the user. # and report to the user.
if(CRYPTO_BACKEND) if(CRYPTO_BACKEND)
set(SPECIFIC_CRYPTO_REQUIREMENT "REQUIRED") set(_specific_crypto_requirement "REQUIRED")
endif() endif()
if(CRYPTO_BACKEND STREQUAL "OpenSSL" OR NOT CRYPTO_BACKEND) if(CRYPTO_BACKEND STREQUAL "OpenSSL" OR NOT CRYPTO_BACKEND)
find_package(OpenSSL ${SPECIFIC_CRYPTO_REQUIREMENT}) find_package(OpenSSL ${_specific_crypto_requirement})
if(OPENSSL_FOUND) if(OPENSSL_FOUND)
set(CRYPTO_BACKEND "OpenSSL") set(CRYPTO_BACKEND "OpenSSL")
@@ -340,7 +341,7 @@ endif()
if(CRYPTO_BACKEND STREQUAL "wolfSSL" OR NOT CRYPTO_BACKEND) if(CRYPTO_BACKEND STREQUAL "wolfSSL" OR NOT CRYPTO_BACKEND)
find_package(WolfSSL ${SPECIFIC_CRYPTO_REQUIREMENT}) find_package(WolfSSL ${_specific_crypto_requirement})
if(WOLFSSL_FOUND) if(WOLFSSL_FOUND)
set(CRYPTO_BACKEND "wolfSSL") set(CRYPTO_BACKEND "wolfSSL")
@@ -367,7 +368,7 @@ endif()
if(CRYPTO_BACKEND STREQUAL "Libgcrypt" OR NOT CRYPTO_BACKEND) if(CRYPTO_BACKEND STREQUAL "Libgcrypt" OR NOT CRYPTO_BACKEND)
find_package(Libgcrypt ${SPECIFIC_CRYPTO_REQUIREMENT}) find_package(Libgcrypt ${_specific_crypto_requirement})
if(LIBGCRYPT_FOUND) if(LIBGCRYPT_FOUND)
set(CRYPTO_BACKEND "Libgcrypt") set(CRYPTO_BACKEND "Libgcrypt")
@@ -381,7 +382,7 @@ endif()
if(CRYPTO_BACKEND STREQUAL "mbedTLS" OR NOT CRYPTO_BACKEND) if(CRYPTO_BACKEND STREQUAL "mbedTLS" OR NOT CRYPTO_BACKEND)
find_package(MbedTLS ${SPECIFIC_CRYPTO_REQUIREMENT}) find_package(MbedTLS ${_specific_crypto_requirement})
if(MBEDTLS_FOUND) if(MBEDTLS_FOUND)
set(CRYPTO_BACKEND "mbedTLS") set(CRYPTO_BACKEND "mbedTLS")
@@ -404,7 +405,7 @@ if(CRYPTO_BACKEND STREQUAL "WinCNG" OR NOT CRYPTO_BACKEND)
list(APPEND LIBRARIES "crypt32" "bcrypt") list(APPEND LIBRARIES "crypt32" "bcrypt")
list(APPEND LIBSSH2_PC_LIBS_PRIVATE "-lcrypt32" "-lbcrypt") list(APPEND LIBSSH2_PC_LIBS_PRIVATE "-lcrypt32" "-lbcrypt")
option(ENABLE_ECDSA_WINCNG "WinCNG ECDSA support (requires Windows 10 or later)" OFF) option(ENABLE_ECDSA_WINCNG "Enable WinCNG ECDSA support (requires Windows 10 or later)" OFF)
add_feature_info(WinCNG ENABLE_ECDSA_WINCNG "WinCNG ECDSA support") add_feature_info(WinCNG ENABLE_ECDSA_WINCNG "WinCNG ECDSA support")
if(ENABLE_ECDSA_WINCNG) if(ENABLE_ECDSA_WINCNG)
add_definitions("-DLIBSSH2_ECDSA_WINCNG") add_definitions("-DLIBSSH2_ECDSA_WINCNG")
@@ -414,7 +415,7 @@ if(CRYPTO_BACKEND STREQUAL "WinCNG" OR NOT CRYPTO_BACKEND)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--subsystem,windows:10") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--subsystem,windows:10")
endif() endif()
endif() endif()
elseif(SPECIFIC_CRYPTO_REQUIREMENT STREQUAL "REQUIRED") elseif(_specific_crypto_requirement STREQUAL "REQUIRED")
message(FATAL_ERROR "WinCNG not available") message(FATAL_ERROR "WinCNG not available")
endif() endif()
endif() endif()
@@ -422,14 +423,14 @@ endif()
# Global functions # Global functions
# Convert GNU Make assignments into CMake ones. # Convert GNU Make assignments into CMake ones.
function(transform_makefile_inc INPUT_FILE OUTPUT_FILE) function(transform_makefile_inc _input_file _output_file)
file(READ ${INPUT_FILE} MAKEFILE_INC_CMAKE) file(READ ${_input_file} _makefile_inc_cmake)
string(REGEX REPLACE "\\\\\n" "" MAKEFILE_INC_CMAKE ${MAKEFILE_INC_CMAKE}) string(REGEX REPLACE "\\\\\n" "" _makefile_inc_cmake ${_makefile_inc_cmake})
string(REGEX REPLACE "([A-Za-z_]+) *= *([^\n]*)" "set(\\1 \\2)" MAKEFILE_INC_CMAKE ${MAKEFILE_INC_CMAKE}) string(REGEX REPLACE "([A-Za-z_]+) *= *([^\n]*)" "set(\\1 \\2)" _makefile_inc_cmake ${_makefile_inc_cmake})
file(WRITE ${OUTPUT_FILE} ${MAKEFILE_INC_CMAKE}) file(WRITE ${_output_file} ${_makefile_inc_cmake})
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${INPUT_FILE}") set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${_input_file}")
endfunction() endfunction()
# #

View File

@@ -60,21 +60,19 @@
include(CheckFunctionExists) include(CheckFunctionExists)
include(CheckLibraryExists) include(CheckLibraryExists)
function(check_function_exists_may_need_library function variable) function(check_function_exists_may_need_library _function _variable)
check_function_exists(${function} ${variable}) check_function_exists(${_function} ${_variable})
if(NOT ${variable}) if(NOT ${_variable})
foreach(lib IN LISTS ARGN) foreach(_lib IN LISTS ARGN)
string(TOUPPER ${lib} UP_LIB) string(TOUPPER ${_lib} _up_lib)
# Use new variable to prevent cache from previous step shortcircuiting # Use new variable to prevent cache from previous step shortcircuiting
# new test # new test
check_library_exists(${lib} ${function} "" HAVE_${function}_IN_${lib}) check_library_exists(${_lib} ${_function} "" HAVE_${_function}_IN_${_lib})
if(HAVE_${function}_IN_${lib}) if(HAVE_${_function}_IN_${_lib})
set(${variable} 1 CACHE INTERNAL set(${_variable} 1 CACHE INTERNAL "Function ${_function} found in library ${_lib}")
"Function ${function} found in library ${lib}") set(NEED_LIB_${_up_lib} 1 CACHE INTERNAL "Need to link ${_lib}")
set(NEED_LIB_${UP_LIB} 1 CACHE INTERNAL
"Need to link ${lib}")
break() break()
endif() endif()
endforeach() endforeach()

View File

@@ -41,8 +41,7 @@ function(add_target_to_copy_dependencies)
set(options) set(options)
set(oneValueArgs TARGET) set(oneValueArgs TARGET)
set(multiValueArgs DEPENDENCIES BEFORE_TARGETS) set(multiValueArgs DEPENDENCIES BEFORE_TARGETS)
cmake_parse_arguments(COPY cmake_parse_arguments(COPY "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
"${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT COPY_DEPENDENCIES) if(NOT COPY_DEPENDENCIES)
return() return()
@@ -52,20 +51,18 @@ function(add_target_to_copy_dependencies)
# parallel builds trying to kick off the commands at the same time # parallel builds trying to kick off the commands at the same time
add_custom_target(${COPY_TARGET}) add_custom_target(${COPY_TARGET})
foreach(target IN LISTS COPY_BEFORE_TARGETS) foreach(_target IN LISTS COPY_BEFORE_TARGETS)
add_dependencies(${target} ${COPY_TARGET}) add_dependencies(${_target} ${COPY_TARGET})
endforeach() endforeach()
foreach(dependency IN LISTS COPY_DEPENDENCIES) foreach(_dependency IN LISTS COPY_DEPENDENCIES)
add_custom_command( add_custom_command(
TARGET ${COPY_TARGET} TARGET ${COPY_TARGET}
DEPENDS ${dependency} DEPENDS ${_dependency}
# Make directory first otherwise file is copied in place of # Make directory first otherwise file is copied in place of
# directory instead of into it # directory instead of into it
COMMAND ${CMAKE_COMMAND} COMMAND ${CMAKE_COMMAND} ARGS -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}"
ARGS -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR} COMMAND ${CMAKE_COMMAND} ARGS -E copy ${_dependency} "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}"
COMMAND ${CMAKE_COMMAND}
ARGS -E copy ${dependency} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}
VERBATIM) VERBATIM)
endforeach() endforeach()
endfunction() endfunction()

View File

@@ -43,18 +43,19 @@ list(APPEND LIBRARIES ${SOCKET_LIBRARIES})
transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake") transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
# Get 'noinst_PROGRAMS' variable # Get 'noinst_PROGRAMS' variable
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake") include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
set(EXAMPLES ${noinst_PROGRAMS})
foreach(example IN LISTS EXAMPLES) foreach(_example IN LISTS noinst_PROGRAMS)
add_executable(${example} "${example}.c") add_executable(${_example} "${_example}.c")
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
target_link_libraries(${example} ${LIB_SELECTED} ${LIBRARIES}) "${CMAKE_CURRENT_BINARY_DIR}/../src"
set_target_properties(${example} PROPERTIES UNITY_BUILD OFF) "../src")
target_link_libraries(${_example} ${LIB_SELECTED} ${LIBRARIES})
set_target_properties(${_example} PROPERTIES UNITY_BUILD OFF)
endforeach() endforeach()
add_target_to_copy_dependencies( add_target_to_copy_dependencies(
TARGET copy_example_dependencies TARGET copy_example_dependencies
DEPENDENCIES ${RUNTIME_DEPENDENCIES} DEPENDENCIES ${RUNTIME_DEPENDENCIES}
BEFORE_TARGETS ${EXAMPLE_TARGETS}) BEFORE_TARGETS ${_example_targets})

View File

@@ -36,8 +36,8 @@
# #
# SPDX-License-Identifier: BSD-3-Clause # SPDX-License-Identifier: BSD-3-Clause
set(LIBSSH2_SOVERSION 1) set(_libssh2_soversion 1)
set(LIBSSH2_LIBVERSION 1.0.1) set(_libssh2_libversion 1.0.1)
if(CRYPTO_BACKEND) if(CRYPTO_BACKEND)
list(APPEND PRIVATE_COMPILE_DEFINITIONS ${CRYPTO_BACKEND_DEFINE}) list(APPEND PRIVATE_COMPILE_DEFINITIONS ${CRYPTO_BACKEND_DEFINE})
@@ -49,9 +49,11 @@ endif()
## Options ## Options
unset(_libssh2_definitions)
option(CLEAR_MEMORY "Enable clearing of memory before being freed" ON) option(CLEAR_MEMORY "Enable clearing of memory before being freed" ON)
if(NOT CLEAR_MEMORY) if(NOT CLEAR_MEMORY)
list(APPEND libssh2_DEFINITIONS "LIBSSH2_NO_CLEAR_MEMORY") list(APPEND _libssh2_definitions "LIBSSH2_NO_CLEAR_MEMORY")
endif() endif()
option(ENABLE_ZLIB_COMPRESSION "Use zlib for compression" OFF) option(ENABLE_ZLIB_COMPRESSION "Use zlib for compression" OFF)
@@ -65,7 +67,7 @@ if(ENABLE_ZLIB_COMPRESSION)
list(APPEND LIBSSH2_PC_LIBS_PRIVATE "-lz") list(APPEND LIBSSH2_PC_LIBS_PRIVATE "-lz")
list(APPEND LIBSSH2_PC_REQUIRES_PRIVATE "zlib") list(APPEND LIBSSH2_PC_REQUIRES_PRIVATE "zlib")
if(ZLIB_FOUND) if(ZLIB_FOUND)
list(APPEND libssh2_DEFINITIONS "LIBSSH2_HAVE_ZLIB") list(APPEND _libssh2_definitions "LIBSSH2_HAVE_ZLIB")
endif() endif()
endif() endif()
@@ -89,7 +91,7 @@ include(GNUInstallDirs)
transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake") transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
# Get 'CSOURCES' and 'HHEADERS' variables # Get 'CSOURCES' and 'HHEADERS' variables
include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake") include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
set(SOURCES ${CSOURCES} ${HHEADERS}) set(_sources ${CSOURCES} ${HHEADERS})
## Library definition ## Library definition
@@ -103,40 +105,48 @@ if(WIN32 AND BUILD_STATIC_LIBS AND BUILD_SHARED_LIBS AND
set(STATIC_LIB_SUFFIX "_static") set(STATIC_LIB_SUFFIX "_static")
endif() endif()
unset(_libssh2_export)
# we want it to be called libssh2 on all platforms # we want it to be called libssh2 on all platforms
if(BUILD_STATIC_LIBS) if(BUILD_STATIC_LIBS)
list(APPEND libssh2_export ${LIB_STATIC}) list(APPEND _libssh2_export ${LIB_STATIC})
add_library(${LIB_STATIC} STATIC ${SOURCES}) add_library(${LIB_STATIC} STATIC ${_sources})
add_library(${PROJECT_NAME}::${LIB_STATIC} ALIAS ${LIB_STATIC}) add_library(${PROJECT_NAME}::${LIB_STATIC} ALIAS ${LIB_STATIC})
target_compile_definitions(${LIB_STATIC} PRIVATE ${PRIVATE_COMPILE_DEFINITIONS} ${libssh2_DEFINITIONS}) target_compile_definitions(${LIB_STATIC} PRIVATE ${PRIVATE_COMPILE_DEFINITIONS} ${_libssh2_definitions})
target_link_libraries(${LIB_STATIC} PRIVATE ${LIBRARIES}) target_link_libraries(${LIB_STATIC} PRIVATE ${LIBRARIES})
set_target_properties(${LIB_STATIC} PROPERTIES set_target_properties(${LIB_STATIC} PROPERTIES
PREFIX "" OUTPUT_NAME "libssh2" SOVERSION "${LIBSSH2_SOVERSION}" VERSION "${LIBSSH2_LIBVERSION}" PREFIX "" OUTPUT_NAME "libssh2" SOVERSION "${_libssh2_soversion}" VERSION "${_libssh2_libversion}"
SUFFIX "${STATIC_LIB_SUFFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}") SUFFIX "${STATIC_LIB_SUFFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}")
target_include_directories(${LIB_STATIC} target_include_directories(${LIB_STATIC}
PRIVATE "${PROJECT_SOURCE_DIR}/include" ${libssh2_INCLUDE_DIRS} ${PRIVATE_INCLUDE_DIRECTORIES} PRIVATE
"${PROJECT_SOURCE_DIR}/include"
${libssh2_INCLUDE_DIRS}
${PRIVATE_INCLUDE_DIRECTORIES}
PUBLIC PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>" "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>") "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
endif() endif()
if(BUILD_SHARED_LIBS) if(BUILD_SHARED_LIBS)
list(APPEND libssh2_export ${LIB_SHARED}) list(APPEND _libssh2_export ${LIB_SHARED})
add_library(${LIB_SHARED} SHARED ${SOURCES}) add_library(${LIB_SHARED} SHARED ${_sources})
add_library(${PROJECT_NAME}::${LIB_SHARED} ALIAS ${LIB_SHARED}) add_library(${PROJECT_NAME}::${LIB_SHARED} ALIAS ${LIB_SHARED})
if(WIN32) if(WIN32)
set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES "libssh2.rc") set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES "libssh2.rc")
endif() endif()
target_compile_definitions(${LIB_SHARED} PRIVATE ${PRIVATE_COMPILE_DEFINITIONS} ${libssh2_DEFINITIONS} ${LIB_SHARED_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_compile_options(${LIB_SHARED} PRIVATE ${LIB_SHARED_C_FLAGS})
target_link_libraries(${LIB_SHARED} PRIVATE ${LIBRARIES}) target_link_libraries(${LIB_SHARED} PRIVATE ${LIBRARIES})
set_target_properties(${LIB_SHARED} PROPERTIES set_target_properties(${LIB_SHARED} PROPERTIES
PREFIX "" OUTPUT_NAME "libssh2" SOVERSION "${LIBSSH2_SOVERSION}" VERSION "${LIBSSH2_LIBVERSION}" PREFIX "" OUTPUT_NAME "libssh2" SOVERSION "${_libssh2_soversion}" VERSION "${_libssh2_libversion}"
IMPORT_PREFIX "" IMPORT_SUFFIX "${IMPORT_LIB_SUFFIX}${CMAKE_IMPORT_LIBRARY_SUFFIX}" IMPORT_PREFIX "" IMPORT_SUFFIX "${IMPORT_LIB_SUFFIX}${CMAKE_IMPORT_LIBRARY_SUFFIX}"
POSITION_INDEPENDENT_CODE ON) POSITION_INDEPENDENT_CODE ON)
target_include_directories(${LIB_SHARED} target_include_directories(${LIB_SHARED}
PRIVATE "${PROJECT_SOURCE_DIR}/include" ${libssh2_INCLUDE_DIRS} ${PRIVATE_INCLUDE_DIRECTORIES} PRIVATE
"${PROJECT_SOURCE_DIR}/include"
${libssh2_INCLUDE_DIRS}
${PRIVATE_INCLUDE_DIRECTORIES}
PUBLIC PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>" "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>") "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
@@ -171,7 +181,7 @@ if(BUILD_SHARED_LIBS)
endif() endif()
set(RUNTIME_DEPENDENCIES ${_RUNTIME_DEPENDENCIES} CACHE INTERNAL set(RUNTIME_DEPENDENCIES ${_RUNTIME_DEPENDENCIES} CACHE INTERNAL
"Files that must be in the same directory as the executables at runtime.") "Files that must be in the same directory as the executables at runtime.")
# Package config # Package config
@@ -182,7 +192,7 @@ install(EXPORT "${PROJECT_NAME}-targets"
## During build, register directly from build tree ## During build, register directly from build tree
# create libssh2-targets.cmake # create libssh2-targets.cmake
export(TARGETS ${libssh2_export} NAMESPACE "${PROJECT_NAME}::" FILE "${PROJECT_NAME}-targets.cmake") export(TARGETS ${_libssh2_export} NAMESPACE "${PROJECT_NAME}::" FILE "${PROJECT_NAME}-targets.cmake")
export(PACKAGE ${PROJECT_NAME}) # register it export(PACKAGE ${PROJECT_NAME}) # register it
# Generate libssh2-config.cmake into build tree and install it with dependencies # Generate libssh2-config.cmake into build tree and install it with dependencies

View File

@@ -50,9 +50,9 @@ list(APPEND STANDALONE_TESTS ${STANDALONE_TESTS_STATIC})
if(CMAKE_COMPILER_IS_GNUCC) if(CMAKE_COMPILER_IS_GNUCC)
find_program(GCOV_PATH "gcov") find_program(GCOV_PATH "gcov")
if(GCOV_PATH) if(GCOV_PATH)
set(GCOV_CFLAGS "-g" "--coverage") set(_gcov_cflags "-g" "--coverage")
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0) if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0)
set(GCOV_CFLAGS "${GCOV_CFLAGS} -fprofile-abs-path") set(_gcov_cflags "${_gcov_cflags} -fprofile-abs-path")
endif() endif()
endif() endif()
endif() endif()
@@ -72,67 +72,75 @@ endif()
add_library(runner STATIC ${librunner_la_SOURCES}) add_library(runner STATIC ${librunner_la_SOURCES})
target_compile_definitions(runner PRIVATE "${CRYPTO_BACKEND_DEFINE}") 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_link_libraries(runner PRIVATE libssh2) target_link_libraries(runner PRIVATE libssh2)
foreach(test IN LISTS DOCKER_TESTS STANDALONE_TESTS SSHD_TESTS) foreach(_test IN LISTS DOCKER_TESTS STANDALONE_TESTS SSHD_TESTS)
if(NOT ";${DOCKER_TESTS_STATIC};${STANDALONE_TESTS_STATIC};" MATCHES ";${test};") if(NOT ";${DOCKER_TESTS_STATIC};${STANDALONE_TESTS_STATIC};" MATCHES ";${_test};")
set(LIB_FOR_TESTS ${LIB_SELECTED}) set(_lib_for_tests ${LIB_SELECTED})
elseif(TARGET ${LIB_STATIC}) elseif(TARGET ${LIB_STATIC})
set(LIB_FOR_TESTS ${LIB_STATIC}) set(_lib_for_tests ${LIB_STATIC})
else() else()
unset(LIB_FOR_TESTS) unset(_lib_for_tests)
message(STATUS "Skip test requiring static libssh2 lib: ${test}") message(STATUS "Skip test requiring static libssh2 lib: ${_test}")
endif() endif()
# 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.
# Skip building tests that require the static lib when the static lib is disabled. # Skip building tests that require the static lib when the static lib is disabled.
if(NOT TARGET ${test} AND LIB_FOR_TESTS) if(NOT TARGET ${_test} AND _lib_for_tests)
add_executable(${test} "${test}.c") add_executable(${_test} "${_test}.c")
target_compile_definitions(${test} PRIVATE "${CRYPTO_BACKEND_DEFINE}") target_compile_definitions(${_test} PRIVATE "${CRYPTO_BACKEND_DEFINE}")
target_include_directories(${test} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../src" "../src" "../include" "${CRYPTO_BACKEND_INCLUDE_DIR}") target_include_directories(${_test} PRIVATE
set_target_properties(${test} PROPERTIES UNITY_BUILD OFF) "${CMAKE_CURRENT_BINARY_DIR}/../src"
"../src"
"../include"
"${CRYPTO_BACKEND_INCLUDE_DIR}")
set_target_properties(${_test} PROPERTIES UNITY_BUILD OFF)
# build a single test with gcov # build a single test with gcov
if(GCOV_PATH AND test STREQUAL "test_auth_keyboard_info_request" AND TARGET ${LIB_STATIC}) if(GCOV_PATH AND _test STREQUAL "test_auth_keyboard_info_request" AND TARGET ${LIB_STATIC})
target_compile_options(${test} BEFORE PRIVATE ${GCOV_CFLAGS}) target_compile_options(${_test} BEFORE PRIVATE ${_gcov_cflags})
target_link_libraries(${test} runner ${LIB_FOR_TESTS} ${LIBRARIES} "gcov") target_link_libraries(${_test} runner ${_lib_for_tests} ${LIBRARIES} "gcov")
else() else()
target_link_libraries(${test} runner ${LIB_FOR_TESTS} ${LIBRARIES}) target_link_libraries(${_test} runner ${_lib_for_tests} ${LIBRARIES})
endif() endif()
list(APPEND TEST_TARGETS ${test}) list(APPEND TEST_TARGETS ${_test})
endif() endif()
endforeach() endforeach()
option(RUN_DOCKER_TESTS "Run tests requiring Docker" ON) option(RUN_DOCKER_TESTS "Run tests requiring Docker" ON)
if(RUN_DOCKER_TESTS) if(RUN_DOCKER_TESTS)
foreach(test IN LISTS DOCKER_TESTS) foreach(_test IN LISTS DOCKER_TESTS)
if(TARGET ${test}) if(TARGET ${_test})
add_test(NAME ${test} COMMAND "$<TARGET_FILE:${test}>") add_test(NAME ${_test} COMMAND "$<TARGET_FILE:${_test}>")
set_property(TEST ${test} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}") set_property(TEST ${_test} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
endif() endif()
endforeach() endforeach()
endif() endif()
foreach(test IN LISTS STANDALONE_TESTS) foreach(_test IN LISTS STANDALONE_TESTS)
if(TARGET ${test}) if(TARGET ${_test})
add_test(NAME ${test} COMMAND "$<TARGET_FILE:${test}>") add_test(NAME ${_test} COMMAND "$<TARGET_FILE:${_test}>")
set_property(TEST ${test} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}") set_property(TEST ${_test} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
endif() endif()
endforeach() endforeach()
if(RUN_SSHD_TESTS AND SSHD_EXECUTABLE) if(RUN_SSHD_TESTS AND SSHD_EXECUTABLE)
unset(sshd_test_targets) unset(_sshd_test_targets)
foreach(test IN LISTS SSHD_TESTS) foreach(_test IN LISTS SSHD_TESTS)
if(TARGET ${test}) if(TARGET ${_test})
set(sshd_test_targets "${sshd_test_targets} $<TARGET_FILE:${test}>") set(_sshd_test_targets "${_sshd_test_targets} $<TARGET_FILE:${_test}>")
endif() endif()
endforeach() endforeach()
if(sshd_test_targets) if(_sshd_test_targets)
add_test(NAME test_sshd COMMAND ${SH_EXECUTABLE} -c "${CMAKE_CURRENT_SOURCE_DIR}/test_sshd.test ${sshd_test_targets}") add_test(NAME test_sshd COMMAND ${SH_EXECUTABLE} -c "${CMAKE_CURRENT_SOURCE_DIR}/test_sshd.test ${_sshd_test_targets}")
set_property(TEST test_sshd APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}") set_property(TEST test_sshd APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
set_property(TEST test_sshd APPEND PROPERTY ENVIRONMENT "SSHD=${SSHD_EXECUTABLE}") set_property(TEST test_sshd APPEND PROPERTY ENVIRONMENT "SSHD=${SSHD_EXECUTABLE}")
endif() endif()
@@ -140,17 +148,17 @@ endif()
if(RUN_DOCKER_TESTS) if(RUN_DOCKER_TESTS)
# CRYPT/MAC algo tests # CRYPT/MAC algo tests
file(READ "test_read_algos.txt" ALGO_TESTS) file(READ "test_read_algos.txt" _algo_tests)
string(REGEX REPLACE "\\\n" ";" ALGO_TESTS ${ALGO_TESTS}) string(REGEX REPLACE "\\\n" ";" _algo_tests ${_algo_tests})
foreach(test IN LISTS ALGO_TESTS) foreach(_test IN LISTS _algo_tests)
if(test) if(_test)
set(testname "test_read-${test}") set(_testname "test_read-${_test}")
add_test(NAME ${testname} COMMAND "$<TARGET_FILE:test_read>") add_test(NAME ${_testname} COMMAND "$<TARGET_FILE:test_read>")
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}") set_property(TEST ${_testname} APPEND PROPERTY ENVIRONMENT "srcdir=${CMAKE_CURRENT_SOURCE_DIR}")
if(test MATCHES "mac-") if(_test MATCHES "mac-")
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_MAC=${test}") set_property(TEST ${_testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_MAC=${_test}")
else() else()
set_property(TEST ${testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_CRYPT=${test}") set_property(TEST ${_testname} APPEND PROPERTY ENVIRONMENT "FIXTURE_TEST_CRYPT=${_test}")
endif() endif()
endif() endif()
endforeach() endforeach()