From 549ca7fd1a0412e48ad3eb2ff751ac096de88540 Mon Sep 17 00:00:00 2001 From: Min RK Date: Wed, 20 Jan 2021 11:03:48 +0100 Subject: [PATCH 1/2] specify default target Windows version inherit Py_WIN_VER by default unless specified in extra macros need to always load pyversion_compat for this to work --- .github/workflows/wheels.yml | 1 + buildutils/_cffi.c | 1 + buildutils/templates/zmq_constants.h | 1 + zmq/backend/cffi/message.py | 4 +++- zmq/backend/cython/libzmq.pxd | 5 +++++ zmq/utils/getpid_compat.h | 1 + zmq/utils/ipcmaxlen.h | 2 ++ zmq/utils/pyversion_compat.h | 12 ++++++++++++ zmq/utils/zmq_compat.h | 3 ++- zmq/utils/zmq_constants.h | 1 + 10 files changed, 29 insertions(+), 2 deletions(-) diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index ebe68081e..b710705c4 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -8,6 +8,7 @@ on: - buildutils/** - .github/workflows/wheels.yml - tools/install_libzmq.sh + - zmq/utils/*.h env: cython: "0.29.21" diff --git a/buildutils/_cffi.c b/buildutils/_cffi.c index 1e573ab37..691be3c78 100644 --- a/buildutils/_cffi.c +++ b/buildutils/_cffi.c @@ -1,6 +1,7 @@ #include #include +#include "pyversion_compat.h" #include "mutex.h" #include "ipcmaxlen.h" #include "zmq_compat.h" diff --git a/buildutils/templates/zmq_constants.h b/buildutils/templates/zmq_constants.h index e729d0b06..b1af5f193 100644 --- a/buildutils/templates/zmq_constants.h +++ b/buildutils/templates/zmq_constants.h @@ -1,3 +1,4 @@ +#pragma once #ifndef _PYZMQ_CONSTANT_DEFS #define _PYZMQ_CONSTANT_DEFS diff --git a/zmq/backend/cffi/message.py b/zmq/backend/cffi/message.py index d82958cb0..ea240790d 100644 --- a/zmq/backend/cffi/message.py +++ b/zmq/backend/cffi/message.py @@ -146,7 +146,9 @@ def _buffer_from_zmq_msg(self): for Frames created by recv """ if self._data is None: - self._data = ffi.buffer(C.zmq_msg_data(self.zmq_msg), C.zmq_msg_size(self.zmq_msg)) + self._data = ffi.buffer( + C.zmq_msg_data(self.zmq_msg), C.zmq_msg_size(self.zmq_msg) + ) if self._buffer is None: self._buffer = memoryview(self._data) diff --git a/zmq/backend/cython/libzmq.pxd b/zmq/backend/cython/libzmq.pxd index 0be0313a7..7122d5604 100644 --- a/zmq/backend/cython/libzmq.pxd +++ b/zmq/backend/cython/libzmq.pxd @@ -27,6 +27,11 @@ # Import the C header files #----------------------------------------------------------------------------- +# common includes, such as zmq compat, pyversion_compat +# make sure we load pyversion compat in every Cython module +cdef extern from "pyversion_compat.h": + pass + # were it not for Windows, # we could cimport these from libc.stdint cdef extern from "zmq_compat.h": diff --git a/zmq/utils/getpid_compat.h b/zmq/utils/getpid_compat.h index 47ce90fae..3fe1bec4c 100644 --- a/zmq/utils/getpid_compat.h +++ b/zmq/utils/getpid_compat.h @@ -1,3 +1,4 @@ +#pragma once #ifdef _WIN32 #include #define getpid _getpid diff --git a/zmq/utils/ipcmaxlen.h b/zmq/utils/ipcmaxlen.h index 7218db78b..26ec4e1cf 100644 --- a/zmq/utils/ipcmaxlen.h +++ b/zmq/utils/ipcmaxlen.h @@ -8,6 +8,8 @@ Distributed under the terms of the New BSD License. The full license is in the file COPYING.BSD, distributed as part of this software. */ +#pragma once + #if defined(HAVE_SYS_UN_H) #include "sys/un.h" int get_ipc_path_max_len(void) { diff --git a/zmq/utils/pyversion_compat.h b/zmq/utils/pyversion_compat.h index fac090461..1b4f1d8ac 100644 --- a/zmq/utils/pyversion_compat.h +++ b/zmq/utils/pyversion_compat.h @@ -1,5 +1,17 @@ #include "Python.h" +// default to Python's own target Windows version(s) +// override by setting WINVER, _WIN32_WINNT, (maybe also NTDDI_VERSION?) macros +#ifdef Py_WINVER + #ifndef WINVER + #define WINVER Py_WINVER + #endif + #ifndef _WIN32_WINNT + #define _WIN32_WINNT Py_WINVER + #endif +#endif + + #if PY_VERSION_HEX < 0x02070000 #define PyMemoryView_FromBuffer(info) (PyErr_SetString(PyExc_NotImplementedError, \ "new buffer interface is not available"), (PyObject *)NULL) diff --git a/zmq/utils/zmq_compat.h b/zmq/utils/zmq_compat.h index ed105152d..b43c20127 100644 --- a/zmq/utils/zmq_compat.h +++ b/zmq/utils/zmq_compat.h @@ -5,6 +5,8 @@ // the file COPYING.BSD, distributed as part of this software. //----------------------------------------------------------------------------- +#pragma once + #if defined(_MSC_VER) #define pyzmq_int64_t __int64 #define pyzmq_uint32_t unsigned __int32 @@ -110,4 +112,3 @@ #define zmq_socket_monitor(s, addr, flags) _missing #endif - diff --git a/zmq/utils/zmq_constants.h b/zmq/utils/zmq_constants.h index dc89945bc..af3f31c53 100644 --- a/zmq/utils/zmq_constants.h +++ b/zmq/utils/zmq_constants.h @@ -1,3 +1,4 @@ +#pragma once #ifndef _PYZMQ_CONSTANT_DEFS #define _PYZMQ_CONSTANT_DEFS From 2b5467322ee0ff949c271b9a36a63e7b9831c110 Mon Sep 17 00:00:00 2001 From: Min RK Date: Wed, 20 Jan 2021 13:18:44 +0100 Subject: [PATCH 2/2] set WINVER in compiler configuration we don't have source control over libzmq itself to add includes --- buildutils/config.py | 12 ++++++++++++ setup.py | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/buildutils/config.py b/buildutils/config.py index 8049dbd61..fb37f8d93 100644 --- a/buildutils/config.py +++ b/buildutils/config.py @@ -157,9 +157,21 @@ def discover_settings(conf_base=None): 'bundle_msvcp': None, 'build_ext': {}, 'bdist_egg': {}, + 'win_ver': None, } if sys.platform.startswith('win'): settings['have_sys_un_h'] = False + # target Windows version, sets WINVER, _WIN32_WINNT macros + # see https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt for reference + # see https://github.com/python/cpython/blob/v3.9.1/PC/pyconfig.h#L137-L159 + # for CPython's own values + if sys.version_info >= (3, 9): + # CPython 3.9 targets Windows 8 (0x0602) + settings["win_ver"] = "0x0602" + else: + # older Python, target Windows 7 (0x0601) + # CPython itself targets Vista (0x0600) + settings["win_ver"] = "0x0601" if conf_base: # lowest priority diff --git a/setup.py b/setup.py index 22f582dca..75592bfb6 100755 --- a/setup.py +++ b/setup.py @@ -411,6 +411,13 @@ def init_settings_from_config(self): if cfg['have_sys_un_h']: settings['define_macros'].append(('HAVE_SYS_UN_H', 1)) + if cfg['win_ver']: + # set target minimum Windows version + settings['define_macros'].extend([ + ('WINVER', cfg['win_ver']), + ('_WIN32_WINNT', cfg['win_ver']), + ]) + if cfg.get('zmq_draft_api'): settings['define_macros'].append(('ZMQ_BUILD_DRAFT_API', 1))