forked from eclipse-zenoh/zenoh-pico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
366 lines (311 loc) · 12.7 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
361
362
363
364
365
366
#
# Copyright (c) 2022 ZettaScale Technology
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# ZettaScale Zenoh Team, <[email protected]>
#
cmake_minimum_required(VERSION 3.8)
project(libzenohpico VERSION 0.7.0.1 LANGUAGES C)
option(BUILD_SHARED_LIBS "Build shared libraries if ON, otherwise build static libraries" ON)
option(ZENOH_DEBUG "Use this to set the ZENOH_DEBUG variable." 0)
option(WITH_ZEPHYR "Build for Zephyr RTOS" OFF)
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
set(BUILD_SHARED_LIBS "OFF")
elseif(CMAKE_SYSTEM_NAME MATCHES "Generic")
if(WITH_ZEPHYR)
set(BUILD_SHARED_LIBS "OFF")
endif()
endif()
set(Libname "zenohpico")
if(BUILD_SHARED_LIBS)
add_library(${Libname} SHARED)
else()
add_library(${Libname} STATIC)
endif()
function(add_definition value)
add_definitions(-D${value})
target_compile_definitions(${Libname} PUBLIC ${value})
endfunction()
if(NOT CMAKE_C_STANDARD)
if(c_std_11 IN_LIST CMAKE_C_COMPILE_FEATURES)
set(CMAKE_C_STANDARD 11)
message(STATUS "Setting C11 as the C Standard")
else()
set(CMAKE_C_STANDARD 99)
message(STATUS "Setting C99 as the C Standard")
endif()
endif()
set(CMAKE_C_STANDARD_REQUIRED TRUE)
add_definition(ZENOH_C_STANDARD=${CMAKE_C_STANDARD})
# while in development, use timestamp for patch version:
string(TIMESTAMP PROJECT_VERSION_PATCH "%Y%m%ddev")
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
# Use cmake .. -DCMAKE_BUILD_TYPE=DEBUG for debug
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RELEASE)
endif()
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
add_definition(ZENOH_LINUX)
elseif(CMAKE_SYSTEM_NAME MATCHES "BSD")
add_definition(ZENOH_BSD)
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
add_definition(ZENOH_MACOS)
set(MACOSX_RPATH "ON")
elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten")
add_definition(ZENOH_EMSCRIPTEN)
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
add_definition(ZENOH_WINDOWS)
add_definition(_CRT_SECURE_NO_WARNINGS)
add_definition(ZENOH_NO_STDATOMIC)
elseif(CMAKE_SYSTEM_NAME MATCHES "Generic")
if(WITH_ZEPHYR)
add_definition(ZENOH_ZEPHYR)
endif()
else()
message(FATAL_ERROR "zenoh-pico is not yet available on ${CMAKE_SYSTEM_NAME} platform")
return()
endif()
add_definition(ZENOH_DEBUG=${ZENOH_DEBUG})
# Print summary of CMAKE configurations
message(STATUS "Building in ${CMAKE_BUILD_TYPE} mode")
message(STATUS "Build shared library: ${BUILD_SHARED_LIBS}")
message(STATUS "Zenoh Level Log: ${ZENOH_DEBUG}")
message(STATUS "Build for Zephyr RTOS: ${WITH_ZEPHYR}")
message(STATUS "Configuring for ${CMAKE_SYSTEM_NAME}")
if(SKBUILD)
set(INSTALL_RPATH "zenoh")
set(INSTALL_NAME_DIR "zenoh")
set(INSTALL_INCLUDE_NAME_DIR "zenoh/include")
endif()
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
if(CMAKE_BUILD_TYPE MATCHES "DEBUG")
if(UNIX)
add_compile_options(-c -Wall -Wextra -Werror -Wunused -Wstrict-prototypes -pipe -g -O0)
elseif(MSVC)
add_compile_options(/W4 /WX /Od)
elseif(CMAKE_SYSTEM_NAME MATCHES "Generic")
add_compile_options(-Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes -pipe -g -O0)
endif()
elseif(CMAKE_BUILD_TYPE MATCHES "RELEASE")
if(UNIX)
add_compile_options(-pipe -O3)
elseif(MSVC)
# add_compile_options(/O2)
elseif(CMAKE_SYSTEM_NAME MATCHES "Generic")
add_compile_options(-pipe -O3)
endif()
endif()
file(GLOB PublicHeaders "include/*.h"
"include/zenoh-pico/*.h"
"include/zenoh-pico/api/*.h"
"include/zenoh-pico/collections/*.h"
"include/zenoh-pico/link/*.h"
"include/zenoh-pico/link/config/*.h"
"include/zenoh-pico/protocol/*.h"
"include/zenoh-pico/session/*.h"
"include/zenoh-pico/system/*.h"
"include/zenoh-pico/system/link/*.h"
"include/zenoh-pico/transport/*.h"
"include/zenoh-pico/utils/*.h"
)
target_include_directories(${Libname} PUBLIC ${PROJECT_SOURCE_DIR}/include)
file(GLOB Sources "src/*.c"
"src/api/*.c"
"src/net/*.c"
"src/collections/*.c"
"src/link/*.c"
"src/link/config/*.c"
"src/link/unicast/*.c"
"src/link/multicast/*.c"
"src/protocol/*.c"
"src/protocol/keyexpr/*.c"
"src/session/*.c"
"src/system/*.c"
"src/transport/*.c"
"src/transport/common/*.c"
"src/transport/unicast/*.c"
"src/transport/unicast/link/*.c"
"src/transport/unicast/link/task/*.c"
"src/transport/multicast/*.c"
"src/transport/multicast/link/*.c"
"src/transport/multicast/link/task/*.c"
"src/utils/*.c"
)
if(WITH_ZEPHYR)
file (GLOB Sources_Zephyr "src/system/zephyr/*.c")
list(APPEND Sources ${Sources_Zephyr})
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux" OR CMAKE_SYSTEM_NAME MATCHES "Darwin" OR CMAKE_SYSTEM_NAME MATCHES "BSD")
file (GLOB Sources_Unix "src/system/unix/*.c")
list(APPEND Sources ${Sources_Unix})
elseif(CMAKE_SYSTEM_NAME MATCHES "Emscripten")
file (GLOB Sources_Emscripten "src/system/emscripten/*.c")
list(APPEND Sources ${Sources_Emscripten})
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
file (GLOB Sources_Windows "src/system/windows/*.c")
list(APPEND Sources ${Sources_Windows})
endif()
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
link_directories(${LIBRARY_OUTPUT_PATH})
target_sources(${Libname} PRIVATE ${Sources})
target_link_libraries(${Libname} Threads::Threads)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
target_link_libraries(${Libname} rt)
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
target_link_libraries(${Libname} Ws2_32 Iphlpapi)
endif()
#
# Build tests, examples, intallation only when project is root
#
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
option(PACKAGING "Use option on Linux to produce Debian and RPM packages." OFF)
option(BUILD_EXAMPLES "Use this to also build the examples." ON)
option(BUILD_TOOLS "Use this to also build the tools." OFF)
option(BUILD_TESTING "Use this to also build tests." ON)
option(BUILD_INTEGRATION "Use this to also build integration tests." OFF)
message(STATUS "Produce Debian and RPM packages: ${PACKAGING}")
message(STATUS "Build examples: ${BUILD_EXAMPLES}")
message(STATUS "Build tools: ${BUILD_TOOLS}")
message(STATUS "Build tests: ${BUILD_TESTING}")
message(STATUS "Build integration: ${BUILD_INTEGRATION}")
install(TARGETS ${Libname}
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
COMPONENT Library
)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/zenoh-pico.h
DESTINATION include
COMPONENT Headers
)
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/zenoh-pico
DESTINATION include
COMPONENT Headers
)
if(BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
if(UNIX OR MSVC)
if(BUILD_TOOLS)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tools)
add_executable(z_keyexpr_canonizer ${PROJECT_SOURCE_DIR}/tools/z_keyexpr_canonizer.c)
target_link_libraries(z_keyexpr_canonizer ${Libname})
endif()
if(BUILD_TESTING)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
add_executable(z_data_struct_test ${PROJECT_SOURCE_DIR}/tests/z_data_struct_test.c)
add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c)
add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c)
add_executable(z_msgcodec_test ${PROJECT_SOURCE_DIR}/tests/z_msgcodec_test.c)
add_executable(z_keyexpr_test ${PROJECT_SOURCE_DIR}/tests/z_keyexpr_test.c)
add_executable(z_api_null_drop_test ${PROJECT_SOURCE_DIR}/tests/z_api_null_drop_test.c)
add_executable(z_api_double_drop_test ${PROJECT_SOURCE_DIR}/tests/z_api_double_drop_test.c)
target_link_libraries(z_data_struct_test ${Libname})
target_link_libraries(z_endpoint_test ${Libname})
target_link_libraries(z_iobuf_test ${Libname})
target_link_libraries(z_msgcodec_test ${Libname})
target_link_libraries(z_keyexpr_test ${Libname})
target_link_libraries(z_api_null_drop_test ${Libname})
target_link_libraries(z_api_double_drop_test ${Libname})
enable_testing()
add_test(z_data_struct_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_data_struct_test)
add_test(z_endpoint_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_endpoint_test)
add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test)
add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test)
add_test(z_keyexpr_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_keyexpr_test)
add_test(z_api_null_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_null_drop_test)
add_test(z_api_double_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_double_drop_test)
endif()
if(BUILD_MULTICAST)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
if(CMAKE_C_STANDARD MATCHES "11")
add_executable(z_peer_multicast_test ${PROJECT_SOURCE_DIR}/tests/z_peer_multicast_test.c)
target_link_libraries(z_peer_multicast_test ${Libname})
configure_file(${PROJECT_SOURCE_DIR}/tests/multicast.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh COPYONLY)
enable_testing()
add_test(z_peer_multicast_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh z_peer_multicast_test)
endif()
endif()
if(BUILD_INTEGRATION)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
if(CMAKE_C_STANDARD MATCHES "11")
add_executable(z_client_test ${PROJECT_SOURCE_DIR}/tests/z_client_test.c)
add_executable(z_api_alignment_test ${PROJECT_SOURCE_DIR}/tests/z_api_alignment_test.c)
add_executable(z_session_test ${PROJECT_SOURCE_DIR}/tests/z_session_test.c)
target_link_libraries(z_client_test ${Libname})
target_link_libraries(z_api_alignment_test ${Libname})
target_link_libraries(z_session_test ${Libname})
configure_file(${PROJECT_SOURCE_DIR}/tests/routed.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh COPYONLY)
configure_file(${PROJECT_SOURCE_DIR}/tests/api.sh ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh COPYONLY)
enable_testing()
add_test(z_client_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/routed.sh z_client_test)
add_test(z_api_alignment_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh z_api_alignment_test)
add_test(z_session_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/api.sh z_session_test)
endif()
endif()
endif()
# For packaging
if(PACKAGING)
set(CPACK_PACKAGE_DIRECTORY "${CMAKE_BINARY_DIR}/packages")
set(CPACK_COMPONENTS_ALL Library Headers)
set(CPACK_COMPONENT_LIBRARY_GROUP "lib")
set(CPACK_COMPONENT_HEADERS_GROUP "dev")
set(CPACK_COMPONENT_HEADERS_DEPENDS Library)
set(CPACK_PACKAGE_CHECKSUM MD5)
set(CPACK_PACKAGE_VENDOR "The Eclipse Foundation")
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
set(CPACK_COMPONENT_LIB_DESCRIPTION "The C client library for Eclipse zenoh targeting pico devices")
set(CPACK_COMPONENT_DEV_DESCRIPTION "${CPACK_COMPONENT_LIB_DESCRIPTION} - devel files")
# Sources package
set(CPACK_SOURCE_GENERATOR "TGZ")
set(CPACK_SOURCE_IGNORE_FILES "/.git/;/.github/;/build/;/crossbuilds/")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-src-${PROJECT_VERSION}")
if(PACKAGING MATCHES "DEB")
if(NOT DEBARCH)
set(DEBARCH ${CMAKE_SYSTEM_PROCESSOR})
endif()
message(STATUS "Configure DEB packaging for Linux ${DEBARCH}")
if(CPACK_GENERATOR)
set(CPACK_GENERATOR "${CPACK_GENERATOR};DEB")
else()
set(CPACK_GENERATOR "DEB")
endif()
# DEB package
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "ZettaScale Zenoh Team, <[email protected]>")
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${DEBARCH})
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
set(CPACK_DEBIAN_LIB_PACKAGE_NAME ${PROJECT_NAME}) # avoid "-lib" suffix for "lib" package
set(CPACK_DEBIAN_LIB_PACKAGE_DEPENDS "libc6 (>=2.12)")
set(CPACK_DEBIAN_DEV_PACKAGE_DEPENDS "${CPACK_DEBIAN_LIB_PACKAGE_NAME} (=${PROJECT_VERSION})")
endif()
if(PACKAGING MATCHES "RPM")
if(NOT RPMARCH)
set(RPMARCH ${CMAKE_SYSTEM_PROCESSOR})
endif()
message(STATUS "Configure RPM packaging for Linux ${RPMARCH}")
if(CPACK_GENERATOR)
set(CPACK_GENERATOR "${CPACK_GENERATOR};RPM")
else()
set(CPACK_GENERATOR "RPM")
endif()
# RPM package
set(CPACK_RPM_PACKAGE_ARCHITECTURE ${RPMARCH})
set(CPACK_RPM_COMPONENT_INSTALL ON)
set(CPACK_RPM_FILE_NAME RPM-DEFAULT)
set(CPACK_RPM_LIB_PACKAGE_NAME ${PROJECT_NAME}) # avoid "-lib" suffix for "lib" package
set(CPACK_RPM_DEV_PACKAGE_REQUIRES "${CPACK_RPM_LIB_PACKAGE_NAME} = ${PROJECT_VERSION}")
endif()
include(CPack)
endif()
endif()