From 8ac8f4ac8a9394ae684faedd12b860b3cffdc0d8 Mon Sep 17 00:00:00 2001 From: Valerie Snyder Date: Wed, 30 Apr 2025 12:35:19 -0400 Subject: [PATCH] CMake: Improvement for CMake/Rust openssl detection We observed build failures on Ubuntu 20.04 ARM64 because the Rust code saw extra OpenSSL dependencies in the OPENSSL_LIBS environment variable and was confused. This change switches from using OPENSSL_LIBRARIES, which may have extra dependencies for libcrypto/libssl, to only use OPENSSL_CRYPTO_LIBRARY and OPENSSL_SSL_LIBRARY. --- libclamav_rust/CMakeLists.txt | 40 +++++++++++++++------------------ unit_tests/CMakeLists.txt | 42 ++++++++++++++++------------------- 2 files changed, 37 insertions(+), 45 deletions(-) diff --git a/libclamav_rust/CMakeLists.txt b/libclamav_rust/CMakeLists.txt index 3f669b110..79cc2460a 100644 --- a/libclamav_rust/CMakeLists.txt +++ b/libclamav_rust/CMakeLists.txt @@ -8,29 +8,25 @@ get_filename_component(OPENSSL_DIR "${OPENSSL_INCLUDE_DIR}" DIRECTORY) set(OPENSSL_LIBS "") -foreach(LIB IN LISTS OPENSSL_LIBRARIES) - # Skip any libraries starting with `-l` (e.g. -lpthread). - # These are system libraries and won't be found in the OPENSSL_LIB_DIR. - if (NOT LIB MATCHES "^-l") - # Remove path and extension - get_filename_component(LIBNAME "${LIB}" NAME_WLE) - # Remove "lib" prefix, if present - string(REGEX REPLACE "^lib" "" LIBNAME "${LIBNAME}") - - if (NOT OPENSSL_LIBS) - # Add first openssl lib. - set(OPENSSL_LIBS "${LIBNAME}") - - # While we're at it... get directory of the first library to use for the OPENSSL_LIB_DIR. - # Note: This assumes that all libs are in the same directory. - get_filename_component(OPENSSL_LIB_DIR "${LIB}" DIRECTORY) - else() - # Add additional openssl libs. - set(OPENSSL_LIBS "${OPENSSL_LIBS}:${LIBNAME}") - endif() - endif() -endforeach() +# Get the libcrypto library. +# Remove path and extension. +get_filename_component(LIBNAME "${OPENSSL_CRYPTO_LIBRARY}" NAME_WLE) +# Remove "lib" prefix, if present. +string(REGEX REPLACE "^lib" "" LIBNAME "${LIBNAME}") +# Add libcrypto. +set(OPENSSL_LIBS "${LIBNAME}") + +# Get the libssl library. +# Remove path and extension. +get_filename_component(LIBNAME "${OPENSSL_SSL_LIBRARY}" NAME_WLE) +# Remove "lib" prefix, if present. +string(REGEX REPLACE "^lib" "" LIBNAME "${LIBNAME}") +# Add libssl. +set(OPENSSL_LIBS "${OPENSSL_LIBS}:${LIBNAME}") + +# Get directory of the first library to use for the OPENSSL_LIB_DIR. +get_filename_component(OPENSSL_LIB_DIR "${OPENSSL_CRYPTO_LIBRARY}" DIRECTORY) set(ENVIRONMENT "") list(APPEND ENVIRONMENT "OPENSSL_DIR=${OPENSSL_DIR}") diff --git a/unit_tests/CMakeLists.txt b/unit_tests/CMakeLists.txt index 94e938a7f..3224a35f2 100644 --- a/unit_tests/CMakeLists.txt +++ b/unit_tests/CMakeLists.txt @@ -303,29 +303,25 @@ set(ENVIRONMENT get_filename_component(OPENSSL_DIR "${OPENSSL_INCLUDE_DIR}" DIRECTORY) set(OPENSSL_LIBS "") -foreach(LIB IN LISTS OPENSSL_LIBRARIES) - # Skip any libraries starting with `-l` (e.g. -lpthread). - # These are system libraries and won't be found in the OPENSSL_LIB_DIR. - if (NOT LIB MATCHES "^-l") - # Remove path and extension - get_filename_component(LIBNAME "${LIB}" NAME_WLE) - - # Remove "lib" prefix, if present - string(REGEX REPLACE "^lib" "" LIBNAME "${LIBNAME}") - - if (NOT OPENSSL_LIBS) - # Add first openssl lib. - set(OPENSSL_LIBS "${LIBNAME}") - - # While we're at it... get directory of the first library to use for the OPENSSL_LIB_DIR. - # Note: This assumes that all libs are in the same directory. - get_filename_component(OPENSSL_LIB_DIR "${LIB}" DIRECTORY) - else() - # Add additional openssl libs. - set(OPENSSL_LIBS "${OPENSSL_LIBS}:${LIBNAME}") - endif() - endif() -endforeach() + +# Get the libcrypto library. +# Remove path and extension. +get_filename_component(LIBNAME "${OPENSSL_CRYPTO_LIBRARY}" NAME_WLE) +# Remove "lib" prefix, if present. +string(REGEX REPLACE "^lib" "" LIBNAME "${LIBNAME}") +# Add libcrypto. +set(OPENSSL_LIBS "${LIBNAME}") + +# Get the libssl library. +# Remove path and extension. +get_filename_component(LIBNAME "${OPENSSL_SSL_LIBRARY}" NAME_WLE) +# Remove "lib" prefix, if present. +string(REGEX REPLACE "^lib" "" LIBNAME "${LIBNAME}") +# Add libssl. +set(OPENSSL_LIBS "${OPENSSL_LIBS}:${LIBNAME}") + +# Get directory of the first library to use for the OPENSSL_LIB_DIR. +get_filename_component(OPENSSL_LIB_DIR "${OPENSSL_CRYPTO_LIBRARY}" DIRECTORY) list(APPEND ENVIRONMENT "OPENSSL_DIR=${OPENSSL_DIR}") list(APPEND ENVIRONMENT "OPENSSL_INCLUDE_DIR=${OPENSSL_INCLUDE_DIR}")