cmd/ebitenmobile: Clean the temporary directory up after the execution

This commit is contained in:
Hajime Hoshi 2021-06-10 01:03:25 +09:00
parent cb0f4c4378
commit f1fc6fe3e8
2 changed files with 37 additions and 21 deletions

View File

@ -51,6 +51,16 @@ func runCommand(command string, args []string, env []string) error {
return nil
}
func removeAll(path string) error {
if buildX || buildN {
fmt.Printf("rm -rf %s\n", path)
}
if buildN {
return nil
}
return os.RemoveAll(path)
}
func runGo(args ...string) error {
env := []string{
"GO111MODULE=on",
@ -67,10 +77,10 @@ func exe(filename string) string {
return filename
}
func prepareGomobileCommands() error {
func prepareGomobileCommands() (string, error) {
tmp, err := ioutil.TempDir("", "ebitenmobile-")
if err != nil {
return err
return "", err
}
newpath := filepath.Join(tmp, "bin")
@ -82,13 +92,13 @@ func prepareGomobileCommands() error {
}
if !buildN {
if err := os.Setenv("PATH", newpath); err != nil {
return err
return tmp, err
}
}
pwd, err := os.Getwd()
if err != nil {
return err
return tmp, err
}
// cd
@ -96,7 +106,7 @@ func prepareGomobileCommands() error {
fmt.Printf("cd %s\n", tmp)
}
if err := os.Chdir(tmp); err != nil {
return err
return tmp, err
}
defer func() {
os.Chdir(pwd)
@ -104,7 +114,7 @@ func prepareGomobileCommands() error {
const modname = "ebitenmobiletemporary"
if err := runGo("mod", "init", modname); err != nil {
return err
return tmp, err
}
if err := ioutil.WriteFile("tools.go", []byte(fmt.Sprintf(`// +build tools
@ -115,47 +125,47 @@ import (
_ "golang.org/x/mobile/cmd/gomobile"
)
`, modname)), 0644); err != nil {
return err
return tmp, err
}
// To record gomobile to go.sum for Go 1.16 and later, go-get gomobile instaed of golang.org/x/mobile (#1487).
// 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).
if err := runGo("get", "golang.org/x/mobile/cmd/gomobile@"+gomobileHash); err != nil {
return err
return tmp, err
}
if localgm := os.Getenv("EBITENMOBILE_GOMOBILE"); localgm != "" {
if !filepath.IsAbs(localgm) {
localgm = filepath.Join(pwd, localgm)
}
if err := runGo("mod", "edit", "-replace=golang.org/x/mobile="+localgm); err != nil {
return err
return tmp, err
}
}
if err := runGo("mod", "tidy"); err != nil {
return err
return tmp, err
}
if err := runGo("build", "-o", exe(filepath.Join("bin", "gomobile")), "golang.org/x/mobile/cmd/gomobile"); err != nil {
return err
return tmp, err
}
if err := runGo("build", "-o", exe(filepath.Join("bin", "gobind-original")), "golang.org/x/mobile/cmd/gobind"); err != nil {
return err
return tmp, err
}
if err := os.Mkdir("src", 0755); err != nil {
return err
return tmp, err
}
if err := ioutil.WriteFile(filepath.Join("src", "gobind.go"), gobindsrc, 0644); err != nil {
return err
return tmp, err
}
if err := runGo("build", "-o", exe(filepath.Join("bin", "gobind")), "-tags", "ebitenmobilegobind", filepath.Join("src", "gobind.go")); err != nil {
return err
return tmp, err
}
if err := runCommand("gomobile", []string{"init"}, nil); err != nil {
return err
return tmp, err
}
return nil
return tmp, nil
}

View File

@ -115,27 +115,33 @@ func main() {
flagset.Parse(args[1:])
os, err := osFromBuildTarget(buildTarget)
buildTarget, err := osFromBuildTarget(buildTarget)
if err != nil {
log.Fatal(err)
}
// Add ldflags to suppress linker errors (#932).
// See https://github.com/golang/go/issues/17807
if os == "android" {
if buildTarget == "android" {
if buildLdflags != "" {
buildLdflags += " "
}
buildLdflags += "-extldflags=-Wl,-soname,libgojni.so"
}
if err := prepareGomobileCommands(); err != nil {
dir, err := prepareGomobileCommands()
defer func() {
if dir != "" && !buildWork {
removeAll(dir)
}
}()
if err != nil {
log.Fatal(err)
}
switch args[0] {
case "bind":
if err := doBind(args, &flagset, os); err != nil {
if err := doBind(args, &flagset, buildTarget); err != nil {
log.Fatal(err)
}
default: