2015-08-03 12:23:00 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2019-06-12 09:02:27 +00:00
|
|
|
"github.com/go-python/gopy/bind"
|
2015-07-24 14:16:31 +00:00
|
|
|
"github.com/gonuts/commander"
|
|
|
|
"github.com/gonuts/flag"
|
|
|
|
)
|
|
|
|
|
|
|
|
func gopyMakeCmdGen() *commander.Command {
|
|
|
|
cmd := &commander.Command{
|
|
|
|
Run: gopyRunCmdGen,
|
2019-02-22 10:33:05 +00:00
|
|
|
UsageLine: "gen <go-package-name> [other-go-package...]",
|
2015-07-24 14:16:31 +00:00
|
|
|
Short: "generate (C)Python language bindings for Go",
|
|
|
|
Long: `
|
2019-02-22 10:33:05 +00:00
|
|
|
gen generates (C)Python language bindings for Go package(s).
|
2015-07-24 14:16:31 +00:00
|
|
|
|
|
|
|
ex:
|
2019-02-22 10:33:05 +00:00
|
|
|
$ gopy gen [options] <go-package-name> [other-go-package...]
|
2015-07-24 14:16:31 +00:00
|
|
|
$ gopy gen github.com/go-python/gopy/_examples/hi
|
|
|
|
`,
|
|
|
|
Flag: *flag.NewFlagSet("gopy-gen", flag.ExitOnError),
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:00:12 +00:00
|
|
|
cmd.Flag.String("vm", "python", "path to python interpreter")
|
2015-07-24 14:16:31 +00:00
|
|
|
cmd.Flag.String("output", "", "output directory for bindings")
|
2019-02-22 10:33:05 +00:00
|
|
|
cmd.Flag.String("name", "", "name of output package (otherwise name of first package is used)")
|
2019-02-25 09:58:56 +00:00
|
|
|
cmd.Flag.String("main", "", "code string to run in the go main() function in the cgo library")
|
2021-03-17 21:02:07 +00:00
|
|
|
cmd.Flag.String("package-prefix", ".", "custom package prefix used when generating import "+
|
|
|
|
"statements for generated package")
|
2021-03-26 08:16:11 +00:00
|
|
|
cmd.Flag.Bool("rename", false, "rename Go symbols to python PEP snake_case")
|
2020-03-23 23:23:22 +00:00
|
|
|
cmd.Flag.Bool("no-warn", false, "suppress warning messages, which may be expected")
|
2020-09-20 01:03:45 +00:00
|
|
|
cmd.Flag.Bool("no-make", false, "do not generate a Makefile, e.g., when called from Makefile")
|
2022-07-01 06:30:35 +00:00
|
|
|
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`")
|
2015-07-24 14:16:31 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func gopyRunCmdGen(cmdr *commander.Command, args []string) error {
|
|
|
|
var err error
|
|
|
|
|
2019-02-23 07:13:27 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
err := fmt.Errorf("gopy: expect a fully qualified go package name as argument")
|
|
|
|
log.Println(err)
|
|
|
|
return err
|
2015-07-24 14:16:31 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
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)
|
2021-03-26 08:16:11 +00:00
|
|
|
cfg.RenameCase = cmdr.Flag.Lookup("rename").Value.Get().(bool)
|
2021-03-17 21:02:07 +00:00
|
|
|
cfg.NoWarn = cmdr.Flag.Lookup("no-warn").Value.Get().(bool)
|
|
|
|
cfg.NoMake = cmdr.Flag.Lookup("no-make").Value.Get().(bool)
|
2022-07-01 06:30:35 +00:00
|
|
|
cfg.DynamicLinking = cmdr.Flag.Lookup("dynamic-link").Value.Get().(bool)
|
|
|
|
cfg.BuildTags = cmdr.Flag.Lookup("build-tags").Value.Get().(string)
|
2019-02-22 10:33:05 +00:00
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
if cfg.VM == "" {
|
|
|
|
cfg.VM = "python"
|
2019-01-11 11:00:12 +00:00
|
|
|
}
|
2015-07-24 14:16:31 +00:00
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
bind.NoWarn = cfg.NoWarn
|
|
|
|
bind.NoMake = cfg.NoMake
|
2020-03-23 23:23:22 +00:00
|
|
|
|
2019-02-22 10:33:05 +00:00
|
|
|
for _, path := range args {
|
2022-07-14 20:58:59 +00:00
|
|
|
bpkg, err := loadPackage(path, true, cfg.BuildTags) // build first
|
2020-11-24 01:18:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("gopy-gen: go build / load of package failed with path=%q: %v", path, err)
|
|
|
|
}
|
|
|
|
pkg, err := parsePackage(bpkg)
|
2021-03-17 21:02:07 +00:00
|
|
|
if cfg.Name == "" {
|
|
|
|
cfg.Name = pkg.Name()
|
2019-02-22 10:33:05 +00:00
|
|
|
}
|
2015-07-24 14:16:31 +00:00
|
|
|
if err != nil {
|
2020-11-24 01:18:30 +00:00
|
|
|
return err
|
2015-07-24 14:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
err = genPkg(bind.ModeGen, cfg)
|
2015-07-24 14:16:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|