@@ -6,10 +6,10 @@ 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
12
- # Debug build configuration
13
13
if (ENABLE_DEBUG )
14
14
list (APPEND BASE_FLAGS
15
15
-g3
@@ -21,12 +21,32 @@ endif()
21
21
22
22
# Fuzzing configuration
23
23
if (ENABLE_FUZZING )
24
- if (USE_AFLPLUSPLUS )
25
- list (APPEND BASE_FLAGS -O3 )
26
- else ()
27
- list (APPEND BASE_FLAGS -O2 )
24
+ if (CMAKE_C_COMPILER MATCHES ".*afl-.*" OR CMAKE_CXX_COMPILER MATCHES ".*afl-.*" )
25
+ set (USE_AFLPLUSPLUS ON CACHE BOOL "Use AFL++ instead of libFuzzer" FORCE )
26
+ message (STATUS "AFL++ compiler detected - automatically enabling AFL++ mode" )
27
+ endif ()
28
+
29
+ # When building for fuzzing, we still want static library by default
30
+ set (BTCPP_SHARED_LIBS OFF CACHE BOOL "Build static library for fuzzing" FORCE )
31
+
32
+ # Only apply static linking settings if explicitly requested
33
+ if (FORCE_STATIC_LINKING )
34
+ set (CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES} )
35
+ set (BUILD_SHARED_LIBS OFF )
36
+
37
+ # Force static linking for dependencies
38
+ if (BTCPP_GROOT_INTERFACE )
39
+ set (ZeroMQ_USE_STATIC_LIBS ON )
40
+ set (ZEROMQ_STATIC_LIBRARY ON )
41
+ endif ()
42
+
43
+ if (BTCPP_SQLITE_LOGGING )
44
+ set (SQLite3_USE_STATIC_LIBS ON )
45
+ endif ()
28
46
endif ()
29
47
48
+ list (APPEND BASE_FLAGS -O2 )
49
+
30
50
if (USE_AFLPLUSPLUS )
31
51
set (SANITIZER_FLAGS
32
52
-fsanitize=address,undefined
@@ -41,33 +61,47 @@ if(ENABLE_FUZZING)
41
61
# Apply sanitizer flags to the base library
42
62
list (APPEND BASE_FLAGS ${SANITIZER_FLAGS} )
43
63
44
- # Apply base flags globally
45
64
add_compile_options (${BASE_FLAGS} )
46
65
add_link_options (${BASE_FLAGS} )
47
66
48
67
function (apply_fuzzing_flags target )
49
- if (USE_AFLPLUSPLUS )
50
- # AFL++ specific flags
51
- target_compile_options (${target} PRIVATE
68
+ target_compile_options (${target} PRIVATE
69
+ ${BASE_FLAGS}
70
+ ${SANITIZER_FLAGS}
71
+ )
72
+
73
+ if (FORCE_STATIC_LINKING )
74
+ if (USE_AFLPLUSPLUS )
75
+ target_link_options (${target} PRIVATE
52
76
${BASE_FLAGS}
53
77
${SANITIZER_FLAGS}
78
+ -static-libstdc++
79
+ -static-libgcc
80
+ -fsanitize=fuzzer
54
81
)
55
- target_link_options (${target} PRIVATE
82
+ else ()
83
+ target_link_options (${target} PRIVATE
56
84
${BASE_FLAGS}
57
- -fsanitize=fuzzer,address,undefined
85
+ -fsanitize=fuzzer
86
+ ${SANITIZER_FLAGS}
87
+ -static-libstdc++
88
+ -static-libgcc
58
89
)
90
+ endif ()
59
91
else ()
60
- # libFuzzer specific flags
61
- target_compile_options (${target} PRIVATE
92
+ if ( USE_AFLPLUSPLUS )
93
+ target_link_options (${target} PRIVATE
62
94
${BASE_FLAGS}
63
- -fsanitize=fuzzer
64
95
${SANITIZER_FLAGS}
96
+ -fsanitize=fuzzer
65
97
)
66
- target_link_options (${target} PRIVATE
98
+ else ()
99
+ target_link_options (${target} PRIVATE
67
100
${BASE_FLAGS}
68
101
-fsanitize=fuzzer
69
102
${SANITIZER_FLAGS}
70
103
)
104
+ endif ()
71
105
endif ()
72
106
endfunction ()
73
107
@@ -277,27 +311,31 @@ add_library(BT::${BTCPP_LIBRARY} ALIAS ${BTCPP_LIBRARY})
277
311
278
312
# Add fuzzing targets
279
313
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
314
foreach (fuzzer bt_fuzzer script_fuzzer bb_fuzzer )
315
+ add_executable (${fuzzer} fuzzing/${fuzzer}.cpp )
316
+ apply_fuzzing_flags (${fuzzer} )
317
+
318
+ if (FORCE_STATIC_LINKING )
319
+ target_link_libraries (${fuzzer} PRIVATE
320
+ -static-libstdc++
321
+ -static-libgcc
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