forked from go-python/gopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_build.go
217 lines (187 loc) · 6.39 KB
/
cmd_build.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// 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"
"log"
"os"
"os/exec"
"strings"
"github.com/go-python/gopy/bind"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
)
func gopyMakeCmdBuild() *commander.Command {
cmd := &commander.Command{
Run: gopyRunCmdBuild,
UsageLine: "build <go-package-name> [other-go-package...]",
Short: "generate and compile (C)Python language bindings for Go",
Long: `
build generates and compiles (C)Python language bindings for Go package(s).
ex:
$ gopy build [options] <go-package-name> [other-go-package...]
$ gopy build github.com/go-python/gopy/_examples/hi
`,
Flag: *flag.NewFlagSet("gopy-build", flag.ExitOnError),
}
cmd.Flag.String("vm", "python", "path to python interpreter")
cmd.Flag.String("output", "", "output directory for bindings")
cmd.Flag.String("name", "", "name of output package (otherwise name of first package is used)")
cmd.Flag.String("main", "", "code string to run in the go main() function in the cgo library")
cmd.Flag.Bool("symbols", true, "include symbols in output")
return cmd
}
func gopyRunCmdBuild(cmdr *commander.Command, args []string) error {
if len(args) == 0 {
err := fmt.Errorf("gopy: expect a fully qualified go package name as argument")
log.Println(err)
return err
}
var (
odir = cmdr.Flag.Lookup("output").Value.Get().(string)
name = cmdr.Flag.Lookup("name").Value.Get().(string)
mainstr = cmdr.Flag.Lookup("main").Value.Get().(string)
vm = cmdr.Flag.Lookup("vm").Value.Get().(string)
symbols = cmdr.Flag.Lookup("symbols").Value.Get().(bool)
)
cmdstr := argStr()
for _, path := range args {
pkg, err := newPackage(path)
if err != nil {
return fmt.Errorf("gopy-build: go/build.Import failed with path=%q: %v", path, err)
}
if name == "" {
name = pkg.Name()
}
}
return runBuild("build", odir, name, cmdstr, vm, mainstr, symbols)
}
// runBuild calls genPkg and then executes commands to build the resulting files
// exe = executable mode to build an executable instead of a library
// mode = gen, build, pkg, exe
func runBuild(mode bind.BuildMode, odir, outname, cmdstr, vm, mainstr string, symbols bool) error {
var err error
odir, err = genOutDir(odir)
if err != nil {
return err
}
err = genPkg(mode, odir, outname, cmdstr, vm, mainstr)
if err != nil {
return err
}
fmt.Printf("\n--- building package ---\n%s\n", cmdstr)
buildname := outname + "_go"
var cmdout []byte
cwd, err := os.Getwd()
os.Chdir(odir)
defer os.Chdir(cwd)
os.Remove(outname + ".c") // may fail, we don't care
fmt.Printf("goimports -w %v\n", outname+".go")
cmd := exec.Command("goimports", "-w", outname+".go")
cmdout, err = cmd.CombinedOutput()
if err != nil {
fmt.Printf("cmd had error: %v output:\no%v\n", err, string(cmdout))
return err
}
if mode == bind.ModeExe {
of, err := os.Create(buildname + ".h") // overwrite existing
fmt.Fprintf(of, "typedef uint8_t bool;\n")
of.Close()
fmt.Printf("%v build.py # will fail, but needed to generate .c file\n", vm)
cmd = exec.Command(vm, "build.py")
cmd.Run() // will fail, we don't care about errors
args := []string{"build", "-buildmode=c-shared", "-o", buildname + libExt, "."}
fmt.Printf("go %v\n", strings.Join(args, " "))
cmd = exec.Command("go", args...)
cmdout, err = cmd.CombinedOutput()
if err != nil {
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cmdout))
return err
}
fmt.Printf("%v build.py # should work this time\n", vm)
cmd = exec.Command(vm, "build.py")
cmdout, err = cmd.CombinedOutput()
if err != nil {
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cmdout))
return err
}
err = os.Remove(outname + "_go" + libExt)
fmt.Printf("go build -o py%s\n", outname)
cmd = exec.Command("go", "build", "-o", "py"+outname)
cmdout, err = cmd.CombinedOutput()
if err != nil {
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cmdout))
return err
}
} else {
args := []string{"build", "-buildmode=c-shared"}
if !symbols {
// These flags will omit the various symbol tables, thereby
// reducing the final size of the binary. From https://golang.org/cmd/link/
// -s Omit the symbol table and debug information
// -w Omit the DWARF symbol table
args = append(args, "-ldflags=-s -w")
}
args = append(args, "-o", buildname+libExt, ".")
fmt.Printf("go %v\n", strings.Join(args, " "))
cmd = exec.Command("go", args...)
cmdout, err = cmd.CombinedOutput()
if err != nil {
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cmdout))
return err
}
fmt.Printf("%v build.py\n", vm)
cmd = exec.Command(vm, "build.py")
cmdout, err = cmd.CombinedOutput()
if err != nil {
fmt.Printf("cmd had error: %v output:\no%v\n", err, string(cmdout))
return err
}
fmt.Printf("go env CC\n")
cmd = exec.Command("go", "env", "CC")
cccmdb, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cccmdb))
return err
}
cccmd := strings.TrimSpace(string(cccmdb))
fmt.Printf("%v-config --cflags\n", vm)
cmd = exec.Command(vm+"-config", "--cflags") // TODO: need minor version!
cflags, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cflags))
return err
}
fmt.Printf("%v-config --ldflags\n", vm)
cmd = exec.Command(vm+"-config", "--ldflags")
ldflags, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(ldflags))
return err
}
modlib := "_" + outname + libExt
gccargs := []string{outname + ".c", extraGccArgs, outname + "_go" + libExt, "-o", modlib}
gccargs = append(gccargs, strings.Split(strings.TrimSpace(string(cflags)), " ")...)
gccargs = append(gccargs, strings.Split(strings.TrimSpace(string(ldflags)), " ")...)
gccargs = append(gccargs, "-fPIC", "--shared")
gccargs = func(vs []string) []string {
o := make([]string, 0, len(gccargs))
for _, v := range vs {
if v == "" {
continue
}
o = append(o, v)
}
return o
}(gccargs)
fmt.Printf("%s %v\n", cccmd, strings.Join(gccargs, " "))
cmd = exec.Command(cccmd, gccargs...)
cmdout, err = cmd.CombinedOutput()
if err != nil {
fmt.Printf("cmd had error: %v\noutput: %v\n", err, string(cmdout))
return err
}
}
return err
}