forked from saghul/pyuv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_libuv.py
135 lines (118 loc) · 5.09 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
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, 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, 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,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 = '8e50b60'
libuv_patches = []
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.libuv_force_fetch or self.libuv_clean_compile:
self.force = 1
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.library_dirs.append(self.libuv_dir)
if sys.platform.startswith('linux'):
self.libraries.append('rt')
elif sys.platform == 'darwin':
self.extensions[0].extra_link_args = ['-framework', 'CoreServices']
self.extensions[0].extra_objects = [os.path.join(self.libuv_dir, 'uv.a')]
def get_libuv(self):
#self.debug_mode = bool(self.debug) or hasattr(sys, 'gettotalrefcount')
def download_libuv():
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)
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)
log.info('Building libuv...')
exec_process(['make', 'uv.a'], cwd=self.libuv_dir, env=env)
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:
exec_process(['make', 'clean'], cwd=self.libuv_dir)
if not os.path.exists(os.path.join(self.libuv_dir, 'uv.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'))