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)
	}

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

	objcPkg, err := ctx.Import("github.com/c-darwin/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 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))
		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)
	}
}