Beispiel #1
0
func (b *binder) GenObjc(outdir string) error {
	name := strings.Title(b.pkg.Name())
	mfile := filepath.Join(outdir, "Go"+name+".m")
	hfile := filepath.Join(outdir, "Go"+name+".h")

	if buildX {
		printcmd("gobind -lang=objc %s > %s", b.pkg.Path(), mfile)
	}

	const objcPrefix = "" // TODO(hyangah): -prefix

	generate := func(w io.Writer) error {
		return bind.GenObjc(w, b.fset, b.pkg, objcPrefix, false)
	}
	if err := writeFile(mfile, generate); err != nil {
		return err
	}
	generate = func(w io.Writer) error {
		return bind.GenObjc(w, b.fset, b.pkg, objcPrefix, true)
	}
	if err := writeFile(hfile, generate); err != nil {
		return err
	}

	objcPkg, err := ctx.Import("golang.org/x/mobile/bind/objc", "", build.FindOnly)
	if err != nil {
		return err
	}
	return copyFile(filepath.Join(outdir, "seq.h"), filepath.Join(objcPkg.Dir, "seq.h"))
}
Beispiel #2
0
func (b *binder) GenObjc(outdir string) error {
	name := strings.Title(b.pkg.Name())
	bindOption := "-lang=objc"
	prefix := "Go"
	if bindPrefix != "" {
		prefix = bindPrefix
		bindOption += " -prefix=" + bindPrefix
	}

	mfile := filepath.Join(outdir, prefix+name+".m")
	hfile := filepath.Join(outdir, prefix+name+".h")

	generate := func(w io.Writer) error {
		if buildX {
			printcmd("gobind %s -outdir=%s %s", bindOption, outdir, b.pkg.Path())
		}
		return bind.GenObjc(w, b.fset, b.pkg, prefix, false)
	}
	if err := writeFile(mfile, generate); err != nil {
		return err
	}
	generate = func(w io.Writer) error {
		return bind.GenObjc(w, b.fset, b.pkg, prefix, true)
	}
	if err := writeFile(hfile, generate); err != nil {
		return err
	}

	objcPkg, err := ctx.Import("golang.org/x/mobile/bind/objc", "", build.FindOnly)
	if err != nil {
		return err
	}
	return copyFile(filepath.Join(outdir, "seq.h"), filepath.Join(objcPkg.Dir, "seq.h"))
}
Beispiel #3
0
func genPkg(p *types.Package) {
	fname := defaultFileName(*lang, p)
	switch *lang {
	case "java":
		w, closer := writer(fname, p)
		processErr(bind.GenJava(w, fset, p, *javaPkg))
		closer()
	case "go":
		w, closer := writer(fname, p)
		processErr(bind.GenGo(w, fset, p))
		closer()
	case "objc":
		if fname == "" {
			processErr(bind.GenObjc(os.Stdout, fset, p, *prefix, true))
			processErr(bind.GenObjc(os.Stdout, fset, p, *prefix, false))
		} else {
			hname := fname[:len(fname)-2] + ".h"
			w, closer := writer(hname, p)
			processErr(bind.GenObjc(w, fset, p, *prefix, true))
			closer()
			w, closer = writer(fname, p)
			processErr(bind.GenObjc(w, fset, p, *prefix, false))
			closer()
		}
	default:
		errorf("unknown target language: %q", *lang)
	}
}
Beispiel #4
0
func (b *binder) GenObjc(pkg *types.Package, allPkg []*types.Package, outdir string) (string, error) {
	const bindPrefixDefault = "Go"
	if bindPrefix == "" {
		bindPrefix = bindPrefixDefault
	}
	name := strings.Title(pkg.Name())
	bindOption := "-lang=objc"
	if bindPrefix != bindPrefixDefault {
		bindOption += " -prefix=" + bindPrefix
	}

	fileBase := bindPrefix + name
	mfile := filepath.Join(outdir, fileBase+".m")
	hfile := filepath.Join(outdir, fileBase+".h")
	gohfile := filepath.Join(outdir, pkg.Name()+".h")

	conf := &bind.GeneratorConfig{
		Fset:   b.fset,
		Pkg:    pkg,
		AllPkg: allPkg,
	}
	generate := func(w io.Writer) error {
		if buildX {
			printcmd("gobind %s -outdir=%s %s", bindOption, outdir, pkg.Path())
		}
		if buildN {
			return nil
		}
		conf.Writer = w
		return bind.GenObjc(conf, bindPrefix, bind.ObjcM)
	}
	if err := writeFile(mfile, generate); err != nil {
		return "", err
	}
	generate = func(w io.Writer) error {
		if buildN {
			return nil
		}
		conf.Writer = w
		return bind.GenObjc(conf, bindPrefix, bind.ObjcH)
	}
	if err := writeFile(hfile, generate); err != nil {
		return "", err
	}
	generate = func(w io.Writer) error {
		if buildN {
			return nil
		}
		conf.Writer = w
		return bind.GenObjc(conf, bindPrefix, bind.ObjcGoH)
	}
	if err := writeFile(gohfile, generate); err != nil {
		return "", err
	}

	return fileBase, nil
}
Beispiel #5
0
func genPkg(pkg *build.Package) {
	files := parseFiles(pkg.Dir, pkg.GoFiles)
	if len(files) == 0 {
		return // some error has been reported
	}

	conf := loader.Config{
		Fset:        fset,
		AllowErrors: true,
	}
	conf.TypeChecker.IgnoreFuncBodies = true
	conf.TypeChecker.FakeImportC = true
	conf.TypeChecker.DisableUnusedImportCheck = true
	var tcErrs []error
	conf.TypeChecker.Error = func(err error) {
		tcErrs = append(tcErrs, err)
	}
	conf.CreateFromFiles(pkg.ImportPath, files...)
	program, err := conf.Load()
	if err != nil {
		for _, err := range tcErrs {
			errorf("%v", err)
		}
		errorf("%v", err)
		return
	}
	p := program.Created[0].Pkg

	fname := defaultFileName(*lang, p)
	switch *lang {
	case "java":
		w, closer := writer(fname, p)
		processErr(bind.GenJava(w, fset, p, *javaPkg))
		closer()
	case "go":
		w, closer := writer(fname, p)
		processErr(bind.GenGo(w, fset, p))
		closer()
	case "objc":
		if fname == "" {
			processErr(bind.GenObjc(os.Stdout, fset, p, *prefix, true))
			processErr(bind.GenObjc(os.Stdout, fset, p, *prefix, false))
		} else {
			hname := fname[:len(fname)-2] + ".h"
			w, closer := writer(hname, p)
			processErr(bind.GenObjc(w, fset, p, *prefix, true))
			closer()
			w, closer = writer(fname, p)
			processErr(bind.GenObjc(w, fset, p, *prefix, false))
			closer()
		}
	default:
		errorf("unknown target language: %q", *lang)
	}
}
Beispiel #6
0
func genPkg(pkg *build.Package) {
	if len(pkg.CgoFiles) > 0 {
		errorf("gobind: cannot use cgo-dependent package as service definition: %s", pkg.CgoFiles[0])
		return
	}

	files := parseFiles(pkg.Dir, pkg.GoFiles)
	if len(files) == 0 {
		return // some error has been reported
	}

	conf := loader.Config{
		Fset: fset,
	}
	conf.TypeChecker.Error = func(err error) {
		errorf("%v", err)
	}
	conf.CreateFromFiles(pkg.ImportPath, files...)
	program, err := conf.Load()
	if err != nil {
		errorf("%v", err)
		return
	}
	p := program.Created[0].Pkg

	fname := defaultFileName(*lang, p)
	switch *lang {
	case "java":
		w, closer := writer(fname, p)
		processErr(bind.GenJava(w, fset, p))
		closer()
	case "go":
		w, closer := writer(fname, p)
		processErr(bind.GenGo(w, fset, p))
		closer()
	case "objc":
		if fname == "" {
			processErr(bind.GenObjc(os.Stdout, fset, p, true))
			processErr(bind.GenObjc(os.Stdout, fset, p, false))
		} else {
			hname := fname[:len(fname)-2] + ".h"
			w, closer := writer(hname, p)
			processErr(bind.GenObjc(w, fset, p, true))
			closer()
			w, closer = writer(fname, p)
			processErr(bind.GenObjc(w, fset, p, false))
			closer()
		}
	default:
		errorf("unknown target language: %q", *lang)
	}
}
Beispiel #7
0
func genPkg(p *types.Package, allPkg []*types.Package) {
	fname := defaultFileName(*lang, p)
	conf := &bind.GeneratorConfig{
		Fset:   fset,
		Pkg:    p,
		AllPkg: allPkg,
	}
	switch *lang {
	case "java":
		w, closer := writer(fname)
		conf.Writer = w
		processErr(bind.GenJava(conf, *javaPkg, bind.Java))
		closer()
		cname := "java_" + p.Name() + ".c"
		w, closer = writer(cname)
		conf.Writer = w
		processErr(bind.GenJava(conf, *javaPkg, bind.JavaC))
		closer()
		hname := p.Name() + ".h"
		w, closer = writer(hname)
		conf.Writer = w
		processErr(bind.GenJava(conf, *javaPkg, bind.JavaH))
		closer()
	case "go":
		w, closer := writer(fname)
		conf.Writer = w
		processErr(bind.GenGo(conf))
		closer()
	case "objc":
		gohname := p.Name() + ".h"
		w, closer := writer(gohname)
		conf.Writer = w
		processErr(bind.GenObjc(conf, *prefix, bind.ObjcGoH))
		closer()
		hname := fname[:len(fname)-2] + ".h"
		w, closer = writer(hname)
		conf.Writer = w
		processErr(bind.GenObjc(conf, *prefix, bind.ObjcH))
		closer()
		w, closer = writer(fname)
		conf.Writer = w
		processErr(bind.GenObjc(conf, *prefix, bind.ObjcM))
		closer()
	default:
		errorf("unknown target language: %q", *lang)
	}
}
Beispiel #8
0
func (b *binder) GenObjc(pkg *types.Package, outdir string) (string, error) {
	const bindPrefixDefault = "Go"
	if bindPrefix == "" {
		bindPrefix = bindPrefixDefault
	}
	name := strings.Title(pkg.Name())
	bindOption := "-lang=objc"
	if bindPrefix != bindPrefixDefault {
		bindOption += " -prefix=" + bindPrefix
	}

	fileBase := bindPrefix + name
	mfile := filepath.Join(outdir, fileBase+".m")
	hfile := filepath.Join(outdir, fileBase+".h")

	generate := func(w io.Writer) error {
		if buildX {
			printcmd("gobind %s -outdir=%s %s", bindOption, outdir, pkg.Path())
		}
		if buildN {
			return nil
		}
		return bind.GenObjc(w, b.fset, pkg, bindPrefix, false)
	}
	if err := writeFile(mfile, generate); err != nil {
		return "", err
	}
	generate = func(w io.Writer) error {
		if buildN {
			return nil
		}
		return bind.GenObjc(w, b.fset, pkg, bindPrefix, true)
	}
	if err := writeFile(hfile, generate); err != nil {
		return "", err
	}

	objcPkg, err := ctx.Import("golang.org/x/mobile/bind/objc", "", build.FindOnly)
	if err != nil {
		return "", err
	}
	if err := copyFile(filepath.Join(outdir, "seq.h"), filepath.Join(objcPkg.Dir, "seq.h")); err != nil {
		return "", err
	}
	return fileBase, nil
}