Skip to content

Commit 77bc6dc

Browse files
committed
forgot about setup.py let's add it
1 parent 0bb1a68 commit 77bc6dc

File tree

1 file changed

+79
-24
lines changed

1 file changed

+79
-24
lines changed

setup.py

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
if vi < (3, 8):
55
raise RuntimeError('uvloop requires Python 3.8 or greater')
66

7-
if sys.platform in ('win32', 'cygwin', 'cli'):
8-
raise RuntimeError('uvloop does not support Windows at the moment')
7+
# TODO: Remove Completely because Winloop Author is mergeing his project to uvloop.
8+
# if sys.platform in ('win32', 'cygwin', 'cli'):
9+
# raise RuntimeError('uvloop does not support Windows at the moment')
910

1011
import os
1112
import os.path
@@ -28,6 +29,9 @@
2829
LIBUV_DIR = str(_ROOT / 'vendor' / 'libuv')
2930
LIBUV_BUILD_DIR = str(_ROOT / 'build' / 'libuv-{}'.format(MACHINE))
3031

32+
# NOTE: Mingw was added by another contributor in the winloop project.
33+
MINGW = bool(os.environ.get("MINGW_PREFIX", ""))
34+
3135

3236
def _libuv_build_env():
3337
env = os.environ.copy()
@@ -83,7 +87,9 @@ class uvloop_build_ext(build_ext):
8387

8488
def initialize_options(self):
8589
super().initialize_options()
86-
self.use_system_libuv = False
90+
# Use mingw if prefix was given for it otherwise it
91+
# will always be false.
92+
self.use_system_libuv = MINGW
8793
self.cython_always = False
8894
self.cython_annotate = None
8995
self.cython_directives = None
@@ -108,7 +114,8 @@ def finalize_options(self):
108114
need_cythonize = True
109115

110116
if need_cythonize:
111-
import pkg_resources
117+
from packaging.requirements import Requirement
118+
from packaging.version import Version
112119

113120
# Double check Cython presence in case setup_requires
114121
# didn't go into effect (most likely because someone
@@ -118,17 +125,21 @@ def finalize_options(self):
118125
import Cython
119126
except ImportError:
120127
raise RuntimeError(
121-
'please install {} to compile uvloop from source'.format(
122-
CYTHON_DEPENDENCY))
128+
"please install {} to compile uvloop from source".format(
129+
CYTHON_DEPENDENCY
130+
)
131+
)
123132

124-
cython_dep = pkg_resources.Requirement.parse(CYTHON_DEPENDENCY)
125-
if Cython.__version__ not in cython_dep:
133+
cython_dep = Requirement(CYTHON_DEPENDENCY)
134+
if not cython_dep.specifier.contains(Version(Cython.__version__)):
126135
raise RuntimeError(
127-
'uvloop requires {}, got Cython=={}'.format(
136+
"uvloop requires {}, got Cython=={}".format(
128137
CYTHON_DEPENDENCY, Cython.__version__
129-
))
138+
)
139+
)
130140

131141
from Cython.Build import cythonize
142+
132143

133144
directives = {}
134145
if self.cython_directives:
@@ -190,6 +201,15 @@ def build_libuv(self):
190201
cwd=LIBUV_BUILD_DIR, env=env, check=True)
191202

192203
def build_extensions(self):
204+
if sys.platform == "win32" and not MINGW:
205+
path = pathlib.Path("vendor", "libuv", "src")
206+
c_files = [p.as_posix() for p in path.iterdir() if p.suffix == ".c"]
207+
c_files += [
208+
p.as_posix() for p in (path / "win").iterdir() if p.suffix == ".c"
209+
]
210+
self.extensions[-1].sources += c_files
211+
super().build_extensions()
212+
return
193213
if self.use_system_libuv:
194214
self.compiler.add_library('uv')
195215

@@ -229,28 +249,63 @@ def build_extensions(self):
229249
raise RuntimeError(
230250
'unable to read the version from uvloop/_version.py')
231251

252+
if sys.platform == "win32":
253+
from Cython.Build import cythonize
254+
from Cython.Compiler.Main import default_options
255+
256+
default_options["compile_time_env"] = dict(DEFAULT_FREELIST_SIZE=250)
257+
ext = cythonize(
258+
[
259+
Extension(
260+
"uvloop.loop",
261+
sources=["uvloop/loop.pyx"],
262+
include_dirs=[]
263+
if MINGW
264+
else [
265+
"vendor/libuv/src",
266+
"vendor/libuv/src/win",
267+
"vendor/libuv/include",
268+
],
269+
extra_compile_args=["/std:c11", "/experimental:c11atomics"],
270+
# subset of libuv Windows libraries:
271+
extra_link_args=[
272+
(f"-l{lib}" if MINGW else f"{lib}.lib")
273+
for lib in (
274+
"Shell32",
275+
"Ws2_32",
276+
"Advapi32",
277+
"iphlpapi",
278+
"Userenv",
279+
"User32",
280+
"Dbghelp",
281+
"Ole32",
282+
)
283+
],
284+
define_macros=[("WIN32_LEAN_AND_MEAN", 1), ("_WIN32_WINNT", "0x0602")],
285+
),
286+
]
287+
)
288+
else:
289+
ext = [
290+
Extension(
291+
"uvloop.loop",
292+
sources=[
293+
"uvloop/loop.pyx",
294+
],
295+
extra_compile_args=MODULES_CFLAGS,
296+
),
297+
]
232298

233299
setup_requires = []
234300

235-
if not (_ROOT / 'uvloop' / 'loop.c').exists() or '--cython-always' in sys.argv:
301+
if not (_ROOT / "uvloop" / "loop.c").exists() or "--cython-always" in sys.argv:
236302
# No Cython output, require Cython to build.
237303
setup_requires.append(CYTHON_DEPENDENCY)
238304

239305

240306
setup(
241307
version=VERSION,
242-
cmdclass={
243-
'sdist': uvloop_sdist,
244-
'build_ext': uvloop_build_ext
245-
},
246-
ext_modules=[
247-
Extension(
248-
"uvloop.loop",
249-
sources=[
250-
"uvloop/loop.pyx",
251-
],
252-
extra_compile_args=MODULES_CFLAGS
253-
),
254-
],
308+
cmdclass={"sdist": uvloop_sdist, "build_ext": uvloop_build_ext},
309+
ext_modules=ext,
255310
setup_requires=setup_requires,
256311
)

0 commit comments

Comments
 (0)