/
CMakeLists.txt
360 lines (331 loc) · 13 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#
# Copyright(c) 2006 to 2022 ZettaScale Technology and others
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v. 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
# v. 1.0 which is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#
include(GenerateExportHeader)
include(CheckCSourceCompiles)
include(CheckLibraryExists)
include(CheckIncludeFile)
include(CheckTypeSize)
include(CheckSymbolExists)
# Lightweight IP stack can be used on non-embedded targets too, but the
# runtime must be instructed to use it instead of the native stack. Of course
# for embedded targets there is no "native" stack and the runtime module must
# always be instructed to use an "alternative" stack.
option(WITH_LWIP "Use lightweight IP stack" OFF)
option(WITH_FREERTOS "Build for FreeRTOS" OFF)
set(source_dir "${CMAKE_CURRENT_SOURCE_DIR}")
set(binary_dir "${CMAKE_CURRENT_BINARY_DIR}")
add_library(ddsrt INTERFACE)
target_include_directories(
ddsrt INTERFACE
"$<BUILD_INTERFACE:${source_dir}/include>"
"$<BUILD_INTERFACE:${source_dir}/src>"
"$<BUILD_INTERFACE:${binary_dir}/include>")
set(headers
"${binary_dir}/include/dds/config.h"
"${source_dir}/include/dds/ddsrt/avl.h"
"${source_dir}/include/dds/ddsrt/bits.h"
"${source_dir}/include/dds/ddsrt/fibheap.h"
"${source_dir}/include/dds/ddsrt/hopscotch.h"
"${source_dir}/include/dds/ddsrt/log.h"
"${source_dir}/include/dds/ddsrt/retcode.h"
"${source_dir}/include/dds/ddsrt/attributes.h"
"${source_dir}/include/dds/ddsrt/endian.h"
"${source_dir}/include/dds/ddsrt/arch.h"
"${source_dir}/include/dds/ddsrt/misc.h"
"${source_dir}/include/dds/ddsrt/mh3.h"
"${source_dir}/include/dds/ddsrt/io.h"
"${source_dir}/include/dds/ddsrt/process.h"
"${source_dir}/include/dds/ddsrt/sched.h"
"${source_dir}/include/dds/ddsrt/strtod.h"
"${source_dir}/include/dds/ddsrt/strtol.h"
"${source_dir}/include/dds/ddsrt/types.h"
"${source_dir}/include/dds/ddsrt/countargs.h"
"${source_dir}/include/dds/ddsrt/static_assert.h"
"${source_dir}/include/dds/ddsrt/circlist.h"
"${source_dir}/include/dds/ddsrt/atomics.h"
"${source_dir}/include/dds/ddsrt/atomics/arm.h"
"${source_dir}/include/dds/ddsrt/atomics/gcc.h"
"${source_dir}/include/dds/ddsrt/atomics/msvc.h"
"${source_dir}/include/dds/ddsrt/atomics/sun.h"
"${source_dir}/include/dds/ddsrt/dynlib.h"
"${source_dir}/include/dds/ddsrt/environ.h"
"${source_dir}/include/dds/ddsrt/heap.h"
"${source_dir}/include/dds/ddsrt/ifaddrs.h"
"${source_dir}/include/dds/ddsrt/md5.h"
"${source_dir}/include/dds/ddsrt/netstat.h"
"${source_dir}/include/dds/ddsrt/string.h"
"${source_dir}/include/dds/ddsrt/sockets.h"
"${source_dir}/src/sockets_priv.h"
"${source_dir}/include/dds/ddsrt/threads.h"
"${source_dir}/src/threads_priv.h"
"${source_dir}/include/dds/ddsrt/cdtors.h"
"${source_dir}/include/dds/ddsrt/random.h"
"${source_dir}/include/dds/ddsrt/align.h")
set(sources
"${source_dir}/src/atomics.c"
"${source_dir}/src/avl.c"
"${source_dir}/src/dynlib.c"
"${source_dir}/src/bits.c"
"${source_dir}/src/bswap.c"
"${source_dir}/src/io.c"
"${source_dir}/src/log.c"
"${source_dir}/src/retcode.c"
"${source_dir}/src/strtod.c"
"${source_dir}/src/strtol.c"
"${source_dir}/src/mh3.c"
"${source_dir}/src/environ.c"
"${source_dir}/src/expand_vars.c"
"${source_dir}/src/fibheap.c"
"${source_dir}/src/hopscotch.c"
"${source_dir}/src/circlist.c"
"${source_dir}/src/threads.c"
"${source_dir}/src/string.c"
"${source_dir}/src/sockets.c"
"${source_dir}/src/md5.c"
"${source_dir}/src/xmlparser.c"
"${source_dir}/src/ifaddrs.c"
"${source_dir}/src/cdtors.c"
"${source_dir}/src/random.c"
"${source_dir}/src/time.c")
# Not every target offers the same set of features. For embedded targets the
# set of features may even be different between builds. e.g. a FreeRTOS build
# could use the lightweight IP stack, but later change to FreeRTOS+TCP.
#
# Most features and target specific settings can be determined at compile time
# by a combination of pre-defined macros. However, some features require input
# from the build system. e.g. that the target operating system is FreeRTOS or
# that the network stack to be used is lwIP as opposed to the target native
# network stack. In order to mix-and-match various compilers, architectures,
# operating systems, etc input from the build system is required.
if(WITH_LWIP)
set(CMAKE_EXTRA_INCLUDE_FILES "ws2tcpip.h")
set(hostname_header "unistd.h")
set(inet_header "lwip/sockets.h")
set(netdb_header "lwip/netdb.h")
list(APPEND headers
"${source_dir}/include/dds/ddsrt/sockets/posix.h")
list(APPEND sources
"${source_dir}/src/ifaddrs/lwip/ifaddrs.c"
"${source_dir}/src/sockets/posix/socket.c"
"${source_dir}/src/sockets/posix/gethostname.c")
else()
if(WIN32)
if(POLICY CMP0075)
# suppress warnings concerning a change in behaviour of check_include_files in
# combination with CMAKE_REQUIRED_LIBRARIES
cmake_policy(SET CMP0075 NEW)
endif()
set(CMAKE_EXTRA_INCLUDE_FILES "ws2tcpip.h")
set(CMAKE_REQUIRED_LIBRARIES ws2_32 iphlpapi)
set(hostname_header "winsock2.h")
set(inet_header "ws2tcpip.h")
set(netdb_header "ws2tcpip.h")
list(APPEND headers
"${source_dir}/include/dds/ddsrt/sockets/windows.h")
list(APPEND sources
"${source_dir}/src/ifaddrs/windows/ifaddrs.c"
"${source_dir}/src/sockets/windows/socket.c"
"${source_dir}/src/sockets/windows/gethostname.c")
else()
set(CMAKE_EXTRA_INCLUDE_FILES "netinet/in.h")
set(hostname_header "unistd.h")
set(inet_header "arpa/inet.h")
set(netdb_header "netdb.h")
list(APPEND headers
"${source_dir}/include/dds/ddsrt/sockets/posix.h")
list(APPEND sources
"${source_dir}/src/sockets/posix/socket.c"
"${source_dir}/src/sockets/posix/gethostname.c")
if(WITH_ZEPHYR)
list(APPEND sources
"${source_dir}/src/ifaddrs/zephyr/ifaddrs.c")
else()
list(APPEND sources
"${source_dir}/src/ifaddrs/posix/ifaddrs.c")
endif()
endif()
endif()
check_symbol_exists("gethostname" ${hostname_header} DDSRT_HAVE_GETHOSTNAME)
check_symbol_exists("inet_ntop" ${inet_header} DDSRT_HAVE_INET_NTOP)
check_symbol_exists("inet_pton" ${inet_header} DDSRT_HAVE_INET_PTON)
check_symbol_exists("getaddrinfo" ${netdb_header} DDSRT_HAVE_GETADDRINFO)
check_symbol_exists("gethostbyname_r" ${netdb_header} DDSRT_HAVE_GETHOSTBYNAME_R)
if(DDSRT_HAVE_GETADDRINFO OR DDSRT_HAVE_GETHOSTBYNAME_R)
set(DDSRT_HAVE_DNS TRUE)
endif()
check_type_size("struct sockaddr_in6" SIZEOF_SOCKADDR_IN6)
if(ENABLE_IPV6)
if(SIZEOF_SOCKADDR_IN6)
set(DDSRT_HAVE_IPV6 TRUE)
endif()
endif()
if(WITH_FREERTOS)
list(APPEND headers
"${source_dir}/include/dds/ddsrt/sync/freertos.h"
"${source_dir}/include/dds/ddsrt/threads/freertos.h"
"${source_dir}/include/dds/ddsrt/time/freertos.h")
list(APPEND sources
"${source_dir}/src/environ/posix/environ.c"
"${source_dir}/src/heap/freertos/heap.c"
"${source_dir}/src/sync/freertos/sync.c"
"${source_dir}/src/sync/freertos/tasklist.c"
"${source_dir}/src/threads/freertos/threads.c"
"${source_dir}/src/time/freertos/time.c"
"${source_dir}/src/process/freertos/process.c"
"${source_dir}/src/random/posix/random.c")
string(CONCAT rusage_code
"#include <FreeRTOS.h>\n"
"#if configUSE_TRACE_FACILITY == 1 && configGENERATE_RUN_TIME_STATS == 1\n"
"int main(int argc, char *argv[]) { return 0; }\n"
"#endif\n")
check_c_source_compiles("${rusage_code}" DDSRT_HAVE_RUSAGE)
if(DDSRT_HAVE_RUSAGE)
list(APPEND sources
"${source_dir}/src/rusage/freertos/rusage.c")
endif()
elseif(WITH_ZEPHYR)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
target_link_libraries(ddsrt INTERFACE Threads::Threads)
list(APPEND headers
"${source_dir}/include/dds/ddsrt/sync/posix.h"
"${source_dir}/include/dds/ddsrt/threads/posix.h"
"${source_dir}/include/dds/ddsrt/types/posix.h")
list(APPEND sources
"${source_dir}/src/environ/zephyr/environ.c"
"${source_dir}/src/heap/posix/heap.c"
"${source_dir}/src/process/posix/process.c"
"${source_dir}/src/random/posix/random.c"
"${source_dir}/src/sync/posix/sync.c"
"${source_dir}/src/threads/posix/threads.c"
"${source_dir}/src/time/posix/time.c")
else()
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads REQUIRED)
target_link_libraries(ddsrt INTERFACE Threads::Threads)
target_link_libraries(ddsrt INTERFACE ${CMAKE_DL_LIBS})
set(DDSRT_HAVE_DYNLIB TRUE)
set(DDSRT_HAVE_FILESYSTEM TRUE)
list(APPEND sources
"${source_dir}/src/heap/posix/heap.c")
if(WIN32)
set(DDSRT_HAVE_NETSTAT TRUE)
set(DDSRT_HAVE_RUSAGE TRUE)
list(APPEND headers
"${source_dir}/include/dds/ddsrt/filesystem/windows.h"
"${source_dir}/include/dds/ddsrt/sync/windows.h"
"${source_dir}/include/dds/ddsrt/threads/windows.h"
"${source_dir}/include/dds/ddsrt/types/windows.h")
list(APPEND sources
"${source_dir}/src/dynlib/windows/dynlib.c"
"${source_dir}/src/environ/windows/environ.c"
"${source_dir}/src/filesystem/windows/filesystem.c"
"${source_dir}/src/netstat/windows/netstat.c"
"${source_dir}/src/process/windows/process.c"
"${source_dir}/src/random/windows/random.c"
"${source_dir}/src/rusage/windows/rusage.c"
"${source_dir}/src/sync/windows/sync.c"
"${source_dir}/src/threads/windows/threads.c"
"${source_dir}/src/time/windows/time.c")
target_link_libraries(ddsrt INTERFACE ws2_32 iphlpapi bcrypt)
else()
list(APPEND headers
"${source_dir}/include/dds/ddsrt/filesystem/posix.h"
"${source_dir}/include/dds/ddsrt/sync/posix.h"
"${source_dir}/include/dds/ddsrt/threads/posix.h"
"${source_dir}/include/dds/ddsrt/types/posix.h")
list(APPEND sources
"${source_dir}/src/dynlib/posix/dynlib.c"
"${source_dir}/src/environ/posix/environ.c"
"${source_dir}/src/filesystem/posix/filesystem.c"
"${source_dir}/src/process/posix/process.c"
"${source_dir}/src/random/posix/random.c"
"${source_dir}/src/sync/posix/sync.c"
"${source_dir}/src/threads/posix/threads.c")
if(CMAKE_SYSTEM MATCHES "Linux")
set(DDSRT_HAVE_NETSTAT TRUE)
list(APPEND sources
"${source_dir}/src/netstat/linux/netstat.c"
"${source_dir}/src/time/posix/time.c")
elseif(CMAKE_SYSTEM MATCHES "Darwin")
set(DDSRT_HAVE_NETSTAT TRUE)
list(APPEND sources
"${source_dir}/src/netstat/darwin/netstat.c"
"${source_dir}/src/time/darwin/time.c")
else()
list(APPEND sources
"${source_dir}/src/time/posix/time.c")
endif()
if(CMAKE_SYSTEM MATCHES "Darwin|Linux")
set(DDSRT_HAVE_RUSAGE TRUE)
list(APPEND sources
"${source_dir}/src/rusage/posix/rusage.c")
endif()
check_library_exists(c clock_gettime "" HAVE_CLOCK_GETTIME)
if(NOT HAVE_CLOCK_GETTIME)
# Before glibc 2.17, clock_gettime was in librt.
check_library_exists(rt clock_gettime "time.h" HAVE_CLOCK_GETTIME_RT)
if(HAVE_CLOCK_GETTIME_RT)
set(HAVE_CLOCK_GETTIME TRUE)
target_link_libraries(ddsrt INTERFACE rt)
endif()
endif()
if(CMAKE_C_COMPILER_ID STREQUAL "SunPro")
target_link_libraries(ddsrt INTERFACE socket nsl)
elseif(CMAKE_SYSTEM_NAME MATCHES QNX)
target_link_libraries(ddsrt INTERFACE socket m)
endif()
endif()
endif()
# Generate configuration file
set(DDSRT_WITH_LWIP ${WITH_LWIP})
set(DDSRT_WITH_FREERTOS ${WITH_FREERTOS})
foreach(feature TCP_TLS SECURITY LIFESPAN DEADLINE_MISSED NETWORK_PARTITIONS
SSM TYPELIB TYPE_DISCOVERY TOPIC_DISCOVERY)
set(DDS_HAS_${feature} ${ENABLE_${feature}})
endforeach()
if(BUILD_TESTING)
set(DDS_ALLOW_NESTED_DOMAIN 1)
endif()
if (NOT BUILD_SHARED_LIBS)
set(DDS_IS_STATIC_LIBRARY 1)
endif()
configure_file(include/dds/config.h.in include/dds/config.h)
configure_file(include/dds/features.h.in include/dds/features.h)
configure_file(include/dds/version.h.in include/dds/version.h)
list(APPEND headers
"${binary_dir}/include/dds/config.h"
"${binary_dir}/include/dds/features.h"
"${binary_dir}/include/dds/version.h")
target_sources(ddsrt INTERFACE ${headers} ${sources})
# Add library for internal use, i.e. for ddsconf and cunit_ddsrt, to avoid
# having to compile the same sources twice.
add_library(ddsrt-internal STATIC)
generate_export_header(
ddsrt-internal BASE_NAME DDS EXPORT_FILE_NAME ddsrt-internal/include/dds/export.h)
target_link_libraries(
ddsrt-internal PUBLIC $<TARGET_PROPERTY:ddsrt,INTERFACE_LINK_LIBRARIES>)
target_include_directories(
ddsrt-internal PUBLIC ${binary_dir}/ddsrt-internal/include
$<TARGET_PROPERTY:ddsrt,INTERFACE_INCLUDE_DIRECTORIES>)
target_sources(
ddsrt-internal PRIVATE $<TARGET_PROPERTY:ddsrt,INTERFACE_SOURCES>)
if(BUILD_TESTING)
add_subdirectory(tests)
endif()
install(
DIRECTORY
"${source_dir}/include/"
"${binary_dir}/include/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
COMPONENT dev
FILES_MATCHING PATTERN "*.h")