From e9503909cf8a0ca1f00a253d42a421eeec97648b Mon Sep 17 00:00:00 2001 From: Krzysztof Dudkiewicz <8515450+doodeck@users.noreply.github.com> Date: Sun, 5 Mar 2023 09:31:43 +0100 Subject: [PATCH 1/3] Squash a combination of 38 commits Create cmake.yml Add MacOS builiding hints Add libtins as a submodule Add build hints for external libtins and gtest Create tests.yml Upgrade google test version 1.11.0 -> 1.13.0 Add build and tests badges to README Address the issues in the discussion of the PR: * use the system libraries, make alternatives optional, activated with switches * keep the lines up to 80 chars in length, unless technically not possible * set the level of redundant messages to DEBUG, mark them for removal with FIXME: * provide details of build problems using Apple/CLang in README * Use built type Debug for tests --- .github/workflows/cmake-macos.yml | 37 +++++++++++++++++++++ .github/workflows/cmake.yml | 30 +++++++++++++++++ .github/workflows/tests-own.yml | 46 ++++++++++++++++++++++++++ .github/workflows/tests.yml | 44 ++++++++++++++++++++++++ .gitignore | 1 + .gitmodules | 3 ++ CMakeLists.txt | 12 ++++++- README.md | 31 ++++++++++++++++- external/README.md | 28 ++++++++++++++++ external/libtins | 1 + src/types.h | 1 + test/CMakeLists.txt | 33 +++++++++++++++--- test/memory-stress-test/CMakeLists.txt | 8 +++++ 13 files changed, 269 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/cmake-macos.yml create mode 100644 .github/workflows/cmake.yml create mode 100644 .github/workflows/tests-own.yml create mode 100644 .github/workflows/tests.yml create mode 100644 .gitmodules create mode 100644 external/README.md create mode 160000 external/libtins diff --git a/.github/workflows/cmake-macos.yml b/.github/workflows/cmake-macos.yml new file mode 100644 index 0000000..665946e --- /dev/null +++ b/.github/workflows/cmake-macos.yml @@ -0,0 +1,37 @@ +name: CMake-MacOS + +on: + push: + branches: [ "feature_branch" ] + pull_request: + branches: [ "feature_branch" ] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + build: + # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac. + # You can convert this to a matrix build if you need cross-platform coverage. + # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix + runs-on: macos-12 # macos-11 + + steps: + - uses: actions/checkout@v3 + + - name: Configure CMake + # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. + # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type + run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} + + - name: Build + # Build your program with the given configuration + run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} + + - name: Test + working-directory: ${{github.workspace}}/build + # Execute tests defined by the CMake configuration. + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ctest -C ${{env.BUILD_TYPE}} + 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..974f2b9 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/doodeck/libnetflow9/actions/workflows/cmake.yml/badge.svg) + +### Testing ### + +![test workflow sys](https://github.com/doodeck/libnetflow9/actions/workflows/tests.yml/badge.svg) + +![test workflow own](https://github.com/doodeck/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 From 1fe76713c8550f1d200ce7629904e465d21850bf Mon Sep 17 00:00:00 2001 From: Krzysztof Dudkiewicz <8515450+doodeck@users.noreply.github.com> Date: Sun, 23 Apr 2023 17:02:03 +0200 Subject: [PATCH 2/3] Delete cmake-macos.yml Not needed in master, is not operational yet, used only to reproduce MacOS problems --- .github/workflows/cmake-macos.yml | 37 ------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 .github/workflows/cmake-macos.yml diff --git a/.github/workflows/cmake-macos.yml b/.github/workflows/cmake-macos.yml deleted file mode 100644 index 665946e..0000000 --- a/.github/workflows/cmake-macos.yml +++ /dev/null @@ -1,37 +0,0 @@ -name: CMake-MacOS - -on: - push: - branches: [ "feature_branch" ] - pull_request: - branches: [ "feature_branch" ] - -env: - # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) - BUILD_TYPE: Release - -jobs: - build: - # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac. - # You can convert this to a matrix build if you need cross-platform coverage. - # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix - runs-on: macos-12 # macos-11 - - steps: - - uses: actions/checkout@v3 - - - name: Configure CMake - # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. - # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type - run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} - - - name: Build - # Build your program with the given configuration - run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} - - - name: Test - working-directory: ${{github.workspace}}/build - # Execute tests defined by the CMake configuration. - # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail - run: ctest -C ${{env.BUILD_TYPE}} - From 7a104445ed601c737dc6d192323f4bb4b8a655d4 Mon Sep 17 00:00:00 2001 From: Krzysztof Dudkiewicz <8515450+doodeck@users.noreply.github.com> Date: Thu, 15 Jun 2023 17:02:37 +0200 Subject: [PATCH 3/3] Make build/test badges point to the (future) actions in forked-from project For the time being, in pull request's branch, the badges are expected to remain grey and show "no status" --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 974f2b9..af252bc 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,13 @@ libnetflow9 is written in C++17, and has a compatible C API. ### Building ### -![build workflow](https://github.com/doodeck/libnetflow9/actions/workflows/cmake.yml/badge.svg) +![build workflow](https://github.com/exatel/libnetflow9/actions/workflows/cmake.yml/badge.svg) ### Testing ### -![test workflow sys](https://github.com/doodeck/libnetflow9/actions/workflows/tests.yml/badge.svg) +![test workflow sys](https://github.com/exatel/libnetflow9/actions/workflows/tests.yml/badge.svg) -![test workflow own](https://github.com/doodeck/libnetflow9/actions/workflows/tests-own.yml/badge.svg) +![test workflow own](https://github.com/exatel/libnetflow9/actions/workflows/tests-own.yml/badge.svg) ## Dependencies ##