@@ -6,6 +6,7 @@ project(behaviortree_cpp VERSION 4.6.2 LANGUAGES C CXX)
6
6
option (ENABLE_FUZZING "Enable fuzzing builds" OFF )
7
7
option (USE_AFLPLUSPLUS "Use AFL++ instead of libFuzzer" OFF )
8
8
option (ENABLE_DEBUG "Enable debug build with full symbols" OFF )
9
+ option (FORCE_STATIC_LINKING "Force static linking of all dependencies" OFF )
9
10
10
11
set (BASE_FLAGS "" )
11
12
@@ -21,6 +22,25 @@ endif()
21
22
22
23
# Fuzzing configuration
23
24
if (ENABLE_FUZZING)
25
+ # When building for fuzzing, we still want static library by default
26
+ set (BTCPP_SHARED_LIBS OFF CACHE BOOL "Build static library for fuzzing" FORCE)
27
+
28
+ # Only apply static linking settings if explicitly requested
29
+ if (FORCE_STATIC_LINKING)
30
+ set (CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES} )
31
+ set (BUILD_SHARED_LIBS OFF )
32
+
33
+ # Force static linking for dependencies
34
+ if (BTCPP_GROOT_INTERFACE)
35
+ set (ZeroMQ_USE_STATIC_LIBS ON )
36
+ set (ZEROMQ_STATIC_LIBRARY ON )
37
+ endif ()
38
+
39
+ if (BTCPP_SQLITE_LOGGING)
40
+ set (SQLite3_USE_STATIC_LIBS ON )
41
+ endif ()
42
+ endif ()
43
+
24
44
if (USE_AFLPLUSPLUS)
25
45
list (APPEND BASE_FLAGS -O3)
26
46
else ()
@@ -46,28 +66,43 @@ if(ENABLE_FUZZING)
46
66
add_link_options (${BASE_FLAGS} )
47
67
48
68
function (apply_fuzzing_flags target )
49
- if (USE_AFLPLUSPLUS)
50
- # AFL++ specific flags
51
- target_compile_options (${target} PRIVATE
69
+ target_compile_options (${target} PRIVATE
70
+ ${BASE_FLAGS}
71
+ ${SANITIZER_FLAGS}
72
+ )
73
+
74
+ if (FORCE_STATIC_LINKING)
75
+ if (USE_AFLPLUSPLUS)
76
+ target_link_options (${target} PRIVATE
52
77
${BASE_FLAGS}
53
78
${SANITIZER_FLAGS}
79
+ -static -libstdc++
80
+ -static -libgcc
81
+ -fsanitize=fuzzer
54
82
)
55
- target_link_options (${target} PRIVATE
83
+ else ()
84
+ target_link_options (${target} PRIVATE
56
85
${BASE_FLAGS}
57
- -fsanitize=fuzzer,address,undefined
86
+ -fsanitize=fuzzer
87
+ ${SANITIZER_FLAGS}
88
+ -static -libstdc++
89
+ -static -libgcc
58
90
)
91
+ endif ()
59
92
else ()
60
- # libFuzzer specific flags
61
- target_compile_options (${target} PRIVATE
93
+ if (USE_AFLPLUSPLUS)
94
+ target_link_options (${target} PRIVATE
62
95
${BASE_FLAGS}
63
- -fsanitize=fuzzer
64
96
${SANITIZER_FLAGS}
97
+ -fsanitize=fuzzer
65
98
)
66
- target_link_options (${target} PRIVATE
99
+ else ()
100
+ target_link_options (${target} PRIVATE
67
101
${BASE_FLAGS}
68
102
-fsanitize=fuzzer
69
103
${SANITIZER_FLAGS}
70
104
)
105
+ endif ()
71
106
endif ()
72
107
endfunction ()
73
108
@@ -277,27 +312,30 @@ add_library(BT::${BTCPP_LIBRARY} ALIAS ${BTCPP_LIBRARY})
277
312
278
313
# Add fuzzing targets
279
314
if (ENABLE_FUZZING)
280
- add_executable (bt_fuzzer fuzzing/bt_fuzzer.cpp)
281
- apply_fuzzing_flags(bt_fuzzer)
282
- target_link_libraries (bt_fuzzer PRIVATE ${BTCPP_LIBRARY} ${BTCPP_EXTRA_LIBRARIES} )
283
-
284
- add_executable (script_fuzzer fuzzing/script_fuzzer.cpp)
285
- apply_fuzzing_flags(script_fuzzer)
286
- target_link_libraries (script_fuzzer PRIVATE ${BTCPP_LIBRARY} ${BTCPP_EXTRA_LIBRARIES} )
287
-
288
- add_executable (bb_fuzzer fuzzing/bb_fuzzer.cpp)
289
- apply_fuzzing_flags(bb_fuzzer)
290
- target_link_libraries (bb_fuzzer PRIVATE ${BTCPP_LIBRARY} ${BTCPP_EXTRA_LIBRARIES} )
291
-
292
315
foreach (fuzzer bt_fuzzer script_fuzzer bb_fuzzer)
316
+ add_executable (${fuzzer} fuzzing/${fuzzer} .cpp)
317
+ apply_fuzzing_flags(${fuzzer} )
318
+
319
+ if (FORCE_STATIC_LINKING)
320
+ target_link_libraries (${fuzzer} PRIVATE
321
+ -static
322
+ ${BTCPP_LIBRARY}
323
+ ${BTCPP_EXTRA_LIBRARIES}
324
+ )
325
+ else ()
326
+ target_link_libraries (${fuzzer} PRIVATE
327
+ ${BTCPP_LIBRARY}
328
+ ${BTCPP_EXTRA_LIBRARIES}
329
+ )
330
+ endif ()
331
+
293
332
set (CORPUS_DIR ${CMAKE_BINARY_DIR} /corpus/${fuzzer} )
294
333
file (MAKE_DIRECTORY ${CORPUS_DIR} )
295
334
endforeach ()
296
335
297
- file (GLOB BT_CORPUS_FILES "fuzzing/corpus/bt_fuzzer/*" )
298
- file (GLOB SCRIPT_CORPUS_FILES "fuzzing/corpus/script_fuzzer/*" )
299
- file (GLOB BB_CORPUS_FILES "fuzzing/corpus/bb_fuzzer/*" )
300
-
336
+ file (GLOB BT_CORPUS_FILES "${CMAKE_SOURCE_DIR} /fuzzing/corpus/bt_corpus/*" )
337
+ file (GLOB SCRIPT_CORPUS_FILES "${CMAKE_SOURCE_DIR} /fuzzing/corpus/script_corpus/*" )
338
+ file (GLOB BB_CORPUS_FILES "${CMAKE_SOURCE_DIR} /fuzzing/corpus/bb_corpus/*" )
301
339
if (BT_CORPUS_FILES)
302
340
file (COPY ${BT_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR} /corpus/bt_fuzzer)
303
341
endif ()
0 commit comments