diff --git a/CMakeLists.txt b/CMakeLists.txt index cf91d71c..7ad0ac3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,16 +11,31 @@ project(bpfilter include(GNUInstallDirs) -find_package(Doxygen REQUIRED) +option(BUILD_TESTING "Enable testing" OFF) +option(BUILD_DOCS "Enable building documentation" OFF) + +if(BUILD_DOCS) + find_package(Doxygen REQUIRED) +endif() find_package(PkgConfig REQUIRED) find_package(FLEX REQUIRED) find_package(BISON REQUIRED) pkg_check_modules(bpf REQUIRED IMPORTED_TARGET libbpf) pkg_check_modules(elf REQUIRED IMPORTED_TARGET libelf) -pkg_check_modules(cmocka REQUIRED IMPORTED_TARGET cmocka) +if(BUILD_TESTING) + pkg_check_modules(cmocka REQUIRED IMPORTED_TARGET cmocka) +endif() pkg_check_modules(nl REQUIRED IMPORTED_TARGET libnl-3.0) +find_library(ARGP_LIBRARY + NAMES argp + PATHS ${CMAKE_SYSROOT}/usr/lib + REQUIRED) +if(NOT ARGP_LIBRARY) + message(FATAL_ERROR "libargp not found") +endif() + # Required to get CMake to pass PIE flags to the compiler/linker include(CheckPIESupported) check_pie_supported() @@ -30,13 +45,34 @@ if(NOT CMAKE_C_LINK_PIE_SUPPORTED) "If PIE is enabled by default, test auto discovery might fail.") endif() -find_program(SPHINX_BIN sphinx-build REQUIRED) -find_program(LCOV_BIN lcov REQUIRED) -find_program(GENHTML_BIN genhtml REQUIRED) -find_program(CLANG_TIDY_BIN NAMES clang-tidy-18 clang-tidy REQUIRED) -find_program(CLANG_FORMAT_BIN NAMES clang-format-18 clang-format REQUIRED) +if(BUILD_DOCS) + find_program(SPHINX_BIN sphinx-build REQUIRED) + find_program(LCOV_BIN lcov REQUIRED) + find_program(GENHTML_BIN genhtml REQUIRED) + find_program(CLANG_TIDY_BIN NAMES clang-tidy-18 clang-tidy REQUIRED) + find_program(CLANG_FORMAT_BIN NAMES clang-format-18 clang-format REQUIRED) +endif() + +# Replace mandatory REQUIRED flag +find_program(BPFTOOL_BIN bpftool) + +# Add fallback paths +if(NOT BPFTOOL_BIN) + find_program(BPFTOOL_BIN bpftool + PATHS + ${CMAKE_SYSROOT}/usr/sbin + ${CMAKE_SYSROOT}/sbin + /usr/sbin + /sbin + ) +endif() + +# Make it optional with warning +if(NOT BPFTOOL_BIN) + message(WARNING "bpftool not found - some features may be disabled") + set(BPFTOOL_BIN "bpftool") # Set default value +endif() find_program(CLANG_BIN clang REQUIRED) -find_program(BPFTOOL_BIN bpftool REQUIRED) find_program(JQ_BIN jq REQUIRED) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) @@ -84,8 +120,12 @@ target_link_options(bf_global_flags ) add_subdirectory(src) -add_subdirectory(tests) -add_subdirectory(doc) +if(BUILD_DOCS) + add_subdirectory(doc) +endif() +if(BUILD_TESTING) + add_subdirectory(tests) +endif() install(TARGETS bpfilter libbpfilter_a libbpfilter_so bfcli) diff --git a/src/bfcli/CMakeLists.txt b/src/bfcli/CMakeLists.txt index 74946a45..57643d18 100644 --- a/src/bfcli/CMakeLists.txt +++ b/src/bfcli/CMakeLists.txt @@ -37,4 +37,5 @@ target_link_libraries(bfcli bf_global_flags core libbpfilter_a + ${ARGP_LIBRARY} ) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 0023b386..8c24dc78 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -40,6 +40,7 @@ target_link_libraries(core PUBLIC bf_global_flags PkgConfig::bpf + ${ARGP_LIBRARY} ) add_library(core.pic diff --git a/src/core/helper.h b/src/core/helper.h index b3af1ed9..f826f269 100644 --- a/src/core/helper.h +++ b/src/core/helper.h @@ -13,8 +13,6 @@ #include #include -extern const char *strerrordesc_np(int errnum); - #define bf_packed __attribute__((packed)) #define bf_aligned(x) __attribute__((aligned(x))) #define bf_unused __attribute__((unused)) @@ -80,7 +78,7 @@ extern const char *strerrordesc_np(int errnum); * * @param v Error code, can be positive or negative. */ -#define bf_strerror(v) strerrordesc_np(abs(v)) +#define bf_strerror(v) strerror(abs(v)) /** * Swap two values. diff --git a/tests/benchmark/CMakeLists.txt b/tests/benchmark/CMakeLists.txt index 8a23a631..dc97c138 100644 --- a/tests/benchmark/CMakeLists.txt +++ b/tests/benchmark/CMakeLists.txt @@ -29,6 +29,7 @@ target_link_libraries(benchmark_bin PRIVATE PkgConfig::bpf PkgConfig::git2 + ${ARGP_LIBRARY} benchmark::benchmark ) diff --git a/tests/e2e/CMakeLists.txt b/tests/e2e/CMakeLists.txt index fa676181..8dd85234 100644 --- a/tests/e2e/CMakeLists.txt +++ b/tests/e2e/CMakeLists.txt @@ -30,6 +30,7 @@ target_link_libraries(e2e_bin PRIVATE harness libbpfilter_a + ${ARGP_LIBRARY} ) add_custom_target(e2e diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 2ed6be14..275cac80 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -131,6 +131,7 @@ target_link_libraries(unit_tests bf_global_flags harness gcov + ${ARGP_LIBRARY} ) add_custom_target(test