cmake_minimum_required(VERSION 3.16) project(pkt2flow VERSION 1.4.2 DESCRIPTION "A simple utility to classify packets into flows" LANGUAGES C CXX) # Set C and C++ standards set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 17) set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Enable compile commands for better IDE support set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Set default build type to Release if not specified if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() # Compiler flags set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") set(CMAKE_C_FLAGS_DEBUG "-g -O0") set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG") set(CMAKE_CXX_FLAGS_DEBUG "-g -O0") set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") # Platform-specific definitions if(APPLE) add_definitions(-Ddarwin) endif() # Find required packages find_package(PkgConfig REQUIRED) # Find libpcap pkg_check_modules(PCAP REQUIRED libpcap) if(NOT PCAP_FOUND) find_library(PCAP_LIBRARIES pcap) if(NOT PCAP_LIBRARIES) message(FATAL_ERROR "libpcap not found") endif() set(PCAP_INCLUDE_DIRS "") endif() # Source files for the original executable set(PKT2FLOW_SOURCES src/pkt2flow.c src/flow_db.c src/utilities.c src/main.c ) # Create the original executable add_executable(pkt2flow ${PKT2FLOW_SOURCES}) # Link libraries for both executables target_link_libraries(pkt2flow ${PCAP_LIBRARIES} ) # Include directories for both executables target_include_directories(pkt2flow PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${PCAP_INCLUDE_DIRS} ) # Set compile definitions for both executables target_compile_definitions(pkt2flow PRIVATE _GNU_SOURCE) # Installation include(GNUInstallDirs) install(TARGETS pkt2flow RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) # Enable testing enable_testing() # Build tests if requested option(BUILD_TESTS "Build unit tests" ON) if(BUILD_TESTS) # Find GoogleTest find_package(GTest REQUIRED) find_package(Glog REQUIRED) # Test source files set(TEST_SOURCES tests/test_flow_db.cpp tests/test_utilities.cpp tests/test_pkt2flow_base.cpp tests/test_pkt2flow_integration.cpp tests/test_pkt2flow_tcp_integration.cpp tests/test_pkt2flow_udp_integration.cpp tests/test_pkt2flow_udplite_integration.cpp tests/test_main.cpp ) # Create test executable add_executable(pkt2flow_tests ${TEST_SOURCES}) # Link test libraries target_link_libraries(pkt2flow_tests ${PCAP_LIBRARIES} glog::glog GTest::gtest GTest::gtest_main ) # Include directories for tests target_include_directories(pkt2flow_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${PCAP_INCLUDE_DIRS} ) # Set compile definitions for tests target_compile_definitions(pkt2flow_tests PRIVATE _GNU_SOURCE) # Add test add_test(NAME pkt2flow_unit_tests COMMAND pkt2flow_tests) # Create a library from the source files for testing add_library(pkt2flow_lib STATIC src/flow_db.c src/utilities.c ) target_include_directories(pkt2flow_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${PCAP_INCLUDE_DIRS} ) target_link_libraries(pkt2flow_lib ${PCAP_LIBRARIES} glog::glog ) target_compile_definitions(pkt2flow_lib PRIVATE _GNU_SOURCE) # Create a library that includes the main pkt2flow.c for testing (excluding main) add_library(pkt2flow_test_lib STATIC src/pkt2flow.c ) # Exclude main function from the test library target_compile_definitions(pkt2flow_test_lib PRIVATE _GNU_SOURCE TESTING_MODE) target_include_directories(pkt2flow_test_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${PCAP_INCLUDE_DIRS} ) target_link_libraries(pkt2flow_test_lib ${PCAP_LIBRARIES} glog::glog ) target_compile_definitions(pkt2flow_test_lib PRIVATE _GNU_SOURCE) # Link both libraries to tests target_link_libraries(pkt2flow_tests pkt2flow_lib pkt2flow_test_lib) endif() # Build example in examples/ only when tests and libs are built if(BUILD_TESTS) add_executable(example_pkt2flow examples/example_pkt2flow.c) target_link_libraries(example_pkt2flow pkt2flow_lib) target_include_directories(example_pkt2flow PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) endif() # CPack configuration for packaging include(CPack) set(CPACK_PACKAGE_NAME "pkt2flow") set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}") set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PROJECT_DESCRIPTION}") set(CPACK_PACKAGE_VENDOR "chenxm") set(CPACK_PACKAGE_CONTACT "chenxm35@gmail.com")