Beispiel #1
0
func genPkg(odir string, p *bind.Package, lang string) error {
	var err error
	var o *os.File

	switch lang {
	case "python", "py":
		lang, err = getPythonVersion()
		if err != nil {
			return err
		}
	default:
		// no-op
	}

	pyvers := 2
	switch lang {
	case "python2", "py2":
		pyvers = 2
	case "python3", "py3":
		pyvers = 3
	}

	if err != nil {
		return err
	}

	switch lang {
	case "python2", "py2":
		o, err = os.Create(filepath.Join(odir, p.Name()+".c"))
		if err != nil {
			return err
		}
		defer o.Close()
		err = bind.GenCPython(o, fset, p, 2)
		if err != nil {
			return err
		}

	case "python3", "py3":
		return fmt.Errorf("gopy: python-3 support not yet implemented")

	case "go":
		o, err = os.Create(filepath.Join(odir, p.Name()+".go"))
		if err != nil {
			return err
		}
		defer o.Close()

		err = bind.GenGo(o, fset, p, pyvers)
		if err != nil {
			return err
		}

		var tmpdir string
		tmpdir, err = ioutil.TempDir("", "gopy-go-cgo-")
		if err != nil {
			return err
		}
		defer os.RemoveAll(tmpdir)

		hdr := filepath.Join(odir, p.Name()+".h")
		cmd := exec.Command(
			"go", "tool", "cgo",
			"-exportheader", hdr,
			o.Name(),
		)
		cmd.Dir = tmpdir
		cmd.Stdin = os.Stdin
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		err = cmd.Run()
		if err != nil {
			return err
		}

	default:
		return fmt.Errorf("unknown target language: %q\n", lang)
	}

	if err != nil {
		if list, _ := err.(bind.ErrorList); len(list) > 0 {
			for _, err := range list {
				log.Printf("%v\n", err)
			}
		} else {
			log.Printf("%v\n", err)
		}
	}

	if err != nil {
		return err
	}

	err = o.Close()
	if err != nil {
		return err
	}

	return err
}
Beispiel #2
0
func genPkg(odir string, pkg *build.Package, lang string) error {
	var err error
	var o *os.File

	files, err := parseFiles(pkg.Dir, pkg.GoFiles)
	if err != nil {
		return err
	}

	conf := loader.Config{
		Fset: fset,
	}
	conf.TypeChecker.Error = func(e error) {
		log.Printf("%v\n", e)
		err = e
	}

	p, err := newPackage(files, &conf, pkg)
	if err != nil {
		log.Printf("%v\n", err)
		return err
	}

	switch lang {
	case "python", "py":
		o, err = os.Create(filepath.Join(odir, p.Name()+".c"))
		if err != nil {
			return err
		}
		defer o.Close()
		err = bind.GenCPython(o, fset, p)
		if err != nil {
			return err
		}

	case "go":
		o, err = os.Create(filepath.Join(odir, p.Name()+".go"))
		if err != nil {
			return err
		}
		defer o.Close()

		err = bind.GenGo(o, fset, p)
		if err != nil {
			return err
		}

		var tmpdir string
		tmpdir, err = ioutil.TempDir("", "gopy-go-cgo-")
		if err != nil {
			return err
		}
		defer os.RemoveAll(tmpdir)

		hdr := filepath.Join(odir, p.Name()+".h")
		cmd := exec.Command(
			"go", "tool", "cgo",
			"-exportheader", hdr,
			o.Name(),
		)
		cmd.Dir = tmpdir
		cmd.Stdin = os.Stdin
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		err = cmd.Run()
		if err != nil {
			return err
		}

	default:
		return fmt.Errorf("unknown target language: %q\n", lang)
	}

	if err != nil {
		if list, _ := err.(bind.ErrorList); len(list) > 0 {
			for _, err := range list {
				log.Printf("%v\n", err)
			}
		} else {
			log.Printf("%v\n", err)
		}
	}

	if err != nil {
		return err
	}

	err = o.Close()
	if err != nil {
		return err
	}

	return err
}