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
This commit is contained in:
Micah Snyder 2022-01-25 10:27:00 -08:00 committed by Micah Snyder
parent 375ecf678c
commit 9bdc28b052
8 changed files with 217 additions and 101 deletions

View file

@ -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()

View file

@ -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)

View file

@ -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.

View file

@ -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@"

View file

@ -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.

View file

@ -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
)

View file

@ -166,6 +166,7 @@ target_link_libraries( yara
PCRE2::pcre2
JSONC::jsonc )
if(NOT ENABLE_EXTERNAL_TOMSFASTMATH)
add_library( tomsfastmath OBJECT )
target_sources( tomsfastmath
PRIVATE
@ -259,6 +260,7 @@ target_include_directories( tomsfastmath
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

View file

@ -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 $<TARGET_FILE:PThreadW32::pthreadw32> LIBPTHREADW32)
file(TO_NATIVE_PATH $<TARGET_FILE:ClamAV::win32_compat> LIBWIN32COMPAT)
if (ENABLE_EXTERNAL_TOMSFASTMATH)
file(TO_NATIVE_PATH $<TARGET_FILE:${LIBTFM}> LIBTFM_PATH)
endif()
if(ENABLE_STATIC_LIB)
file(TO_NATIVE_PATH $<TARGET_FILE:clamav_static> LIBCLAMAV)
file(TO_NATIVE_PATH $<TARGET_FILE:clammspack_static> LIBCLAMMSPACK)
@ -230,7 +234,10 @@ else()
if (ENABLE_UNRAR)
set(LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:$<TARGET_FILE_DIR:ClamAV::libunrar_iface>:$<TARGET_FILE_DIR:ClamAV::libunrar>)
endif()
if (ENABLE_EXTERNAL_TOMSFASTMATH)
set(LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:$<TARGET_FILE_DIR:${LIBTFM}>)
set(LIBTFM_PATH $<TARGET_FILE:${LIBTFM}>)
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}