@@ -2,14 +2,22 @@ cmake_minimum_required(VERSION 3.16.3) # version on Ubuntu Focal
22
33project (behaviortree_cpp VERSION 4.6.2 LANGUAGES C CXX)
44
5- # Build configuration options
5+ #---- project configuration ----
6+ option (BTCPP_SHARED_LIBS "Build shared libraries" ON )
7+ option (BTCPP_BUILD_TOOLS "Build commandline tools" ON )
8+ option (BTCPP_EXAMPLES "Build tutorials and examples" ON )
9+ option (BTCPP_UNIT_TESTS "Build the unit tests" ON )
10+ option (BTCPP_GROOT_INTERFACE "Add Groot2 connection. Requires ZeroMQ" ON )
11+ option (BTCPP_SQLITE_LOGGING "Add SQLite logging." ON )
12+
13+ option (USE_V3_COMPATIBLE_NAMES "Use some alias to compile more easily old 3.x code" OFF )
614option (ENABLE_FUZZING "Enable fuzzing builds" OFF )
715option (USE_AFLPLUSPLUS "Use AFL++ instead of libFuzzer" OFF )
816option (ENABLE_DEBUG "Enable debug build with full symbols" OFF )
17+ option (FORCE_STATIC_LINKING "Force static linking of all dependencies" OFF )
918
1019set (BASE_FLAGS "" )
1120
12- # Debug build configuration
1321if (ENABLE_DEBUG)
1422 list (APPEND BASE_FLAGS
1523 -g3
@@ -21,12 +29,32 @@ endif()
2129
2230# Fuzzing configuration
2331if (ENABLE_FUZZING)
24- if (USE_AFLPLUSPLUS)
25- list (APPEND BASE_FLAGS -O3)
26- else ()
27- list (APPEND BASE_FLAGS -O2)
32+ if (CMAKE_C_COMPILER MATCHES ".*afl-.*" OR CMAKE_CXX_COMPILER MATCHES ".*afl-.*" )
33+ set (USE_AFLPLUSPLUS ON CACHE BOOL "Use AFL++ instead of libFuzzer" FORCE)
34+ message (STATUS "AFL++ compiler detected - automatically enabling AFL++ mode" )
35+ endif ()
36+
37+ # When building for fuzzing, we still want static library by default
38+ set (BTCPP_SHARED_LIBS OFF CACHE BOOL "Build static library for fuzzing" FORCE)
39+
40+ # Only apply static linking settings if explicitly requested
41+ if (FORCE_STATIC_LINKING)
42+ set (CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES} )
43+ set (BUILD_SHARED_LIBS OFF )
44+
45+ # Force static linking for dependencies
46+ if (BTCPP_GROOT_INTERFACE)
47+ set (ZeroMQ_USE_STATIC_LIBS ON )
48+ set (ZEROMQ_STATIC_LIBRARY ON )
49+ endif ()
50+
51+ if (BTCPP_SQLITE_LOGGING)
52+ set (SQLite3_USE_STATIC_LIBS ON )
53+ endif ()
2854 endif ()
2955
56+ list (APPEND BASE_FLAGS -O2)
57+
3058 if (USE_AFLPLUSPLUS)
3159 set (SANITIZER_FLAGS
3260 -fsanitize=address,undefined
@@ -41,33 +69,47 @@ if(ENABLE_FUZZING)
4169 # Apply sanitizer flags to the base library
4270 list (APPEND BASE_FLAGS ${SANITIZER_FLAGS} )
4371
44- # Apply base flags globally
4572 add_compile_options (${BASE_FLAGS} )
4673 add_link_options (${BASE_FLAGS} )
4774
4875 function (apply_fuzzing_flags target )
49- if (USE_AFLPLUSPLUS)
50- # AFL++ specific flags
51- target_compile_options (${target} PRIVATE
76+ target_compile_options (${target} PRIVATE
77+ ${BASE_FLAGS}
78+ ${SANITIZER_FLAGS}
79+ )
80+
81+ if (FORCE_STATIC_LINKING)
82+ if (USE_AFLPLUSPLUS)
83+ target_link_options (${target} PRIVATE
5284 ${BASE_FLAGS}
5385 ${SANITIZER_FLAGS}
86+ -static -libstdc++
87+ -static -libgcc
88+ -fsanitize=fuzzer
5489 )
55- target_link_options (${target} PRIVATE
90+ else ()
91+ target_link_options (${target} PRIVATE
5692 ${BASE_FLAGS}
57- -fsanitize=fuzzer,address,undefined
93+ -fsanitize=fuzzer
94+ ${SANITIZER_FLAGS}
95+ -static -libstdc++
96+ -static -libgcc
5897 )
98+ endif ()
5999 else ()
60- # libFuzzer specific flags
61- target_compile_options (${target} PRIVATE
100+ if (USE_AFLPLUSPLUS)
101+ target_link_options (${target} PRIVATE
62102 ${BASE_FLAGS}
63- -fsanitize=fuzzer
64103 ${SANITIZER_FLAGS}
104+ -fsanitize=fuzzer
65105 )
66- target_link_options (${target} PRIVATE
106+ else ()
107+ target_link_options (${target} PRIVATE
67108 ${BASE_FLAGS}
68109 -fsanitize=fuzzer
69110 ${SANITIZER_FLAGS}
70111 )
112+ endif ()
71113 endif ()
72114 endfunction ()
73115
@@ -99,17 +141,6 @@ else()
99141 add_definitions (-Wpedantic -fno-omit-frame-pointer)
100142endif ()
101143
102-
103- #---- project configuration ----
104- option (BTCPP_SHARED_LIBS "Build shared libraries" ON )
105- option (BTCPP_BUILD_TOOLS "Build commandline tools" ON )
106- option (BTCPP_EXAMPLES "Build tutorials and examples" ON )
107- option (BTCPP_UNIT_TESTS "Build the unit tests" ON )
108- option (BTCPP_GROOT_INTERFACE "Add Groot2 connection. Requires ZeroMQ" ON )
109- option (BTCPP_SQLITE_LOGGING "Add SQLite logging." ON )
110-
111- option (USE_V3_COMPATIBLE_NAMES "Use some alias to compile more easily old 3.x code" OFF )
112-
113144if (USE_V3_COMPATIBLE_NAMES)
114145 add_definitions (-DUSE_BTCPP3_OLD_NAMES)
115146endif ()
@@ -277,27 +308,31 @@ add_library(BT::${BTCPP_LIBRARY} ALIAS ${BTCPP_LIBRARY})
277308
278309# Add fuzzing targets
279310if (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-
292311 foreach (fuzzer bt_fuzzer script_fuzzer bb_fuzzer)
312+ add_executable (${fuzzer} fuzzing/${fuzzer} .cpp)
313+ apply_fuzzing_flags(${fuzzer} )
314+
315+ if (FORCE_STATIC_LINKING)
316+ target_link_libraries (${fuzzer} PRIVATE
317+ -static -libstdc++
318+ -static -libgcc
319+ ${BTCPP_LIBRARY}
320+ ${BTCPP_EXTRA_LIBRARIES}
321+ )
322+ else ()
323+ target_link_libraries (${fuzzer} PRIVATE
324+ ${BTCPP_LIBRARY}
325+ ${BTCPP_EXTRA_LIBRARIES}
326+ )
327+ endif ()
328+
293329 set (CORPUS_DIR ${CMAKE_BINARY_DIR} /corpus/${fuzzer} )
294330 file (MAKE_DIRECTORY ${CORPUS_DIR} )
295331 endforeach ()
296332
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-
333+ file (GLOB BT_CORPUS_FILES "${CMAKE_SOURCE_DIR} /fuzzing/corpus/bt_corpus/*" )
334+ file (GLOB SCRIPT_CORPUS_FILES "${CMAKE_SOURCE_DIR} /fuzzing/corpus/script_corpus/*" )
335+ file (GLOB BB_CORPUS_FILES "${CMAKE_SOURCE_DIR} /fuzzing/corpus/bb_corpus/*" )
301336 if (BT_CORPUS_FILES)
302337 file (COPY ${BT_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR} /corpus/bt_fuzzer)
303338 endif ()
0 commit comments