Browse code

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

Micah Snyder authored on 2022/01/26 03:27:00
Showing 8 changed files
... ...
@@ -979,10 +979,19 @@ if(NOT ENABLE_EXTERNAL_MSPACK)
979 979
     add_subdirectory( libclammspack )
980 980
     set(LIBMSPACK "ClamAV::libmspack")
981 981
 else()
982
-    find_package(MSPack)
982
+    find_package(MSPack REQUIRED)
983 983
     set(LIBMSPACK "MSPack::mspack")
984 984
 endif()
985 985
 
986
+if(NOT ENABLE_EXTERNAL_TOMSFASTMATH)
987
+    # TomsFastMath built as an object library within libclamav, at least for now.
988
+    set(LIBTFM "tomsfastmath")
989
+else()
990
+    find_package(TomsFastMath REQUIRED)
991
+    set(HAVE_SYSTEM_TOMSFASTMATH 1)
992
+    set(LIBTFM "TomsFastMath::TomsFastMath")
993
+endif()
994
+
986 995
 if(WIN32)
987 996
     add_subdirectory( win32/compat )
988 997
 endif()
... ...
@@ -65,6 +65,9 @@ option(ENABLE_FUZZ
65 65
 option(ENABLE_EXTERNAL_MSPACK
66 66
     "Use external mspack instead of internal libclammspack.")
67 67
 
68
+option(ENABLE_EXTERNAL_TOMSFASTMATH
69
+    "Use external TomsFastMath instead of compiling vendored source into libclamav.")
70
+
68 71
 option(ENABLE_JSON_SHARED
69 72
     "Prefer linking with libjson-c shared library instead of static."
70 73
     ON)
... ...
@@ -118,6 +118,7 @@ libclamav requires these library dependencies:
118 118
 - `json-c`
119 119
 - `libjson-c` / `json-c`
120 120
 - `libmspack` (built-in by default, enable with `ENABLE_EXTERNAL_MSPACK=ON`)
121
+- `libtfm` (built-in by default, enable with `ENABLE_EXTERNAL_TOMSFASTMATH=ON`)
121 122
 - `libiconv` (built-in to `libc` 99% of the time, not requires on Windows)
122 123
 - `pthreads` (provided by Linux/Unix; requires `pthreads-win32` on Windows)
123 124
 - `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:
423 423
 
424 424
   _Default: `OFF`_
425 425
 
426
+- `ENABLE_EXTERNAL_TOMSFASTMATH`: Use external TomsFastMath instead of using
427
+  vendored TomsFastMath source compiled into libclamav.
428
+
429
+  _Default: `OFF`_
430
+
426 431
 - `ENABLE_JSON_SHARED`: Prefer linking with libjson-c shared library instead of
427 432
   static.
428 433
 
... ...
@@ -401,6 +401,9 @@
401 401
 /* Define if UNRAR is linked instead of loaded. */
402 402
 #cmakedefine UNRAR_LINKED 1
403 403
 
404
+/* Define if UNRAR is linked instead of loaded. */
405
+#cmakedefine HAVE_SYSTEM_TOMSFASTMATH 1
406
+
404 407
 /* "Full clamav library version number" */
405 408
 #define LIBCLAMAV_FULLVER "@LIBCLAMAV_VERSION@"
406 409
 
... ...
@@ -35,7 +35,7 @@ Cache Variables
35 35
 The following cache variables may also be set:
36 36
 
37 37
 ``MSPack_INCLUDE_DIR``
38
-  The directory containing ``foo.h``.
38
+  The directory containing ``mspack.h``.
39 39
 ``MSPack_LIBRARY``
40 40
   The path to the MSPack library.
41 41
 
42 42
new file mode 100644
... ...
@@ -0,0 +1,85 @@
0
+# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
1
+# file Copyright.txt or https://cmake.org/licensing for details.
2
+
3
+#[=======================================================================[.rst:
4
+FindTomsFastMath
5
+-------
6
+
7
+Finds the TomsFastMath library.
8
+
9
+Imported Targets
10
+^^^^^^^^^^^^^^^^
11
+
12
+This module provides the following imported targets, if found:
13
+
14
+``TomsFastMath::TomsFastMath``
15
+  The TomsFastMath library
16
+
17
+Result Variables
18
+^^^^^^^^^^^^^^^^
19
+
20
+This will define the following variables:
21
+
22
+``TomsFastMath_FOUND``
23
+  True if the system has the TomsFastMath library.
24
+``TomsFastMath_VERSION``
25
+  The version of the TomsFastMath library which was found.
26
+``TomsFastMath_INCLUDE_DIRS``
27
+  Include directories needed to use TomsFastMath.
28
+``TomsFastMath_LIBRARIES``
29
+  Libraries needed to link to TomsFastMath.
30
+
31
+Cache Variables
32
+^^^^^^^^^^^^^^^
33
+
34
+The following cache variables may also be set:
35
+
36
+``TomsFastMath_INCLUDE_DIR``
37
+  The directory containing ``tfm.h``.
38
+``TomsFastMath_LIBRARY``
39
+  The path to the TomsFastMath library.
40
+
41
+#]=======================================================================]
42
+
43
+find_package(PkgConfig QUIET)
44
+pkg_check_modules(PC_TomsFastMath QUIET toms)
45
+
46
+find_path(TomsFastMath_INCLUDE_DIR
47
+  NAMES tfm.h
48
+  PATHS ${PC_TomsFastMath_INCLUDE_DIRS}
49
+)
50
+find_library(TomsFastMath_LIBRARY
51
+  NAMES tfm
52
+  PATHS ${PC_TomsFastMath_LIBRARY_DIRS}
53
+)
54
+
55
+set(TomsFastMath_VERSION ${PC_TomsFastMath_VERSION})
56
+
57
+include(FindPackageHandleStandardArgs)
58
+find_package_handle_standard_args(TomsFastMath
59
+  FOUND_VAR TomsFastMath_FOUND
60
+  REQUIRED_VARS
61
+    TomsFastMath_LIBRARY
62
+    TomsFastMath_INCLUDE_DIR
63
+  VERSION_VAR TomsFastMath_VERSION
64
+)
65
+
66
+if(TomsFastMath_FOUND)
67
+  set(TomsFastMath_LIBRARIES ${TomsFastMath_LIBRARY})
68
+  set(TomsFastMath_INCLUDE_DIRS ${TomsFastMath_INCLUDE_DIR})
69
+  set(TomsFastMath_DEFINITIONS ${PC_TomsFastMath_CFLAGS_OTHER})
70
+endif()
71
+
72
+if(TomsFastMath_FOUND AND NOT TARGET TomsFastMath::TomsFastMath)
73
+  add_library(TomsFastMath::TomsFastMath UNKNOWN IMPORTED)
74
+  set_target_properties(TomsFastMath::TomsFastMath PROPERTIES
75
+    IMPORTED_LOCATION "${TomsFastMath_LIBRARY}"
76
+    INTERFACE_COMPILE_OPTIONS "${PC_TomsFastMath_CFLAGS_OTHER}"
77
+    INTERFACE_INCLUDE_DIRECTORIES "${TomsFastMath_INCLUDE_DIR}"
78
+  )
79
+endif()
80
+
81
+mark_as_advanced(
82
+  TomsFastMath_INCLUDE_DIR
83
+  TomsFastMath_LIBRARY
84
+)
... ...
@@ -166,99 +166,101 @@ target_link_libraries( yara
166 166
         PCRE2::pcre2
167 167
         JSONC::jsonc )
168 168
 
169
-add_library( tomsfastmath OBJECT )
170
-target_sources( tomsfastmath
171
-    PRIVATE
172
-        tomsfastmath/addsub/fp_add.c
173
-        tomsfastmath/addsub/fp_add_d.c
174
-        tomsfastmath/addsub/fp_addmod.c
175
-        tomsfastmath/addsub/fp_cmp.c
176
-        tomsfastmath/addsub/fp_cmp_d.c
177
-        tomsfastmath/addsub/fp_cmp_mag.c
178
-        tomsfastmath/addsub/fp_sub.c
179
-        tomsfastmath/addsub/fp_sub_d.c
180
-        tomsfastmath/addsub/fp_submod.c
181
-        tomsfastmath/addsub/s_fp_add.c
182
-        tomsfastmath/addsub/s_fp_sub.c
183
-        tomsfastmath/bin/fp_radix_size.c
184
-        tomsfastmath/bin/fp_read_radix.c
185
-        tomsfastmath/bin/fp_read_signed_bin.c
186
-        tomsfastmath/bin/fp_read_unsigned_bin.c
187
-        tomsfastmath/bin/fp_reverse.c
188
-        tomsfastmath/bin/fp_s_rmap.c
189
-        tomsfastmath/bin/fp_signed_bin_size.c
190
-        tomsfastmath/bin/fp_to_signed_bin.c
191
-        tomsfastmath/bin/fp_to_unsigned_bin.c
192
-        tomsfastmath/bin/fp_toradix.c
193
-        tomsfastmath/bin/fp_toradix_n.c
194
-        tomsfastmath/bin/fp_unsigned_bin_size.c
195
-        tomsfastmath/bit/fp_cnt_lsb.c
196
-        tomsfastmath/bit/fp_count_bits.c
197
-        tomsfastmath/bit/fp_div_2.c
198
-        tomsfastmath/bit/fp_div_2d.c
199
-        tomsfastmath/bit/fp_lshd.c
200
-        tomsfastmath/bit/fp_mod_2d.c
201
-        tomsfastmath/bit/fp_rshd.c
202
-        tomsfastmath/divide/fp_div.c
203
-        tomsfastmath/divide/fp_div_d.c
204
-        tomsfastmath/divide/fp_mod.c
205
-        tomsfastmath/divide/fp_mod_d.c
206
-        tomsfastmath/exptmod/fp_2expt.c
207
-        tomsfastmath/exptmod/fp_exptmod.c
208
-        tomsfastmath/misc/fp_ident.c
209
-        tomsfastmath/misc/fp_set.c
210
-        tomsfastmath/mont/fp_montgomery_calc_normalization.c
211
-        tomsfastmath/mont/fp_montgomery_reduce.c
212
-        tomsfastmath/mont/fp_montgomery_setup.c
213
-        tomsfastmath/mul/fp_mul.c
214
-        tomsfastmath/mul/fp_mul_comba.c
215
-        tomsfastmath/mul/fp_mul_2.c
216
-        tomsfastmath/mul/fp_mul_2d.c
217
-        tomsfastmath/mul/fp_mul_comba_12.c
218
-        tomsfastmath/mul/fp_mul_comba_17.c
219
-        tomsfastmath/mul/fp_mul_comba_20.c
220
-        tomsfastmath/mul/fp_mul_comba_24.c
221
-        tomsfastmath/mul/fp_mul_comba_28.c
222
-        tomsfastmath/mul/fp_mul_comba_3.c
223
-        tomsfastmath/mul/fp_mul_comba_32.c
224
-        tomsfastmath/mul/fp_mul_comba_4.c
225
-        tomsfastmath/mul/fp_mul_comba_48.c
226
-        tomsfastmath/mul/fp_mul_comba_6.c
227
-        tomsfastmath/mul/fp_mul_comba_64.c
228
-        tomsfastmath/mul/fp_mul_comba_7.c
229
-        tomsfastmath/mul/fp_mul_comba_8.c
230
-        tomsfastmath/mul/fp_mul_comba_9.c
231
-        tomsfastmath/mul/fp_mul_comba_small_set.c
232
-        tomsfastmath/mul/fp_mul_d.c
233
-        tomsfastmath/mul/fp_mulmod.c
234
-        tomsfastmath/numtheory/fp_invmod.c
235
-        tomsfastmath/sqr/fp_sqr.c
236
-        tomsfastmath/sqr/fp_sqr_comba_12.c
237
-        tomsfastmath/sqr/fp_sqr_comba_17.c
238
-        tomsfastmath/sqr/fp_sqr_comba_20.c
239
-        tomsfastmath/sqr/fp_sqr_comba_24.c
240
-        tomsfastmath/sqr/fp_sqr_comba_28.c
241
-        tomsfastmath/sqr/fp_sqr_comba_3.c
242
-        tomsfastmath/sqr/fp_sqr_comba_32.c
243
-        tomsfastmath/sqr/fp_sqr_comba_4.c
244
-        tomsfastmath/sqr/fp_sqr_comba_48.c
245
-        tomsfastmath/sqr/fp_sqr_comba_6.c
246
-        tomsfastmath/sqr/fp_sqr_comba_64.c
247
-        tomsfastmath/sqr/fp_sqr_comba_7.c
248
-        tomsfastmath/sqr/fp_sqr_comba_8.c
249
-        tomsfastmath/sqr/fp_sqr_comba_9.c
250
-        tomsfastmath/sqr/fp_sqr_comba_generic.c
251
-        tomsfastmath/sqr/fp_sqr_comba_small_set.c
252
-        tomsfastmath/sqr/fp_sqrmod.c
253
-    PUBLIC
254
-        bignum.h )
255
-target_include_directories( tomsfastmath
256
-    PRIVATE
257
-        ${CMAKE_BINARY_DIR}
258
-        ${CMAKE_CURRENT_SOURCE_DIR}/tomsfastmath/headers
259
-    PUBLIC  ${CMAKE_CURRENT_SOURCE_DIR} )
260
-set_target_properties( tomsfastmath PROPERTIES
261
-    COMPILE_FLAGS "${WARNCFLAGS}" )
169
+if(NOT ENABLE_EXTERNAL_TOMSFASTMATH)
170
+    add_library( tomsfastmath OBJECT )
171
+    target_sources( tomsfastmath
172
+        PRIVATE
173
+            tomsfastmath/addsub/fp_add.c
174
+            tomsfastmath/addsub/fp_add_d.c
175
+            tomsfastmath/addsub/fp_addmod.c
176
+            tomsfastmath/addsub/fp_cmp.c
177
+            tomsfastmath/addsub/fp_cmp_d.c
178
+            tomsfastmath/addsub/fp_cmp_mag.c
179
+            tomsfastmath/addsub/fp_sub.c
180
+            tomsfastmath/addsub/fp_sub_d.c
181
+            tomsfastmath/addsub/fp_submod.c
182
+            tomsfastmath/addsub/s_fp_add.c
183
+            tomsfastmath/addsub/s_fp_sub.c
184
+            tomsfastmath/bin/fp_radix_size.c
185
+            tomsfastmath/bin/fp_read_radix.c
186
+            tomsfastmath/bin/fp_read_signed_bin.c
187
+            tomsfastmath/bin/fp_read_unsigned_bin.c
188
+            tomsfastmath/bin/fp_reverse.c
189
+            tomsfastmath/bin/fp_s_rmap.c
190
+            tomsfastmath/bin/fp_signed_bin_size.c
191
+            tomsfastmath/bin/fp_to_signed_bin.c
192
+            tomsfastmath/bin/fp_to_unsigned_bin.c
193
+            tomsfastmath/bin/fp_toradix.c
194
+            tomsfastmath/bin/fp_toradix_n.c
195
+            tomsfastmath/bin/fp_unsigned_bin_size.c
196
+            tomsfastmath/bit/fp_cnt_lsb.c
197
+            tomsfastmath/bit/fp_count_bits.c
198
+            tomsfastmath/bit/fp_div_2.c
199
+            tomsfastmath/bit/fp_div_2d.c
200
+            tomsfastmath/bit/fp_lshd.c
201
+            tomsfastmath/bit/fp_mod_2d.c
202
+            tomsfastmath/bit/fp_rshd.c
203
+            tomsfastmath/divide/fp_div.c
204
+            tomsfastmath/divide/fp_div_d.c
205
+            tomsfastmath/divide/fp_mod.c
206
+            tomsfastmath/divide/fp_mod_d.c
207
+            tomsfastmath/exptmod/fp_2expt.c
208
+            tomsfastmath/exptmod/fp_exptmod.c
209
+            tomsfastmath/misc/fp_ident.c
210
+            tomsfastmath/misc/fp_set.c
211
+            tomsfastmath/mont/fp_montgomery_calc_normalization.c
212
+            tomsfastmath/mont/fp_montgomery_reduce.c
213
+            tomsfastmath/mont/fp_montgomery_setup.c
214
+            tomsfastmath/mul/fp_mul.c
215
+            tomsfastmath/mul/fp_mul_comba.c
216
+            tomsfastmath/mul/fp_mul_2.c
217
+            tomsfastmath/mul/fp_mul_2d.c
218
+            tomsfastmath/mul/fp_mul_comba_12.c
219
+            tomsfastmath/mul/fp_mul_comba_17.c
220
+            tomsfastmath/mul/fp_mul_comba_20.c
221
+            tomsfastmath/mul/fp_mul_comba_24.c
222
+            tomsfastmath/mul/fp_mul_comba_28.c
223
+            tomsfastmath/mul/fp_mul_comba_3.c
224
+            tomsfastmath/mul/fp_mul_comba_32.c
225
+            tomsfastmath/mul/fp_mul_comba_4.c
226
+            tomsfastmath/mul/fp_mul_comba_48.c
227
+            tomsfastmath/mul/fp_mul_comba_6.c
228
+            tomsfastmath/mul/fp_mul_comba_64.c
229
+            tomsfastmath/mul/fp_mul_comba_7.c
230
+            tomsfastmath/mul/fp_mul_comba_8.c
231
+            tomsfastmath/mul/fp_mul_comba_9.c
232
+            tomsfastmath/mul/fp_mul_comba_small_set.c
233
+            tomsfastmath/mul/fp_mul_d.c
234
+            tomsfastmath/mul/fp_mulmod.c
235
+            tomsfastmath/numtheory/fp_invmod.c
236
+            tomsfastmath/sqr/fp_sqr.c
237
+            tomsfastmath/sqr/fp_sqr_comba_12.c
238
+            tomsfastmath/sqr/fp_sqr_comba_17.c
239
+            tomsfastmath/sqr/fp_sqr_comba_20.c
240
+            tomsfastmath/sqr/fp_sqr_comba_24.c
241
+            tomsfastmath/sqr/fp_sqr_comba_28.c
242
+            tomsfastmath/sqr/fp_sqr_comba_3.c
243
+            tomsfastmath/sqr/fp_sqr_comba_32.c
244
+            tomsfastmath/sqr/fp_sqr_comba_4.c
245
+            tomsfastmath/sqr/fp_sqr_comba_48.c
246
+            tomsfastmath/sqr/fp_sqr_comba_6.c
247
+            tomsfastmath/sqr/fp_sqr_comba_64.c
248
+            tomsfastmath/sqr/fp_sqr_comba_7.c
249
+            tomsfastmath/sqr/fp_sqr_comba_8.c
250
+            tomsfastmath/sqr/fp_sqr_comba_9.c
251
+            tomsfastmath/sqr/fp_sqr_comba_generic.c
252
+            tomsfastmath/sqr/fp_sqr_comba_small_set.c
253
+            tomsfastmath/sqr/fp_sqrmod.c
254
+        PUBLIC
255
+            bignum.h )
256
+    target_include_directories( tomsfastmath
257
+        PRIVATE
258
+            ${CMAKE_BINARY_DIR}
259
+            ${CMAKE_CURRENT_SOURCE_DIR}/tomsfastmath/headers
260
+        PUBLIC  ${CMAKE_CURRENT_SOURCE_DIR} )
261
+    set_target_properties( tomsfastmath PROPERTIES
262
+        COMPILE_FLAGS "${WARNCFLAGS}" )
263
+endif()
262 264
 
263 265
 # Bytecode Runtime
264 266
 add_library( bytecode_runtime OBJECT )
... ...
@@ -527,7 +529,7 @@ if(ENABLE_SHARED_LIB)
527 527
             regex
528 528
             lzma_sdk
529 529
             yara
530
-            tomsfastmath
530
+            ${LIBTFM}
531 531
             bytecode_runtime
532 532
             ${LIBMSPACK}
533 533
             ClamAV::libclamav_rust
... ...
@@ -637,7 +639,7 @@ if(ENABLE_STATIC_LIB)
637 637
             regex
638 638
             lzma_sdk
639 639
             yara
640
-            tomsfastmath
640
+            ${LIBTFM}
641 641
             bytecode_runtime
642 642
             ${LIBMSPACK}
643 643
             ClamAV::libclamav_rust
... ...
@@ -52,7 +52,7 @@ if(ENABLE_APP)
52 52
             regex
53 53
             lzma_sdk
54 54
             yara
55
-            tomsfastmath
55
+            ${LIBTFM}
56 56
             bytecode_runtime
57 57
             JSONC::jsonc
58 58
             ${LIBMSPACK}
... ...
@@ -92,7 +92,7 @@ if(ENABLE_APP)
92 92
             regex
93 93
             lzma_sdk
94 94
             yara
95
-            tomsfastmath
95
+            ${LIBTFM}
96 96
             bytecode_runtime
97 97
             JSONC::jsonc
98 98
             ${LIBMSPACK}
... ...
@@ -144,7 +144,7 @@ target_link_libraries(check_clamav
144 144
         regex
145 145
         lzma_sdk
146 146
         yara
147
-        tomsfastmath
147
+        ${LIBTFM}
148 148
         bytecode_runtime
149 149
         JSONC::jsonc
150 150
         ${LIBMSPACK}
... ...
@@ -198,6 +198,10 @@ if(WIN32)
198 198
     file(TO_NATIVE_PATH $<TARGET_FILE:PThreadW32::pthreadw32>                   LIBPTHREADW32)
199 199
     file(TO_NATIVE_PATH $<TARGET_FILE:ClamAV::win32_compat>                     LIBWIN32COMPAT)
200 200
 
201
+    if (ENABLE_EXTERNAL_TOMSFASTMATH)
202
+        file(TO_NATIVE_PATH $<TARGET_FILE:${LIBTFM}>                            LIBTFM_PATH)
203
+    endif()
204
+
201 205
     if(ENABLE_STATIC_LIB)
202 206
         file(TO_NATIVE_PATH $<TARGET_FILE:clamav_static>                        LIBCLAMAV)
203 207
         file(TO_NATIVE_PATH $<TARGET_FILE:clammspack_static>                    LIBCLAMMSPACK)
... ...
@@ -230,7 +234,10 @@ else()
230 230
     if (ENABLE_UNRAR)
231 231
         set(LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:$<TARGET_FILE_DIR:ClamAV::libunrar_iface>:$<TARGET_FILE_DIR:ClamAV::libunrar>)
232 232
     endif()
233
-
233
+    if (ENABLE_EXTERNAL_TOMSFASTMATH)
234
+        set(LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:$<TARGET_FILE_DIR:${LIBTFM}>)
235
+        set(LIBTFM_PATH     $<TARGET_FILE:${LIBTFM}>)
236
+    endif()
234 237
 
235 238
     set(SOURCE             ${CMAKE_SOURCE_DIR})
236 239
     set(BUILD              ${CMAKE_BINARY_DIR})
... ...
@@ -302,6 +309,7 @@ set(ENVIRONMENT
302 302
     LIBCURL=${LIBCURL}
303 303
     LIBJSONC=${LIBJSONC}
304 304
     LIBICONV=${LIBICONV}
305
+    LIBTFM=${LIBTFM_PATH}
305 306
     LIBPTHREADW32=${LIBPTHREADW32}
306 307
     LIBWIN32COMPAT=${LIBWIN32COMPAT}
307 308
     LIBCLAMAV=${LIBCLAMAV}