2019-08-17 12:00:37 +02:00
|
|
|
// Copyright 2019 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-06-10 18:03:35 +02:00
|
|
|
//go:build ebitenmobilegobind
|
2019-08-17 12:00:37 +02:00
|
|
|
|
|
|
|
// gobind is a wrapper of the original gobind. This command adds extra files like a view controller.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-11-04 09:20:21 +01:00
|
|
|
_ "embed"
|
2019-08-17 12:00:37 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2024-05-03 16:39:36 +02:00
|
|
|
"os/exec"
|
2019-08-17 12:00:37 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/packages"
|
|
|
|
)
|
|
|
|
|
2022-11-04 09:20:21 +01:00
|
|
|
//go:embed _files/EbitenViewController.m
|
|
|
|
var objcM string
|
|
|
|
|
|
|
|
//go:embed _files/EbitenView.java
|
|
|
|
var viewJava string
|
|
|
|
|
|
|
|
//go:embed _files/EbitenSurfaceView.java
|
|
|
|
var surfaceViewJava string
|
|
|
|
|
2019-08-17 12:00:37 +02:00
|
|
|
var (
|
|
|
|
lang = flag.String("lang", "", "")
|
|
|
|
outdir = flag.String("outdir", "", "")
|
|
|
|
javaPkg = flag.String("javapkg", "", "")
|
|
|
|
prefix = flag.String("prefix", "", "")
|
|
|
|
bootclasspath = flag.String("bootclasspath", "", "")
|
|
|
|
classpath = flag.String("classpath", "", "")
|
|
|
|
tags = flag.String("tags", "", "")
|
|
|
|
)
|
|
|
|
|
|
|
|
var usage = `The Gobind tool generates Java language bindings for Go.
|
|
|
|
|
|
|
|
For usage details, see doc.go.`
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
if err := run(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-23 11:53:46 +02:00
|
|
|
func invokeOriginalGobind(lang string) (pkgName string, err error) {
|
2019-08-17 12:00:37 +02:00
|
|
|
cmd := exec.Command("gobind-original", os.Args[1:]...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
2019-09-23 11:53:46 +02:00
|
|
|
return "", err
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 11:53:46 +02:00
|
|
|
cfgtags := strings.Join(strings.Split(*tags, ","), " ")
|
|
|
|
cfg := &packages.Config{}
|
|
|
|
switch lang {
|
|
|
|
case "java":
|
|
|
|
cfg.Env = append(os.Environ(), "GOOS=android")
|
|
|
|
case "objc":
|
|
|
|
cfg.Env = append(os.Environ(), "GOOS=darwin")
|
|
|
|
if cfgtags != "" {
|
|
|
|
cfgtags += " "
|
|
|
|
}
|
|
|
|
cfgtags += "ios"
|
|
|
|
}
|
|
|
|
cfg.BuildFlags = []string{"-tags", cfgtags}
|
|
|
|
pkgs, err := packages.Load(cfg, flag.Args()[0])
|
2019-08-17 12:00:37 +02:00
|
|
|
if err != nil {
|
2019-09-23 11:53:46 +02:00
|
|
|
return "", err
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
2019-09-23 11:53:46 +02:00
|
|
|
return pkgs[0].Name, nil
|
|
|
|
}
|
2019-08-17 12:00:37 +02:00
|
|
|
|
2019-09-23 11:53:46 +02:00
|
|
|
func run() error {
|
2019-08-17 12:00:37 +02:00
|
|
|
writeFile := func(filename string, content string) error {
|
2022-09-14 19:45:36 +02:00
|
|
|
if err := os.WriteFile(filepath.Join(*outdir, filename), []byte(content), 0644); err != nil {
|
2019-08-17 12:00:37 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add additional files.
|
|
|
|
langs := strings.Split(*lang, ",")
|
|
|
|
for _, lang := range langs {
|
2019-09-23 11:53:46 +02:00
|
|
|
pkgName, err := invokeOriginalGobind(lang)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
prefixLower := *prefix + pkgName
|
|
|
|
prefixUpper := strings.Title(*prefix) + strings.Title(pkgName)
|
|
|
|
replacePrefixes := func(content string) string {
|
|
|
|
content = strings.ReplaceAll(content, "{{.PrefixUpper}}", prefixUpper)
|
|
|
|
content = strings.ReplaceAll(content, "{{.PrefixLower}}", prefixLower)
|
|
|
|
content = strings.ReplaceAll(content, "{{.JavaPkg}}", *javaPkg)
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2019-08-17 12:00:37 +02:00
|
|
|
switch lang {
|
|
|
|
case "objc":
|
|
|
|
// iOS
|
2019-08-18 08:53:25 +02:00
|
|
|
if err := writeFile(filepath.Join("src", "gobind", prefixLower+"ebitenviewcontroller_ios.m"), replacePrefixes(objcM)); err != nil {
|
2019-08-17 12:00:37 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-01-04 19:06:22 +01:00
|
|
|
if err := writeFile(filepath.Join("src", "gobind", prefixLower+"ebitenviewcontroller_ios.go"), `package main
|
|
|
|
|
|
|
|
// #cgo CFLAGS: -DGLES_SILENCE_DEPRECATION
|
|
|
|
import "C"`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-17 12:00:37 +02:00
|
|
|
case "java":
|
|
|
|
// Android
|
2019-08-18 08:53:25 +02:00
|
|
|
dir := filepath.Join(strings.Split(*javaPkg, ".")...)
|
|
|
|
dir = filepath.Join(dir, prefixLower)
|
|
|
|
if err := writeFile(filepath.Join("java", dir, "EbitenView.java"), replacePrefixes(viewJava)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := writeFile(filepath.Join("java", dir, "EbitenSurfaceView.java"), replacePrefixes(surfaceViewJava)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-17 12:00:37 +02:00
|
|
|
case "go":
|
|
|
|
// Do nothing.
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unsupported language: %s", lang))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|