22from __future__ import unicode_literals
33
44import contextlib
5- import distutils .sysconfig
65import os
76import pipes
87import shutil
1312from setuptools .command .build_ext import build_ext as _build_ext
1413
1514
16- PYPY = '__pypy__' in sys .builtin_module_names
17-
18-
1915def _get_cflags (compiler ):
2016 return ' ' .join ('-I{}' .format (p ) for p in compiler .include_dirs )
2117
2218
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 ):
6020 envparts = [
6121 '{}={}' .format (k , pipes .quote (v ))
6222 for k , v in sorted (tuple (env .items ()))
@@ -65,6 +25,7 @@ def _print_cmd(env, cmd):
6525 '$ {}' .format (' ' .join (envparts + [pipes .quote (p ) for p in cmd ])),
6626 file = sys .stderr ,
6727 )
28+ subprocess .check_call (cmd , cwd = cwd , env = dict (os .environ , ** env ))
6829
6930
7031@contextlib .contextmanager
@@ -106,25 +67,19 @@ def _raise_error(msg):
10667 shutil .copytree ('.' , root_path )
10768 pkg_path = os .path .join (root_path , main_dir )
10869
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 )
11973
74+ env .update ({
75+ 'CGO_CFLAGS' : _get_cflags (self .compiler ),
76+ 'CGO_LDFLAGS' : '-Wl,--unresolved-symbols=ignore-all' ,
77+ })
12078 cmd_build = (
12179 'go' , 'build' , '-buildmode=c-shared' ,
12280 '-o' , os .path .abspath (self .get_ext_fullpath (ext .name )),
12381 )
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 )
12883
12984 return build_extension
13085
0 commit comments