forked from saghul/pyuv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_libuv.py
162 lines (144 loc) · 6.77 KB
/
setup_libuv.py
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
import errno
import os
import shutil
import subprocess
import sys
from distutils import log
from distutils.command.build_ext import build_ext
from distutils.command.sdist import sdist
from distutils.errors import DistutilsError
def makedirs(path):
try:
os.makedirs(path)
except OSError as e:
if e.errno==errno.EEXIST and os.path.isdir(path) and os.access(path, os.R_OK | os.W_OK | os.X_OK):
return
raise
def rmtree(path):
try:
shutil.rmtree(path)
except OSError as e:
if e.errno != errno.ENOENT:
raise
def exec_process(cmdline, silent=True, input=None, **kwargs):
"""Execute a subprocess and returns the returncode, stdout buffer and stderr buffer.
Optionally prints stdout and stderr while running."""
try:
sub = subprocess.Popen(args=cmdline, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
stdout, stderr = sub.communicate(input=input)
returncode = sub.returncode
if not silent:
sys.stdout.write(stdout)
sys.stderr.write(stderr)
except OSError as e:
if e.errno == 2:
raise DistutilsError('"%s" is not present on this system' % cmdline[0])
else:
raise
if returncode != 0:
raise DistutilsError('Got return value %d while executing "%s", stderr output was:\n%s' % (returncode, " ".join(cmdline), stderr.rstrip("\n")))
return stdout
class libuv_build_ext(build_ext):
libuv_dir = os.path.join('deps', 'libuv')
libuv_repo = 'https://github.com/joyent/libuv.git'
libuv_branch = 'master'
libuv_revision = '8f66bfc'
libuv_patches = ['patches/common.gypi.patch']
user_options = build_ext.user_options
user_options.extend([
("libuv-clean-compile", None, "Clean libuv tree before compilation"),
("libuv-force-fetch", None, "Remove libuv (if present) and fetch it again")
])
boolean_options = build_ext.boolean_options
boolean_options.extend(["libuv-clean-compile", "libuv-force-fetch"])
def initialize_options(self):
build_ext.initialize_options(self)
self.libuv_clean_compile = 0
self.libuv_force_fetch = 0
def build_extensions(self):
if self.compiler.compiler_type == 'mingw32':
# Dirty hack to avoid linking with more than one C runtime when using MinGW
self.compiler.dll_libraries = [lib for lib in self.compiler.dll_libraries if not lib.startswith('msvcr')]
self.force = self.libuv_force_fetch or self.libuv_clean_compile
self.get_libuv()
build_ext.build_extensions(self)
def finalize_options(self):
build_ext.finalize_options(self)
self.include_dirs.append(os.path.join(self.libuv_dir, 'include'))
self.include_dirs.append(os.path.join(self.libuv_dir, 'src', 'ares'))
self.library_dirs.append(self.libuv_dir)
self.libraries.append('uv')
if sys.platform.startswith('linux'):
self.libraries.append('rt')
elif sys.platform == 'darwin':
self.extensions[0].extra_link_args = ['-framework', 'CoreServices']
elif sys.platform == 'win32':
self.libraries.append('iphlpapi')
self.libraries.append('psapi')
self.libraries.append('ws2_32')
self.libraries.append('advapi32')
def get_libuv(self):
#self.debug_mode = bool(self.debug) or hasattr(sys, 'gettotalrefcount')
def download_libuv():
log.info('Downloading libuv...')
makedirs(self.libuv_dir)
exec_process(['git', 'clone', '-b', self.libuv_branch, self.libuv_repo, self.libuv_dir])
exec_process(['git', 'checkout', self.libuv_revision], cwd=self.libuv_dir)
def patch_libuv():
log.info('Patching libuv...')
for patch_file in self.libuv_patches:
exec_process(['patch', '--forward', '-d', self.libuv_dir, '-p0', '-i', os.path.abspath(patch_file)])
def build_libuv():
cflags = '-fPIC'
env = os.environ.copy()
env['CFLAGS'] = ' '.join(x for x in (cflags, env.get('CFLAGS', None)) if x)
if sys.platform=='win32' and self.compiler.compiler_type=='msvc':
log.info('build libuv on win32...')
exec_process(['cmd.exe','/C','vcbuild.bat release'], cwd=self.libuv_dir, env=env)
shutil.move(os.path.join(self.libuv_dir, 'Release', 'lib', 'uv.lib'), os.path.join(self.libuv_dir, 'uv.lib'))
else:
log.info('Building libuv...')
exec_process(['make', 'uv.a'], cwd=self.libuv_dir, env=env)
shutil.move(os.path.join(self.libuv_dir, 'uv.a'), os.path.join(self.libuv_dir, 'libuv.a'))
if self.libuv_force_fetch:
rmtree('deps')
if not os.path.exists(self.libuv_dir):
download_libuv()
patch_libuv()
build_libuv()
else:
if self.libuv_clean_compile:
if sys.platform=='win32' and self.compiler.compiler_type=='msvc':
exec_process(['cmd.exe','/C','vcbuild.bat clean'], cwd=self.libuv_dir, env=env)
os.remove(os.path.join(self.libuv_dir, 'uv.lib'))
else:
exec_process(['make', 'clean'], cwd=self.libuv_dir)
if sys.platform=='win32' and self.compiler.compiler_type=='msvc':
if not os.path.exists(os.path.join(self.libuv_dir, 'uv.lib')):
log.info('libuv needs to be compiled.')
build_libuv()
else:
log.info('No need to build libuv.')
else:
if not os.path.exists(os.path.join(self.libuv_dir, 'libuv.a')):
log.info('libuv needs to be compiled.')
build_libuv()
else:
log.info('No need to build libuv.')
class libuv_sdist(sdist):
libuv_dir = os.path.join('deps', 'libuv')
libuv_repo = libuv_build_ext.libuv_repo
libuv_branch = libuv_build_ext.libuv_branch
libuv_revision = libuv_build_ext.libuv_revision
libuv_patches = libuv_build_ext.libuv_patches
def initialize_options(self):
sdist.initialize_options(self)
rmtree('deps')
makedirs(self.libuv_dir)
log.info('Downloading libuv...')
exec_process(['git', 'clone', '-b', self.libuv_branch, self.libuv_repo, self.libuv_dir])
exec_process(['git', 'checkout', self.libuv_revision], cwd=self.libuv_dir)
log.info('Patching libuv...')
for patch_file in self.libuv_patches:
exec_process(['patch', '--forward', '-d', self.libuv_dir, '-p0', '-i', os.path.abspath(patch_file)])
rmtree(os.path.join(self.libuv_dir, '.git'))