-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
269 lines (210 loc) · 7.56 KB
/
Copy pathCMakeLists.txt
File metadata and controls
269 lines (210 loc) · 7.56 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
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
# aws-greengrass-system-log-forwarder - AWS Greengrass component for forwarding
# logs to CloudWatch.
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.22)
project(aws-greengrass-system-log-forwarder C)
option(ENABLE_WERROR "Compile warnings as errors")
set(MAX_UPLOAD_SIZE
CACHE STRING "Maximum size in bytes for CloudWatch log upload payload")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include(GNUInstallDirs)
# Put outputs in build/bin and build/lib
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
"${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(WARNING "CMAKE_BUILD_TYPE not set, using MinSizeRel.")
set(CMAKE_BUILD_TYPE MinSizeRel)
endif()
# Enable a compiler option for compile and link
macro(add_cflags)
add_compile_options(${ARGN})
add_link_options(${ARGN})
endmacro()
include(CheckCCompilerFlag)
include(CheckLinkerFlag)
# Enable a compiler/linker option if supported with a condition
macro(try_add_cflag_if name cond option)
check_c_compiler_flag("${option}" compiler_has_${name})
if(compiler_has_${name})
add_cflags("$<${cond}:${option}>")
endif()
endmacro()
# Enable a compiler/linker option if supported
macro(try_add_cflag name option)
try_add_cflag_if(${name} 1 ${option})
endmacro()
# Enable a linker option if supported with a condition
macro(try_add_link_option_if name cond option)
check_linker_flag(C "${option}" linker_has_${name})
if(linker_has_${name})
add_link_options("$<${cond}:${option}>")
endif()
endmacro()
# Enable a linker option if supported
macro(try_add_link_option name option)
try_add_link_option_if(${name} 1 ${option})
endmacro()
# Clear CMake defaults
set(CMAKE_C_FLAGS_DEBUG "")
set(CMAKE_C_FLAGS_RELEASE "")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "")
set(CMAKE_C_FLAGS_MINSIZEREL "")
set(CMAKE_COLOR_DIAGNOSTICS ON)
add_cflags($<$<CONFIG:Debug>:-O0>)
add_cflags($<$<CONFIG:Release,RelWithDebInfo>:-O3>)
try_add_cflag_if(Oz $<CONFIG:MinSizeRel> -Oz)
if(NOT compiler_has_Oz)
add_cflags($<$<CONFIG:MinSizeRel>:-Os>)
endif()
add_cflags($<$<CONFIG:Debug,RelWithDebInfo>:-ggdb3>)
add_cflags($<$<NOT:$<CONFIG:Debug>>:-fPIE>)
# These flags are required
add_cflags(-pthread -fno-strict-aliasing)
# Following flags are optional
add_cflags(-std=gnu11 -fvisibility=hidden -fno-semantic-interposition
-fno-common)
# This stops GCC from generating unstrippable inline DWARF debug info
add_cflags(-fno-unwind-tables -fno-asynchronous-unwind-tables)
try_add_cflag(strict-flex-arrays -fstrict-flex-arrays=3)
try_add_cflag(macro-prefix-map "-fmacro-prefix-map=${CMAKE_CURRENT_LIST_DIR}/=")
add_cflags($<$<CONFIG:Debug>:-fno-omit-frame-pointer>)
try_add_cflag_if(trivial-auto-var-init-pattern $<CONFIG:Debug>
-ftrivial-auto-var-init=pattern)
try_add_cflag_if(function-sections $<NOT:$<CONFIG:Debug>> -ffunction-sections)
try_add_cflag_if(data-sections $<NOT:$<CONFIG:Debug>> -fdata-sections)
if(ENABLE_WERROR)
add_cflags(-Werror)
endif()
add_cflags(
-Wall
-Wextra
-Wwrite-strings
-Wno-missing-braces
-Wvla
-Wshadow
-Wformat
-Wformat=2
-Wmissing-prototypes
-Wstrict-prototypes
-Wold-style-definition
-Wunused
-Wundef
-Wconversion
-Wsign-conversion
-Wimplicit-fallthrough
-Wredundant-decls
-Wdate-time
-Wstack-protector)
try_add_cflag(Wenum-int-mismatch -Wenum-int-mismatch)
try_add_cflag(Wtrampolines -Wtrampolines)
try_add_cflag(Wbidi-chars -Wbidi-chars=any,ucn)
add_cflags(-Werror=format-security -Werror=implicit
-Werror=incompatible-pointer-types -Werror=int-conversion)
add_compile_definitions(_GNU_SOURCE)
add_compile_definitions($<$<NOT:$<CONFIG:Debug>>:_FORTIFY_SOURCE=3>)
add_link_options($<$<NOT:$<CONFIG:Debug>>:-pie>)
add_link_options(
LINKER:-z,relro,-z,now,-z,noexecstack,-z,nodlopen LINKER:--as-needed
LINKER:--no-copy-dt-needed-entries LINKER:--enable-new-dtags,--hash-style=gnu)
try_add_link_option(compress-debug-sections-zlib
LINKER:--compress-debug-sections=zlib)
try_add_link_option_if(gc-sections $<NOT:$<CONFIG:Debug>> LINKER:--gc-sections)
try_add_link_option_if(strip-all $<CONFIG:Release,MinSizeRel> LINKER:-s)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DEBUG FALSE)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH "\$ORIGIN/../${CMAKE_INSTALL_LIBDIR}")
set(ENV{SOURCE_DATE_EPOCH} "0")
set(ENV{ZERO_AR_DATE} "1")
#
# FetchContent deps
#
set(FETCHCONTENT_QUIET FALSE)
include(FetchContent)
file(READ fc_deps.json FC_DEPS_JSON)
string(JSON FC_DEPS_COUNT LENGTH "${FC_DEPS_JSON}")
math(EXPR FC_DEPS_INDEX_MAX "${FC_DEPS_COUNT} - 1")
foreach(index RANGE ${FC_DEPS_INDEX_MAX})
string(JSON dep_name MEMBER "${FC_DEPS_JSON}" ${index})
string(JSON dep_url GET "${FC_DEPS_JSON}" "${dep_name}" url)
string(JSON dep_rev GET "${FC_DEPS_JSON}" "${dep_name}" rev)
fetchcontent_declare(
"${dep_name}"
GIT_REPOSITORY "${dep_url}"
GIT_TAG "${dep_rev}")
fetchcontent_makeavailable(${dep_name})
endforeach()
#
# System deps
#
find_package(PkgConfig REQUIRED)
pkg_search_module(openssl REQUIRED IMPORTED_TARGET openssl)
pkg_search_module(libsystemd REQUIRED IMPORTED_TARGET libsystemd)
include(CheckCSourceCompiles)
check_c_source_compiles(
"
#include <argp.h>
int main(void) { argp_parse(0, 0, 0, 0, 0, 0); }
"
has_argp)
#
# Add Dependencies
#
# Add ggl-sdk
target_include_directories(ggl-sdk INTERFACE ${ggl_sdk_SOURCE_DIR}/priv_include)
# Add coreHTTP
include("${core_http_SOURCE_DIR}/httpFilePaths.cmake")
add_library(coreHTTP ${HTTP_SOURCES})
# Supress warning coming coreHTTP
target_compile_options(
coreHTTP
PRIVATE -Wno-sign-conversion
-Wno-conversion
-Wno-unused-parameter
-Wno-missing-prototypes
-Wno-redundant-decls
-Wno-undef
-Wno-implicit-fallthrough)
# Add all directories to include paths
target_include_directories(coreHTTP PUBLIC ${HTTP_INCLUDE_PUBLIC_DIRS})
target_include_directories(coreHTTP PUBLIC dep_modules/coreHTTP/)
target_link_libraries(coreHTTP PUBLIC ggl-sdk)
# Add sigV4
include("${aws_sigv4_SOURCE_DIR}/sigv4FilePaths.cmake")
add_library(aws_sigv4 ${SIGV4_SOURCES})
target_include_directories(aws_sigv4 PUBLIC ${SIGV4_INCLUDE_PUBLIC_DIRS})
target_include_directories(aws_sigv4 PUBLIC dep_modules/aws-sigv4/)
# Add backoff_algorithm
include("${backoff_algorithm_SOURCE_DIR}/backoffAlgorithmFilePaths.cmake")
add_library(backoff_algorithm ${BACKOFF_ALGORITHM_SOURCES})
target_include_directories(backoff_algorithm
PUBLIC ${BACKOFF_ALGORITHM_INCLUDE_PUBLIC_DIRS})
#
# Build Executable
#
file(GLOB_RECURSE SRCS CONFIGURE_DEPENDS "src/*.c")
list(LENGTH SRCS SRCS_LEN)
foreach(src ${SRCS})
set_property(
SOURCE ${src}
APPEND_STRING
PROPERTY COMPILE_FLAGS "-frandom-seed=${src}")
endforeach()
add_executable(system-log-forwarder ${SRCS})
target_compile_definitions(system-log-forwarder
PRIVATE "GGL_MODULE=(\"system-log-forwarder\")")
if(MAX_UPLOAD_SIZE)
target_compile_definitions(system-log-forwarder
PRIVATE "MAX_UPLOAD_SIZE=${MAX_UPLOAD_SIZE}")
endif()
target_include_directories(system-log-forwarder PRIVATE include)
target_link_libraries(
system-log-forwarder PRIVATE ggl-sdk coreHTTP aws_sigv4 backoff_algorithm
PkgConfig::openssl PkgConfig::libsystemd)
if(NOT has_argp)
target_link_libraries(system-log-forwarder PRIVATE argp)
endif()
install(TARGETS system-log-forwarder)