Debian Codegraph @

crt/aws-c-io/CMakeLists.txt

raw
1
2 cmake_minimum_required(VERSION 3.9...3.31)
3 project(aws-c-io C)
4
5 if (NOT IN_SOURCE_BUILD)
6 # this is required so we can use aws-c-common's CMake modules
7 find_package(aws-c-common REQUIRED)
8 endif()
9
10 include(AwsCFlags)
11 include(AwsCheckHeaders)
12 include(AwsSharedLibSetup)
13 include(AwsSanitizers)
14 include(AwsFindPackage)
15 include(CTest)
16 include(GNUInstallDirs)
17
18 option(BUILD_RELOCATABLE_BINARIES
19 "Build Relocatable Binaries, this will turn off features that will fail on older kernels than used for the build."
20 OFF)
21 option(BYO_CRYPTO "Don't build a tls implementation or link against a crypto interface. This feature is only for unix builds currently."
22 OFF)
23
24 file(GLOB AWS_IO_HEADERS
25 "include/aws/io/*.h"
26 )
27
28 file(GLOB AWS_IO_TESTING_HEADERS
29 "include/aws/testing/*.h"
30 )
31
32
33 file(GLOB AWS_IO_PRIV_HEADERS
34 "include/aws/io/private/*.h"
35 )
36
37 file(GLOB AWS_IO_SRC
38 "source/*.c"
39 )
40
41 if (WIN32)
42 option(USE_IO_COMPLETION_PORTS
43 "Use I/O Completion Ports to drive event-loops. \
44 If disabled, a less performant implementation based on select() is used. \
45 Disable this if implementing your own event-loop whose interface does not match the IOCP interface."
46 ON)
47
48 file(GLOB AWS_IO_OS_HEADERS
49 )
50
51 file(GLOB AWS_IO_OS_SRC
52 "source/windows/*.c"
53 )
54
55 if (USE_IO_COMPLETION_PORTS)
56 file(GLOB AWS_IO_IOCP_SRC
57 "source/windows/iocp/*.c"
58 )
59 list(APPEND AWS_IO_OS_SRC ${AWS_IO_IOCP_SRC})
60
61 list(APPEND EVENT_LOOP_DEFINES "IO_COMPLETION_PORTS")
62 endif ()
63
64 if (MSVC)
65 source_group("Header Files\\aws\\io" FILES ${AWS_IO_HEADERS})
66 source_group("Header Files\\aws\\io\\private" FILES ${AWS_IO_PRIV_HEADERS})
67 source_group("Source Files" FILES ${AWS_IO_SRC})
68 source_group("Source Files\\windows" FILES ${AWS_IO_OS_SRC})
69 endif ()
70 #platform libs come from aws-c-common transitively, so we don't specify them here, but for documentation purposes,
71 #Kernel32 and wsock2 are pulled in automatically. Here we add the lib containing the schannel API.
72 #Also note, you don't get a choice on TLS implementation for Windows.
73 set(PLATFORM_LIBS secur32 crypt32)
74 elseif (CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android")
75 option(USE_VSOCK
76 "Build in support for VSOCK sockets"
77 OFF)
78
79 file(GLOB AWS_IO_OS_HEADERS
80 )
81
82 file(GLOB AWS_IO_OS_SRC
83 "source/linux/*.c"
84 "source/posix/*.c"
85 )
86 set(PLATFORM_LIBS "")
87
88 list(APPEND EVENT_LOOP_DEFINES "EPOLL")
89 set(USE_S2N ON)
90
91 elseif (APPLE)
92
93 file(GLOB AWS_IO_OS_HEADERS
94 )
95
96 file(GLOB AWS_IO_OS_SRC
97 "source/bsd/*.c"
98 "source/posix/*.c"
99 "source/darwin/*.c"
100 )
101
102 find_library(SECURITY_LIB Security)
103 find_library(NETWORK_LIB Network)
104
105 # Enable dispatch queue if the libraries are available
106 if (NETWORK_LIB AND SECURITY_LIB)
107 list(APPEND PLATFORM_LIBS "-framework Security -framework Network")
108 list(APPEND EVENT_LOOP_DEFINES "DISPATCH_QUEUE")
109 endif ()
110
111 # Enable KQUEUE on MacOS only if AWS_USE_SECITEM is not declared. SecItem requires Dispatch Queue.
112 if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT DEFINED AWS_USE_SECITEM)
113 list(APPEND EVENT_LOOP_DEFINES "KQUEUE")
114 endif()
115
116 # On MacOS s2n-tls is optional (the Apple SecItem/Security framework provides a TLS implementation).
117 # Default to using s2n-tls only when SecItem is not requested. Users can override with -DUSE_S2N=ON/OFF.
118 if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT DEFINED AWS_USE_SECITEM)
119 option(USE_S2N "Use s2n-tls as the TLS implementation." ON)
120 else()
121 option(USE_S2N "Use s2n-tls as the TLS implementation." OFF)
122 endif()
123
124 # Force USE_S2N OFF to avoid pulling in an unused s2n dependency.
125 if (DEFINED AWS_USE_SECITEM AND USE_S2N)
126 message(WARNING "AWS_USE_SECITEM takes precedence over s2n-tls; forcing USE_S2N=OFF to avoid linking unused s2n.")
127 set(USE_S2N OFF CACHE BOOL "Use s2n-tls as the TLS implementation." FORCE)
128 endif()
129
130 elseif (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR CMAKE_SYSTEM_NAME STREQUAL "NetBSD" OR CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
131 file(GLOB AWS_IO_OS_HEADERS
132 )
133
134 file(GLOB AWS_IO_OS_SRC
135 "source/bsd/*.c"
136 "source/posix/*.c"
137 )
138
139 list(APPEND EVENT_LOOP_DEFINES "KQUEUE")
140 set(USE_S2N ON)
141
142 endif()
143
144 if (BYO_CRYPTO)
145 set(USE_S2N OFF)
146
147 if (APPLE OR WIN32)
148 message(FATAL_ERROR "BYO_CRYPTO is only for use with unix systems. It cannot be used on your current platform target")
149 endif()
150 endif()
151
152 if (USE_S2N)
153 file(GLOB AWS_IO_TLS_SRC
154 "source/s2n/*.c"
155 )
156 # Prefer find_package() because it's the normal CMake way to do dependencies.
157 # But fall back on aws_use_package() because some projects still need to do an IN_SOURCE_BUILD of S2N.
158 # (e.g. aws-crt-java until this is resolved: https://github.com/awslabs/aws-crt-java/pull/817)
159 find_package(s2n QUIET)
160
161 if (s2n_FOUND)
162 list(APPEND DEP_AWS_LIBS AWS::s2n)
163 else()
164 # Set flag to use in-source path to <s2n/unstable/*.h> headers if we do an IN_SOURCE_BUILD.
165 aws_use_package(s2n)
166 add_definitions(-DAWS_S2N_INSOURCE_PATH)
167 endif()
168 endif()
169
170 file(GLOB IO_HEADERS
171 ${AWS_IO_HEADERS}
172 ${AWS_IO_OS_HEADERS}
173 ${AWS_IO_PRIV_HEADERS}
174 )
175
176 file(GLOB IO_SRC
177 ${AWS_IO_SRC}
178 ${AWS_IO_OS_SRC}
179 ${AWS_IO_TLS_SRC}
180 )
181
182 add_library(${PROJECT_NAME} ${LIBTYPE} ${IO_HEADERS} ${IO_SRC})
183 aws_set_common_properties(${PROJECT_NAME})
184 aws_prepare_symbol_visibility_args(${PROJECT_NAME} "AWS_IO")
185 aws_check_headers(${PROJECT_NAME} ${AWS_IO_HEADERS})
186
187 aws_add_sanitizers(${PROJECT_NAME})
188
189 # We are not ABI stable yet
190 set_target_properties(${PROJECT_NAME} PROPERTIES VERSION 1.0.0)
191
192 if (NOT EVENT_LOOP_DEFINES)
193 message(FATAL_ERROR "Event Loop is not setup on the platform.")
194 endif()
195 foreach(EVENT_LOOP_DEFINE IN LISTS EVENT_LOOP_DEFINES)
196 target_compile_definitions(${PROJECT_NAME} PUBLIC "-DAWS_ENABLE_${EVENT_LOOP_DEFINE}")
197 endforeach()
198
199 if (AWS_USE_SECITEM)
200 target_compile_definitions(${PROJECT_NAME} PUBLIC "-DAWS_USE_SECITEM")
201 endif()
202
203 if (BYO_CRYPTO)
204 target_compile_definitions(${PROJECT_NAME} PUBLIC "-DBYO_CRYPTO")
205 endif()
206
207 if (USE_S2N)
208 target_compile_definitions(${PROJECT_NAME} PRIVATE "-DUSE_S2N")
209 endif()
210
211 if (BUILD_RELOCATABLE_BINARIES)
212 target_compile_definitions(${PROJECT_NAME} PRIVATE "-DCOMPAT_MODE")
213 endif()
214
215 if (USE_VSOCK)
216 target_compile_definitions(${PROJECT_NAME} PUBLIC "-DUSE_VSOCK")
217 endif()
218
219 if (AWS_USE_APPLE_NETWORK_FRAMEWORK)
220 target_compile_definitions(${PROJECT_NAME} PUBLIC "-DAWS_USE_APPLE_NETWORK_FRAMEWORK")
221 endif()
222
223 if (AWS_USE_APPLE_DISPATCH_QUEUE)
224 target_compile_definitions(${PROJECT_NAME} PUBLIC "-DAWS_USE_APPLE_DISPATCH_QUEUE")
225 endif()
226
227 target_include_directories(${PROJECT_NAME} PUBLIC
228 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
229 $<INSTALL_INTERFACE:include>)
230
231 aws_use_package(aws-c-common)
232 aws_use_package(aws-c-cal)
233 target_link_libraries(${PROJECT_NAME} PUBLIC ${DEP_AWS_LIBS})
234 target_link_libraries(${PROJECT_NAME} PRIVATE ${PLATFORM_LIBS})
235
236 aws_prepare_shared_lib_exports(${PROJECT_NAME})
237
238 install(FILES ${AWS_IO_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/aws/io" COMPONENT Development)
239 install(FILES ${AWS_IO_TESTING_HEADERS} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/aws/testing" COMPONENT Development)
240
241 if (BUILD_SHARED_LIBS)
242 set (TARGET_DIR "shared")
243 else()
244 set (TARGET_DIR "static")
245 endif()
246
247 if(${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
248 # OpenBSD by defaults links with --execute-only, which is problematic because
249 # some AWS assembly sources still have references to static data in the .text section
250 if(NOT BUILD_SHARED_LIBS)
251 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--no-execute-only")
252 else()
253 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-execute-only")
254 endif()
255 endif()
256
257 install(EXPORT "${PROJECT_NAME}-targets"
258 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}/${TARGET_DIR}"
259 NAMESPACE AWS::
260 COMPONENT Development)
261
262 configure_file("cmake/${PROJECT_NAME}-config.cmake"
263 "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
264 @ONLY)
265
266 install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
267 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}/"
268 COMPONENT Development)
269
270 if (NOT CMAKE_CROSSCOMPILING)
271 if (BUILD_TESTING)
272 add_subdirectory(tests)
273 endif()
274 endif()