main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. var bindTarget string
  70. if debugEnabled {
  71. bindTarget = "android/arm64"
  72. } else {
  73. bindTarget = "android"
  74. }
  75. args := []string{
  76. "bind",
  77. "-v",
  78. "-target", bindTarget,
  79. "-androidapi", "21",
  80. "-javapkg=io.nekohasekai",
  81. "-libname=box",
  82. }
  83. if !debugEnabled {
  84. args = append(args, sharedFlags...)
  85. } else {
  86. args = append(args, debugFlags...)
  87. }
  88. args = append(args, "-tags")
  89. if !debugEnabled {
  90. args = append(args, strings.Join(sharedTags, ","))
  91. } else {
  92. args = append(args, strings.Join(append(sharedTags, debugTags...), ","))
  93. }
  94. args = append(args, "./experimental/libbox")
  95. command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)
  96. command.Stdout = os.Stdout
  97. command.Stderr = os.Stderr
  98. err = command.Run()
  99. if err != nil {
  100. log.Fatal(err)
  101. }
  102. const name = "libbox.aar"
  103. copyPath := filepath.Join("..", "sing-box-for-android", "app", "libs")
  104. if rw.IsDir(copyPath) {
  105. copyPath, _ = filepath.Abs(copyPath)
  106. err = rw.CopyFile(name, filepath.Join(copyPath, name))
  107. if err != nil {
  108. log.Fatal(err)
  109. }
  110. log.Info("copied to ", copyPath)
  111. }
  112. }
  113. func buildiOS() {
  114. args := []string{
  115. "bind",
  116. "-v",
  117. "-target", "ios,iossimulator,tvos,tvossimulator,macos",
  118. "-libname=box",
  119. }
  120. if !debugEnabled {
  121. args = append(args, sharedFlags...)
  122. } else {
  123. args = append(args, debugFlags...)
  124. }
  125. tags := append(sharedTags, iosTags...)
  126. args = append(args, "-tags")
  127. if !debugEnabled {
  128. args = append(args, strings.Join(tags, ","))
  129. } else {
  130. args = append(args, strings.Join(append(tags, debugTags...), ","))
  131. }
  132. args = append(args, "./experimental/libbox")
  133. command := exec.Command(build_shared.GoBinPath+"/gomobile", args...)
  134. command.Stdout = os.Stdout
  135. command.Stderr = os.Stderr
  136. err := command.Run()
  137. if err != nil {
  138. log.Fatal(err)
  139. }
  140. copyPath := filepath.Join("..", "sing-box-for-apple")
  141. if rw.IsDir(copyPath) {
  142. targetDir := filepath.Join(copyPath, "Libbox.xcframework")
  143. targetDir, _ = filepath.Abs(targetDir)
  144. os.RemoveAll(targetDir)
  145. os.Rename("Libbox.xcframework", targetDir)
  146. log.Info("copied to ", targetDir)
  147. }
  148. }