Gobind uses strings for passing errors across the language barrier. However, since Gobind doesn't have a concept of a nil string, it can't separate an empty native string from a nil string. In turn, that means that empty errors, exceptions or NSError * with an empty description are treated as no error. With ObjC, empty errors are replaced with a default string to workaround the issue, while with Java empty errors are silently ignored. Fix this by replacing strings with actual error objects, wrapping the Go error, Java Throwable or ObjC NSError *, and letting the existing bind machinery take care of passing the references across. It's a large change for a small corner case, but I believe objects are a better fit for exception that strings. Error objects also naturally leads to future additions, for example accessing the exception class name or chained exception. Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265 Reviewed-on: https://go-review.googlesource.com/24100 Reviewed-by: David Crawshaw <crawshaw@golang.org>
216 lines
5.2 KiB
Go
216 lines
5.2 KiB
Go
// Copyright 2015 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"go/build"
|
|
"io"
|
|
"io/ioutil"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"text/template"
|
|
)
|
|
|
|
func goIOSBind(pkgs []*build.Package) error {
|
|
typesPkgs, err := loadExportData(pkgs, darwinArmEnv)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
binder, err := newBinder(typesPkgs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
name := binder.pkgs[0].Name()
|
|
title := strings.Title(name)
|
|
|
|
if buildO != "" && !strings.HasSuffix(buildO, ".framework") {
|
|
return fmt.Errorf("static framework name %q missing .framework suffix", buildO)
|
|
}
|
|
if buildO == "" {
|
|
buildO = title + ".framework"
|
|
}
|
|
|
|
srcDir := filepath.Join(tmpdir, "src", "gomobile_bind")
|
|
for _, pkg := range binder.pkgs {
|
|
if err := binder.GenGo(pkg, binder.pkgs, srcDir); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// Generate the error type.
|
|
if err := binder.GenGo(nil, binder.pkgs, srcDir); err != nil {
|
|
return err
|
|
}
|
|
mainFile := filepath.Join(tmpdir, "src/iosbin/main.go")
|
|
err = writeFile(mainFile, func(w io.Writer) error {
|
|
_, err := w.Write(iosBindFile)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create the binding package for iOS: %v", err)
|
|
}
|
|
|
|
fileBases := make([]string, len(typesPkgs)+1)
|
|
for i, pkg := range binder.pkgs {
|
|
if fileBases[i], err = binder.GenObjc(pkg, binder.pkgs, srcDir); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if fileBases[len(fileBases)-1], err = binder.GenObjc(nil, binder.pkgs, srcDir); err != nil {
|
|
return err
|
|
}
|
|
if err := binder.GenObjcSupport(srcDir); err != nil {
|
|
return err
|
|
}
|
|
if err := binder.GenGoSupport(srcDir); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd := exec.Command("xcrun", "lipo", "-create")
|
|
|
|
for _, env := range [][]string{darwinArmEnv, darwinArm64Env, darwinAmd64Env} {
|
|
arch := archClang(getenv(env, "GOARCH"))
|
|
path, err := goIOSBindArchive(name, mainFile, env, fileBases)
|
|
if err != nil {
|
|
return fmt.Errorf("darwin-%s: %v", arch, err)
|
|
}
|
|
cmd.Args = append(cmd.Args, "-arch", arch, path)
|
|
}
|
|
|
|
// Build static framework output directory.
|
|
if err := removeAll(buildO); err != nil {
|
|
return err
|
|
}
|
|
headers := buildO + "/Versions/A/Headers"
|
|
if err := mkdir(headers); err != nil {
|
|
return err
|
|
}
|
|
if err := symlink("A", buildO+"/Versions/Current"); err != nil {
|
|
return err
|
|
}
|
|
if err := symlink("Versions/Current/Headers", buildO+"/Headers"); err != nil {
|
|
return err
|
|
}
|
|
if err := symlink("Versions/Current/"+title, buildO+"/"+title); err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd.Args = append(cmd.Args, "-o", buildO+"/Versions/A/"+title)
|
|
if err := runCmd(cmd); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Copy header file next to output archive.
|
|
headerFiles := make([]string, len(fileBases))
|
|
if len(fileBases) == 1 {
|
|
headerFiles[0] = title + ".h"
|
|
err = copyFile(
|
|
headers+"/"+title+".h",
|
|
srcDir+"/"+bindPrefix+title+".h",
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
for i, fileBase := range fileBases {
|
|
headerFiles[i] = fileBase + ".h"
|
|
err = copyFile(
|
|
headers+"/"+fileBase+".h",
|
|
srcDir+"/"+fileBase+".h")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
headerFiles = append(headerFiles, title+".h")
|
|
err = writeFile(headers+"/"+title+".h", func(w io.Writer) error {
|
|
return iosBindHeaderTmpl.Execute(w, map[string]interface{}{
|
|
"pkgs": pkgs, "title": title, "bases": fileBases,
|
|
})
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
resources := buildO + "/Versions/A/Resources"
|
|
if err := mkdir(resources); err != nil {
|
|
return err
|
|
}
|
|
if err := symlink("Versions/Current/Resources", buildO+"/Resources"); err != nil {
|
|
return err
|
|
}
|
|
if err := ioutil.WriteFile(buildO+"/Resources/Info.plist", []byte(iosBindInfoPlist), 0666); err != nil {
|
|
return err
|
|
}
|
|
|
|
var mmVals = struct {
|
|
Module string
|
|
Headers []string
|
|
}{
|
|
Module: title,
|
|
Headers: headerFiles,
|
|
}
|
|
err = writeFile(buildO+"/Versions/A/Modules/module.modulemap", func(w io.Writer) error {
|
|
return iosModuleMapTmpl.Execute(w, mmVals)
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return symlink("Versions/Current/Modules", buildO+"/Modules")
|
|
}
|
|
|
|
const iosBindInfoPlist = `<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
<plist version="1.0">
|
|
<dict>
|
|
</dict>
|
|
</plist>
|
|
`
|
|
|
|
var iosModuleMapTmpl = template.Must(template.New("iosmmap").Parse(`framework module "{{.Module}}" {
|
|
{{range .Headers}} header "{{.}}"
|
|
{{end}}
|
|
export *
|
|
}`))
|
|
|
|
func goIOSBindArchive(name, path string, env, fileBases []string) (string, error) {
|
|
arch := getenv(env, "GOARCH")
|
|
archive := filepath.Join(tmpdir, name+"-"+arch+".a")
|
|
err := goBuild(path, env, "-buildmode=c-archive", "-tags=ios", "-o", archive)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return archive, nil
|
|
}
|
|
|
|
var iosBindFile = []byte(`
|
|
package main
|
|
|
|
import (
|
|
_ "../gomobile_bind"
|
|
)
|
|
|
|
import "C"
|
|
|
|
func main() {}
|
|
`)
|
|
|
|
var iosBindHeaderTmpl = template.Must(template.New("ios.h").Parse(`
|
|
// Objective-C API for talking to the following Go packages
|
|
//
|
|
{{range .pkgs}}// {{.ImportPath}}
|
|
{{end}}//
|
|
// File is generated by gomobile bind. Do not edit.
|
|
#ifndef __{{.title}}_H__
|
|
#define __{{.title}}_H__
|
|
|
|
{{range .bases}}#include "{{.}}.h"
|
|
{{end}}
|
|
#endif
|
|
`))
|