From 9bdc28b0528dad60e01b06c705db5ca78c7da65f Mon Sep 17 00:00:00 2001 From: Micah Snyder Date: Tue, 25 Jan 2022 10:27:00 -0800 Subject: [PATCH] CMake: Support external TomsFastMath library (libtfm) Add support for compiling with external TomsFastMath library provided by the system instead of compiling the vendored copy into libclamav. The vendored source is still built directly into libclamav instead of as a separate library the way libmspack is done. The rationale is that: A) it's more complicated to deal with possibly compiling as static or dynamic, and also B) libmspack and libunrar are compiled separately primarily because of licensing concerns. TomsFastMath public domain, so that isn't a concern. Resolves: https://bugzilla.clamav.net/show_bug.cgi?id=12562 --- CMakeLists.txt | 11 +- CMakeOptions.cmake | 3 + INSTALL.md | 6 ++ clamav-config.h.cmake.in | 3 + cmake/FindMSPack.cmake | 2 +- cmake/FindTomsFastMath.cmake | 85 ++++++++++++++++ libclamav/CMakeLists.txt | 192 ++++++++++++++++++----------------- unit_tests/CMakeLists.txt | 16 ++- 8 files changed, 217 insertions(+), 101 deletions(-) create mode 100644 cmake/FindTomsFastMath.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cd1d195b..ec544d449 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -979,10 +979,19 @@ if(NOT ENABLE_EXTERNAL_MSPACK) add_subdirectory( libclammspack ) set(LIBMSPACK "ClamAV::libmspack") else() - find_package(MSPack) + find_package(MSPack REQUIRED) set(LIBMSPACK "MSPack::mspack") endif() +if(NOT ENABLE_EXTERNAL_TOMSFASTMATH) + # TomsFastMath built as an object library within libclamav, at least for now. + set(LIBTFM "tomsfastmath") +else() + find_package(TomsFastMath REQUIRED) + set(HAVE_SYSTEM_TOMSFASTMATH 1) + set(LIBTFM "TomsFastMath::TomsFastMath") +endif() + if(WIN32) add_subdirectory( win32/compat ) endif() diff --git a/CMakeOptions.cmake b/CMakeOptions.cmake index 945bac527..02353fcd5 100644 --- a/CMakeOptions.cmake +++ b/CMakeOptions.cmake @@ -65,6 +65,9 @@ option(ENABLE_FUZZ option(ENABLE_EXTERNAL_MSPACK "Use external mspack instead of internal libclammspack.") +option(ENABLE_EXTERNAL_TOMSFASTMATH + "Use external TomsFastMath instead of compiling vendored source into libclamav.") + option(ENABLE_JSON_SHARED "Prefer linking with libjson-c shared library instead of static." ON) diff --git a/INSTALL.md b/INSTALL.md index 31d39f0e8..75d0c516c 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -118,6 +118,7 @@ libclamav requires these library dependencies: - `json-c` - `libjson-c` / `json-c` - `libmspack` (built-in by default, enable with `ENABLE_EXTERNAL_MSPACK=ON`) +- `libtfm` (built-in by default, enable with `ENABLE_EXTERNAL_TOMSFASTMATH=ON`) - `libiconv` (built-in to `libc` 99% of the time, not requires on Windows) - `pthreads` (provided by Linux/Unix; requires `pthreads-win32` on Windows) - `llvm` (optional, see: [Bytecode Runtime](#bytecode-runtime), below) @@ -423,6 +424,11 @@ The following is a complete list of CMake options unique to configuring ClamAV: _Default: `OFF`_ +- `ENABLE_EXTERNAL_TOMSFASTMATH`: Use external TomsFastMath instead of using + vendored TomsFastMath source compiled into libclamav. + + _Default: `OFF`_ + - `ENABLE_JSON_SHARED`: Prefer linking with libjson-c shared library instead of static. diff --git a/clamav-config.h.cmake.in b/clamav-config.h.cmake.in index b21af8778..5de4cbf32 100644 --- a/clamav-config.h.cmake.in +++ b/clamav-config.h.cmake.in @@ -401,6 +401,9 @@ /* Define if UNRAR is linked instead of loaded. */ #cmakedefine UNRAR_LINKED 1 +/* Define if UNRAR is linked instead of loaded. */ +#cmakedefine HAVE_SYSTEM_TOMSFASTMATH 1 + /* "Full clamav library version number" */ #define LIBCLAMAV_FULLVER "@LIBCLAMAV_VERSION@" diff --git a/cmake/FindMSPack.cmake b/cmake/FindMSPack.cmake index cad448f4e..4d2b40347 100644 --- a/cmake/FindMSPack.cmake +++ b/cmake/FindMSPack.cmake @@ -35,7 +35,7 @@ Cache Variables The following cache variables may also be set: ``MSPack_INCLUDE_DIR`` - The directory containing ``foo.h``. + The directory containing ``mspack.h``. ``MSPack_LIBRARY`` The path to the MSPack library. diff --git a/cmake/FindTomsFastMath.cmake b/cmake/FindTomsFastMath.cmake new file mode 100644 index 000000000..221b19ee1 --- /dev/null +++ b/cmake/FindTomsFastMath.cmake @@ -0,0 +1,85 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +#[=======================================================================[.rst: +FindTomsFastMath +------- + +Finds the TomsFastMath library. + +Imported Targets +^^^^^^^^^^^^^^^^ + +This module provides the following imported targets, if found: + +``TomsFastMath::TomsFastMath`` + The TomsFastMath library + +Result Variables +^^^^^^^^^^^^^^^^ + +This will define the following variables: + +``TomsFastMath_FOUND`` + True if the system has the TomsFastMath library. +``TomsFastMath_VERSION`` + The version of the TomsFastMath library which was found. +``TomsFastMath_INCLUDE_DIRS`` + Include directories needed to use TomsFastMath. +``TomsFastMath_LIBRARIES`` + Libraries needed to link to TomsFastMath. + +Cache Variables +^^^^^^^^^^^^^^^ + +The following cache variables may also be set: + +``TomsFastMath_INCLUDE_DIR`` + The directory containing ``tfm.h``. +``TomsFastMath_LIBRARY`` + The path to the TomsFastMath library. + +#]=======================================================================] + +find_package(PkgConfig QUIET) +pkg_check_modules(PC_TomsFastMath QUIET toms) + +find_path(TomsFastMath_INCLUDE_DIR + NAMES tfm.h + PATHS ${PC_TomsFastMath_INCLUDE_DIRS} +) +find_library(TomsFastMath_LIBRARY + NAMES tfm + PATHS ${PC_TomsFastMath_LIBRARY_DIRS} +) + +set(TomsFastMath_VERSION ${PC_TomsFastMath_VERSION}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(TomsFastMath + FOUND_VAR TomsFastMath_FOUND + REQUIRED_VARS + TomsFastMath_LIBRARY + TomsFastMath_INCLUDE_DIR + VERSION_VAR TomsFastMath_VERSION +) + +if(TomsFastMath_FOUND) + set(TomsFastMath_LIBRARIES ${TomsFastMath_LIBRARY}) + set(TomsFastMath_INCLUDE_DIRS ${TomsFastMath_INCLUDE_DIR}) + set(TomsFastMath_DEFINITIONS ${PC_TomsFastMath_CFLAGS_OTHER}) +endif() + +if(TomsFastMath_FOUND AND NOT TARGET TomsFastMath::TomsFastMath) + add_library(TomsFastMath::TomsFastMath UNKNOWN IMPORTED) + set_target_properties(TomsFastMath::TomsFastMath PROPERTIES + IMPORTED_LOCATION "${TomsFastMath_LIBRARY}" + INTERFACE_COMPILE_OPTIONS "${PC_TomsFastMath_CFLAGS_OTHER}" + INTERFACE_INCLUDE_DIRECTORIES "${TomsFastMath_INCLUDE_DIR}" + ) +endif() + +mark_as_advanced( + TomsFastMath_INCLUDE_DIR + TomsFastMath_LIBRARY +) diff --git a/libclamav/CMakeLists.txt b/libclamav/CMakeLists.txt index fa37761c3..b2055e361 100644 --- a/libclamav/CMakeLists.txt +++ b/libclamav/CMakeLists.txt @@ -166,99 +166,101 @@ target_link_libraries( yara PCRE2::pcre2 JSONC::jsonc ) -add_library( tomsfastmath OBJECT ) -target_sources( tomsfastmath - PRIVATE - tomsfastmath/addsub/fp_add.c - tomsfastmath/addsub/fp_add_d.c - tomsfastmath/addsub/fp_addmod.c - tomsfastmath/addsub/fp_cmp.c - tomsfastmath/addsub/fp_cmp_d.c - tomsfastmath/addsub/fp_cmp_mag.c - tomsfastmath/addsub/fp_sub.c - tomsfastmath/addsub/fp_sub_d.c - tomsfastmath/addsub/fp_submod.c - tomsfastmath/addsub/s_fp_add.c - tomsfastmath/addsub/s_fp_sub.c - tomsfastmath/bin/fp_radix_size.c - tomsfastmath/bin/fp_read_radix.c - tomsfastmath/bin/fp_read_signed_bin.c - tomsfastmath/bin/fp_read_unsigned_bin.c - tomsfastmath/bin/fp_reverse.c - tomsfastmath/bin/fp_s_rmap.c - tomsfastmath/bin/fp_signed_bin_size.c - tomsfastmath/bin/fp_to_signed_bin.c - tomsfastmath/bin/fp_to_unsigned_bin.c - tomsfastmath/bin/fp_toradix.c - tomsfastmath/bin/fp_toradix_n.c - tomsfastmath/bin/fp_unsigned_bin_size.c - tomsfastmath/bit/fp_cnt_lsb.c - tomsfastmath/bit/fp_count_bits.c - tomsfastmath/bit/fp_div_2.c - tomsfastmath/bit/fp_div_2d.c - tomsfastmath/bit/fp_lshd.c - tomsfastmath/bit/fp_mod_2d.c - tomsfastmath/bit/fp_rshd.c - tomsfastmath/divide/fp_div.c - tomsfastmath/divide/fp_div_d.c - tomsfastmath/divide/fp_mod.c - tomsfastmath/divide/fp_mod_d.c - tomsfastmath/exptmod/fp_2expt.c - tomsfastmath/exptmod/fp_exptmod.c - tomsfastmath/misc/fp_ident.c - tomsfastmath/misc/fp_set.c - tomsfastmath/mont/fp_montgomery_calc_normalization.c - tomsfastmath/mont/fp_montgomery_reduce.c - tomsfastmath/mont/fp_montgomery_setup.c - tomsfastmath/mul/fp_mul.c - tomsfastmath/mul/fp_mul_comba.c - tomsfastmath/mul/fp_mul_2.c - tomsfastmath/mul/fp_mul_2d.c - tomsfastmath/mul/fp_mul_comba_12.c - tomsfastmath/mul/fp_mul_comba_17.c - tomsfastmath/mul/fp_mul_comba_20.c - tomsfastmath/mul/fp_mul_comba_24.c - tomsfastmath/mul/fp_mul_comba_28.c - tomsfastmath/mul/fp_mul_comba_3.c - tomsfastmath/mul/fp_mul_comba_32.c - tomsfastmath/mul/fp_mul_comba_4.c - tomsfastmath/mul/fp_mul_comba_48.c - tomsfastmath/mul/fp_mul_comba_6.c - tomsfastmath/mul/fp_mul_comba_64.c - tomsfastmath/mul/fp_mul_comba_7.c - tomsfastmath/mul/fp_mul_comba_8.c - tomsfastmath/mul/fp_mul_comba_9.c - tomsfastmath/mul/fp_mul_comba_small_set.c - tomsfastmath/mul/fp_mul_d.c - tomsfastmath/mul/fp_mulmod.c - tomsfastmath/numtheory/fp_invmod.c - tomsfastmath/sqr/fp_sqr.c - tomsfastmath/sqr/fp_sqr_comba_12.c - tomsfastmath/sqr/fp_sqr_comba_17.c - tomsfastmath/sqr/fp_sqr_comba_20.c - tomsfastmath/sqr/fp_sqr_comba_24.c - tomsfastmath/sqr/fp_sqr_comba_28.c - tomsfastmath/sqr/fp_sqr_comba_3.c - tomsfastmath/sqr/fp_sqr_comba_32.c - tomsfastmath/sqr/fp_sqr_comba_4.c - tomsfastmath/sqr/fp_sqr_comba_48.c - tomsfastmath/sqr/fp_sqr_comba_6.c - tomsfastmath/sqr/fp_sqr_comba_64.c - tomsfastmath/sqr/fp_sqr_comba_7.c - tomsfastmath/sqr/fp_sqr_comba_8.c - tomsfastmath/sqr/fp_sqr_comba_9.c - tomsfastmath/sqr/fp_sqr_comba_generic.c - tomsfastmath/sqr/fp_sqr_comba_small_set.c - tomsfastmath/sqr/fp_sqrmod.c - PUBLIC - bignum.h ) -target_include_directories( tomsfastmath - PRIVATE - ${CMAKE_BINARY_DIR} - ${CMAKE_CURRENT_SOURCE_DIR}/tomsfastmath/headers - PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ) -set_target_properties( tomsfastmath PROPERTIES - COMPILE_FLAGS "${WARNCFLAGS}" ) +if(NOT ENABLE_EXTERNAL_TOMSFASTMATH) + add_library( tomsfastmath OBJECT ) + target_sources( tomsfastmath + PRIVATE + tomsfastmath/addsub/fp_add.c + tomsfastmath/addsub/fp_add_d.c + tomsfastmath/addsub/fp_addmod.c + tomsfastmath/addsub/fp_cmp.c + tomsfastmath/addsub/fp_cmp_d.c + tomsfastmath/addsub/fp_cmp_mag.c + tomsfastmath/addsub/fp_sub.c + tomsfastmath/addsub/fp_sub_d.c + tomsfastmath/addsub/fp_submod.c + tomsfastmath/addsub/s_fp_add.c + tomsfastmath/addsub/s_fp_sub.c + tomsfastmath/bin/fp_radix_size.c + tomsfastmath/bin/fp_read_radix.c + tomsfastmath/bin/fp_read_signed_bin.c + tomsfastmath/bin/fp_read_unsigned_bin.c + tomsfastmath/bin/fp_reverse.c + tomsfastmath/bin/fp_s_rmap.c + tomsfastmath/bin/fp_signed_bin_size.c + tomsfastmath/bin/fp_to_signed_bin.c + tomsfastmath/bin/fp_to_unsigned_bin.c + tomsfastmath/bin/fp_toradix.c + tomsfastmath/bin/fp_toradix_n.c + tomsfastmath/bin/fp_unsigned_bin_size.c + tomsfastmath/bit/fp_cnt_lsb.c + tomsfastmath/bit/fp_count_bits.c + tomsfastmath/bit/fp_div_2.c + tomsfastmath/bit/fp_div_2d.c + tomsfastmath/bit/fp_lshd.c + tomsfastmath/bit/fp_mod_2d.c + tomsfastmath/bit/fp_rshd.c + tomsfastmath/divide/fp_div.c + tomsfastmath/divide/fp_div_d.c + tomsfastmath/divide/fp_mod.c + tomsfastmath/divide/fp_mod_d.c + tomsfastmath/exptmod/fp_2expt.c + tomsfastmath/exptmod/fp_exptmod.c + tomsfastmath/misc/fp_ident.c + tomsfastmath/misc/fp_set.c + tomsfastmath/mont/fp_montgomery_calc_normalization.c + tomsfastmath/mont/fp_montgomery_reduce.c + tomsfastmath/mont/fp_montgomery_setup.c + tomsfastmath/mul/fp_mul.c + tomsfastmath/mul/fp_mul_comba.c + tomsfastmath/mul/fp_mul_2.c + tomsfastmath/mul/fp_mul_2d.c + tomsfastmath/mul/fp_mul_comba_12.c + tomsfastmath/mul/fp_mul_comba_17.c + tomsfastmath/mul/fp_mul_comba_20.c + tomsfastmath/mul/fp_mul_comba_24.c + tomsfastmath/mul/fp_mul_comba_28.c + tomsfastmath/mul/fp_mul_comba_3.c + tomsfastmath/mul/fp_mul_comba_32.c + tomsfastmath/mul/fp_mul_comba_4.c + tomsfastmath/mul/fp_mul_comba_48.c + tomsfastmath/mul/fp_mul_comba_6.c + tomsfastmath/mul/fp_mul_comba_64.c + tomsfastmath/mul/fp_mul_comba_7.c + tomsfastmath/mul/fp_mul_comba_8.c + tomsfastmath/mul/fp_mul_comba_9.c + tomsfastmath/mul/fp_mul_comba_small_set.c + tomsfastmath/mul/fp_mul_d.c + tomsfastmath/mul/fp_mulmod.c + tomsfastmath/numtheory/fp_invmod.c + tomsfastmath/sqr/fp_sqr.c + tomsfastmath/sqr/fp_sqr_comba_12.c + tomsfastmath/sqr/fp_sqr_comba_17.c + tomsfastmath/sqr/fp_sqr_comba_20.c + tomsfastmath/sqr/fp_sqr_comba_24.c + tomsfastmath/sqr/fp_sqr_comba_28.c + tomsfastmath/sqr/fp_sqr_comba_3.c + tomsfastmath/sqr/fp_sqr_comba_32.c + tomsfastmath/sqr/fp_sqr_comba_4.c + tomsfastmath/sqr/fp_sqr_comba_48.c + tomsfastmath/sqr/fp_sqr_comba_6.c + tomsfastmath/sqr/fp_sqr_comba_64.c + tomsfastmath/sqr/fp_sqr_comba_7.c + tomsfastmath/sqr/fp_sqr_comba_8.c + tomsfastmath/sqr/fp_sqr_comba_9.c + tomsfastmath/sqr/fp_sqr_comba_generic.c + tomsfastmath/sqr/fp_sqr_comba_small_set.c + tomsfastmath/sqr/fp_sqrmod.c + PUBLIC + bignum.h ) + target_include_directories( tomsfastmath + PRIVATE + ${CMAKE_BINARY_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/tomsfastmath/headers + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ) + set_target_properties( tomsfastmath PROPERTIES + COMPILE_FLAGS "${WARNCFLAGS}" ) +endif() # Bytecode Runtime add_library( bytecode_runtime OBJECT ) @@ -527,7 +529,7 @@ if(ENABLE_SHARED_LIB) regex lzma_sdk yara - tomsfastmath + ${LIBTFM} bytecode_runtime ${LIBMSPACK} ClamAV::libclamav_rust @@ -637,7 +639,7 @@ if(ENABLE_STATIC_LIB) regex lzma_sdk yara - tomsfastmath + ${LIBTFM} bytecode_runtime ${LIBMSPACK} ClamAV::libclamav_rust diff --git a/unit_tests/CMakeLists.txt b/unit_tests/CMakeLists.txt index 942a2fd65..7007a6bf4 100644 --- a/unit_tests/CMakeLists.txt +++ b/unit_tests/CMakeLists.txt @@ -52,7 +52,7 @@ if(ENABLE_APP) regex lzma_sdk yara - tomsfastmath + ${LIBTFM} bytecode_runtime JSONC::jsonc ${LIBMSPACK} @@ -92,7 +92,7 @@ if(ENABLE_APP) regex lzma_sdk yara - tomsfastmath + ${LIBTFM} bytecode_runtime JSONC::jsonc ${LIBMSPACK} @@ -144,7 +144,7 @@ target_link_libraries(check_clamav regex lzma_sdk yara - tomsfastmath + ${LIBTFM} bytecode_runtime JSONC::jsonc ${LIBMSPACK} @@ -198,6 +198,10 @@ if(WIN32) file(TO_NATIVE_PATH $ LIBPTHREADW32) file(TO_NATIVE_PATH $ LIBWIN32COMPAT) + if (ENABLE_EXTERNAL_TOMSFASTMATH) + file(TO_NATIVE_PATH $ LIBTFM_PATH) + endif() + if(ENABLE_STATIC_LIB) file(TO_NATIVE_PATH $ LIBCLAMAV) file(TO_NATIVE_PATH $ LIBCLAMMSPACK) @@ -230,7 +234,10 @@ else() if (ENABLE_UNRAR) set(LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:$:$) endif() - + if (ENABLE_EXTERNAL_TOMSFASTMATH) + set(LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:$) + set(LIBTFM_PATH $) + endif() set(SOURCE ${CMAKE_SOURCE_DIR}) set(BUILD ${CMAKE_BINARY_DIR}) @@ -302,6 +309,7 @@ set(ENVIRONMENT LIBCURL=${LIBCURL} LIBJSONC=${LIBJSONC} LIBICONV=${LIBICONV} + LIBTFM=${LIBTFM_PATH} LIBPTHREADW32=${LIBPTHREADW32} LIBWIN32COMPAT=${LIBWIN32COMPAT} LIBCLAMAV=${LIBCLAMAV}