@@ -6,6 +6,7 @@ project(behaviortree_cpp VERSION 4.6.2 LANGUAGES C CXX)
66option (ENABLE_FUZZING "Enable fuzzing builds" OFF )
77option (USE_AFLPLUSPLUS "Use AFL++ instead of libFuzzer" OFF )
88option (ENABLE_DEBUG "Enable debug build with full symbols" OFF )
9+ option (FORCE_STATIC_LINKING "Force static linking of all dependencies" OFF )
910
1011set (BASE_FLAGS "" )
1112
@@ -21,6 +22,25 @@ endif()
2122
2223# Fuzzing configuration
2324if (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+
2444 if (USE_AFLPLUSPLUS)
2545 list (APPEND BASE_FLAGS -O3)
2646 else ()
@@ -46,28 +66,43 @@ if(ENABLE_FUZZING)
4666 add_link_options (${BASE_FLAGS} )
4767
4868 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
5277 ${BASE_FLAGS}
5378 ${SANITIZER_FLAGS}
79+ -static -libstdc++
80+ -static -libgcc
81+ -fsanitize=fuzzer
5482 )
55- target_link_options (${target} PRIVATE
83+ else ()
84+ target_link_options (${target} PRIVATE
5685 ${BASE_FLAGS}
57- -fsanitize=fuzzer,address,undefined
86+ -fsanitize=fuzzer
87+ ${SANITIZER_FLAGS}
88+ -static -libstdc++
89+ -static -libgcc
5890 )
91+ endif ()
5992 else ()
60- # libFuzzer specific flags
61- target_compile_options (${target} PRIVATE
93+ if (USE_AFLPLUSPLUS)
94+ target_link_options (${target} PRIVATE
6295 ${BASE_FLAGS}
63- -fsanitize=fuzzer
6496 ${SANITIZER_FLAGS}
97+ -fsanitize=fuzzer
6598 )
66- target_link_options (${target} PRIVATE
99+ else ()
100+ target_link_options (${target} PRIVATE
67101 ${BASE_FLAGS}
68102 -fsanitize=fuzzer
69103 ${SANITIZER_FLAGS}
70104 )
105+ endif ()
71106 endif ()
72107 endfunction ()
73108
@@ -277,27 +312,30 @@ add_library(BT::${BTCPP_LIBRARY} ALIAS ${BTCPP_LIBRARY})
277312
278313# Add fuzzing targets
279314if (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-
292315 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+
293332 set (CORPUS_DIR ${CMAKE_BINARY_DIR} /corpus/${fuzzer} )
294333 file (MAKE_DIRECTORY ${CORPUS_DIR} )
295334 endforeach ()
296335
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/*" )
301339 if (BT_CORPUS_FILES)
302340 file (COPY ${BT_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR} /corpus/bt_fuzzer)
303341 endif ()
0 commit comments