2
2
from __future__ import unicode_literals
3
3
4
4
import contextlib
5
- import distutils .sysconfig
6
5
import os
7
6
import pipes
8
7
import shutil
13
12
from setuptools .command .build_ext import build_ext as _build_ext
14
13
15
14
16
- PYPY = '__pypy__' in sys .builtin_module_names
17
-
18
-
19
15
def _get_cflags (compiler ):
20
16
return ' ' .join ('-I{}' .format (p ) for p in compiler .include_dirs )
21
17
22
18
23
- def _get_ldflags_pypy ():
24
- if PYPY : # pragma: no cover (pypy only)
25
- return '-L{} -lpypy-c' .format (
26
- os .path .dirname (os .path .realpath (sys .executable )),
27
- )
28
- else :
29
- return None
30
-
31
-
32
- def _get_ldflags_pkg_config ():
33
- try :
34
- return subprocess .check_output ((
35
- 'pkg-config' , '--libs' ,
36
- 'python-{}.{}' .format (* sys .version_info [:2 ]),
37
- )).decode ('UTF-8' ).strip ()
38
- except (subprocess .CalledProcessError , OSError ):
39
- return None
40
-
41
-
42
- def _get_ldflags_bldlibrary ():
43
- return distutils .sysconfig .get_config_var ('BLDLIBRARY' )
44
-
45
-
46
- def _get_ldflags ():
47
- for func in (
48
- _get_ldflags_pypy ,
49
- _get_ldflags_pkg_config ,
50
- _get_ldflags_bldlibrary ,
51
- ):
52
- ret = func ()
53
- if ret is not None :
54
- return ret
55
- else :
56
- raise AssertionError ('Could not determine ldflags!' )
57
-
58
-
59
- def _print_cmd (env , cmd ):
19
+ def _check_call (cmd , cwd , env ):
60
20
envparts = [
61
21
'{}={}' .format (k , pipes .quote (v ))
62
22
for k , v in sorted (tuple (env .items ()))
@@ -65,6 +25,7 @@ def _print_cmd(env, cmd):
65
25
'$ {}' .format (' ' .join (envparts + [pipes .quote (p ) for p in cmd ])),
66
26
file = sys .stderr ,
67
27
)
28
+ subprocess .check_call (cmd , cwd = cwd , env = dict (os .environ , ** env ))
68
29
69
30
70
31
@contextlib .contextmanager
@@ -106,25 +67,19 @@ def _raise_error(msg):
106
67
shutil .copytree ('.' , root_path )
107
68
pkg_path = os .path .join (root_path , main_dir )
108
69
109
- env = {
110
- 'GOPATH' : tempdir ,
111
- 'CGO_CFLAGS' : _get_cflags (self .compiler ),
112
- 'CGO_LDFLAGS' : _get_ldflags (),
113
- }
114
- cmd_get = ('go' , 'get' )
115
- _print_cmd (env , cmd_get )
116
- subprocess .check_call (
117
- cmd_get , cwd = pkg_path , env = dict (os .environ , ** env ),
118
- )
70
+ env = {'GOPATH' : tempdir }
71
+ cmd_get = ('go' , 'get' , '-d' )
72
+ _check_call (cmd_get , cwd = pkg_path , env = env )
119
73
74
+ env .update ({
75
+ 'CGO_CFLAGS' : _get_cflags (self .compiler ),
76
+ 'CGO_LDFLAGS' : '-Wl,--unresolved-symbols=ignore-all' ,
77
+ })
120
78
cmd_build = (
121
79
'go' , 'build' , '-buildmode=c-shared' ,
122
80
'-o' , os .path .abspath (self .get_ext_fullpath (ext .name )),
123
81
)
124
- _print_cmd (env , cmd_build )
125
- subprocess .check_call (
126
- cmd_build , cwd = pkg_path , env = dict (os .environ , ** env ),
127
- )
82
+ _check_call (cmd_build , cwd = pkg_path , env = env )
128
83
129
84
return build_extension
130
85
0 commit comments