-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
179 lines (145 loc) · 4.67 KB
/
CMakeLists.txt
File metadata and controls
179 lines (145 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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")