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"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/gonuts/commander"
|
|
|
|
"github.com/gonuts/flag"
|
2019-01-11 11:00:12 +00:00
|
|
|
"github.com/pkg/errors"
|
2015-07-24 14:16:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:00:12 +00:00
|
|
|
cmd.Flag.String("vm", "python", "path to python interpreter")
|
|
|
|
cmd.Flag.String("api", "cpython", "bindings API to use (cpython, cffi)")
|
2015-07-24 14:16:31 +00:00
|
|
|
cmd.Flag.String("output", "", "output directory for bindings")
|
2017-07-13 23:04:38 +00:00
|
|
|
cmd.Flag.Bool("symbols", true, "include symbols in output")
|
2017-08-08 09:16:10 +00:00
|
|
|
cmd.Flag.Bool("work", false, "print the name of temporary work directory and do not delete it when exiting")
|
2015-07-24 14:16:31 +00:00
|
|
|
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",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:00:12 +00:00
|
|
|
var (
|
|
|
|
odir = cmdr.Flag.Lookup("output").Value.Get().(string)
|
|
|
|
vm = cmdr.Flag.Lookup("vm").Value.Get().(string)
|
|
|
|
api = cmdr.Flag.Lookup("api").Value.Get().(string)
|
|
|
|
symbols = cmdr.Flag.Lookup("symbols").Value.Get().(bool)
|
|
|
|
printWork = cmdr.Flag.Lookup("work").Value.Get().(bool)
|
|
|
|
)
|
2015-07-24 14:16:31 +00:00
|
|
|
|
|
|
|
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(
|
2017-08-09 20:22:09 +00:00
|
|
|
"gopy-bind: could not create output directory: %v", err,
|
2015-07-24 14:16:31 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
odir, err = filepath.Abs(odir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := args[0]
|
2015-08-03 12:24:50 +00:00
|
|
|
pkg, err := newPackage(path)
|
2015-07-24 14:16:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
2017-08-09 20:22:09 +00:00
|
|
|
"gopy-bind: go/build.Import failed with path=%q: %v",
|
2015-07-24 14:16:31 +00:00
|
|
|
path,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-09-02 15:13:33 +00:00
|
|
|
// go-get it to tickle the GOPATH cache (and make sure it compiles
|
|
|
|
// correctly)
|
2015-08-06 16:32:30 +00:00
|
|
|
cmd := exec.Command(
|
2017-06-27 06:14:11 +00:00
|
|
|
"go", "get", pkg.ImportPath(),
|
2015-08-06 16:32:30 +00:00
|
|
|
)
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-24 14:16:31 +00:00
|
|
|
work, err := ioutil.TempDir("", "gopy-")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("gopy-bind: could not create temp-workdir (%v)", err)
|
|
|
|
}
|
2017-08-08 09:16:10 +00:00
|
|
|
if printWork {
|
|
|
|
log.Printf("work: %s\n", work)
|
|
|
|
}
|
2015-07-24 14:16:31 +00:00
|
|
|
|
|
|
|
err = os.MkdirAll(work, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("gopy-bind: could not create workdir (%v)", err)
|
|
|
|
}
|
2017-08-08 09:16:10 +00:00
|
|
|
if !printWork {
|
|
|
|
defer os.RemoveAll(work)
|
|
|
|
}
|
2015-07-24 14:16:31 +00:00
|
|
|
|
2019-01-11 11:00:12 +00:00
|
|
|
err = genPkg(work, pkg, vm, api)
|
2015-07-24 14:16:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-24 14:26:27 +00:00
|
|
|
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)
|
|
|
|
|
2017-05-10 10:10:01 +00:00
|
|
|
buildname := pkg.Name()
|
2019-01-11 11:00:12 +00:00
|
|
|
switch api {
|
2017-05-10 10:10:01 +00:00
|
|
|
case "cffi":
|
2019-01-11 11:00:12 +00:00
|
|
|
// Since Python importing module priority is XXXX.so > XXXX.py,
|
|
|
|
// We need to change shared module name from 'XXXX.so' to '_XXXX.so'.
|
|
|
|
// As the result, an user can import XXXX.py.
|
2017-05-10 10:10:01 +00:00
|
|
|
buildname = "_" + buildname
|
2017-07-13 23:04:38 +00:00
|
|
|
cmd = getBuildCommand(wbind, buildname, work, symbols)
|
2017-05-10 10:10:01 +00:00
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-07 14:57:51 +00:00
|
|
|
|
2019-01-11 11:00:12 +00:00
|
|
|
err = copyCmd(
|
|
|
|
filepath.Join(wbind, buildname)+libExt,
|
|
|
|
filepath.Join(odir, buildname)+libExt,
|
2017-06-07 14:57:51 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:00:12 +00:00
|
|
|
err = copyCmd(
|
2017-06-07 14:57:51 +00:00
|
|
|
filepath.Join(work, pkg.Name())+".py",
|
|
|
|
filepath.Join(odir, pkg.Name())+".py",
|
|
|
|
)
|
2017-05-10 10:10:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-07 14:57:51 +00:00
|
|
|
|
2019-01-11 11:00:12 +00:00
|
|
|
case "cpython":
|
2017-07-13 23:04:38 +00:00
|
|
|
cmd = getBuildCommand(wbind, buildname, work, symbols)
|
2017-05-10 10:10:01 +00:00
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
2019-01-11 11:00:12 +00:00
|
|
|
return errors.Wrapf(err, "could not generate build command")
|
2017-05-10 10:10:01 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 11:00:12 +00:00
|
|
|
err = copyCmd(
|
|
|
|
filepath.Join(wbind, buildname)+libExt,
|
|
|
|
filepath.Join(odir, buildname)+libExt,
|
2017-05-10 10:10:01 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
2019-01-11 11:00:12 +00:00
|
|
|
return fmt.Errorf("gopy: unknown target API: %q", api)
|
2017-05-10 10:10:01 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2017-07-13 23:04:38 +00:00
|
|
|
|
|
|
|
func getBuildCommand(wbind string, buildname string, work string, symbols bool) (cmd *exec.Cmd) {
|
|
|
|
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")
|
|
|
|
}
|
2019-01-11 11:00:12 +00:00
|
|
|
args = append(args, "-o", filepath.Join(wbind, buildname)+libExt, ".")
|
2017-07-13 23:04:38 +00:00
|
|
|
cmd = exec.Command(
|
|
|
|
"go", args...,
|
|
|
|
)
|
|
|
|
cmd.Dir = work
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd
|
|
|
|
}
|