-
Notifications
You must be signed in to change notification settings - Fork 113
/
gen.go
160 lines (142 loc) · 3.85 KB
/
gen.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
// 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"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/pkg/errors"
"golang.org/x/tools/go/packages"
"github.com/go-python/gopy/bind"
)
// argStr returns the full command args as a string, without path to exe
func argStr() string {
ma := make([]string, len(os.Args))
copy(ma, os.Args)
_, cmd := filepath.Split(ma[0])
ma[0] = cmd
for i := range ma {
if strings.HasPrefix(ma[i], "-main=") {
ma[i] = "-main=\"" + ma[i][6:] + "\""
}
}
return strings.Join(ma, " ")
}
// genOutDir makes the output directory and returns its absolute path
func genOutDir(odir string) (string, error) {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
if odir == "" {
odir = cwd
} else {
err = os.MkdirAll(odir, 0755)
if err != nil {
return odir, fmt.Errorf("gopy-gen: could not create output directory: %v", err)
}
}
odir, err = filepath.Abs(odir)
if err != nil {
return odir, fmt.Errorf("gopy-gen: could not infer absolute path to output directory: %v", err)
}
return odir, nil
}
// genPkg generates output for all the current packages that have been parsed,
// in the given output directory
// mode = gen, build, pkg, exe
func genPkg(mode bind.BuildMode, cfg *BuildCfg) error {
var err error
cfg.OutputDir, err = genOutDir(cfg.OutputDir)
if err != nil {
return err
}
if !filepath.IsAbs(cfg.VM) {
cfg.VM, err = exec.LookPath(cfg.VM)
if err != nil {
return errors.Wrapf(err, "could not locate absolute path to python VM")
}
}
pyvers, err := getPythonVersion(cfg.VM)
if err != nil {
return err
}
err = bind.GenPyBind(mode, libExt, extraGccArgs, pyvers, cfg.DynamicLinking, &cfg.BindCfg)
if err != nil {
log.Println(err)
}
return err
}
func loadPackage(path string, buildFirst bool, buildTags string) (*packages.Package, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
if buildFirst {
args := []string{"build"}
if buildTags != "" {
buildTagStr := fmt.Sprintf("\"%s\"", strings.Join(strings.Split(buildTags, ","), " "))
args = append(args, "-tags", buildTagStr)
}
args = append(args, "-v", path)
fmt.Printf("go %s\n", strings.Join(args, " "))
cmd := exec.Command("go", args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = cwd
err = cmd.Run()
if err != nil {
log.Printf("Note: there was an error building [%s] -- will continue but it may fail later: %v\n",
path,
err,
)
}
}
// golang.org/x/tools/go/packages supports modules or GOPATH etc
mode := packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedDeps | packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes
bpkgs, err := packages.Load(&packages.Config{Mode: mode}, path)
if err != nil {
log.Printf("error resolving import path [%s]: %v\n",
path,
err,
)
return nil, err
}
bpkg := bpkgs[0] // only ever have one at a time
return bpkg, nil
}
func parsePackage(bpkg *packages.Package) (*bind.Package, error) {
if len(bpkg.GoFiles) == 0 {
err := fmt.Errorf("gopy: no files in package %q", bpkg.PkgPath)
fmt.Println(err)
return nil, err
}
dir, _ := filepath.Split(bpkg.GoFiles[0])
p := bpkg.Types
if bpkg.Name == "main" {
err := fmt.Errorf("gopy: skipping 'main' package %q", bpkg.PkgPath)
fmt.Println(err)
return nil, err
}
fset := token.NewFileSet()
var pkgast *ast.Package
pkgs, err := parser.ParseDir(fset, dir, nil, parser.ParseComments)
if err != nil {
return nil, err
}
pkgast = pkgs[p.Name()]
if pkgast == nil {
return nil, fmt.Errorf("gopy: could not find AST for package %q", p.Name())
}
pkgdoc := doc.New(pkgast, bpkg.PkgPath, 0)
return bind.NewPackage(p, pkgdoc)
}