-
Notifications
You must be signed in to change notification settings - Fork 139
/
CMakeLists.txt
305 lines (266 loc) · 10.1 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
cmake_minimum_required(VERSION 3.13)
project(cnats)
include(CTest)
include(FindPackageHandleStandardArgs)
include(CMakePackageConfigHelpers)
# Uncomment to have the build process verbose
# set(CMAKE_VERBOSE_MAKEFILE TRUE)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
# Set output directories for libraries and executables.
# This is important for Windows builds to have the DLLs in the same directory as the executables.
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Set a default build type if none was specified
set(default_build_type "Release")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE
STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Release" "Debug" "MinSizeRel" "RelWithDebInfo")
endif()
option(NATS_UPDATE_VERSION "Update the version file" OFF)
option(NATS_UPDATE_DOC "Update the doc template file" OFF)
option(NATS_COVERAGE "Code coverage" OFF)
option(NATS_BUILD_NO_SPIN "Enable if spin code does not compile on your arch" OFF)
option(NATS_BUILD_WITH_TLS "Build with TLS support" ON)
option(NATS_BUILD_TLS_FORCE_HOST_VERIFY "Forces hostname verification" ON)
option(NATS_BUILD_TLS_USE_OPENSSL_1_1_API "Build for OpenSSL 1.1+" ON)
option(NATS_BUILD_USE_SODIUM "Build using libsodium library" OFF)
option(NATS_BUILD_EXAMPLES "Build examples" ON)
option(NATS_BUILD_LIBUV_EXAMPLE "Build libuv examples" OFF)
option(NATS_BUILD_LIBEVENT_EXAMPLE "Build libevent examples" OFF)
option(NATS_BUILD_STATIC_EXAMPLES "Statically link examples" OFF)
option(NATS_BUILD_STREAMING "Build NATS Streaming" ON)
option(NATS_BUILD_NO_PREFIX_CONNSTS "No prefix for connection status enum" OFF)
option(NATS_BUILD_LIB_STATIC "Build static library" ON)
option(NATS_BUILD_LIB_SHARED "Build shared library" ON)
option(NATS_COMPILER_HARDENING "Compiler hardening flags" OFF)
if(UNIX AND APPLE)
option(CMAKE_MACOSX_RPATH "Build with macOS RPath" ON)
endif()
if(DEFINED ENV{NATS_SANITIZE})
set(NATS_STANITIZE "$ENV{NATS_SANITIZE}")
mark_as_advanced(NATS_SANITIZE)
endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_BINARY_DIR})
if(NATS_COVERAGE)
message("-- Coverage Enabled")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
endif(NATS_COVERAGE)
if(NATS_BUILD_WITH_TLS)
set(OPENSSL_USE_STATIC_LIBS ${NATS_BUILD_OPENSSL_STATIC_LIBS})
find_package(OpenSSL REQUIRED)
endif(NATS_BUILD_WITH_TLS)
set(LIBUV_DIR "" CACHE PATH "Libuv install directory")
set(LIBEVENT_DIR "" CACHE PATH "Libevent install directory")
set(NATS_DOC_PROJECT_NAME "NATS C Client with JetStream support")
if(NATS_BUILD_STREAMING)
IF(DEFINED ENV{NATS_PROTOBUF_DIR})
SET(NATS_PROTOBUF_DIR "$ENV{NATS_PROTOBUF_DIR}")
ENDIF()
FIND_PATH(
NATS_PROTOBUF_INCLUDE_DIR
protobuf-c/protobuf-c.h
HINTS
${NATS_PROTOBUF_DIR}
)
FIND_LIBRARY(NATS_PROTOBUF_LIBRARY
NAMES libprotobuf-c.so libprotobuf-c.dylib libprotobuf-c.a protobuf-c.lib protobuf-c.dll
HINTS ${NATS_PROTOBUF_DIR}
)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(libprotobuf-c DEFAULT_MSG
NATS_PROTOBUF_INCLUDE_DIR
NATS_PROTOBUF_LIBRARY
)
IF(libprotobuf-c_FOUND)
SET(NATS_PROTOBUF_INCLUDE_DIRS ${NATS_PROTOBUF_INCLUDE_DIR})
SET(NATS_PROTOBUF_LIBRARIES ${NATS_PROTOBUF_LIBRARY})
MARK_AS_ADVANCED(
NATS_PROTOBUF_LIBRARY
NATS_PROTOBUF_INCLUDE_DIR
NATS_PROTOBUF_DIR
)
ELSE()
SET(NATS_PROTOBUF_DIR "" CACHE STRING
"An optional hint to a directory for finding `libprotobuf-c`"
)
MESSAGE(FATAL_ERROR
"Could not find libprotobuf-c package. Check build instructions: https://github.com/nats-io/nats.c#building-with-streaming")
ENDIF()
add_definitions(-DNATS_HAS_STREAMING)
set(NATS_DOC_INCLUDE_STREAMING "NATS_HAS_STREAMING")
set(NATS_DOC_PROJECT_NAME "NATS C Client with JetStream and Streaming support")
endif(NATS_BUILD_STREAMING)
if(NATS_BUILD_USE_SODIUM)
IF(DEFINED ENV{NATS_SODIUM_DIR})
SET(NATS_SODIUM_DIR "$ENV{NATS_SODIUM_DIR}")
ENDIF()
FIND_PATH(
NATS_SODIUM_INCLUDE_DIR
sodium.h
HINTS
${NATS_SODIUM_DIR}
)
FIND_LIBRARY(NATS_SODIUM_LIBRARY
NAMES libsodium.so libsodium.dylib libsodium.a libsodium.lib libsodium.dll
HINTS ${NATS_SODIUM_DIR}
)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(libsodium DEFAULT_MSG
NATS_SODIUM_INCLUDE_DIR
NATS_SODIUM_LIBRARY
)
IF(libsodium_FOUND)
SET(NATS_SODIUM_INCLUDE_DIRS ${NATS_SODIUM_INCLUDE_DIR})
SET(NATS_SODIUM_LIBRARIES ${NATS_SODIUM_LIBRARY})
MARK_AS_ADVANCED(
NATS_SODIUM_LIBRARY
NATS_SODIUM_INCLUDE_DIR
NATS_SODIUM_DIR
)
ELSE()
SET(NATS_SODIUM_DIR "" CACHE STRING
"An optional hint to a directory for finding `libsodium`"
)
MESSAGE(FATAL_ERROR
"Could not find libsodium package. Check build instructions: https://github.com/nats-io/nats.c#building-with-libsodium")
ENDIF()
add_definitions(-DNATS_USE_LIBSODIUM)
endif(NATS_BUILD_USE_SODIUM)
if(NATS_BUILD_NO_PREFIX_CONNSTS)
add_definitions(-DNATS_CONN_STATUS_NO_PREFIX)
endif(NATS_BUILD_NO_PREFIX_CONNSTS)
# Platform specific settings
if(UNIX)
#---------------------------------------------------------------------------
# Define NATS cache variables that override the CMAKE and MEMCHECK variables
#---------------------------------------------------------------------------
include(GNUInstallDirs)
set(NATS_LIBDIR ${CMAKE_INSTALL_LIBDIR})
set(NATS_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
set(NATS_BUILD_ARCH "64" CACHE STRING "32 for 32bits builds")
find_program(MEMORYCHECK_COMMAND valgrind)
set(NATS_MEMCHECK_CMD_OPTS "--leak-check=full --track-fds=yes --show-reachable=yes --num-callers=50" CACHE STRING "Memcheck options")
set(MEMORYCHECK_COMMAND_OPTIONS ${NATS_MEMCHECK_CMD_OPTS} CACHE INTERNAL "")
set(NATS_COMMON_C_FLAGS "-std=c99 -pedantic")
# Some compiler hardening flags.
if(NATS_COMPILER_HARDENING)
set(NATS_COMMON_C_FLAGS "${NATS_COMMON_C_FLAGS} -fPIE -fstack-protector-all -D_FORTIFY_SOURCE=2 -O1")
# Works only with GCC.
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(NATS_COMMON_C_FLAGS "${NATS_COMMON_C_FLAGS} -Wl,-z,relro,-z,now -pie")
endif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
endif(NATS_COMPILER_HARDENING)
set(NATS_WARNINGS "-fstrict-aliasing -Wall -W -Wno-unused-parameter -Wno-unused-function -Wstrict-prototypes -Wwrite-strings")
set(NATS_PLATFORM_INCLUDE "unix")
if(APPLE)
set(NATS_OS "DARWIN")
else(APPLE)
set(NATS_OS "LINUX")
set(NATS_USE_PTHREAD "-pthread")
if(NOT ANDROID)
set(NATS_EXTRA_LIB "rt -lpthread")
endif()
endif(APPLE)
if (${NATS_BUILD_ARCH} MATCHES "32")
if(NOT APPLE)
message("-----------------------------------------------------------")
message("If build fails, you probably need to install libc6-dev-i386")
message("apt-get install libc6-dev-i386")
message("-----------------------------------------------------------")
endif(NOT APPLE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
set(CMAKE_C_LINKER_FLAGS "${CMAKE_C_LINKER_FLAGS} -m32")
endif(${NATS_BUILD_ARCH} MATCHES "32")
elseif(WIN32)
set(NATS_LIBDIR "lib")
set(NATS_INCLUDE_DIR "include")
set(NATS_EXTRA_LIB "ws2_32")
set(NATS_OS "_WIN32")
set(NATS_PLATFORM_INCLUDE "win")
# Warning control.
add_compile_options(/W4) # Set warning level to maximum, then disable:
add_compile_options(/wd4100) # unreferenced formal parameter
add_compile_options(/wd4200) # nonstandard extension used: zero-sized array in struct/union
add_compile_options(/wd4130) # logical operation on address of string constant
add_compile_options(/wd4127) # conditional expression is constant
if(sodium_USE_STATIC_LIBS)
add_definitions(
-DSODIUM_STATIC
-DSODIUM_EXPORT=
)
endif(sodium_USE_STATIC_LIBS)
endif(UNIX)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${NATS_COMMON_C_FLAGS} ${NATS_USE_PTHREAD} ${NATS_WARNINGS}")
if(NATS_SANITIZE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-omit-frame-pointer -fno-optimize-sibling-calls")
endif(NATS_SANITIZE)
if(UNIX)
if(APPLE)
else(APPLE)
add_definitions(-D_GNU_SOURCE)
if(NATS_BUILD_NO_SPIN)
add_definitions(-DNATS_NO_SPIN)
endif(NATS_BUILD_NO_SPIN)
endif(APPLE)
endif(UNIX)
add_definitions(-D${NATS_OS})
add_definitions(-D_REENTRANT)
if(NATS_BUILD_WITH_TLS)
add_definitions(-DNATS_HAS_TLS)
if(NATS_BUILD_TLS_USE_OPENSSL_1_1_API)
add_definitions(-DNATS_USE_OPENSSL_1_1)
endif(NATS_BUILD_TLS_USE_OPENSSL_1_1_API)
if(NATS_BUILD_TLS_FORCE_HOST_VERIFY)
add_definitions(-DNATS_FORCE_HOST_VERIFICATION)
endif(NATS_BUILD_TLS_FORCE_HOST_VERIFY)
endif(NATS_BUILD_WITH_TLS)
#------------
# Versionning and Doc
set(NATS_VERSION_MAJOR 3)
set(NATS_VERSION_MINOR 10)
set(NATS_VERSION_PATCH 0)
set(NATS_VERSION_SUFFIX "-beta")
set(NATS_VERSION_REQUIRED_NUMBER 0x030900)
write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/cnats-config-version.cmake"
COMPATIBILITY SameMinorVersion
VERSION ${NATS_VERSION_MAJOR}.${NATS_VERSION_MINOR}.${NATS_VERSION_PATCH}${NATS_VERSION_SUFFIX})
if(NATS_UPDATE_VERSION OR NATS_UPDATE_DOC)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in
${CMAKE_CURRENT_SOURCE_DIR}/src/version.h
@ONLY)
configure_file(
${CMAKE_SOURCE_DIR}/doc/DoxyFile.NATS.Client.in
${CMAKE_SOURCE_DIR}/doc/DoxyFile.NATS.Client
@ONLY)
endif(NATS_UPDATE_VERSION OR NATS_UPDATE_DOC)
#------------
#------------
# pkg-config
if(UNIX)
configure_file(
${PROJECT_SOURCE_DIR}/src/libnats.pc.in
${PROJECT_BINARY_DIR}/libnats.pc
@ONLY
)
install (
FILES "${PROJECT_BINARY_DIR}/libnats.pc"
DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
endif(UNIX)
#------------
#----------------------------
# Add the project directories
add_subdirectory(src)
add_subdirectory(examples)
add_subdirectory(examples/getstarted)
if(NATS_BUILD_STREAMING)
add_subdirectory(examples/stan)
endif()
add_subdirectory(test)
add_subdirectory(test/dylib)
#----------------------------