-
Notifications
You must be signed in to change notification settings - Fork 87
/
CMakeLists.txt
169 lines (145 loc) · 4.89 KB
/
CMakeLists.txt
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
cmake_minimum_required(VERSION 3.16)
project(UA2F LANGUAGES C CXX)
set(CMAKE_C_STANDARD 17)
OPTION(UA2F_BUILD_TESTS "Build tests" OFF)
OPTION(UA2F_ENABLE_UCI "Enable UCI support" ON)
OPTION(UA2F_NO_CACHE "Disable cache" OFF)
find_package(Git)
if (GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE GIT_TAG_RESULT
)
if ((NOT GIT_TAG_RESULT EQUAL 0) OR (GIT_TAG STREQUAL ""))
set(GIT_TAG "unknown")
endif ()
execute_process(
COMMAND ${GIT_EXECUTABLE} branch --show-current
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE GIT_BRANCH_RESULT
)
if ((NOT GIT_BRANCH_RESULT EQUAL 0) OR (GIT_BRANCH STREQUAL ""))
set(GIT_BRANCH "unknown")
endif ()
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE GIT_COMMIT_RESULT
)
if ((NOT GIT_COMMIT_RESULT EQUAL 0) OR (GIT_COMMIT STREQUAL ""))
set(GIT_COMMIT "unknown")
endif ()
execute_process(
COMMAND ${GIT_EXECUTABLE} status --porcelain
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_DIRTY_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE GIT_DIRTY_RESULT
)
if ((NOT GIT_DIRTY_RESULT EQUAL 0))
set(GIT_DIRTY "unknown")
elseif (GIT_DIRTY_OUTPUT STREQUAL "")
set(GIT_DIRTY "clean")
else ()
set(GIT_DIRTY "dirty")
endif ()
else ()
set(GIT_COMMIT "unknown")
set(GIT_BRANCH "unknown")
set(GIT_TAG "unknown")
set(GIT_DIRTY "unknown")
endif ()
if (UA2F_VERSION_STR)
message(STATUS "Using version string: ${UA2F_VERSION_STR}")
else ()
set(UA2F_VERSION_STR "unknown")
message(STATUS "Using version string: unknown")
endif ()
add_compile_definitions(UA2F_GIT_COMMIT="${GIT_COMMIT}")
add_compile_definitions(UA2F_GIT_BRANCH="${GIT_BRANCH}")
add_compile_definitions(UA2F_GIT_TAG="${GIT_TAG}")
add_compile_definitions(UA2F_GIT_DIRTY="${GIT_DIRTY}")
add_compile_definitions(UA2F_VERSION="${UA2F_VERSION_STR}")
if (DEFINED ENV{UA2F_ENABLE_ASAN})
set(UA2F_ENABLE_ASAN $ENV{UA2F_ENABLE_ASAN})
message(STATUS "UA2F_ENABLE_ASAN set from environment: ${UA2F_ENABLE_ASAN}")
else ()
message(STATUS "UA2F_ENABLE_ASAN not set in environment")
endif ()
if (UA2F_ENABLE_ASAN)
add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
message(STATUS "AddressSanitizer is enabled.")
else ()
message(STATUS "AddressSanitizer is disabled.")
endif ()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-gc-sections")
if (UA2F_CUSTOM_USER_AGENT)
if (NOT UA2F_USER_AGENT_STRING)
message(FATAL_ERROR "UA2F_USER_AGENT_STRING is not set")
endif ()
message(STATUS "Using custom user agent string: ${UA2F_USER_AGENT_STRING}")
add_compile_definitions(UA2F_USE_CUSTOM_UA=1)
endif ()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/custom.h.in ${CMAKE_CURRENT_BINARY_DIR}/custom.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_executable(ua2f
src/ua2f.c
src/statistics.c
src/util.c
src/cache.c
src/handler.c
src/cli.c
src/config.c
src/third/nfqueue-mnl.c)
target_link_libraries(ua2f mnl netfilter_queue pthread nfnetlink)
if (UA2F_ENABLE_UCI)
add_compile_definitions(UA2F_ENABLE_UCI=1)
target_link_libraries(ua2f uci)
else ()
message(STATUS "UCI support is disabled.")
endif ()
if (UA2F_NO_CACHE)
add_compile_definitions(UA2F_NO_CACHE=1)
message(STATUS "Cache is disabled.")
else ()
message(STATUS "Cache is auto.")
endif ()
install(TARGETS ua2f RUNTIME DESTINATION bin)
if (UA2F_BUILD_TESTS)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()
add_executable(
ua2f_test
test/util_test.cc
test/cache_test.cc
src/util.c
src/cache.c
src/cli.c
)
target_link_libraries(
ua2f_test
GTest::gtest_main
)
target_include_directories(ua2f_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
include(GoogleTest)
gtest_discover_tests(ua2f_test)
else ()
message(STATUS "Tests are disabled.")
endif ()