This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 139
/
gen-cgoflags.go
141 lines (120 loc) · 2.89 KB
/
gen-cgoflags.go
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
135
136
137
138
139
140
141
// +build ignore
// simple command to generate CGOFLAGS for a given python VM
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"strings"
"github.com/pkg/errors"
)
func main() {
vm := flag.String("vm", "python", "path to a python VM")
flag.Parse()
if *vm == "" {
log.Fatalf("need a python VM")
}
cfg, err := getPythonConfig(*vm)
if err != nil {
log.Fatalf("could not infer python configuration: %v", err)
}
oname := "cgoflags_unix.go"
switch runtime.GOOS {
case "windows":
oname = "cgoflags_windows.go"
}
err = ioutil.WriteFile(oname, []byte(fmt.Sprintf(tmpl,
cfg.cflags,
cfg.ldflags,
runtime.GOOS,
)), 0644)
if err != nil {
log.Fatalf("could not write %q: %v", oname, err)
}
}
// getPythonConfig returns the needed python configuration for the given
// python VM (python, python2, python3, pypy, etc...)
func getPythonConfig(vm string) (pyconfig, error) {
code := `import sys
import distutils.sysconfig as ds
import json
print(ds.get_config_vars())
print(ds.get_python_lib())
print(json.dumps({
"version": sys.version_info.major,
"prefix": ds.get_config_var("prefix"),
"incdir": ds.get_python_inc(),
"libdir": ds.get_config_var("LIBDIR"),
"libdest": ds.get_config_var("LIBDEST"),
"libpy": ds.get_config_var("LIBRARY"),
"shlibs": ds.get_config_var("SHLIBS"),
"syslibs": ds.get_config_var("SYSLIBS"),
"shlinks": ds.get_config_var("LINKFORSHARED"),
"DLLs": ds.get_config_var("DLLLIBRARY"),
}))
`
var cfg pyconfig
bin, err := exec.LookPath(vm)
if err != nil {
return cfg, errors.Wrapf(err, "could not locate python vm %q", vm)
}
buf := new(bytes.Buffer)
cmd := exec.Command(bin, "-c", code)
cmd.Stdin = os.Stdin
cmd.Stdout = buf
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return cfg, errors.Wrap(err, "could not run python-config script")
}
log.Printf("distutils:\n%s", buf.String())
var raw struct {
Version int `json:"version"`
IncDir string `json:"incdir"`
LibDir string `json:"libdir"`
LibPy string `json:"libpy"`
ShLibs string `json:"shlibs"`
SysLibs string `json:"syslibs"`
}
err = json.NewDecoder(buf).Decode(&raw)
if err != nil {
return cfg, errors.Wrapf(err, "could not decode JSON script output")
}
if strings.HasSuffix(raw.LibPy, ".a") {
raw.LibPy = raw.LibPy[:len(raw.LibPy)-len(".a")]
}
if strings.HasPrefix(raw.LibPy, "lib") {
raw.LibPy = raw.LibPy[len("lib"):]
}
cfg.version = raw.Version
cfg.cflags = strings.Join([]string{
"-I " + raw.IncDir,
}, " ")
cfg.ldflags = strings.Join([]string{
"-L " + raw.LibDir,
"-l " + raw.LibPy,
raw.ShLibs,
raw.SysLibs,
}, " ")
return cfg, nil
}
type pyconfig struct {
version int
cflags string
ldflags string
}
const tmpl = `// Automatically generated. Do not edit.
// +build %[3]s
package python
// #cgo %[3]s CFLAGS: %[1]s
// #cgo %[3]s LDFLAGS: %[2]s
//
// #include "go-python.h"
import "C"
`