Exemplo n.º 1
0
func GenClasses(pkgs []*build.Package, srcDir, jpkgSrc string) ([]*java.Class, error) {
	refs, err := importers.AnalyzePackages(pkgs, "Java/")
	if err != nil {
		return nil, err
	}
	bClspath, err := bootClasspath()
	if err != nil {
		return nil, err
	}
	classes, err := java.Import(bClspath, bindClasspath, refs)
	if err != nil {
		return nil, err
	}
	var buf bytes.Buffer
	g := &bind.ClassGen{
		Printer: &bind.Printer{
			IndentEach: []byte("\t"),
			Buf:        &buf,
		},
	}
	g.Init(classes)
	for i, jpkg := range g.Packages() {
		pkgDir := filepath.Join(jpkgSrc, "src", "Java", jpkg)
		if err := os.MkdirAll(pkgDir, 0700); err != nil {
			return nil, err
		}
		pkgFile := filepath.Join(pkgDir, "package.go")
		generate := func(w io.Writer) error {
			if buildN {
				return nil
			}
			buf.Reset()
			g.GenPackage(i)
			_, err := io.Copy(w, &buf)
			return err
		}
		if err := writeFile(pkgFile, generate); err != nil {
			return nil, fmt.Errorf("failed to create the Java wrapper package %s: %v", jpkg, err)
		}
	}
	generate := func(w io.Writer) error {
		if buildN {
			return nil
		}
		buf.Reset()
		g.GenGo()
		_, err := io.Copy(w, &buf)
		return err
	}
	if err := writeFile(filepath.Join(srcDir, "classes.go"), generate); err != nil {
		return nil, fmt.Errorf("failed to create the Java classes Go file: %v", err)
	}
	generate = func(w io.Writer) error {
		if buildN {
			return nil
		}
		buf.Reset()
		g.GenH()
		_, err := io.Copy(w, &buf)
		return err
	}
	if err := writeFile(filepath.Join(srcDir, "classes.h"), generate); err != nil {
		return nil, fmt.Errorf("failed to create the Java classes header file: %v", err)
	}
	generate = func(w io.Writer) error {
		if buildN {
			return nil
		}
		buf.Reset()
		g.GenC()
		_, err := io.Copy(w, &buf)
		return err
	}
	if err := writeFile(filepath.Join(srcDir, "classes.c"), generate); err != nil {
		return nil, fmt.Errorf("failed to create the Java classes C file: %v", err)
	}
	generate = func(w io.Writer) error {
		if buildN {
			return nil
		}
		buf.Reset()
		g.GenInterfaces()
		_, err := io.Copy(w, &buf)
		return err
	}
	if err := writeFile(filepath.Join(jpkgSrc, "src", "Java", "interfaces.go"), generate); err != nil {
		return nil, fmt.Errorf("failed to create the Java classes interfaces file: %v", err)
	}
	return classes, nil
}
Exemplo n.º 2
0
func main() {
	flag.Parse()

	if *lang != "java" && *javaPkg != "" {
		log.Fatalf("Invalid option -javapkg for gobind -lang=%s", *lang)
	} else if *lang != "objc" && *prefix != "" {
		log.Fatalf("Invalid option -prefix for gobind -lang=%s", *lang)
	}

	oldCtx := build.Default
	ctx := &build.Default
	var allPkg []*build.Package
	for _, path := range flag.Args() {
		pkg, err := ctx.Import(path, ".", build.ImportComment)
		if err != nil {
			log.Fatalf("package %q: %v", path, err)
		}
		allPkg = append(allPkg, pkg)
	}
	var classes []*java.Class
	refs, err := importers.AnalyzePackages(allPkg, "Java/")
	if err != nil {
		log.Fatal(err)
	}
	if len(refs.Refs) > 0 {
		classes, err = java.Import(*bootclasspath, *classpath, refs)
		if err != nil {
			log.Fatal(err)
		}
		if len(classes) > 0 {
			tmpGopath, err := ioutil.TempDir(os.TempDir(), "gobind-")
			if err != nil {
				log.Fatal(err)
			}
			defer os.RemoveAll(tmpGopath)
			if err := genJavaPackages(ctx, tmpGopath, classes); err != nil {
				log.Fatal(err)
			}
			gopath := ctx.GOPATH
			if gopath != "" {
				gopath = string(filepath.ListSeparator)
			}
			ctx.GOPATH = gopath + tmpGopath
		}
	}

	typePkgs := make([]*types.Package, len(allPkg))
	fset := token.NewFileSet()
	conf := &types.Config{
		Importer: importer.Default(),
	}
	conf.Error = func(err error) {
		// Ignore errors. They're probably caused by as-yet undefined
		// Java wrappers.
	}
	for i, pkg := range allPkg {
		var files []*ast.File
		for _, name := range pkg.GoFiles {
			f, err := parser.ParseFile(fset, filepath.Join(pkg.Dir, name), nil, 0)
			if err != nil {
				log.Fatalf("Failed to parse Go file %s: %v", name, err)
			}
			files = append(files, f)
		}
		tpkg, _ := conf.Check(pkg.Name, fset, files, nil)
		typePkgs[i] = tpkg
	}
	build.Default = oldCtx
	for _, pkg := range typePkgs {
		genPkg(pkg, typePkgs, classes)
	}
	// Generate the error package and support files
	genPkg(nil, typePkgs, classes)
	os.Exit(exitStatus)
}
Exemplo n.º 3
0
func GenObjcWrappers(pkgs []*build.Package, srcDir, pkgGen string) ([]*objc.Named, error) {
	refs, err := importers.AnalyzePackages(pkgs, "ObjC/")
	if err != nil {
		return nil, err
	}
	types, err := objc.Import(refs)
	if err != nil {
		return nil, err
	}
	var buf bytes.Buffer
	g := &bind.ObjcWrapper{
		Printer: &bind.Printer{
			IndentEach: []byte("\t"),
			Buf:        &buf,
		},
	}
	g.Init(types)
	for i, name := range g.Packages() {
		pkgDir := filepath.Join(pkgGen, "src", "ObjC", name)
		if err := os.MkdirAll(pkgDir, 0700); err != nil {
			return nil, err
		}
		pkgFile := filepath.Join(pkgDir, "package.go")
		generate := func(w io.Writer) error {
			if buildN {
				return nil
			}
			buf.Reset()
			g.GenPackage(i)
			_, err := io.Copy(w, &buf)
			return err
		}
		if err := writeFile(pkgFile, generate); err != nil {
			return nil, fmt.Errorf("failed to create the ObjC wrapper package %s: %v", name, err)
		}
	}
	generate := func(w io.Writer) error {
		if buildN {
			return nil
		}
		buf.Reset()
		g.GenGo()
		_, err := io.Copy(w, &buf)
		return err
	}
	if err := writeFile(filepath.Join(srcDir, "interfaces.go"), generate); err != nil {
		return nil, fmt.Errorf("failed to create the ObjC wrapper Go file: %v", err)
	}
	generate = func(w io.Writer) error {
		if buildN {
			return nil
		}
		buf.Reset()
		g.GenInterfaces()
		_, err := io.Copy(w, &buf)
		return err
	}
	if err := writeFile(filepath.Join(pkgGen, "src", "ObjC", "interfaces.go"), generate); err != nil {
		return nil, fmt.Errorf("failed to create the ObjC wrapper Go file: %v", err)
	}
	generate = func(w io.Writer) error {
		if buildN {
			return nil
		}
		buf.Reset()
		g.GenH()
		_, err := io.Copy(w, &buf)
		return err
	}
	if err := writeFile(filepath.Join(srcDir, "interfaces.h"), generate); err != nil {
		return nil, fmt.Errorf("failed to create the ObjC wrapper header file: %v", err)
	}
	generate = func(w io.Writer) error {
		if buildN {
			return nil
		}
		buf.Reset()
		g.GenM()
		_, err := io.Copy(w, &buf)
		return err
	}
	if err := writeFile(filepath.Join(srcDir, "interfaces.m"), generate); err != nil {
		return nil, fmt.Errorf("failed to create the Java classes ObjC file: %v", err)
	}
	return types, nil
}