1
1
import os
2
2
import re
3
+ import shutil
3
4
import subprocess
4
5
import sys
5
6
import unittest
17
18
18
19
19
20
from setuptools import setup , Extension
20
- from setuptools .command .build_ext import build_ext
21
+ from setuptools .command .build_ext import build_ext as build_ext
22
+ from setuptools .command .sdist import sdist as sdist
21
23
22
24
23
25
VERSION = '0.6.5'
24
26
CFLAGS = ['-O2' ]
25
27
LIBUV_DIR = os .path .join (os .path .dirname (__file__ ), 'vendor' , 'libuv' )
28
+ LIBUV_BUILD_DIR = os .path .join (os .path .dirname (__file__ ), 'build' , 'libuv' )
26
29
27
30
28
31
def discover_tests ():
@@ -31,7 +34,33 @@ def discover_tests():
31
34
return test_suite
32
35
33
36
34
- class libuv_build_ext (build_ext ):
37
+ def _libuv_build_env ():
38
+ env = os .environ .copy ()
39
+
40
+ cur_cflags = env .get ('CFLAGS' , '' )
41
+ if not re .search ('-O\d' , cur_cflags ):
42
+ cur_cflags += ' -O2'
43
+
44
+ env ['CFLAGS' ] = (cur_cflags + ' -fPIC ' + env .get ('ARCHFLAGS' , '' ))
45
+
46
+ return env
47
+
48
+
49
+ def _libuv_autogen (env ):
50
+ if not os .path .exists (os .path .join (LIBUV_DIR , 'configure' )):
51
+ subprocess .run (
52
+ ['/bin/sh' , 'autogen.sh' ], cwd = LIBUV_DIR , env = env , check = True )
53
+
54
+
55
+ class uvloop_sdist (sdist ):
56
+ def run (self ):
57
+ # Make sure sdist archive contains configure
58
+ # to avoid the dependency on autotools.
59
+ _libuv_autogen (_libuv_build_env ())
60
+ super ().run ()
61
+
62
+
63
+ class uvloop_build_ext (build_ext ):
35
64
user_options = build_ext .user_options + [
36
65
('cython-always' , None ,
37
66
'run cythonize() even if .c files are present' ),
@@ -177,31 +206,34 @@ def _patch_cfile(self, cfile):
177
206
f .write (src )
178
207
179
208
def build_libuv (self ):
180
- env = os .environ .copy ()
181
-
182
- cur_cflags = env .get ('CFLAGS' , '' )
183
- if not re .search ('-O\d' , cur_cflags ):
184
- cur_cflags += ' -O2'
209
+ env = _libuv_build_env ()
185
210
186
- env ['CFLAGS' ] = (cur_cflags + ' -fPIC ' + env .get ('ARCHFLAGS' , '' ))
211
+ # Make sure configure and friends are present in case
212
+ # we are building from a git checkout.
213
+ _libuv_autogen (env )
187
214
188
- j_flag = '-j{}' . format ( os . cpu_count () or 1 )
189
-
190
- if not os .path .exists (os . path . join ( LIBUV_DIR , 'configure' ) ):
191
- subprocess . run ([ '/bin/sh' , 'autogen.sh' ], cwd = LIBUV_DIR , env = env ,
192
- check = True )
215
+ # Copy the libuv tree to build/ so that its build
216
+ # products don't pollute sdist accidentally.
217
+ if os .path .exists (LIBUV_BUILD_DIR ):
218
+ shutil . rmtree ( LIBUV_BUILD_DIR )
219
+ shutil . copytree ( LIBUV_DIR , LIBUV_BUILD_DIR )
193
220
194
221
# Sometimes pip fails to preserve the timestamps correctly,
195
222
# in which case, make will try to run autotools again.
196
- subprocess .run (['touch' , 'configure.ac' , 'aclocal.m4' ,
197
- 'configure' , 'Makefile.am' , 'Makefile.in' ],
198
- cwd = LIBUV_DIR , env = env , check = True )
223
+ subprocess .run (
224
+ ['touch' , 'configure.ac' , 'aclocal.m4' , 'configure' ,
225
+ 'Makefile.am' , 'Makefile.in' ],
226
+ cwd = LIBUV_BUILD_DIR , env = env , check = True )
199
227
200
- subprocess .run (['./configure' ], cwd = LIBUV_DIR , env = env , check = True )
228
+ subprocess .run (
229
+ ['./configure' ],
230
+ cwd = LIBUV_BUILD_DIR , env = env , check = True )
201
231
232
+ j_flag = '-j{}' .format (os .cpu_count () or 1 )
202
233
c_flag = "CFLAGS={}" .format (env ['CFLAGS' ])
203
- subprocess .run (['make' , j_flag , c_flag ],
204
- cwd = LIBUV_DIR , env = env , check = True )
234
+ subprocess .run (
235
+ ['make' , j_flag , c_flag ],
236
+ cwd = LIBUV_BUILD_DIR , env = env , check = True )
205
237
206
238
def build_extensions (self ):
207
239
if self .use_system_libuv :
@@ -212,7 +244,7 @@ def build_extensions(self):
212
244
# Support macports on Mac OS X.
213
245
self .compiler .add_include_dir ('/opt/local/include' )
214
246
else :
215
- libuv_lib = os .path .join (LIBUV_DIR , '.libs' , 'libuv.a' )
247
+ libuv_lib = os .path .join (LIBUV_BUILD_DIR , '.libs' , 'libuv.a' )
216
248
if not os .path .exists (libuv_lib ):
217
249
self .build_libuv ()
218
250
if not os .path .exists (libuv_lib ):
@@ -241,7 +273,10 @@ def build_extensions(self):
241
273
platforms = ['*nix' ],
242
274
version = VERSION ,
243
275
packages = ['uvloop' ],
244
- cmdclass = {'build_ext' : libuv_build_ext },
276
+ cmdclass = {
277
+ 'sdist' : uvloop_sdist ,
278
+ 'build_ext' : uvloop_build_ext
279
+ },
245
280
ext_modules = [
246
281
Extension (
247
282
"uvloop.loop" ,
0 commit comments