Repository navigation
Expand file tree
/
Copy pathcmd_gen.go
More file actions
105 lines (90 loc) · 3.76 KB
/
Copy pathcmd_gen.go
File metadata and controls
105 lines (90 loc) · 3.76 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
// Copyright 2015-present, 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"
"github.com/go-python/gopy/bind"
"github.com/gonuts/commander"
"github.com/gonuts/flag"
)
// backendHelp ends the help text of the commands that GOPY_BACKEND applies to.
const backendHelp = `
backends:
GOPY_BACKEND chooses the tool that binds Go to python: pybindgen (the
default), capi, cffi, pybind11 or nanobind. capi needs nothing beyond python
itself; the others need their own python package (pip install <name>), and
pybind11 and nanobind also a C++ compiler.
$ GOPY_BACKEND=capi gopy build github.com/go-python/gopy/_examples/hi
`
func gopyMakeCmdGen() *commander.Command {
cmd := &commander.Command{
Run: gopyRunCmdGen,
UsageLine: "gen <go-package-name> [other-go-package...]",
Short: "generate (C)Python language bindings for Go",
Long: `
gen generates (C)Python language bindings for Go package(s).
ex:
$ gopy gen [options] <go-package-name> [other-go-package...]
$ gopy gen github.com/go-python/gopy/_examples/hi
` + backendHelp,
Flag: *flag.NewFlagSet("gopy-gen", 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.String("package-prefix", ".", "custom package prefix used when generating import "+
"statements for generated package")
cmd.Flag.Bool("rename", false, "rename Go symbols to python PEP snake_case")
cmd.Flag.Bool("no-warn", false, "suppress warning messages, which may be expected")
cmd.Flag.Bool("no-make", false, "do not generate a Makefile, e.g., when called from Makefile")
cmd.Flag.Bool("clear-go-tls", false, "emit a _gopy_clear_go_tls() call before every CGo entry (issue #370); off by default, needed only when several gopy extensions share one process and known to crash CPython 3.12+ (issue #395)")
cmd.Flag.Bool("dynamic-link", false, "whether to link output shared library dynamically to Python")
cmd.Flag.String("build-tags", "", "build tags to be passed to `go build`")
return cmd
}
func gopyRunCmdGen(cmdr *commander.Command, args []string) error {
var err error
if len(args) == 0 {
err := fmt.Errorf("gopy: expect a fully qualified go package name as argument")
log.Println(err)
return err
}
cfg := NewBuildCfg()
cfg.OutputDir = cmdr.Flag.Lookup("output").Value.Get().(string)
cfg.VM = cmdr.Flag.Lookup("vm").Value.Get().(string)
cfg.Name = cmdr.Flag.Lookup("name").Value.Get().(string)
cfg.Main = cmdr.Flag.Lookup("main").Value.Get().(string)
cfg.PkgPrefix = cmdr.Flag.Lookup("package-prefix").Value.Get().(string)
cfg.RenameCase = cmdr.Flag.Lookup("rename").Value.Get().(bool)
cfg.NoWarn = cmdr.Flag.Lookup("no-warn").Value.Get().(bool)
cfg.NoMake = cmdr.Flag.Lookup("no-make").Value.Get().(bool)
cfg.DynamicLinking = cmdr.Flag.Lookup("dynamic-link").Value.Get().(bool)
cfg.BuildTags = cmdr.Flag.Lookup("build-tags").Value.Get().(string)
if cfg.VM == "" {
cfg.VM = "python"
}
bind.NoWarn = cfg.NoWarn
bind.NoMake = cfg.NoMake
bind.ClearGoTLS = cmdr.Flag.Lookup("clear-go-tls").Value.Get().(bool)
for _, path := range args {
bpkg, err := loadPackage(path, true, cfg.BuildTags) // build first
if err != nil {
return fmt.Errorf("gopy-gen: go build / load of package failed with path=%q: %v", path, err)
}
pkg, err := parsePackage(bpkg)
if cfg.Name == "" {
cfg.Name = pkg.Name()
}
if err != nil {
return err
}
}
err = genPkg(bind.ModeGen, cfg)
if err != nil {
return err
}
return err
}