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.
|
|
|
|
|
2022-08-29 04:16:39 +02:00
|
|
|
// ebitenmobile is a wrapper of gomobile for Ebitengine.
|
2020-07-24 20:00:52 +02:00
|
|
|
//
|
2022-09-23 12:08:35 +02:00
|
|
|
// For the usage, see https://ebitengine.org/en/documents/mobile.html.
|
2020-07-24 20:00:52 +02:00
|
|
|
//
|
2024-02-18 10:04:16 +01:00
|
|
|
// ebitenmobile uses github.com/ebitengine/gomobile for gomobile, not golang.org/x/mobile.
|
2020-07-24 20:00:52 +02:00
|
|
|
// gomobile's version is fixed by ebitenmobile.
|
|
|
|
// You can specify gomobile's version by EBITENMOBILE_GOMOBILE environment variable.
|
2019-08-17 12:00:37 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-11-04 09:20:21 +01:00
|
|
|
_ "embed"
|
2019-08-17 12:00:37 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
2024-01-28 07:33:20 +01:00
|
|
|
"unicode"
|
2019-08-17 12:00:37 +02:00
|
|
|
|
2022-11-01 15:07:16 +01:00
|
|
|
exec "golang.org/x/sys/execabs"
|
2019-08-17 12:00:37 +02:00
|
|
|
"golang.org/x/tools/go/packages"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ebitenmobileCommand = "ebitenmobile"
|
|
|
|
)
|
|
|
|
|
2022-11-04 09:20:21 +01:00
|
|
|
//go:embed _files/EbitenViewController.h
|
|
|
|
var objcH string
|
|
|
|
|
2019-08-17 12:00:37 +02:00
|
|
|
func goEnv(name string) string {
|
|
|
|
if val := os.Getenv(name); val != "" {
|
|
|
|
return val
|
|
|
|
}
|
2020-01-24 14:50:48 +01:00
|
|
|
val, err := exec.Command("go", "env", name).Output()
|
2019-08-17 12:00:37 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(val))
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Copied from gomobile.
|
|
|
|
minAndroidAPI = 15
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
buildA bool // -a
|
|
|
|
buildI bool // -i
|
|
|
|
buildN bool // -n
|
|
|
|
buildV bool // -v
|
|
|
|
buildX bool // -x
|
|
|
|
buildO string // -o
|
|
|
|
buildGcflags string // -gcflags
|
|
|
|
buildLdflags string // -ldflags
|
|
|
|
buildTarget string // -target
|
2020-02-16 09:31:57 +01:00
|
|
|
buildTrimpath bool // -trimpath
|
2019-08-17 12:00:37 +02:00
|
|
|
buildWork bool // -work
|
|
|
|
buildBundleID string // -bundleid
|
|
|
|
buildIOSVersion string // -iosversion
|
|
|
|
buildAndroidAPI int // -androidapi
|
|
|
|
buildTags string // -tags
|
|
|
|
|
|
|
|
bindPrefix string // -prefix
|
|
|
|
bindJavaPkg string // -javapkg
|
|
|
|
bindClasspath string // -classpath
|
|
|
|
bindBootClasspath string // -bootclasspath
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-01-28 07:33:20 +01:00
|
|
|
flag.Usage = func() {
|
|
|
|
// This message is copied from `gomobile bind -h`
|
|
|
|
fmt.Fprintf(os.Stderr, "%s bind [-target android|ios] [-bootclasspath <path>] [-classpath <path>] [-o output] [build flags] [package]\n", ebitenmobileCommand)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
flag.Parse()
|
|
|
|
|
2019-08-17 12:00:37 +02:00
|
|
|
args := flag.Args()
|
|
|
|
if len(args) < 1 {
|
|
|
|
flag.Usage()
|
|
|
|
}
|
|
|
|
|
2022-05-02 16:03:26 +02:00
|
|
|
if args[0] != "bind" {
|
|
|
|
flag.Usage()
|
|
|
|
}
|
|
|
|
|
2019-08-17 12:00:37 +02:00
|
|
|
var flagset flag.FlagSet
|
|
|
|
flagset.StringVar(&buildO, "o", "", "")
|
|
|
|
flagset.StringVar(&buildGcflags, "gcflags", "", "")
|
|
|
|
flagset.StringVar(&buildLdflags, "ldflags", "", "")
|
|
|
|
flagset.StringVar(&buildTarget, "target", "android", "")
|
|
|
|
flagset.StringVar(&buildBundleID, "bundleid", "", "")
|
|
|
|
flagset.StringVar(&buildIOSVersion, "iosversion", "7.0", "")
|
|
|
|
flagset.StringVar(&buildTags, "tags", "", "")
|
|
|
|
flagset.IntVar(&buildAndroidAPI, "androidapi", minAndroidAPI, "")
|
|
|
|
flagset.BoolVar(&buildA, "a", false, "")
|
|
|
|
flagset.BoolVar(&buildI, "i", false, "")
|
|
|
|
flagset.BoolVar(&buildN, "n", false, "")
|
|
|
|
flagset.BoolVar(&buildV, "v", false, "")
|
|
|
|
flagset.BoolVar(&buildX, "x", false, "")
|
2020-02-16 09:31:57 +01:00
|
|
|
flagset.BoolVar(&buildTrimpath, "trimpath", false, "")
|
2019-08-17 12:00:37 +02:00
|
|
|
flagset.BoolVar(&buildWork, "work", false, "")
|
|
|
|
flagset.StringVar(&bindJavaPkg, "javapkg", "", "")
|
|
|
|
flagset.StringVar(&bindPrefix, "prefix", "", "")
|
|
|
|
flagset.StringVar(&bindClasspath, "classpath", "", "")
|
|
|
|
flagset.StringVar(&bindBootClasspath, "bootclasspath", "", "")
|
|
|
|
|
2022-09-09 18:52:46 +02:00
|
|
|
_ = flagset.Parse(args[1:])
|
2019-08-17 12:00:37 +02:00
|
|
|
|
2021-06-09 18:03:25 +02:00
|
|
|
buildTarget, err := osFromBuildTarget(buildTarget)
|
2020-04-23 05:37:08 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-09-22 13:58:01 +02:00
|
|
|
// Add ldflags to suppress linker errors (#932).
|
|
|
|
// See https://github.com/golang/go/issues/17807
|
2021-06-09 18:03:25 +02:00
|
|
|
if buildTarget == "android" {
|
2020-04-22 17:18:53 +02:00
|
|
|
if buildLdflags != "" {
|
|
|
|
buildLdflags += " "
|
|
|
|
}
|
|
|
|
buildLdflags += "-extldflags=-Wl,-soname,libgojni.so"
|
2024-01-28 07:33:20 +01:00
|
|
|
|
|
|
|
if !isValidJavaPackageName(bindJavaPkg) {
|
|
|
|
log.Fatalf("invalid Java package name: %s", bindJavaPkg)
|
|
|
|
}
|
2019-09-22 13:58:01 +02:00
|
|
|
}
|
|
|
|
|
2021-06-09 18:03:25 +02:00
|
|
|
dir, err := prepareGomobileCommands()
|
|
|
|
defer func() {
|
|
|
|
if dir != "" && !buildWork {
|
2022-09-09 18:52:46 +02:00
|
|
|
_ = removeAll(dir)
|
2021-06-09 18:03:25 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err != nil {
|
2019-08-17 12:00:37 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-05-02 16:03:26 +02:00
|
|
|
if err := doBind(args, &flagset, buildTarget); err != nil {
|
|
|
|
log.Fatal(err)
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:37:08 +02:00
|
|
|
func osFromBuildTarget(buildTarget string) (string, error) {
|
|
|
|
var os string
|
|
|
|
for i, pair := range strings.Split(buildTarget, ",") {
|
|
|
|
osarch := strings.SplitN(pair, "/", 2)
|
|
|
|
if i == 0 {
|
|
|
|
os = osarch[0]
|
|
|
|
}
|
|
|
|
if os != osarch[0] {
|
|
|
|
return "", fmt.Errorf("ebitenmobile: cannot target different OSes")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if os == "ios" {
|
|
|
|
os = "darwin"
|
|
|
|
}
|
|
|
|
return os, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func doBind(args []string, flagset *flag.FlagSet, buildOS string) error {
|
2019-09-23 11:12:20 +02:00
|
|
|
tags := buildTags
|
|
|
|
cfg := &packages.Config{}
|
2020-04-23 05:37:08 +02:00
|
|
|
cfg.Env = append(os.Environ(), "GOOS="+buildOS)
|
|
|
|
if buildOS == "darwin" {
|
2019-09-23 11:12:20 +02:00
|
|
|
if tags != "" {
|
|
|
|
tags += " "
|
|
|
|
}
|
|
|
|
tags += "ios"
|
|
|
|
}
|
|
|
|
cfg.BuildFlags = []string{"-tags", tags}
|
|
|
|
|
2020-07-21 17:51:47 +02:00
|
|
|
flagsetArgs := flagset.Args()
|
|
|
|
if len(flagsetArgs) == 0 {
|
|
|
|
flagsetArgs = []string{"."}
|
|
|
|
}
|
|
|
|
pkgs, err := packages.Load(cfg, flagsetArgs[0])
|
2019-08-17 12:00:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
prefixLower := bindPrefix + pkgs[0].Name
|
|
|
|
prefixUpper := strings.Title(bindPrefix) + strings.Title(pkgs[0].Name)
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
args = append(args, "github.com/hajimehoshi/ebiten/v2/mobile/ebitenmobileview")
|
2019-08-17 12:00:37 +02:00
|
|
|
|
|
|
|
if buildO == "" {
|
|
|
|
fmt.Fprintln(os.Stderr, "-o must be specified.")
|
|
|
|
os.Exit(2)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buildN {
|
|
|
|
fmt.Print("gomobile")
|
|
|
|
for _, arg := range args {
|
|
|
|
fmt.Print(" ", arg)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("gomobile", args...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
os.Exit(err.(*exec.ExitError).ExitCode())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
replacePrefixes := func(content string) string {
|
|
|
|
content = strings.ReplaceAll(content, "{{.PrefixUpper}}", prefixUpper)
|
|
|
|
content = strings.ReplaceAll(content, "{{.PrefixLower}}", prefixLower)
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2020-04-23 05:37:08 +02:00
|
|
|
if buildOS == "darwin" {
|
2022-08-29 04:16:39 +02:00
|
|
|
// TODO: Use os.ReadDir after Ebitengine stops supporting Go 1.15.
|
2022-01-04 18:27:17 +01:00
|
|
|
f, err := os.Open(buildO)
|
|
|
|
if err != nil {
|
2019-08-17 12:00:37 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-09-09 18:52:46 +02:00
|
|
|
defer func() {
|
|
|
|
_ = f.Close()
|
|
|
|
}()
|
2019-08-17 12:00:37 +02:00
|
|
|
|
2022-01-04 18:27:17 +01:00
|
|
|
names, err := f.Readdirnames(-1)
|
2019-08-17 12:00:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-04 18:27:17 +01:00
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
if name == "Info.plist" {
|
|
|
|
continue
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
2022-01-04 18:27:17 +01:00
|
|
|
frameworkName := filepath.Base(buildO)
|
2022-07-09 11:30:38 +02:00
|
|
|
frameworkNameBase := frameworkName[:len(frameworkName)-len(".xcframework")]
|
|
|
|
// The first character must be an upper case (#2192).
|
|
|
|
// TODO: strings.Title is used here for the consistency with gomobile (see cmd/gomobile/bind_iosapp.go).
|
|
|
|
// As strings.Title is deprecated, golang.org/x/text/cases should be used.
|
|
|
|
frameworkNameBase = strings.Title(frameworkNameBase)
|
|
|
|
dir := filepath.Join(buildO, name, frameworkNameBase+".framework", "Versions", "A")
|
2019-08-17 12:00:37 +02:00
|
|
|
|
2022-09-14 19:45:36 +02:00
|
|
|
if err := os.WriteFile(filepath.Join(dir, "Headers", prefixUpper+"EbitenViewController.h"), []byte(replacePrefixes(objcH)), 0644); err != nil {
|
2022-01-04 18:27:17 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// TODO: Remove 'Ebitenmobileview.objc.h' here. Now it is hard since there is a header file importing
|
|
|
|
// that header file.
|
2019-08-17 12:00:37 +02:00
|
|
|
|
2022-09-14 19:45:36 +02:00
|
|
|
fs, err := os.ReadDir(filepath.Join(dir, "Headers"))
|
2022-01-04 18:27:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var headerFiles []string
|
|
|
|
for _, f := range fs {
|
|
|
|
if strings.HasSuffix(f.Name(), ".h") {
|
|
|
|
headerFiles = append(headerFiles, f.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := os.OpenFile(filepath.Join(dir, "Modules", "module.modulemap"), os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-09 18:52:46 +02:00
|
|
|
defer func() {
|
|
|
|
_ = w.Close()
|
|
|
|
}()
|
2022-01-04 18:27:17 +01:00
|
|
|
var mmVals = struct {
|
|
|
|
Module string
|
|
|
|
Headers []string
|
|
|
|
}{
|
|
|
|
Module: prefixUpper,
|
|
|
|
Headers: headerFiles,
|
|
|
|
}
|
|
|
|
if err := iosModuleMapTmpl.Execute(w, mmVals); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Remove Ebitenmobileview.objc.h?
|
|
|
|
}
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var iosModuleMapTmpl = template.Must(template.New("iosmmap").Parse(`framework module "{{.Module}}" {
|
|
|
|
{{range .Headers}} header "{{.}}"
|
|
|
|
{{end}}
|
|
|
|
export *
|
|
|
|
}`))
|
2024-01-28 07:33:20 +01:00
|
|
|
|
|
|
|
func isValidJavaPackageName(name string) bool {
|
|
|
|
if name == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// A Java package name consists of one or more Java identifiers separated by dots.
|
|
|
|
for _, token := range strings.Split(name, ".") {
|
|
|
|
if !isValidJavaIdentifier(token) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// isValidJavaIdentifier reports whether the given strings is a valid Java identifier.
|
|
|
|
func isValidJavaIdentifier(name string) bool {
|
|
|
|
if name == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-01-28 08:06:02 +01:00
|
|
|
// Java identifiers must not be a Java keyword or a boolean/null literal.
|
|
|
|
// https://docs.oracle.com/javase/specs/jls/se21/html/jls-3.html#jls-Identifier
|
2024-01-28 07:33:20 +01:00
|
|
|
switch name {
|
|
|
|
case "_", "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while":
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if name == "null" || name == "true" || name == "false" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// References:
|
|
|
|
// * https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Character.html#isJavaIdentifierPart(int)
|
|
|
|
// * https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Character.html#isJavaIdentifierStart(int)
|
|
|
|
|
|
|
|
isJavaLetter := func(r rune) bool {
|
|
|
|
return unicode.IsLetter(r) || unicode.Is(unicode.Pc, r) || unicode.Is(unicode.Sc, r)
|
|
|
|
}
|
|
|
|
isJavaDigit := unicode.IsDigit
|
|
|
|
|
|
|
|
// A Java identifier is a Java letter or Java letter followed by Java letters or Java digits.
|
2024-01-28 08:06:02 +01:00
|
|
|
// https://docs.oracle.com/javase/specs/jls/se21/html/jls-3.html#jls-Identifier
|
2024-01-28 07:33:20 +01:00
|
|
|
for i, r := range name {
|
|
|
|
if !isJavaLetter(r) && (i == 0 || !isJavaDigit(r)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|