forked from zeromq/pyzmq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
349 lines (310 loc) · 10.9 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
# 3.28 needed for EXCLUDE_FROM_ALL
cmake_minimum_required(VERSION 3.28)
project(${SKBUILD_PROJECT_NAME} LANGUAGES C CXX)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
find_package(
Python
COMPONENTS Interpreter Development.Module
REQUIRED)
set(ZMQ_PREFIX "auto" CACHE STRING "libzmq installation prefix or 'bundled'")
option(ZMQ_DRAFT_API "whether to build the libzmq draft API" OFF)
set(LIBZMQ_BUNDLED_VERSION "4.3.5" CACHE STRING "libzmq version when bundling")
set(LIBSODIUM_BUNDLED_VERSION "1.0.19" CACHE STRING "libsodium version when bundling")
set(LIBZMQ_BUNDLED_URL "" CACHE STRING "full URL to download bundled libzmq")
set(LIBSODIUM_BUNDLED_URL "" CACHE STRING "full URL to download bundled libsodium")
set(LIBSODIUM_CONFIGURE_ARGS "" CACHE STRING "semicolon-separated list of arguments to pass to ./configure for bundled libsodium")
set(LIBSODIUM_MSBUILD_ARGS "" CACHE STRING "semicolon-separated list of arguments to pass to msbuild for bundled libsodium")
set(LIBSODIUM_VS_VERSION "" CACHE STRING "Visual studio solution version for bundled libsodium (default: detect from MSVC_VERSION)")
if (NOT CMAKE_BUILD_TYPE)
# default to Release
set(CMAKE_BUILD_TYPE "Release")
endif()
# get options from env
if (DEFINED ENV{ZMQ_PREFIX})
set(ZMQ_PREFIX "$ENV{ZMQ_PREFIX}")
endif()
if (DEFINED ENV{ZMQ_DRAFT_API})
if ("$ENV{ZMQ_DRAFT_API}" STREQUAL "1")
set(ZMQ_DRAFT_API TRUE)
else()
set(ZMQ_DRAFT_API FALSE)
endif()
endif()
# load options from env with PYZMQ_ prefix
foreach(_optname
LIBZMQ_BUNDLED_VERSION
LIBZMQ_BUNDLED_URL
LIBSODIUM_BUNDLED_VERSION
LIBSODIUM_BUNDLED_URL
LIBSODIUM_CONFIGURE_ARGS
LIBSODIUM_MSBUILD_ARGS
LIBSODIUM_VS_VERSION
)
if (DEFINED ENV{PYZMQ_${_optname}})
if (_optname MATCHES ".*_ARGS")
# if it's an _ARGS, split "-a -b" into "-a" "-b"
# use native CMake lists for cmake args,
# native command-line strings for env variables
separate_arguments(${_optname} NATIVE_COMMAND "$ENV{PYZMQ_${_optname}}")
else()
set(${_optname} "$ENV{PYZMQ_${_optname}}")
endif()
endif()
endforeach()
if(ZMQ_DRAFT_API)
message(STATUS "enabling ZMQ_DRAFT_API")
add_compile_definitions(ZMQ_BUILD_DRAFT_API=1)
endif()
if (LIBSODIUM_BUNDLED_VERSION AND NOT LIBSODIUM_BUNDLED_URL)
set(LIBSODIUM_BUNDLED_URL "https://download.libsodium.org/libsodium/releases/libsodium-${LIBSODIUM_BUNDLED_VERSION}.tar.gz")
endif()
if (LIBZMQ_BUNDLED_VERSION AND NOT LIBZMQ_BUNDLED_URL)
set(LIBZMQ_BUNDLED_URL "https://github.com/zeromq/libzmq/releases/download/v${LIBZMQ_BUNDLED_VERSION}/zeromq-${LIBZMQ_BUNDLED_VERSION}.tar.gz")
endif()
#------- bundle libzmq ------
if (NOT ZMQ_PREFIX)
# empty string is the same as 'auto'
set(ZMQ_PREFIX "auto")
endif()
# default search paths:
foreach(prefix $ENV{PREFIX} "/opt/homebrew" "/opt/local" "/usr/local" "/usr")
if (IS_DIRECTORY "${prefix}")
list(APPEND CMAKE_PREFIX_PATH "${prefix}")
endif()
endforeach()
if (ZMQ_PREFIX STREQUAL "auto")
message(CHECK_START "Looking for libzmq")
find_package(ZeroMQ QUIET)
if (ZeroMQ_FOUND AND TARGET libzmq)
set(libzmq_target "libzmq")
get_target_property(_ZMQ_LOCATION libzmq IMPORTED_LOCATION)
message(CHECK_PASS "Found with cmake: ${_ZMQ_LOCATION}")
endif()
if (NOT ZeroMQ_FOUND)
find_package(PkgConfig QUIET)
if (PkgConfig_FOUND)
message(CHECK_START "Looking for libzmq with pkg-config")
pkg_check_modules(libzmq libzmq IMPORTED_TARGET)
if (TARGET PkgConfig::libzmq)
set(ZeroMQ_FOUND TRUE)
set(libzmq_target "PkgConfig::libzmq")
message(CHECK_PASS "found: -L${libzmq_LIBRARY_DIRS} -l${libzmq_LIBRARIES}")
else()
message(CHECK_FAIL "no")
endif()
endif()
endif()
if (NOT ZeroMQ_FOUND)
message(STATUS " Fallback: looking for libzmq in ${CMAKE_PREFIX_PATH}")
find_library(LIBZMQ_LIBRARY NAMES zmq)
find_path(LIBZMQ_INCLUDE_DIR "zmq.h")
# check if found
if (LIBZMQ_LIBRARY AND LIBZMQ_INCLUDE_DIR)
set(ZeroMQ_FOUND TRUE)
message(CHECK_PASS "${LIBZMQ_LIBRARY}")
endif()
endif()
if (NOT ZeroMQ_FOUND)
message(CHECK_FAIL "libzmq not found, will bundle libzmq and libsodium")
set(ZMQ_PREFIX "bundled")
endif()
elseif (NOT ZMQ_PREFIX STREQUAL "bundled")
message(CHECK_START "Looking for libzmq in ${ZMQ_PREFIX}")
find_path(
LIBZMQ_INCLUDE_DIR zmq.h
PATHS "${ZMQ_PREFIX}/include"
NO_DEFAULT_PATH
)
find_library(
LIBZMQ_LIBRARY
NAMES zmq
PATHS "${ZMQ_PREFIX}/lib"
NO_DEFAULT_PATH
)
if (LIBZMQ_LIBRARY AND LIBZMQ_INCLUDE_DIR)
message(CHECK_PASS "${LIBZMQ_LIBRARY}")
else()
message(CHECK_FAIL "no")
message(FATAL_ERROR "libzmq not found in ZMQ_PREFIX=${ZMQ_PREFIX}")
endif()
else()
# bundled
endif()
if (ZMQ_PREFIX STREQUAL "bundled")
message(STATUS "Bundling libzmq and libsodium")
include(FetchContent)
add_compile_definitions(ZMQ_STATIC=1)
set(BUNDLE_DIR "${CMAKE_CURRENT_BINARY_DIR}/bundled")
make_directory("${BUNDLE_DIR}/lib")
include_directories(${BUNDLE_DIR}/include)
list(PREPEND CMAKE_PREFIX_PATH ${BUNDLE_DIR})
# libsodium
if (MSVC)
set(libsodium_lib "${BUNDLE_DIR}/lib/libsodium.lib")
else()
set(libsodium_lib "${BUNDLE_DIR}/lib/libsodium.a")
endif()
FetchContent_Declare(bundled_libsodium
URL ${LIBSODIUM_BUNDLED_URL}
PREFIX ${BUNDLE_DIR}
)
FetchContent_MakeAvailable(bundled_libsodium)
# run libsodium build explicitly here, so it's available to libzmq next
set(bundled_libsodium_include "${bundled_libsodium_SOURCE_DIR}/src/libsodium/include")
if(${bundled_libsodium_POPULATED} AND NOT EXISTS "${libsodium_lib}")
message(STATUS "building bundled libsodium")
if (MSVC)
# select vs build solution by msvc version number
if (NOT LIBSODIUM_VS_VERSION)
if(MSVC_VERSION GREATER 1940)
message(STATUS "Unrecognized MSVC_VERSION=${MSVC_VERSION}")
set(MSVC_VERSION 1939)
endif()
if(MSVC_VERSION GREATER_EQUAL 1930)
set(LIBSODIUM_VS_VERSION "2022")
elseif(MSVC_VERSION GREATER_EQUAL 1920)
set(LIBSODIUM_VS_VERSION "2019")
elseif(MSVC_VERSION GREATER_EQUAL 1910)
set(LIBSODIUM_VS_VERSION "2017")
else()
message(FATAL_ERROR "unsupported bundling libsodium for MSVC_VERSION=${MSVC_VERSION} (need at least VS2017)")
endif()
endif()
find_package(Vcvars REQUIRED)
list(APPEND libsodium_build
${Vcvars_LAUNCHER}
"msbuild"
"/m"
"/v:n"
"/p:Configuration=Static${CMAKE_BUILD_TYPE}"
"/p:Platform=${CMAKE_GENERATOR_PLATFORM}"
"builds/msvc/vs${LIBSODIUM_VS_VERSION}/libsodium.sln"
)
list(APPEND libsodium_build ${LIBSODIUM_MSBUILD_ARGS})
execute_process(
COMMAND ${libsodium_build}
WORKING_DIRECTORY ${bundled_libsodium_SOURCE_DIR}
COMMAND_ECHO STDOUT
COMMAND_ERROR_IS_FATAL ANY
)
file(GLOB_RECURSE BUILT_LIB "${bundled_libsodium_SOURCE_DIR}/**/libsodium.lib")
message(STATUS "copy ${BUILT_LIB} ${libsodium_lib}")
file(COPY_FILE ${BUILT_LIB} ${libsodium_lib})
else()
list(APPEND libsodium_configure
./configure
--prefix=${BUNDLE_DIR}
--with-pic
--disable-shared
--enable-static
)
list(APPEND libsodium_configure ${LIBSODIUM_CONFIGURE_ARGS})
execute_process(
COMMAND ${libsodium_configure}
WORKING_DIRECTORY ${bundled_libsodium_SOURCE_DIR}
COMMAND_ECHO STDOUT
COMMAND_ERROR_IS_FATAL ANY
)
execute_process(
COMMAND make
WORKING_DIRECTORY ${bundled_libsodium_SOURCE_DIR}
COMMAND_ECHO STDOUT
COMMAND_ERROR_IS_FATAL ANY
)
execute_process(
COMMAND make install
WORKING_DIRECTORY ${bundled_libsodium_SOURCE_DIR}
COMMAND_ECHO STDOUT
COMMAND_ERROR_IS_FATAL ANY
)
endif()
endif()
# use libzmq's own cmake, so we can import the libzmq-static target
set(ENABLE_CURVE ON)
set(ENABLE_DRAFTS ${ZMQ_DRAFT_API})
set(WITH_LIBSODIUM ON)
set(WITH_LIBSODIUM_STATIC ON)
set(LIBZMQ_PEDANTIC OFF)
set(LIBZMQ_WERROR OFF)
set(WITH_DOC OFF)
set(WITH_DOCS OFF)
set(BUILD_TESTS OFF)
set(BUILD_SHARED OFF)
set(BUILD_STATIC ON)
FetchContent_Declare(bundled_libzmq
URL ${LIBZMQ_BUNDLED_URL}
PREFIX ${BUNDLE_DIR}
CMAKE_ARGS ${LIBZMQ_CMAKE_ARGS}
EXCLUDE_FROM_ALL
)
FetchContent_MakeAvailable(bundled_libzmq)
# target for libzmq static
if (TARGET libzmq-static)
set(libzmq_target "libzmq-static")
else()
message(FATAL_ERROR "libzmq-static target not found in bundled libzmq")
endif()
endif()
if (NOT TARGET "${libzmq_target}" AND LIBZMQ_LIBRARY AND LIBZMQ_INCLUDE_DIR)
set(libzmq_target "libzmq")
# construct target from find_library results
# what if it was static?
add_library(libzmq SHARED IMPORTED)
set_property(TARGET libzmq PROPERTY IMPORTED_LOCATION ${LIBZMQ_LIBRARY})
set_property(TARGET libzmq PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${LIBZMQ_INCLUDE_DIR})
endif()
#------- building pyzmq itself -------
message(STATUS "Using Python ${Python_INTERPRETER_ID} ${Python_EXECUTABLE}")
set(ZMQ_BACKEND "${CMAKE_CURRENT_BINARY_DIR}/zmq/backend")
set(ZMQ_BUILDUTILS "${CMAKE_CURRENT_SOURCE_DIR}/buildutils")
if(Python_INTERPRETER_ID STREQUAL "PyPy")
message(STATUS "Building CFFI backend")
set(ZMQ_EXT_NAME "_cffi")
set(ZMQ_BACKEND_CFFI "${ZMQ_BACKEND}/cffi")
set(ZMQ_BACKEND_DEST "zmq/backend/cffi")
set(ZMQ_C "${ZMQ_BACKEND_CFFI}/${ZMQ_EXT_NAME}.c")
add_custom_command(
OUTPUT ${ZMQ_C}
VERBATIM
COMMAND "${Python_EXECUTABLE}"
"${ZMQ_BUILDUTILS}/build_cffi.py"
"${ZMQ_C}"
)
else()
message(STATUS "Building Cython backend")
find_program(CYTHON "cython")
set(ZMQ_BACKEND_CYTHON "${ZMQ_BACKEND}/cython")
set(ZMQ_BACKEND_DEST "zmq/backend/cython")
set(ZMQ_EXT_NAME "_zmq")
set(ZMQ_C "${ZMQ_BACKEND_CYTHON}/${ZMQ_EXT_NAME}.c")
set(ZMQ_PYX "${CMAKE_CURRENT_SOURCE_DIR}/zmq/backend/cython/${ZMQ_EXT_NAME}.py")
add_custom_command(
OUTPUT ${ZMQ_C}
DEPENDS ${ZMQ_PYX}
VERBATIM
COMMAND "${CYTHON}"
--3str
--output-file ${ZMQ_C}
${ZMQ_PYX}
)
endif()
make_directory(${ZMQ_BACKEND_DEST})
python_add_library(
${ZMQ_EXT_NAME} MODULE
WITH_SOABI
${ZMQ_C}
)
if (TARGET ${libzmq_target})
message(STATUS "Linking libzmq target ${libzmq_target}")
target_link_libraries(${ZMQ_EXT_NAME} PUBLIC ${libzmq_target})
if ("${libzmq_target}" STREQUAL "libzmq-static" AND NOT MSVC)
# seem to need stdc++ for static libzmq on non-Windows
# not sure if/when this should be libc++ or how to know
target_link_libraries(${ZMQ_EXT_NAME} PUBLIC stdc++)
endif()
else()
message(FATAL_ERROR "should have a libzmq target ${libzmq_target} to link to...")
endif()
target_include_directories(${ZMQ_EXT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/zmq/utils")
install(TARGETS ${ZMQ_EXT_NAME} DESTINATION "${ZMQ_BACKEND_DEST}")