Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 316190b

Browse files
author
cktii
committedFeb 5, 2025
fix: prepare static harnesses
1 parent 0700e52 commit 316190b

File tree

1 file changed

+69
-31
lines changed

1 file changed

+69
-31
lines changed
 

‎CMakeLists.txt

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ project(behaviortree_cpp VERSION 4.6.2 LANGUAGES C CXX)
66
option(ENABLE_FUZZING "Enable fuzzing builds" OFF)
77
option(USE_AFLPLUSPLUS "Use AFL++ instead of libFuzzer" OFF)
88
option(ENABLE_DEBUG "Enable debug build with full symbols" OFF)
9+
option(FORCE_STATIC_LINKING "Force static linking of all dependencies" OFF)
910

1011
set(BASE_FLAGS "")
1112

12-
# Debug build configuration
1313
if(ENABLE_DEBUG)
1414
list(APPEND BASE_FLAGS
1515
-g3
@@ -21,12 +21,32 @@ endif()
2121

2222
# Fuzzing configuration
2323
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()
2846
endif()
2947

48+
list(APPEND BASE_FLAGS -O2)
49+
3050
if(USE_AFLPLUSPLUS)
3151
set(SANITIZER_FLAGS
3252
-fsanitize=address,undefined
@@ -41,33 +61,47 @@ if(ENABLE_FUZZING)
4161
# Apply sanitizer flags to the base library
4262
list(APPEND BASE_FLAGS ${SANITIZER_FLAGS})
4363

44-
# Apply base flags globally
4564
add_compile_options(${BASE_FLAGS})
4665
add_link_options(${BASE_FLAGS})
4766

4867
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
5276
${BASE_FLAGS}
5377
${SANITIZER_FLAGS}
78+
-static-libstdc++
79+
-static-libgcc
80+
-fsanitize=fuzzer
5481
)
55-
target_link_options(${target} PRIVATE
82+
else()
83+
target_link_options(${target} PRIVATE
5684
${BASE_FLAGS}
57-
-fsanitize=fuzzer,address,undefined
85+
-fsanitize=fuzzer
86+
${SANITIZER_FLAGS}
87+
-static-libstdc++
88+
-static-libgcc
5889
)
90+
endif()
5991
else()
60-
# libFuzzer specific flags
61-
target_compile_options(${target} PRIVATE
92+
if(USE_AFLPLUSPLUS)
93+
target_link_options(${target} PRIVATE
6294
${BASE_FLAGS}
63-
-fsanitize=fuzzer
6495
${SANITIZER_FLAGS}
96+
-fsanitize=fuzzer
6597
)
66-
target_link_options(${target} PRIVATE
98+
else()
99+
target_link_options(${target} PRIVATE
67100
${BASE_FLAGS}
68101
-fsanitize=fuzzer
69102
${SANITIZER_FLAGS}
70103
)
104+
endif()
71105
endif()
72106
endfunction()
73107

@@ -277,27 +311,31 @@ add_library(BT::${BTCPP_LIBRARY} ALIAS ${BTCPP_LIBRARY})
277311

278312
# Add fuzzing targets
279313
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-
292314
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+
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

Comments
 (0)
Please sign in to comment.