gobind.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2019 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:build ebitenmobilegobind
  15. // gobind is a wrapper of the original gobind. This command adds extra files like a view controller.
  16. package main
  17. import (
  18. _ "embed"
  19. "flag"
  20. "fmt"
  21. "log"
  22. "os"
  23. "os/exec"
  24. "path/filepath"
  25. "strings"
  26. "golang.org/x/tools/go/packages"
  27. )
  28. //go:embed _files/EbitenViewController.m
  29. var objcM string
  30. //go:embed _files/EbitenView.java
  31. var viewJava string
  32. //go:embed _files/EbitenSurfaceView.java
  33. var surfaceViewJava string
  34. var (
  35. lang = flag.String("lang", "", "")
  36. outdir = flag.String("outdir", "", "")
  37. javaPkg = flag.String("javapkg", "", "")
  38. prefix = flag.String("prefix", "", "")
  39. bootclasspath = flag.String("bootclasspath", "", "")
  40. classpath = flag.String("classpath", "", "")
  41. tags = flag.String("tags", "", "")
  42. )
  43. var usage = `The Gobind tool generates Java language bindings for Go.
  44. For usage details, see doc.go.`
  45. func main() {
  46. flag.Parse()
  47. if err := run(); err != nil {
  48. log.Fatal(err)
  49. }
  50. }
  51. func invokeOriginalGobind(lang string) (pkgName string, err error) {
  52. cmd := exec.Command("gobind-original", os.Args[1:]...)
  53. cmd.Stdout = os.Stdout
  54. cmd.Stderr = os.Stderr
  55. if err := cmd.Run(); err != nil {
  56. return "", err
  57. }
  58. cfgtags := strings.Join(strings.Split(*tags, ","), " ")
  59. cfg := &packages.Config{}
  60. switch lang {
  61. case "java":
  62. cfg.Env = append(os.Environ(), "GOOS=android")
  63. case "objc":
  64. cfg.Env = append(os.Environ(), "GOOS=darwin")
  65. if cfgtags != "" {
  66. cfgtags += " "
  67. }
  68. cfgtags += "ios"
  69. }
  70. cfg.BuildFlags = []string{"-tags", cfgtags}
  71. pkgs, err := packages.Load(cfg, flag.Args()[0])
  72. if err != nil {
  73. return "", err
  74. }
  75. return pkgs[0].Name, nil
  76. }
  77. func run() error {
  78. writeFile := func(filename string, content string) error {
  79. if err := os.WriteFile(filepath.Join(*outdir, filename), []byte(content), 0644); err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. // Add additional files.
  85. langs := strings.Split(*lang, ",")
  86. for _, lang := range langs {
  87. pkgName, err := invokeOriginalGobind(lang)
  88. if err != nil {
  89. return err
  90. }
  91. prefixLower := *prefix + pkgName
  92. prefixUpper := strings.Title(*prefix) + strings.Title(pkgName)
  93. replacePrefixes := func(content string) string {
  94. content = strings.ReplaceAll(content, "{{.PrefixUpper}}", prefixUpper)
  95. content = strings.ReplaceAll(content, "{{.PrefixLower}}", prefixLower)
  96. content = strings.ReplaceAll(content, "{{.JavaPkg}}", *javaPkg)
  97. return content
  98. }
  99. switch lang {
  100. case "objc":
  101. // iOS
  102. if err := writeFile(filepath.Join("src", "gobind", prefixLower+"ebitenviewcontroller_ios.m"), replacePrefixes(objcM)); err != nil {
  103. return err
  104. }
  105. if err := writeFile(filepath.Join("src", "gobind", prefixLower+"ebitenviewcontroller_ios.go"), `package main
  106. // #cgo CFLAGS: -DGLES_SILENCE_DEPRECATION
  107. import "C"`); err != nil {
  108. return err
  109. }
  110. case "java":
  111. // Android
  112. dir := filepath.Join(strings.Split(*javaPkg, ".")...)
  113. dir = filepath.Join(dir, prefixLower)
  114. if err := writeFile(filepath.Join("java", dir, "EbitenView.java"), replacePrefixes(viewJava)); err != nil {
  115. return err
  116. }
  117. if err := writeFile(filepath.Join("java", dir, "EbitenSurfaceView.java"), replacePrefixes(surfaceViewJava)); err != nil {
  118. return err
  119. }
  120. case "go":
  121. // Do nothing.
  122. default:
  123. panic(fmt.Sprintf("unsupported language: %s", lang))
  124. }
  125. }
  126. return nil
  127. }