@@ -2,14 +2,22 @@ cmake_minimum_required(VERSION 3.16.3) # version on Ubuntu Focal
2
2
3
3
project (behaviortree_cpp VERSION 4.6.2 LANGUAGES C CXX)
4
4
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 )
6
14
option (ENABLE_FUZZING "Enable fuzzing builds" OFF )
7
15
option (USE_AFLPLUSPLUS "Use AFL++ instead of libFuzzer" OFF )
8
16
option (ENABLE_DEBUG "Enable debug build with full symbols" OFF )
17
+ option (FORCE_STATIC_LINKING "Force static linking of all dependencies" OFF )
9
18
10
19
set (BASE_FLAGS "" )
11
20
12
- # Debug build configuration
13
21
if (ENABLE_DEBUG)
14
22
list (APPEND BASE_FLAGS
15
23
-g3
@@ -21,12 +29,32 @@ endif()
21
29
22
30
# Fuzzing configuration
23
31
if (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 ()
28
54
endif ()
29
55
56
+ list (APPEND BASE_FLAGS -O2)
57
+
30
58
if (USE_AFLPLUSPLUS)
31
59
set (SANITIZER_FLAGS
32
60
-fsanitize=address,undefined
@@ -41,33 +69,47 @@ if(ENABLE_FUZZING)
41
69
# Apply sanitizer flags to the base library
42
70
list (APPEND BASE_FLAGS ${SANITIZER_FLAGS} )
43
71
44
- # Apply base flags globally
45
72
add_compile_options (${BASE_FLAGS} )
46
73
add_link_options (${BASE_FLAGS} )
47
74
48
75
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
52
84
${BASE_FLAGS}
53
85
${SANITIZER_FLAGS}
86
+ -static -libstdc++
87
+ -static -libgcc
88
+ -fsanitize=fuzzer
54
89
)
55
- target_link_options (${target} PRIVATE
90
+ else ()
91
+ target_link_options (${target} PRIVATE
56
92
${BASE_FLAGS}
57
- -fsanitize=fuzzer,address,undefined
93
+ -fsanitize=fuzzer
94
+ ${SANITIZER_FLAGS}
95
+ -static -libstdc++
96
+ -static -libgcc
58
97
)
98
+ endif ()
59
99
else ()
60
- # libFuzzer specific flags
61
- target_compile_options (${target} PRIVATE
100
+ if (USE_AFLPLUSPLUS)
101
+ target_link_options (${target} PRIVATE
62
102
${BASE_FLAGS}
63
- -fsanitize=fuzzer
64
103
${SANITIZER_FLAGS}
104
+ -fsanitize=fuzzer
65
105
)
66
- target_link_options (${target} PRIVATE
106
+ else ()
107
+ target_link_options (${target} PRIVATE
67
108
${BASE_FLAGS}
68
109
-fsanitize=fuzzer
69
110
${SANITIZER_FLAGS}
70
111
)
112
+ endif ()
71
113
endif ()
72
114
endfunction ()
73
115
@@ -99,17 +141,6 @@ else()
99
141
add_definitions (-Wpedantic -fno-omit-frame-pointer)
100
142
endif ()
101
143
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
-
113
144
if (USE_V3_COMPATIBLE_NAMES)
114
145
add_definitions (-DUSE_BTCPP3_OLD_NAMES)
115
146
endif ()
@@ -277,27 +308,31 @@ add_library(BT::${BTCPP_LIBRARY} ALIAS ${BTCPP_LIBRARY})
277
308
278
309
# Add fuzzing targets
279
310
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
311
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
+
293
329
set (CORPUS_DIR ${CMAKE_BINARY_DIR} /corpus/${fuzzer} )
294
330
file (MAKE_DIRECTORY ${CORPUS_DIR} )
295
331
endforeach ()
296
332
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/*" )
301
336
if (BT_CORPUS_FILES)
302
337
file (COPY ${BT_CORPUS_FILES} DESTINATION ${CMAKE_BINARY_DIR} /corpus/bt_fuzzer)
303
338
endif ()
0 commit comments