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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-09-14 16:41:22 +02:00
|
|
|
_ "embed"
|
2019-08-17 12:00:37 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-05-03 16:39:36 +02:00
|
|
|
"os/exec"
|
2019-08-17 12:00:37 +02:00
|
|
|
"path/filepath"
|
2020-03-04 17:05:53 +01:00
|
|
|
"runtime"
|
2022-09-28 18:58:24 +02:00
|
|
|
"runtime/debug"
|
|
|
|
|
|
|
|
// Add a dependency on gomobile in order to get the version via debug.ReadBuildInfo().
|
2024-02-18 10:04:16 +01:00
|
|
|
_ "github.com/ebitengine/gomobile/geom"
|
2019-08-17 12:00:37 +02:00
|
|
|
)
|
|
|
|
|
2022-09-14 16:41:22 +02:00
|
|
|
//go:embed gobind.go
|
|
|
|
var gobind_go []byte
|
|
|
|
|
2022-11-04 09:20:21 +01:00
|
|
|
//go:embed _files/EbitenViewController.m
|
|
|
|
var objcM []byte
|
|
|
|
|
|
|
|
//go:embed _files/EbitenView.java
|
|
|
|
var viewJava []byte
|
|
|
|
|
|
|
|
//go:embed _files/EbitenSurfaceView.java
|
|
|
|
var surfaceViewJava []byte
|
|
|
|
|
2019-08-18 10:11:53 +02:00
|
|
|
func runCommand(command string, args []string, env []string) error {
|
2019-08-17 12:00:37 +02:00
|
|
|
if buildX || buildN {
|
|
|
|
for _, e := range env {
|
|
|
|
fmt.Printf("%s ", e)
|
|
|
|
}
|
2019-08-18 10:11:53 +02:00
|
|
|
fmt.Print(command)
|
2019-08-17 12:00:37 +02:00
|
|
|
for _, arg := range args {
|
|
|
|
fmt.Printf(" %s", arg)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-18 10:11:53 +02:00
|
|
|
cmd := exec.Command(command, args...)
|
|
|
|
if len(env) > 0 {
|
|
|
|
cmd.Env = append(os.Environ(), env...)
|
|
|
|
}
|
2019-08-17 12:00:37 +02:00
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
2019-08-18 10:11:53 +02:00
|
|
|
return fmt.Errorf("%s %v failed: %v\n%v", command, args, string(out), err)
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-09 18:03:25 +02:00
|
|
|
func removeAll(path string) error {
|
|
|
|
if buildX || buildN {
|
|
|
|
fmt.Printf("rm -rf %s\n", path)
|
|
|
|
}
|
|
|
|
if buildN {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return os.RemoveAll(path)
|
|
|
|
}
|
|
|
|
|
2019-08-18 10:11:53 +02:00
|
|
|
func runGo(args ...string) error {
|
2022-09-13 19:41:42 +02:00
|
|
|
return runCommand("go", args, nil)
|
2019-08-18 10:11:53 +02:00
|
|
|
}
|
|
|
|
|
2020-03-04 17:05:53 +01:00
|
|
|
// exe adds the .exe extension to the given filename.
|
|
|
|
// Without .exe, the executable won't be found by exec.LookPath on Windows (#1096).
|
|
|
|
func exe(filename string) string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return filename + ".exe"
|
|
|
|
}
|
|
|
|
return filename
|
|
|
|
}
|
|
|
|
|
2021-06-09 18:03:25 +02:00
|
|
|
func prepareGomobileCommands() (string, error) {
|
2022-09-14 19:45:36 +02:00
|
|
|
tmp, err := os.MkdirTemp("", "ebitenmobile-")
|
2019-08-17 12:00:37 +02:00
|
|
|
if err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return "", err
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
newpath := filepath.Join(tmp, "bin")
|
|
|
|
if path := os.Getenv("PATH"); path != "" {
|
|
|
|
newpath += string(filepath.ListSeparator) + path
|
|
|
|
}
|
2019-08-18 10:11:53 +02:00
|
|
|
if buildX || buildN {
|
|
|
|
fmt.Printf("PATH=%s\n", newpath)
|
|
|
|
}
|
|
|
|
if !buildN {
|
2021-06-09 14:33:20 +02:00
|
|
|
if err := os.Setenv("PATH", newpath); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2021-06-09 14:33:20 +02:00
|
|
|
}
|
2019-08-18 10:11:53 +02:00
|
|
|
}
|
2019-08-17 12:00:37 +02:00
|
|
|
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// cd
|
|
|
|
if buildX {
|
|
|
|
fmt.Printf("cd %s\n", tmp)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(tmp); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
|
|
|
defer func() {
|
2022-09-09 18:52:46 +02:00
|
|
|
_ = os.Chdir(pwd)
|
2019-08-17 12:00:37 +02:00
|
|
|
}()
|
|
|
|
|
2021-06-10 18:03:35 +02:00
|
|
|
const (
|
|
|
|
modname = "ebitenmobiletemporary"
|
2022-11-03 04:55:14 +01:00
|
|
|
buildtags = "//go:build tools"
|
2021-06-10 18:03:35 +02:00
|
|
|
)
|
2021-06-09 15:45:16 +02:00
|
|
|
if err := runGo("mod", "init", modname); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2021-06-09 15:45:16 +02:00
|
|
|
}
|
2022-09-14 19:45:36 +02:00
|
|
|
if err := os.WriteFile("tools.go", []byte(fmt.Sprintf(`%s
|
2021-06-09 15:45:16 +02:00
|
|
|
|
|
|
|
package %s
|
|
|
|
|
|
|
|
import (
|
2024-02-18 10:04:16 +01:00
|
|
|
_ "github.com/ebitengine/gomobile/cmd/gobind"
|
|
|
|
_ "github.com/ebitengine/gomobile/cmd/gomobile"
|
2021-06-09 15:45:16 +02:00
|
|
|
)
|
2021-06-10 18:03:35 +02:00
|
|
|
`, buildtags, modname)), 0644); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
2021-02-09 18:03:06 +01:00
|
|
|
|
2022-09-28 18:58:24 +02:00
|
|
|
h, err := gomobileHash()
|
|
|
|
if err != nil {
|
|
|
|
return tmp, err
|
|
|
|
}
|
|
|
|
|
2024-02-18 10:04:16 +01:00
|
|
|
// To record gomobile to go.sum for Go 1.16 and later, go-get gomobile instead of github.com/ebitengine/gomobile (#1487).
|
2021-02-14 10:24:54 +01:00
|
|
|
// This also records gobind as gomobile depends on gobind indirectly.
|
|
|
|
// Using `...` doesn't work on Windows since mobile/internal/mobileinit cannot be compiled on Windows w/o Cgo (#1493).
|
2024-02-18 10:04:16 +01:00
|
|
|
if err := runGo("get", "github.com/ebitengine/gomobile/cmd/gomobile@"+h); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
2020-07-24 20:00:52 +02:00
|
|
|
if localgm := os.Getenv("EBITENMOBILE_GOMOBILE"); localgm != "" {
|
|
|
|
if !filepath.IsAbs(localgm) {
|
|
|
|
localgm = filepath.Join(pwd, localgm)
|
|
|
|
}
|
2024-02-18 10:04:16 +01:00
|
|
|
if err := runGo("mod", "edit", "-replace=github.com/ebitengine/gomobile="+localgm); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2020-07-24 20:00:52 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-13 18:00:17 +02:00
|
|
|
// runtime.Version() is the current executing Go version. For example, this is the version of the toolchain directive in go.mod.
|
|
|
|
// This might differ from the Go command version under the temporary directory.
|
|
|
|
// To avoid the version mismatch, set the toolchain explicitly (#3086).
|
|
|
|
if err := runGo("mod", "edit", "-toolchain="+runtime.Version()); err != nil {
|
|
|
|
return tmp, err
|
|
|
|
}
|
2021-06-09 15:45:16 +02:00
|
|
|
if err := runGo("mod", "tidy"); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2021-06-09 15:45:16 +02:00
|
|
|
}
|
2024-02-18 10:04:16 +01:00
|
|
|
if err := runGo("build", "-o", exe(filepath.Join("bin", "gomobile")), "github.com/ebitengine/gomobile/cmd/gomobile"); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
2024-02-18 10:04:16 +01:00
|
|
|
if err := runGo("build", "-o", exe(filepath.Join("bin", "gobind-original")), "github.com/ebitengine/gomobile/cmd/gobind"); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Mkdir("src", 0755); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
2022-09-14 19:45:36 +02:00
|
|
|
if err := os.WriteFile(filepath.Join("src", "gobind.go"), gobind_go, 0644); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
|
|
|
|
2022-11-04 09:20:21 +01:00
|
|
|
if err := os.Mkdir(filepath.Join("src", "_files"), 0755); err != nil {
|
|
|
|
return tmp, err
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(filepath.Join("src", "_files", "EbitenViewController.m"), objcM, 0644); err != nil {
|
|
|
|
return tmp, err
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(filepath.Join("src", "_files", "EbitenView.java"), viewJava, 0644); err != nil {
|
|
|
|
return tmp, err
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(filepath.Join("src", "_files", "EbitenSurfaceView.java"), surfaceViewJava, 0644); err != nil {
|
|
|
|
return tmp, err
|
|
|
|
}
|
|
|
|
|
2023-12-16 11:51:48 +01:00
|
|
|
// The newly added Go files like gobind.go might add new dependencies.
|
|
|
|
if err := runGo("mod", "tidy"); err != nil {
|
|
|
|
return tmp, err
|
|
|
|
}
|
2020-03-04 17:05:53 +01:00
|
|
|
if err := runGo("build", "-o", exe(filepath.Join("bin", "gobind")), "-tags", "ebitenmobilegobind", filepath.Join("src", "gobind.go")); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-09 05:57:29 +02:00
|
|
|
if err := runCommand("gomobile", []string{"init"}, nil); err != nil {
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, err
|
2021-06-09 05:57:29 +02:00
|
|
|
}
|
2019-08-17 12:00:37 +02:00
|
|
|
|
2021-06-09 18:03:25 +02:00
|
|
|
return tmp, nil
|
2019-08-17 12:00:37 +02:00
|
|
|
}
|
2022-09-28 18:58:24 +02:00
|
|
|
|
|
|
|
func gomobileHash() (string, error) {
|
|
|
|
info, ok := debug.ReadBuildInfo()
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("ebitenmobile: debug.ReadBuildInfo failed")
|
|
|
|
}
|
|
|
|
for _, m := range info.Deps {
|
2024-02-18 10:04:16 +01:00
|
|
|
if m.Path == "github.com/ebitengine/gomobile" {
|
2022-09-28 18:58:24 +02:00
|
|
|
return m.Version, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("ebitenmobile: getting the gomobile version failed")
|
|
|
|
}
|