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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
2015-08-03 12:24:50 +00:00
|
|
|
cmd.Flag.String("lang", "py2", "python version to use for bindings (python2|py2|python3|py3)")
|
2015-07-24 14:16:31 +00:00
|
|
|
cmd.Flag.String("output", "", "output directory for bindings")
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
odir := cmdr.Flag.Lookup("output").Value.Get().(string)
|
2015-08-03 12:24:50 +00:00
|
|
|
lang := cmdr.Flag.Lookup("lang").Value.Get().(string)
|
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(
|
|
|
|
"gopy-bind: could not create output directory: %v\n", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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(
|
2015-08-06 16:32:30 +00:00
|
|
|
"gopy-bind: go/build.Import failed with path=%q: %v\n",
|
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(
|
2015-09-02 15:13:33 +00:00
|
|
|
"go", "get", "-buildmode=c-shared",
|
2015-08-06 16:32:30 +00:00
|
|
|
pkg.ImportPath(),
|
|
|
|
)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
log.Printf("work: %s\n", work)
|
|
|
|
|
|
|
|
err = os.MkdirAll(work, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("gopy-bind: could not create workdir (%v)", err)
|
|
|
|
}
|
|
|
|
//defer os.RemoveAll(work)
|
|
|
|
|
2015-08-03 12:24:50 +00:00
|
|
|
err = genPkg(work, pkg, lang)
|
2015-07-24 14:16:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-03 12:24:50 +00:00
|
|
|
err = genPkg(work, pkg, "go")
|
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)
|
|
|
|
|
2015-08-06 16:32:30 +00:00
|
|
|
cmd = exec.Command(
|
2015-08-07 11:43:21 +00:00
|
|
|
"go", "build", "-buildmode=c-shared",
|
2015-08-03 12:24:50 +00:00
|
|
|
"-o", filepath.Join(wbind, pkg.Name())+".so",
|
2015-07-24 14:16:31 +00:00
|
|
|
".",
|
|
|
|
)
|
|
|
|
cmd.Dir = work
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-24 14:26:27 +00:00
|
|
|
cmd = exec.Command(
|
|
|
|
"/bin/cp",
|
2015-08-03 12:24:50 +00:00
|
|
|
filepath.Join(wbind, pkg.Name())+".so",
|
|
|
|
filepath.Join(odir, pkg.Name())+".so",
|
2015-07-24 14:26:27 +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
|
|
|
return err
|
|
|
|
}
|