diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml new file mode 100644 index 0000000..56b0d1c --- /dev/null +++ b/.github/workflows/cmake.yml @@ -0,0 +1,30 @@ +# Build the library without tests +name: Build + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Configure CMake + run: | + cmake -B ${{github.workspace}}/build \ + -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DNF9_BUILD_TESTS=OFF + + - name: Build + # Build your program with the given configuration + run: | + cmake --build ${{github.workspace}}/build \ + --config ${{env.BUILD_TYPE}} diff --git a/.github/workflows/tests-own.yml b/.github/workflows/tests-own.yml new file mode 100644 index 0000000..7f14194 --- /dev/null +++ b/.github/workflows/tests-own.yml @@ -0,0 +1,46 @@ +# Build tests using external libraries referencd by the project +name: TestsOwnLibs + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Debug + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Install libpcap + run: sudo apt-get install -y libpcap-dev + + - name: Initialize submodules + run: git submodule init && git submodule update + + - name: Build libtins library + run: | + cd ${{github.workspace}}/external/libtins && mkdir build && \ + cd build && cmake .. -DLIBTINS_ENABLE_CXX11=1 && cmake --build . + + - name: Configure CMake + run: | + cmake -B ${{github.workspace}}/build \ + -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DNF9_BUILD_TESTS=ON \ + -DNF9_USE_OWN_LIBTINS=ON -DNF9_USE_OWN_GTEST=ON + + - name: Build Tests + run: | + cmake --build ${{github.workspace}}/build \ + --config ${{env.BUILD_TYPE}} + + - name: Test + working-directory: ${{github.workspace}}/build + run: ./test/netflowtests + # ctest -C ${{env.BUILD_TYPE}} diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..e562d25 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,44 @@ +# Build tests using the libraries installed in system +name: TestsSysLibs + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Debug + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Install libpcap + run: sudo apt-get install -y libpcap-dev + + - name: Install googletest + run: sudo apt-get install -y libgtest-dev + + - name: Install libtins + run: sudo apt-get install -y libtins-dev + + - name: Configure CMake + run: | + cmake -B ${{github.workspace}}/build \ + -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DNF9_BUILD_TESTS=ON + + - name: Build Tests + # Build your program with the given configuration + run: | + cmake --build ${{github.workspace}}/build \ + --config ${{env.BUILD_TYPE}} + + - name: Test + working-directory: ${{github.workspace}}/build + run: ./test/netflowtests + # ctest -C ${{env.BUILD_TYPE}} diff --git a/.gitignore b/.gitignore index 3131f45..09ac593 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /clang_build/ __pycache__ ENV +.vscode/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..d35da07 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "external/libtins"] + path = external/libtins + url = https://github.com/mfontanini/libtins.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 856a50d..49d1494 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,8 @@ option(NF9_FUZZ "Enable fuzzing with LLVM fuzzer" OFF) option(NF9_BUILD_BENCHMARK "If set, build benchmarks (requires google benchmark library)" OFF) option(NF9_BUILD_EXAMPLES "If set, build examples" OFF) +option(NF9_USE_OWN_LIBTINS "If set, use the libtins in the submodule" OFF) +option(NF9_USE_OWN_GTEST "If set, use googtest library from github" OFF) include(CheckCXXCompilerFlag) include(CheckIncludeFileCXX) @@ -29,7 +31,15 @@ else () add_library(netflow9 STATIC ${CXX_SRC}) endif () -target_include_directories(netflow9 SYSTEM PUBLIC include) +if (NOT NF9_USE_OWN_LIBTINS) + target_include_directories(netflow9 SYSTEM PUBLIC include) +else () + get_property(SRC_DIR DIRECTORY PROPERTY SOURCE_DIR) + target_include_directories(netflow9 SYSTEM PUBLIC include + ${SRC_DIR}/external/libtins/include/ + ) +endif() + target_compile_features(netflow9 PRIVATE cxx_std_17) if (MSVC) diff --git a/README.md b/README.md index b83f178..af252bc 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,18 @@ information about the traffic. libnetflow9 is written in C++17, and has a compatible C API. -# Building # +## Badges ## + +### Building ### + +![build workflow](https://github.com/exatel/libnetflow9/actions/workflows/cmake.yml/badge.svg) + +### Testing ### + +![test workflow sys](https://github.com/exatel/libnetflow9/actions/workflows/tests.yml/badge.svg) + +![test workflow own](https://github.com/exatel/libnetflow9/actions/workflows/tests-own.yml/badge.svg) + ## Dependencies ## @@ -33,6 +44,24 @@ cmake .. make -j4 ``` +## Building on MacOS M1 + +``` +mkdir build +cd build +cmake .. -DCMAKE_C_COMPILER=/opt/homebrew/bin/gcc-12 -DCMAKE_CXX_COMPILER=/opt/homebrew/bin/g++-12 +cmake --build . +``` + +Otherwise it defaults to Clang toolset, which as of version: + +`Apple clang version 14.0.0 (clang-1400.0.29.202)` + +is incapable of compiling the library. More details in the build log + +https://github.com/doodeck/libnetflow9/actions/runs/4681045806/jobs/8293144058 + + ## Building and running tests ## ```console diff --git a/external/README.md b/external/README.md new file mode 100644 index 0000000..7241131 --- /dev/null +++ b/external/README.md @@ -0,0 +1,28 @@ +# external libraries sources directory + +The external libraries are not used by default, instead it's expected they +are installed system-wide. System libraries have advantages, e.g. you can +rely on them in an airgapped environment. On the other hand, they are missing +from some distributions. + +In order to activate the external libraries set to ON the correspoding cmake +option[-s]: + +* `NF9_USE_OWN_LIBTINS` +* `NF9_USE_OWN_GTEST` + +## libtins +https://github.com/mfontanini/libtins.git + +Incorporated as a submodule as described here: + +https://git-scm.com/book/en/v2/Git-Tools-Submodules + +## googtest +pulled directly from github release as recommended here: + +https://github.com/google/googletest/blob/main/googletest/README.md + +Overthere see the description below the last bullet point "Use CMake +to download GoogleTest as part of the build's configure step. This approach +doesn't have the limitations of the other methods." diff --git a/external/libtins b/external/libtins new file mode 160000 index 0000000..fa87e1b --- /dev/null +++ b/external/libtins @@ -0,0 +1 @@ +Subproject commit fa87e1b6f697deafa90288760b7eb89549a00a22 diff --git a/src/types.h b/src/types.h index f1b331d..8ec0d89 100644 --- a/src/types.h +++ b/src/types.h @@ -11,6 +11,7 @@ #include #include #include +#include // unique_ptr #include "config.h" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6a03433..dd73156 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,9 +1,35 @@ cmake_minimum_required(VERSION 3.7) enable_testing() -find_package(GTest REQUIRED) +if (NOT NF9_USE_OWN_GTEST) + find_package(GTest REQUIRED) + list (APPEND GTEST_LIBRARIES + GTest::GTest + GTest::Main + ) +else() + include(FetchContent) + FetchContent_Declare( + googletest + # Specify the commit you depend on and update it regularly. + URL https://github.com/google/googletest/archive/refs/tags/v1.13.0.zip + ) + # For Windows: Prevent overriding the parent project's + # compiler/linker settings + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) + FetchContent_MakeAvailable(googletest) + list (APPEND GTEST_LIBRARIES + gtest_main + ) +endif() -find_library(LIBTINS_LIBRARIES tins) +if (NOT NF9_USE_OWN_LIBTINS) + find_library(LIBTINS_LIBRARIES tins) +else() + get_property(SRC_DIR DIRECTORY PROPERTY SOURCE_DIR) + find_library(LIBTINS_LIBRARIES tins + ${SRC_DIR}/../external/libtins/build/lib/) +endif() file(GLOB src *.cpp) add_executable(netflowtests ${src}) @@ -17,8 +43,7 @@ target_include_directories(netflowtests PRIVATE ) target_link_libraries(netflowtests netflow9 - GTest::GTest - GTest::Main + "${GTEST_LIBRARIES}" "${LIBTINS_LIBRARIES}" ) diff --git a/test/memory-stress-test/CMakeLists.txt b/test/memory-stress-test/CMakeLists.txt index b2091e1..571e073 100644 --- a/test/memory-stress-test/CMakeLists.txt +++ b/test/memory-stress-test/CMakeLists.txt @@ -4,10 +4,18 @@ add_executable(netflow-memory-stress test.cpp) add_test(netflow-memory-stress netflow-memory-stress) target_compile_features(netflow-memory-stress PRIVATE cxx_std_17) + +if (NF9_USE_OWN_GTEST) + list (APPEND GTEST_INCLUDES + "${googletest_SOURCE_DIR}/googletest/include" + ) +endif () + target_include_directories(netflow-memory-stress PRIVATE "../../src" "../" "${CMAKE_CURRENT_BINARY_DIR}/../.." + "${GTEST_INCLUDES}" ) target_link_libraries(netflow-memory-stress netflow9