-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCMakeLists.txt
271 lines (223 loc) · 9.02 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
cmake_minimum_required(VERSION 3.24)
set(CMAKE_C_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
cmake_policy(SET CMP0068 NEW)
cmake_policy(SET CMP0135 NEW)
set(NGINX_DATADOG_VERSION 1.5.0)
project(ngx_http_datadog_module VERSION ${NGINX_DATADOG_VERSION})
option(NGINX_DATADOG_ASM_ENABLED "Build with libddwaf" ON)
option(NGINX_DATADOG_RUM_ENABLED "Build with RUM injection" OFF)
set(NGINX_SRC_DIR "" CACHE PATH "The path to a directory with nginx sources")
set(NGINX_VERSION "" CACHE STRING "The nginx version")
if (NGINX_SRC_DIR STREQUAL "" AND NGINX_VERSION STREQUAL "")
message(FATAL_ERROR "Set NGINX_SRC_DIR or, alternatively NGINX_VERSION")
endif()
option(NGINX_PATCH_AWAY_LIBC "Patch away libc dependency" OFF)
option(NGINX_COVERAGE "Add coverage instrumentation" OFF)
option(ENABLE_FUZZERS "For building fuzzers" OFF)
set(NGINX_DATADOG_FLAVOR "nginx" CACHE STRING "NGINX flavor")
if (NGINX_DATADOG_RUM_ENABLED AND NGINX_DATADOG_ASM_ENABLED)
message(FATAL_ERROR "ASM and RUM features are mutually exclusive")
endif ()
include(CTest)
if(BUILD_TESTING)
add_subdirectory(test/unit)
endif()
if(ENABLE_FUZZERS)
add_subdirectory(test/fuzz)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb -O0")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb -O0")
endif()
message(STATUS "nginx-datadog version=[${NGINX_DATADOG_VERSION}]")
set(NGINX_DATADOG_PRODUCTS_LIST "tracing")
if (NGINX_DATADOG_ASM_ENABLED)
list(APPEND NGINX_DATADOG_PRODUCTS_LIST "appsec")
endif ()
if (NGINX_DATADOG_RUM_ENABLED)
list(APPEND NGINX_DATADOG_PRODUCTS_LIST "rum-injection")
endif ()
list(JOIN NGINX_DATADOG_PRODUCTS_LIST " " NGINX_DATADOG_PRODUCTS)
unset(NGINX_DATADOG_PRODUCTS_LIST)
message(STATUS "nginx-datadog products: ${NGINX_DATADOG_PRODUCTS}")
# Make curl link against a static zlib (requires cmake 3.24)
set(ZLIB_USE_STATIC_LIBS ON)
# Prefer the build mode "release with debug info" if another mode wasn't
# specified.
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE "RelWithDebInfo")
endif()
# Global compiler options
if(CMAKE_COMPILER_IS_GNUCXX)
# This warning has a false positive. See
# <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108088>.
add_compile_options(-Wno-error=free-nonheap-object)
# Looks like a false positive. Doesn't like a constructor taking
# std::optional = nullopt and moving it to a null optional
add_compile_options(-Wno-error=maybe-uninitialized)
endif()
# save frame pointers to make it more profiler friendly
add_compile_options(-fno-omit-frame-pointer)
# Nginx module boilerplate (code within the nginx repository)
include(./cmake/deps/nginx-module.cmake)
include(./cmake/deps/rapidjson.cmake)
add_subdirectory(./dd-trace-cpp EXCLUDE_FROM_ALL)
if(NGINX_DATADOG_ASM_ENABLED)
# we need neither but libddwaf requires one enabled
set(LIBDDWAF_BUILD_STATIC ON)
set(LIBDDWAF_BUILD_SHARED OFF)
set(LIBDDWAF_TESTING OFF)
set(LIBDDWAF_ENABLE_LTO OFF)
add_subdirectory(./libddwaf EXCLUDE_FROM_ALL)
# get libddwaf version
file(READ "./libddwaf/version" LIBDDWAF_VERSION)
# get rules version
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/src/security/recommended.json ASM_RULES REGEX "rules_version")
string(REGEX REPLACE "\"rules_version\": \"(.*)\"" "\\1" RULES_VERSION ${ASM_RULES})
string(STRIP "${RULES_VERSION}" RULES_VERSION)
unset(ASM_RULES)
endif()
if(NGINX_DATADOG_RUM_ENABLED)
execute_process (
COMMAND cargo pkgid --package inject-browser-sdk --manifest-path "${CMAKE_CURRENT_SOURCE_DIR}/deps/inject-browser-sdk/Cargo.toml"
OUTPUT_VARIABLE RUM_SDK_INJECTOR_PKGID
)
string(REGEX REPLACE ".*@(.*)" "\\1" RUM_SDK_INJECTOR_VERSION ${RUM_SDK_INJECTOR_PKGID})
string(STRIP "${RUM_SDK_INJECTOR_VERSION}" RUM_SDK_INJECTOR_VERSION)
unset(RUM_SDK_INJECTOR_PKGID)
add_subdirectory(deps)
endif()
include(./cmake/generate_build_id.cmake)
# Generate the build identifier.
# Known issue: this command is only run during the CMake configuration stage.
# Possible solution: <https://jonathanhamberg.com/post/cmake-embedding-git-hash/>
make_build_id(NGINX_DATADOG_BUILD_ID)
configure_file(src/version.cpp.in ${CMAKE_BINARY_DIR}/version.cpp)
# The shared library (nginx module) that we are building.
add_library(ngx_http_datadog_module SHARED)
# Define `DD_NGINX_FLAVOR`
target_sources(ngx_http_datadog_module PRIVATE ${CMAKE_BINARY_DIR}/version.cpp)
add_library(ngx_http_datadog_objs OBJECT)
set_target_properties(ngx_http_datadog_objs PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_compile_definitions(ngx_http_datadog_objs PUBLIC DD_NGINX_FLAVOR="${NGINX_DATADOG_FLAVOR}")
target_sources(ngx_http_datadog_objs
PRIVATE
src/array_util.cpp
src/datadog_conf.cpp
src/datadog_conf_handler.cpp
src/datadog_context.cpp
src/datadog_directive.cpp
src/datadog_handler.cpp
src/datadog_variable.cpp
src/dd.cpp
src/defer.cpp
src/global_tracer.cpp
src/ngx_event_scheduler.cpp
src/ngx_header_reader.cpp
src/ngx_http_datadog_module.cpp
src/ngx_logger.cpp
src/ngx_script.cpp
src/request_tracing.cpp
src/string_util.cpp
src/tracing_library.cpp
)
if(NGINX_DATADOG_ASM_ENABLED)
target_sources(ngx_http_datadog_objs
PRIVATE
src/security/blocking.cpp
src/security/body_parse/body_json.cpp
src/security/body_parse/body_multipart.cpp
src/security/body_parse/body_parsing.cpp
src/security/body_parse/header.cpp
src/security/client_ip.cpp
src/security/collection.cpp
src/security/context.cpp
src/security/ddwaf_obj.cpp
src/security/decode.cpp
src/security/header_tags.cpp
src/security/library.cpp
src/security/waf_remote_cfg.cpp)
target_compile_definitions(ngx_http_datadog_objs PUBLIC WITH_WAF)
endif()
if(NGINX_DATADOG_RUM_ENABLED)
target_sources(ngx_http_datadog_objs
PRIVATE
src/rum/config.cpp
src/rum/injection.cpp
)
target_compile_definitions(ngx_http_datadog_objs PUBLIC WITH_RUM)
endif()
if(ENABLE_FUZZERS)
target_compile_options(ngx_http_datadog_objs PUBLIC -fsanitize=fuzzer,address,undefined,leak)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(ngx_http_datadog_objs PRIVATE -Wall -Werror)
endif()
if(NGINX_COVERAGE)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(ngx_http_datadog_objs PUBLIC
-fprofile-instr-generate -fcoverage-mapping -mllvm -runtime-counter-relocation=true)
target_link_options(ngx_http_datadog_objs INTERFACE
-fprofile-instr-generate -mllvm -runtime-counter-relocation=true)
target_sources(ngx_http_datadog_module PRIVATE src/coverage_fixup.c)
if(APPLE)
# pass --coverage so the linker is instructed to link against libclang_rt.profile_osx.
target_link_options(ngx_http_datadog_objs INTERFACE -v --coverage)
endif()
else()
message(FATAL_ERROR "NGINX_COVERAGE is only supported with clang")
endif()
endif()
target_include_directories(ngx_http_datadog_objs
PUBLIC
src/
)
target_link_libraries(ngx_http_datadog_objs PUBLIC
dd_trace_cpp-static
nginx_module
rapidjson
)
target_link_libraries(ngx_http_datadog_module PRIVATE
ngx_http_datadog_objs
nginx_module # objects don't come transitively from ngx_http_datadog_objs
)
if(NGINX_DATADOG_ASM_ENABLED)
target_link_libraries(ngx_http_datadog_objs PUBLIC libddwaf_objects)
target_link_libraries(ngx_http_datadog_module PUBLIC libddwaf_objects)
endif()
if(NGINX_DATADOG_RUM_ENABLED)
target_link_libraries(ngx_http_datadog_objs PUBLIC inject_browser_sdk)
target_link_libraries(ngx_http_datadog_module PUBLIC inject_browser_sdk)
endif()
# Remove the "lib" prefix to match NGINX convention
set_property(TARGET ngx_http_datadog_module PROPERTY PREFIX "")
if(CMAKE_COMPILER_IS_GNUCXX OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
if(APPLE)
set_property(TARGET ngx_http_datadog_module PROPERTY SUFFIX ".so")
target_link_options(ngx_http_datadog_module PRIVATE -undefined dynamic_lookup)
else()
target_link_options(ngx_http_datadog_module PRIVATE -static-libstdc++ -static-libgcc)
endif()
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/nginx_module.ld)
set_target_properties(ngx_http_datadog_module PROPERTIES LINK_DEPENDS ${LINKER_SCRIPT})
target_link_options(ngx_http_datadog_module PRIVATE "-Wl,--version-script=${LINKER_SCRIPT}")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(EXPORTED_SYMBOLS ${CMAKE_SOURCE_DIR}/nginx_module.ld-prime)
set_target_properties(ngx_http_datadog_module PROPERTIES LINK_DEPENDS ${EXPORTED_SYMBOLS})
target_link_options(ngx_http_datadog_module PRIVATE "-Wl,-exported_symbols_list" "-Wl,${EXPORTED_SYMBOLS}")
endif()
if(NGINX_PATCH_AWAY_LIBC)
include(./cmake/patchelf.cmake)
patch_away_libc(ngx_http_datadog_module)
endif()
if(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
include(./cmake/split_debug_info.cmake)
split_debug_info(ngx_http_datadog_module)
endif()
add_library(ngx_http_datadog_static_lib STATIC EXCLUDE_FROM_ALL)
target_link_libraries(ngx_http_datadog_static_lib PUBLIC ngx_http_datadog_objs)
# vim: et ts=2 sw=2: