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 (
|
2023-03-31 16:07:50 +00:00
|
|
|
"bytes"
|
2015-07-24 14:16:31 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2020-02-10 15:34:42 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2019-02-11 10:47:24 +00:00
|
|
|
"strings"
|
2020-02-10 15:35:56 +00:00
|
|
|
|
|
|
|
"github.com/gonuts/commander"
|
|
|
|
"github.com/gonuts/flag"
|
2021-03-17 21:02:07 +00:00
|
|
|
|
|
|
|
"github.com/go-python/gopy/bind"
|
2015-07-24 14:16:31 +00:00
|
|
|
)
|
|
|
|
|
2019-02-13 05:53:20 +00:00
|
|
|
func gopyMakeCmdBuild() *commander.Command {
|
2015-07-24 14:16:31 +00:00
|
|
|
cmd := &commander.Command{
|
2019-02-13 05:53:20 +00:00
|
|
|
Run: gopyRunCmdBuild,
|
2019-02-22 10:33:05 +00:00
|
|
|
UsageLine: "build <go-package-name> [other-go-package...]",
|
2015-07-24 14:16:31 +00:00
|
|
|
Short: "generate and compile (C)Python language bindings for Go",
|
|
|
|
Long: `
|
2019-02-22 10:33:05 +00:00
|
|
|
build generates and compiles (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 build [options] <go-package-name> [other-go-package...]
|
2019-02-13 05:53:20 +00:00
|
|
|
$ gopy build github.com/go-python/gopy/_examples/hi
|
2015-07-24 14:16:31 +00:00
|
|
|
`,
|
2019-02-13 05:53:20 +00:00
|
|
|
Flag: *flag.NewFlagSet("gopy-build", flag.ExitOnError),
|
2015-07-24 14:16:31 +00:00
|
|
|
}
|
|
|
|
|
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")
|
2017-07-13 23:04:38 +00:00
|
|
|
cmd.Flag.Bool("symbols", true, "include symbols in output")
|
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-03-29 00:40:14 +00:00
|
|
|
cmd.Flag.Bool("dynamic-link", false, "whether to link output shared library dynamically to Python")
|
2022-06-28 03:46:31 +00:00
|
|
|
cmd.Flag.String("build-tags", "", "build tags to be passed to `go build`")
|
2015-07-24 14:16:31 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-02-13 05:53:20 +00:00
|
|
|
func gopyRunCmdBuild(cmdr *commander.Command, args []string) 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.Name = cmdr.Flag.Lookup("name").Value.Get().(string)
|
|
|
|
cfg.Main = cmdr.Flag.Lookup("main").Value.Get().(string)
|
|
|
|
cfg.VM = cmdr.Flag.Lookup("vm").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.Symbols = cmdr.Flag.Lookup("symbols").Value.Get().(bool)
|
|
|
|
cfg.NoWarn = cmdr.Flag.Lookup("no-warn").Value.Get().(bool)
|
|
|
|
cfg.NoMake = cmdr.Flag.Lookup("no-make").Value.Get().(bool)
|
2022-03-29 00:40:14 +00:00
|
|
|
cfg.DynamicLinking = cmdr.Flag.Lookup("dynamic-link").Value.Get().(bool)
|
2022-06-28 03:46:31 +00:00
|
|
|
cfg.BuildTags = cmdr.Flag.Lookup("build-tags").Value.Get().(string)
|
2020-03-23 23:23:22 +00:00
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
bind.NoWarn = cfg.NoWarn
|
|
|
|
bind.NoMake = cfg.NoMake
|
2019-02-20 11:09:23 +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
|
2019-02-20 11:09:23 +00:00
|
|
|
if err != nil {
|
2020-11-24 01:18:30 +00:00
|
|
|
return fmt.Errorf("gopy-gen: go build / load of package failed with path=%q: %v", path, err)
|
2019-02-20 11:09:23 +00:00
|
|
|
}
|
2020-11-24 01:18:30 +00:00
|
|
|
pkg, err := parsePackage(bpkg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-01 04:30:13 +00:00
|
|
|
if cfg.Name == "" {
|
|
|
|
cfg.Name = pkg.Name()
|
|
|
|
}
|
2019-02-20 11:09:23 +00:00
|
|
|
}
|
2021-03-17 21:02:07 +00:00
|
|
|
return runBuild("build", cfg)
|
2019-02-22 10:33:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 00:10:50 +00:00
|
|
|
// runBuild calls genPkg and then executes commands to build the resulting files
|
|
|
|
// exe = executable mode to build an executable instead of a library
|
2019-03-06 10:41:03 +00:00
|
|
|
// mode = gen, build, pkg, exe
|
2021-03-17 21:02:07 +00:00
|
|
|
func runBuild(mode bind.BuildMode, cfg *BuildCfg) error {
|
2019-02-22 10:33:05 +00:00
|
|
|
var err error
|
2021-03-17 21:02:07 +00:00
|
|
|
cfg.OutputDir, err = genOutDir(cfg.OutputDir)
|
2019-02-20 11:09:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-17 21:02:07 +00:00
|
|
|
err = genPkg(mode, cfg)
|
2015-07-24 14:16:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
fmt.Printf("\n--- building package ---\n%s\n", cfg.Cmd)
|
2019-02-25 09:58:56 +00:00
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
buildname := cfg.Name + "_go"
|
2019-02-11 10:47:24 +00:00
|
|
|
var cmdout []byte
|
2019-03-06 10:41:03 +00:00
|
|
|
cwd, err := os.Getwd()
|
2021-03-17 21:02:07 +00:00
|
|
|
os.Chdir(cfg.OutputDir)
|
2019-03-06 10:41:03 +00:00
|
|
|
defer os.Chdir(cwd)
|
2017-06-07 14:57:51 +00:00
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
os.Remove(cfg.Name + ".c") // may fail, we don't care
|
2019-02-13 05:53:20 +00:00
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
fmt.Printf("goimports -w %v\n", cfg.Name+".go")
|
|
|
|
cmd := exec.Command("goimports", "-w", cfg.Name+".go")
|
2019-02-24 21:17:35 +00:00
|
|
|
cmdout, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("cmd had error: %v output:\no%v\n", err, string(cmdout))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
pycfg, err := bind.GetPythonConfig(cfg.VM)
|
2020-10-27 03:37:07 +00:00
|
|
|
|
2019-06-07 09:55:03 +00:00
|
|
|
if mode == bind.ModeExe {
|
2019-03-02 00:10:50 +00:00
|
|
|
of, err := os.Create(buildname + ".h") // overwrite existing
|
2019-03-07 06:44:32 +00:00
|
|
|
fmt.Fprintf(of, "typedef uint8_t bool;\n")
|
2019-03-02 00:10:50 +00:00
|
|
|
of.Close()
|
2017-06-07 14:57:51 +00:00
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
fmt.Printf("%v build.py # will fail, but needed to generate .c file\n", cfg.VM)
|
|
|
|
cmd = exec.Command(cfg.VM, "build.py")
|
2019-03-02 00:10:50 +00:00
|
|
|
cmd.Run() // will fail, we don't care about errors
|
2017-06-07 14:57:51 +00:00
|
|
|
|
2022-06-28 03:46:31 +00:00
|
|
|
args := []string{"build", "-mod=mod", "-buildmode=c-shared"}
|
|
|
|
if cfg.BuildTags != "" {
|
|
|
|
args = append(args, "-tags", cfg.BuildTags)
|
|
|
|
}
|
|
|
|
args = append(args, "-o", buildname+libExt, ".")
|
|
|
|
|
2019-03-02 00:10:50 +00:00
|
|
|
fmt.Printf("go %v\n", strings.Join(args, " "))
|
|
|
|
cmd = exec.Command("go", args...)
|
|
|
|
cmdout, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cmdout))
|
|
|
|
return err
|
|
|
|
}
|
2017-05-10 10:10:01 +00:00
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
fmt.Printf("%v build.py # should work this time\n", cfg.VM)
|
|
|
|
cmd = exec.Command(cfg.VM, "build.py")
|
2019-03-02 00:10:50 +00:00
|
|
|
cmdout, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cmdout))
|
|
|
|
return err
|
|
|
|
}
|
2019-02-11 10:47:24 +00:00
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
err = os.Remove(cfg.Name + "_go" + libExt)
|
2019-02-11 10:47:24 +00:00
|
|
|
|
2021-03-17 21:02:07 +00:00
|
|
|
fmt.Printf("go build -o py%s\n", cfg.Name)
|
2022-06-28 03:46:31 +00:00
|
|
|
cmd = exec.Command("go", "build", "-mod=mod")
|
|
|
|
if cfg.BuildTags != "" {
|
|
|
|
args = append(args, "-tags", cfg.BuildTags)
|
|
|
|
}
|
|
|
|
args = append(args, "-o", "py"+cfg.Name)
|
2019-03-02 00:10:50 +00:00
|
|
|
cmdout, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cmdout))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2021-03-18 07:45:28 +00:00
|
|
|
buildLib := buildname + libExt
|
|
|
|
extext := libExt
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
extext = ".pyd"
|
|
|
|
}
|
|
|
|
if pycfg.ExtSuffix != "" {
|
|
|
|
extext = pycfg.ExtSuffix
|
|
|
|
}
|
|
|
|
modlib := "_" + cfg.Name + extext
|
|
|
|
|
|
|
|
// build the go shared library upfront to generate the header
|
|
|
|
// needed by our generated cpython code
|
2021-08-28 11:54:17 +00:00
|
|
|
args := []string{"build", "-mod=mod", "-buildmode=c-shared"}
|
2022-06-28 03:46:31 +00:00
|
|
|
if cfg.BuildTags != "" {
|
|
|
|
args = append(args, "-tags", cfg.BuildTags)
|
|
|
|
}
|
2021-03-17 21:02:07 +00:00
|
|
|
if !cfg.Symbols {
|
2019-03-02 00:10:50 +00:00
|
|
|
// 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")
|
|
|
|
}
|
2021-03-18 07:45:28 +00:00
|
|
|
args = append(args, "-o", buildLib, ".")
|
2019-03-02 00:10:50 +00:00
|
|
|
fmt.Printf("go %v\n", strings.Join(args, " "))
|
|
|
|
cmd = exec.Command("go", args...)
|
|
|
|
cmdout, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cmdout))
|
|
|
|
return err
|
|
|
|
}
|
2021-03-18 07:45:28 +00:00
|
|
|
// update the output name to the one with the ABI extension
|
|
|
|
args[len(args)-2] = modlib
|
|
|
|
// we don't need this initial lib because we are going to relink
|
|
|
|
os.Remove(buildLib)
|
2019-03-02 00:10:50 +00:00
|
|
|
|
2021-03-18 07:45:28 +00:00
|
|
|
// generate c code
|
2021-03-17 21:02:07 +00:00
|
|
|
fmt.Printf("%v build.py\n", cfg.VM)
|
|
|
|
cmd = exec.Command(cfg.VM, "build.py")
|
2019-03-02 00:10:50 +00:00
|
|
|
cmdout, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("cmd had error: %v output:\no%v\n", err, string(cmdout))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-30 05:32:08 +00:00
|
|
|
if bind.WindowsOS {
|
|
|
|
fmt.Printf("Doing windows sed hack to fix declspec for PyInit\n")
|
2023-03-31 16:07:50 +00:00
|
|
|
fname := cfg.Name + ".c"
|
|
|
|
raw, err := os.ReadFile(fname)
|
2021-08-30 05:32:08 +00:00
|
|
|
if err != nil {
|
2023-03-31 16:07:50 +00:00
|
|
|
fmt.Printf("could not read %s: %+v", fname, err)
|
|
|
|
return fmt.Errorf("could not read %s: %w", fname, err)
|
|
|
|
}
|
|
|
|
raw = bytes.ReplaceAll(raw, []byte(" PyInit_"), []byte(" __declspec(dllexport) PyInit_"))
|
|
|
|
err = os.WriteFile(fname, raw, 0644)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("could not apply sed hack to fix declspec for PyInit: %+v", err)
|
|
|
|
return fmt.Errorf("could not apply sed hack to fix PyInit: %w", err)
|
2021-08-30 05:32:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 07:45:28 +00:00
|
|
|
cflags := strings.Fields(strings.TrimSpace(pycfg.CFlags))
|
|
|
|
cflags = append(cflags, "-fPIC", "-Ofast")
|
|
|
|
if include, exists := os.LookupEnv("GOPY_INCLUDE"); exists {
|
|
|
|
cflags = append(cflags, "-I"+filepath.ToSlash(include))
|
2019-03-02 00:10:50 +00:00
|
|
|
}
|
2022-03-29 00:40:14 +00:00
|
|
|
var ldflags []string
|
|
|
|
if cfg.DynamicLinking {
|
|
|
|
ldflags = strings.Fields(strings.TrimSpace(pycfg.LdDynamicFlags))
|
|
|
|
} else {
|
|
|
|
ldflags = strings.Fields(strings.TrimSpace(pycfg.LdFlags))
|
|
|
|
}
|
2021-03-17 21:02:07 +00:00
|
|
|
if !cfg.Symbols {
|
2021-03-18 07:45:28 +00:00
|
|
|
ldflags = append(ldflags, "-s")
|
2020-02-10 15:34:42 +00:00
|
|
|
}
|
2021-03-18 07:45:28 +00:00
|
|
|
if lib, exists := os.LookupEnv("GOPY_LIBDIR"); exists {
|
|
|
|
ldflags = append(ldflags, "-L"+filepath.ToSlash(lib))
|
2020-02-10 15:34:42 +00:00
|
|
|
}
|
2021-03-18 07:45:28 +00:00
|
|
|
if libname, exists := os.LookupEnv("GOPY_PYLIB"); exists {
|
|
|
|
ldflags = append(ldflags, "-l"+filepath.ToSlash(libname))
|
2020-02-10 15:34:42 +00:00
|
|
|
}
|
2019-08-01 14:45:45 +00:00
|
|
|
|
2021-03-18 07:45:28 +00:00
|
|
|
removeEmpty := func(src []string) []string {
|
|
|
|
o := make([]string, 0, len(src))
|
|
|
|
for _, v := range src {
|
2019-08-01 14:45:45 +00:00
|
|
|
if v == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
o = append(o, v)
|
|
|
|
}
|
|
|
|
return o
|
2021-03-18 07:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cflags = removeEmpty(cflags)
|
|
|
|
ldflags = removeEmpty(ldflags)
|
2019-03-02 00:10:50 +00:00
|
|
|
|
2021-03-18 07:45:28 +00:00
|
|
|
cflagsEnv := fmt.Sprintf("CGO_CFLAGS=%s", strings.Join(cflags, " "))
|
|
|
|
ldflagsEnv := fmt.Sprintf("CGO_LDFLAGS=%s", strings.Join(ldflags, " "))
|
|
|
|
|
|
|
|
env := os.Environ()
|
|
|
|
env = append(env, cflagsEnv)
|
|
|
|
env = append(env, ldflagsEnv)
|
|
|
|
|
|
|
|
fmt.Println(cflagsEnv)
|
|
|
|
fmt.Println(ldflagsEnv)
|
|
|
|
|
|
|
|
// build extension with go + c
|
|
|
|
fmt.Printf("go %v\n", strings.Join(args, " "))
|
|
|
|
cmd = exec.Command("go", args...)
|
|
|
|
cmd.Env = env
|
2019-03-02 00:10:50 +00:00
|
|
|
cmdout, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2021-03-18 07:45:28 +00:00
|
|
|
fmt.Printf("cmd had error: %v output:\n%v\n", err, string(cmdout))
|
2019-03-02 00:10:50 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-05-10 10:10:01 +00:00
|
|
|
}
|
2019-02-12 09:55:34 +00:00
|
|
|
|
2017-05-10 10:10:01 +00:00
|
|
|
return err
|
|
|
|
}
|