GetPythonConfig returns the needed python configuration for the given python VM (python, python2, python3, pypy, etc...)
(vm string)
| 102 | // GetPythonConfig returns the needed python configuration for the given |
| 103 | // python VM (python, python2, python3, pypy, etc...) |
| 104 | func GetPythonConfig(vm string) (PyConfig, error) { |
| 105 | code := `import sys |
| 106 | try: |
| 107 | import sysconfig as ds |
| 108 | def _get_python_inc(): |
| 109 | return ds.get_path('include') |
| 110 | except ImportError: |
| 111 | import distutils.sysconfig as ds |
| 112 | _get_python_inc = ds.get_config_var |
| 113 | import json |
| 114 | import os |
| 115 | version=sys.version_info.major |
| 116 | |
| 117 | def clear_ld_flags(s): |
| 118 | if s is None: |
| 119 | return '' |
| 120 | skip_first_word = s.split(' ', 1)[1] # skip compiler name |
| 121 | skip_bundle = skip_first_word.replace('-bundle', '') # cgo already passes -dynamiclib |
| 122 | return skip_bundle |
| 123 | |
| 124 | |
| 125 | if "GOPY_INCLUDE" in os.environ and "GOPY_LIBDIR" in os.environ and "GOPY_PYLIB" in os.environ: |
| 126 | print(json.dumps({ |
| 127 | "version": version, |
| 128 | "minor": sys.version_info.minor, |
| 129 | "incdir": os.environ["GOPY_INCLUDE"], |
| 130 | "libdir": os.environ["GOPY_LIBDIR"], |
| 131 | "libpy": os.environ["GOPY_PYLIB"], |
| 132 | "shlibs": ds.get_config_var("SHLIBS"), |
| 133 | "syslibs": ds.get_config_var("SYSLIBS"), |
| 134 | "shlinks": ds.get_config_var("LINKFORSHARED"), |
| 135 | "shflags": clear_ld_flags(ds.get_config_var("LDSHARED")), |
| 136 | "extsuffix": ds.get_config_var("EXT_SUFFIX"), |
| 137 | })) |
| 138 | else: |
| 139 | print(json.dumps({ |
| 140 | "version": sys.version_info.major, |
| 141 | "minor": sys.version_info.minor, |
| 142 | "incdir": _get_python_inc(), |
| 143 | "libdir": ds.get_config_var("LIBDIR"), |
| 144 | "libpy": ds.get_config_var("LIBRARY"), |
| 145 | "shlibs": ds.get_config_var("SHLIBS"), |
| 146 | "syslibs": ds.get_config_var("SYSLIBS"), |
| 147 | "shlinks": ds.get_config_var("LINKFORSHARED"), |
| 148 | "shflags": clear_ld_flags(ds.get_config_var("LDSHARED")), |
| 149 | "extsuffix": ds.get_config_var("EXT_SUFFIX"), |
| 150 | })) |
| 151 | ` |
| 152 | |
| 153 | var cfg PyConfig |
| 154 | bin, err := exec.LookPath(vm) |
| 155 | if err != nil { |
| 156 | return cfg, errors.Wrapf(err, "could not locate python vm %q", vm) |
| 157 | } |
| 158 | |
| 159 | buf := new(bytes.Buffer) |
| 160 | cmd := exec.Command(bin, "-c", code) |
| 161 | cmd.Stdin = os.Stdin |