-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCMakeLists.txt
174 lines (148 loc) · 5.92 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
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)
# This build configuration combines the following other build configurations:
# - `nginx-datadog.cmake`, the actual nginx module source in `src/`,
# - `nginx-module.cmake`, the nginx-specific boilerplate code generated by
# nginx's `configure` script,
# - `dd-trace-cpp/CMakeLists.txt`, the Datadog tracing library.
project(ngx_http_datadog_module VERSION 1.2.1)
option(NGINX_DATADOG_ASM_ENABLED "Build with libddwaf" ON)
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)
# 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)
add_subdirectory(./dd-trace-cpp EXCLUDE_FROM_ALL)
set(RULES_VERSION "")
set(LIBDDWAF_VERSION "")
if(NGINX_DATADOG_ASM_ENABLED)
include(./cmake/deps/rapidjson.cmake)
# 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)
execute_process(
COMMAND grep rules_version src/security/recommended.json
COMMAND sed "s/.*\"\\([0-9.]\\+\\)\".*/\\1/"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE RULES_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)
file(READ ./libddwaf/version LIBDDWAF_VERSION)
endif()
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)
target_sources(ngx_http_datadog_module
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/glibc_compat.c
src/global_tracer.cpp
src/log_conf.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
${CMAKE_BINARY_DIR}/version.cpp
)
if(NGINX_DATADOG_ASM_ENABLED)
target_sources(ngx_http_datadog_module
PRIVATE
src/security/blocking.cpp
src/security/client_ip.cpp
src/security/collection.cpp
src/security/context.cpp
src/security/ddwaf_obj.cpp
src/security/header_tags.cpp
src/security/library.cpp)
target_compile_definitions(ngx_http_datadog_module PRIVATE WITH_WAF)
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(ngx_http_datadog_module PRIVATE -Wall -Werror)
endif()
if(NGINX_COVERAGE)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(ngx_http_datadog_module PRIVATE
-fprofile-instr-generate -fcoverage-mapping -mllvm -runtime-counter-relocation=true)
target_link_options(ngx_http_datadog_module PRIVATE
-fprofile-instr-generate -mllvm -runtime-counter-relocation=true)
target_sources(ngx_http_datadog_module PRIVATE src/coverage_fixup.c)
else()
message(FATAL_ERROR "NGINX_COVERAGE is only supported with clang")
endif()
endif()
target_include_directories(ngx_http_datadog_module
PRIVATE
src/
)
target_link_libraries(ngx_http_datadog_module dd_trace_cpp-objects nginx_module)
if(NGINX_DATADOG_ASM_ENABLED)
target_link_libraries(ngx_http_datadog_module rapidjson libddwaf_objects)
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_libraries(ngx_http_datadog_module -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)
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()
# vim: et ts=2 sw=2: