forked from go-python/gopy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_bind.go
More file actions
157 lines (135 loc) · 3.28 KB
/
Copy pathcmd_bind.go
File metadata and controls
157 lines (135 loc) · 3.28 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2015 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
)
func gopyMakeCmdBind() *commander.Command {
cmd := &commander.Command{
Run: gopyRunCmdBind,
UsageLine: "bind <go-package-name>",
Short: "generate and compile (C)Python language bindings for Go",
Long: `
bind generates and compiles (C)Python language bindings for a Go package.
ex:
$ gopy bind [options] <go-package-name>
$ gopy bind github.com/go-python/gopy/_examples/hi
`,
Flag: *flag.NewFlagSet("gopy-bind", flag.ExitOnError),
}
cmd.Flag.String("lang", defaultPyVersion, "python version to use for bindings (python2|py2|python3|py3)")
cmd.Flag.String("output", "", "output directory for bindings")
return cmd
}
func gopyRunCmdBind(cmdr *commander.Command, args []string) error {
var err error
if len(args) != 1 {
log.Printf("expect a fully qualified go package name as argument\n")
return fmt.Errorf(
"gopy-bind: expect a fully qualified go package name as argument",
)
}
odir := cmdr.Flag.Lookup("output").Value.Get().(string)
lang := cmdr.Flag.Lookup("lang").Value.Get().(string)
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
if odir == "" {
odir = cwd
} else {
err = os.MkdirAll(odir, 0755)
if err != nil {
return fmt.Errorf(
"gopy-bind: could not create output directory: %v\n", err,
)
}
}
odir, err = filepath.Abs(odir)
if err != nil {
return err
}
path := args[0]
pkg, err := newPackage(path)
if err != nil {
return fmt.Errorf(
"gopy-bind: go/build.Import failed with path=%q: %v\n",
path,
err,
)
}
// go-get it to tickle the GOPATH cache (and make sure it compiles
// correctly)
cmd := exec.Command(
"go", "get", "-buildmode=c-shared",
pkg.ImportPath(),
)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return err
}
work, err := ioutil.TempDir("", "gopy-")
if err != nil {
return fmt.Errorf("gopy-bind: could not create temp-workdir (%v)", err)
}
log.Printf("work: %s\n", work)
err = os.MkdirAll(work, 0644)
if err != nil {
return fmt.Errorf("gopy-bind: could not create workdir (%v)", err)
}
//defer os.RemoveAll(work)
err = genPkg(work, pkg, lang)
if err != nil {
return err
}
err = genPkg(work, pkg, "go")
if err != nil {
return err
}
wbind, err := ioutil.TempDir("", "gopy-")
if err != nil {
return fmt.Errorf("gopy-bind: could not create temp-workdir (%v)", err)
}
err = os.MkdirAll(wbind, 0644)
if err != nil {
return fmt.Errorf("gopy-bind: could not create workdir (%v)", err)
}
defer os.RemoveAll(wbind)
cmd = exec.Command(
"go", "build", "-buildmode=c-shared",
"-o", filepath.Join(wbind, pkg.Name())+".so",
".",
)
cmd.Dir = work
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return err
}
cmd = exec.Command(
"/bin/cp",
filepath.Join(wbind, pkg.Name())+".so",
filepath.Join(odir, pkg.Name())+".so",
)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return err
}
return err
}