main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package main
  2. import (
  3. "flag"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "strings"
  8. _ "github.com/sagernet/gomobile"
  9. "github.com/sagernet/sing-box/cmd/internal/build_shared"
  10. "github.com/sagernet/sing-box/log"
  11. E "github.com/sagernet/sing/common/exceptions"
  12. "github.com/sagernet/sing/common/rw"
  13. "github.com/sagernet/sing/common/shell"
  14. )
  15. var (
  16. debugEnabled bool
  17. target string
  18. )
  19. func init() {
  20. flag.BoolVar(&debugEnabled, "debug", false, "enable debug")
  21. flag.StringVar(&target, "target", "android", "target platform")
  22. }
  23. func main() {
  24. flag.Parse()
  25. build_shared.FindMobile()
  26. switch target {
  27. case "android":
  28. buildAndroid()
  29. case "ios":
  30. buildiOS()
  31. }
  32. }
  33. var (
  34. sharedFlags []string
  35. debugFlags []string
  36. sharedTags []string
  37. iosTags []string
  38. debugTags []string
  39. )
  40. func init() {
  41. sharedFlags = append(sharedFlags, "-trimpath")
  42. sharedFlags = append(sharedFlags, "-buildvcs=false")
  43. currentTag, err := build_shared.ReadTag()
  44. if err != nil {
  45. currentTag = "unknown"
  46. }
  47. sharedFlags = append(sharedFlags, "-ldflags", "-X github.com/sagernet/sing-box/constant.Version="+currentTag+" -s -w -buildid=")
  48. debugFlags = append(debugFlags, "-ldflags", "-X github.com/sagernet/sing-box/constant.Version="+currentTag)
  49. sharedTags = append(sharedTags, "with_gvisor", "with_quic", "with_wireguard", "with_ech", "with_utls", "with_clash_api")
  50. iosTags = append(iosTags, "with_dhcp", "with_low_memory", "with_conntrack")
  51. debugTags = append(debugTags, "debug")
  52. }
  53. func buildAndroid() {
  54. build_shared.FindSDK()
  55. var javaPath string
  56. javaHome := os.Getenv("JAVA_HOME")
  57. if javaHome == "" {
  58. javaPath = "java"
  59. } else {
  60. javaPath = filepath.Join(javaHome, "bin", "java")
  61. }
  62. javaVersion, err := shell.Exec(javaPath, "--version").ReadOutput()
  63. if err != nil {
  64. log.Fatal(E.Cause(err, "check java version"))
  65. }
  66. if !strings.Contains(javaVersion, "openjdk 17") {
  67. log.Fatal("java version should be openjdk 17")
  68. }
  69. args := []string{
  70. "bind",
  71. "-v",
  72. "-androidapi", "21",
  73. "-javapkg=io.nekohasekai",
  74. "-libname=box",
  75. }
  76. if !debugEnabled {
  77. args = append(args, sharedFlags...)
  78. } else {
  79. args = append(args, debugFlags...)
  80. }
  81. args = append(args, "-tags")
  82. if !debugEnabled {
  83. args = append(args, strings.Join(sharedTags, ","))
  84. } else {
  85. args = append(args, strings.Join(append(sharedTags, debugTags...), ","))
  86. }
  87. args = append(args, "./experimental/libbox")
  88. command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)
  89. command.Stdout = os.Stdout
  90. command.Stderr = os.Stderr
  91. err = command.Run()
  92. if err != nil {
  93. log.Fatal(err)
  94. }
  95. const name = "libbox.aar"
  96. copyPath := filepath.Join("..", "sing-box-for-android", "app", "libs")
  97. if rw.IsDir(copyPath) {
  98. copyPath, _ = filepath.Abs(copyPath)
  99. err = rw.CopyFile(name, filepath.Join(copyPath, name))
  100. if err != nil {
  101. log.Fatal(err)
  102. }
  103. log.Info("copied to ", copyPath)
  104. }
  105. }
  106. func buildiOS() {
  107. args := []string{
  108. "bind",
  109. "-v",
  110. "-target", "ios,iossimulator,tvos,tvossimulator,macos",
  111. "-libname=box",
  112. }
  113. if !debugEnabled {
  114. args = append(args, sharedFlags...)
  115. } else {
  116. args = append(args, debugFlags...)
  117. }
  118. tags := append(sharedTags, iosTags...)
  119. args = append(args, "-tags")
  120. if !debugEnabled {
  121. args = append(args, strings.Join(tags, ","))
  122. } else {
  123. args = append(args, strings.Join(append(tags, debugTags...), ","))
  124. }
  125. args = append(args, "./experimental/libbox")
  126. command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)
  127. command.Stdout = os.Stdout
  128. command.Stderr = os.Stderr
  129. err := command.Run()
  130. if err != nil {
  131. log.Fatal(err)
  132. }
  133. copyPath := filepath.Join("..", "sing-box-for-apple")
  134. if rw.IsDir(copyPath) {
  135. targetDir := filepath.Join(copyPath, "Libbox.xcframework")
  136. targetDir, _ = filepath.Abs(targetDir)
  137. os.RemoveAll(targetDir)
  138. os.Rename("Libbox.xcframework", targetDir)
  139. log.Info("copied to ", targetDir)
  140. }
  141. }